WordPress.org

Support

Support » Plugins and Hacks » Achievements for WordPress » [Resolved] Is there any badges system

[Resolved] Is there any badges system

Viewing 15 replies - 1 through 15 (of 23 total)
  • Plugin Author Paul Gibbs

    @djpaul

    There’s a feature built-in that I call badges, but I don’t know what you mean specifically by badges πŸ™‚ Let me know what you were thinking of!

    I think he meant to append some kind of badges to user’s profile (next to send private message or add friend, or in that area), which I am also looking for this πŸ™‚

    Yes, @icepin is correct. I mean that kind of badge system. If you got time @djpaul Please, We need that function. Thanks for your replies.

    Plugin Author Paul Gibbs

    @djpaul

    Displaying badges in/around user comments, profile areas, and other similar places is on my to-do list. But some time away.

    Do you guys think this should be the last achievement awarded to that user, or an achievement that can be assigned as their “badge”, say with a checkmark on the user profile admin page?

    I’m thinking of doing something similar, but wanted to see what your thoughts were. In my case we will have achievements of type “rank”, and we want to specify for each user what their rank is and display it on their profile.

    I think if we have some sort of achievement taxonomy in place, you’d want to show the last achievement earned within a chosen taxonomy. Could be levels, ranks, titles, etc.

    Without a taxonomy, I think the last badge earned is a good default.

    I’ll see what I can put together. Just as a disclaimer: all my work-arounds and how-tos are not officially endorsed by Paul, and may in fact lead to unintended consequences. On the plus side, Paul is kind enough to consider integration of those features that make sense to the goals of A4WP, so you might see them make it into the plugin over time.

    That being said: take a look at my How-To on implementing Achievement Categories: http://wordpress.org/support/topic/how-to-achievement-categories?replies=6. This should allow you to create the taxonomies you need. Be sure to read to the end to get all the updates in — if Paul decides this isn’t in-line with his current goals, I will eventually put this together as a plugin.

    Hopefully I can come up with a way to get badges to work with and without the categories functionality. I’ll post back here when I get something. πŸ™‚ Fair warning: Paul did mention that he has something in mind, but it might be a ways off. Paul’s solution will likely supersede mine once it is published.

    Thanks for the disclaimer Mike, and for your contributions.

    I’m working up a beta for my project to test internally in May/June, so even rough/buggy code is helpful to give my testers an idea of what kind of functionality might be possible, even if it gets reworked by you/Paul later.

    Plugin Author Paul Gibbs

    @djpaul

    A4WP is such an awesome short name πŸ™‚

    Making some progress … able to select and store badge. Next step is to retrieve selected badge and display it in comments. Code is still too rough to post, should have something tomorrow though.

    PS: Thanks, Paul! Glad you like it … assumed it was already in use so didn’t think anything of it. πŸ™‚ hehe

    Cool. We all are waiting. Thanks for your replies

    Quick status update before I call it a night:
    Completed Functionality so far:
    – can specify badge manually.
    – display badge next to page/post/comment author

    To Do:
    – select most recent achievement as badge from a specified category
    – In the absence of categories, select most recent overall achievement as badge
    – Show badge in profile

    I should definitely be able to get this done by tomorrow evening.

    “[…]an achievement that can be assigned as their “badge”, say with a checkmark on the user profile admin page”
    “In my case we will have achievements of type “rank”, and we want to specify for each user what their rank is and display it on their profile.”

    @mikebronner that, is exactly what I’m looking for. You are a hero! πŸ™‚

    OK, I’m almost there. The only thing not working is getting the latest achievement from a badge-enabled category (any help on how to construct a WP_QUERY to filter on custom taxonomy custom fields is appreciated!).

    Step 1: if you have used my achievements category “hack”, be sure to remove the dpa_achievements_add_profile_fields_with_categories() from your functions.php file in your child-theme first.

    Step 2: add the following code to your functions.php:

    /* Group Achievements by Category in User Profile and Select Badge ************/
    	function dpa_achievements_add_profile_fields_with_categories($user)
    	{
    //		if (!is_super_admin()) {return;}
    		$uses_categories = function_exists(register_achievements_custom_taxonomy);
    ?>
    		<h3><?php _e( 'Achievements Settings', 'dpa' ); ?>test</h3>
    		<table class="form-table">
    			<tr>
    				<th><label for="dpa_achievements"><?php _ex( 'Total Points', "User’s total points from unlocked Achievements", 'dpa' ); ?></label></th>
    				<td><?php
    		if (is_super_admin())
    		{
    			?><input type="number" name="dpa_achievements" id="dpa_achievements" value="<?php
    		}
    		echo esc_attr( (int)dpa_get_user_points( $user->ID ) );
    		if (is_super_admin())
    		{
    			?>" class="regular-text" /><?php
    		}
    ?>
    				</td>
    			</tr>
    <?php
    		if ( dpa_has_achievements( array( 'ach_populate_progress' => $user->ID, 'ach_progress_status' => dpa_get_unlocked_status_id(), 'posts_per_page' => -1, "orderby"=>"konb_term_relationships.term_taxonomy_id",) ) )
    		{
    ?>
    			<tr>
    				<th scope="row"><?php _e( 'Unlocked Achievements', 'dpa' ); ?></th>
    				<td>
    					<span class="description"><?php
    			if (is_super_admin())
    			{
    				_e('Award achievements through checking the appropriate checkboxes.', 'dpa') . " ";
    			}
    			_e('Use the radio button to choose your badge.', 'dpa'); ?></span>
    					<fieldset>
    						<legend class="screen-reader-text"><?php _e( 'Assign or remove achievements from this user', 'dpa' ); ?></legend>
    <?php
    			$previous_achievment_category = "";
    			while ( dpa_achievements() )
    			{
    				dpa_the_achievement();
    				if ($uses_categories)
    				{
    					$current_category = "";
    					$categories = get_the_terms($post_id, 'dpa_achievement_category');
    					foreach($categories as $category)
    					{
    						if (strlen($current_category) > 0)
    						{
    							$current_category .= ", ";
    						}
    						$current_category .= $category->name;
    					}
    
    					if ($current_category != $previous_achievment_category)
    					{
    						if ($previous_achievment_category != "")
    						{
    ?>
    						<br />
    <?php
    						}
    						$previous_achievment_category = $current_category;
    ?>
    
    						<strong><?php echo $current_category; ?>:</strong><br />
    <?php
    					}
    				}
    				if (dpa_is_achievement_unlocked() || is_super_admin())
    				{
    					if (!is_super_admin())
    					{
    ?>
    						<label>
    <?php
    					}
    ?>
    						<input type="radio" name="dpa_user_achievement_badge" id="dpa_achievement_badge_<?php echo dpa_get_the_achievement_ID(); ?>" value="<?php echo esc_attr( dpa_get_the_achievement_ID() ); ?>" <?php disabled( !dpa_is_achievement_unlocked(), true ); ?> <?php checked((int)get_user_option( '_dpa_user_achievement_badge', $user->ID ), (int)dpa_get_the_achievement_ID()); ?> title="<?php _ex( 'Set Profile Badge', "Select unlocked achievement as profile badge.", 'dpa' ); ?>" />
    <?php
    					if (is_super_admin())
    					{
    ?>
    						<label>
    							<input type="checkbox" name="dpa_user_achievements[]" value="<?php echo esc_attr( dpa_get_the_achievement_ID() ); ?>" <?php checked( dpa_is_achievement_unlocked(), true ); ?> onchange="document.getElementById('dpa_achievement_badge_<?php echo dpa_get_the_achievement_ID(); ?>').disabled = !this.checked; if (document.getElementById('dpa_achievement_badge_<?php echo dpa_get_the_achievement_ID(); ?>').disabled) {document.getElementById('dpa_achievement_badge_<?php echo dpa_get_the_achievement_ID(); ?>').checked = false;}">
    <?php
    					}
    ?>
    							<?php dpa_achievement_title(); ?>
    						</label>
    						<br />
    <?php
    				}
    			}
    ?>
    
    					</fieldset>
    				</td>
    			</tr>
    <?php
    		}
    ?>
    		</table>
    <?php
    		remove_action('edit_user_profile', array(achievements()->admin, 'add_profile_fields'));
    		remove_action('show_user_profile', array(achievements()->admin, 'add_profile_fields'));
    	}
    	add_action('edit_user_profile', 'dpa_achievements_add_profile_fields_with_categories', 9);
    	add_action('show_user_profile', 'dpa_achievements_add_profile_fields_with_categories', 9);
    
    /* Save Achievement Badge on User Profile Admin *******************************/
    	function dpa_achievement_badge_save($user_id)
    	{
    		$saved = false;
    		if ( empty( $user_id ) && is_user_logged_in() )
    		{
    			$user_id = get_current_user_id();
    		}
    		if ( empty( $user_id ) ) {return false;}
    		if ( current_user_can( 'edit_user', $user_id ) )
    		{
    			// As Achievements can run independently (as well as sitewide) on a multisite, decide where to store the user option
    			$store_global = is_multisite() && dpa_is_running_networkwide();
    			$new_value = apply_filters( 'dpa_update_user_points', $_POST['dpa_user_achievement_badge'], $user_id );
    			update_user_option( $user_id, '_dpa_user_achievement_badge', $new_value, $store_global );
    			$saved = true;
    		}
    
    		return true;
    	}
    	add_action( 'personal_options_update', 'dpa_achievement_badge_save' );
    	add_action( 'edit_user_profile_update', 'dpa_achievement_badge_save' );
    
    /* Show Achievement Badge on Comments *****************************************/
    	function dpa_achievement_comment_with_badge($comment, $args, $depth)
    	{
    		$GLOBALS['comment'] = $comment;
    		extract($args, EXTR_SKIP);
    		if ( 'div' == $args['style'] )
    		{
    			$tag = 'div';
    			$add_below = 'comment';
    		}
    		else
    		{
    			$tag = 'li';
    			$add_below = 'div-comment';
    		}
    ?>
    		<<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
    <?php
    		if ( 'div' != $args['style'] )
    		{
    ?>
    	<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    <?php
    		}
    ?>
    		<div class="comment-author vcard">
    			<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
    			<?php echo get_the_post_thumbnail( get_user_option('_dpa_user_achievement_badge', get_comment(get_comment_ID())->user_id), array(39, 39)); ?>
    			<?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
    		</div>
    <?php
    		if ($comment->comment_approved == '0')
    		{
    ?>
    		<em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
    		<br />
    <?php
    		}
    ?>
    		<div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
    			<?php printf( __('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','' ); ?>
    		</div>
    			<?php comment_text(); ?>
    		<div class="reply">
    			<?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
    		</div>
    <?php
    		if ( 'div' != $args['style'] )
    		{
    ?>
    	</div>
    <?php
    		}
    	}
    
    /* Configure Achievement Categories to Support Badges *************************/
    	function dpa_achievement_category_with_badge_add()
    	{
    ?>
    	<div class="form-field">
    		<label for="term_meta[dpa_achievement_category_for_badge]"><?php _e('Badge-Enabled?', 'dpa'); ?></label>
    		<input type="checkbox" name="term_meta[dpa_achievement_category_for_badge]" id="term_meta[dpa_achievement_category_for_badge]" value="1" />
    		<p class="description"><?php _e('Should this category be used to determine user badges? If the user has no badge selected, the most current achievement from badge-enabled categories will be used.', 'dpa'); ?></p>
    	</div>
    <?php
    	}
    	add_action('dpa_achievement_category_add_form_fields', 'dpa_achievement_category_with_badge_add', 10, 2);
    
    	function dpa_achievement_category_with_badge_edit($term)
    	{
    		$t_id = $term->term_id;
    		$term_meta = get_option( "taxonomy_$t_id" );
    ?>
    	<tr>
    		<th scope="row" valign="top"><label for="term_meta[dpa_achievement_category_for_badge]"><?php _e('Badge-Enabled?', 'dpa'); ?></label></th>
    		<td>
    			<input type="checkbox" name="term_meta[dpa_achievement_category_for_badge]" id="term_meta[dpa_achievement_category_for_badge]" value="1" <?php checked($term_meta['dpa_achievement_category_for_badge'], 1); ?> />
    			<p class="description"><?php _e('Should this category be used to determine user badges? If the user has no badge selected, the most current achievement from badge-enabled categories will be used.', 'dpa'); ?></p>
    		</td>
    	</tr>
    <?php
    	}
    	add_action('dpa_achievement_category_edit_form_fields', 'dpa_achievement_category_with_badge_edit', 10, 2);
    
    	function dpa_achievement_category_save_meta($term_id)
    	{
    		if ( isset( $_POST['term_meta'] ) )
    		{
    			$t_id = $term_id;
    			$term_meta = get_option( "taxonomy_$t_id" );
    			$cat_keys = array_keys( $_POST['term_meta'] );
    			foreach ( $cat_keys as $key )
    			{
    				if ( isset ( $_POST['term_meta'][$key] ) )
    				{
    					$term_meta[$key] = $_POST['term_meta'][$key];
    				}
    			}
    			update_option( "taxonomy_$t_id", $term_meta );
    		}
    	}
    	add_action('edited_dpa_achievement_category', 'dpa_achievement_category_save_meta', 10, 2);
    	add_action('create_dpa_achievement_category', 'dpa_achievement_category_save_meta', 10, 2);
    
    /* Get Achievement Thumbnail **************************************************/
    /**
     * Output the featured image of the achievement
     *
     * @param int $achievement_id Optional. Achievement ID
     * @see dpa_get_achievement_title()
     * @since Achievements 3.2.2
     * @author Mike Bronner <mike.bronner@gmail.com>
     */
    function dpa_achievement_image( $achievement_id = 0, $size = 'thumbnail' )
    {
    	echo dpa_get_achievement_image( $achievement_id, $size );
    }
    
    /**
     * Output the featured image of the achievement
     *
     * @param int $achievement_id Optional. Achievement ID
     * @since Achievements 3.2.2
     * @author Mike Bronner <mike.bronner@gmail.com>
     */
    function dpa_get_achievement_image($achievement_id = 0, $size = 'thumbnail')
    {
    	$achievement_id = dpa_get_achievement_ID($achievement_id);
    	$image          = get_the_post_thumbnail($achievement_id, $size, array("title"=>dpa_get_achievement_title($achievement_id)));
    
    	return apply_filters('dpa_get_achievement_image', $image, $achievement_id);
    }
    
    /* Get Achievement Badge **************************************************/
    /**
     * Output the chosen achievement badge for the user
     *
     * @param int $achievement_user_id Required. User ID of the user to fetch the badge for.
     * @param string $size Optional.
     * @see dpa_get_achievement_badge()
     * @since Achievements 3.2.2
     * @author Mike Bronner <mike.bronner@gmail.com>
     */
    function dpa_achievement_badge( $achievement_user_id, $size = 'thumbnail' )
    {
    	echo dpa_get_achievement_badge( $achievement_user_id, $size );
    }
    
    /**
     * Output the chosen achievement badge for the user
     *
     * @param int $achievement_user_id Required. User ID of the user to fetch the badge for.
     * @param string $size Optional.
     * @since Achievements 3.2.2
     * @author Mike Bronner <mike.bronner@gmail.com>
     */
    function dpa_get_achievement_badge($achievement_user_id, $size = 'thumbnail')
    {
    	$achievement_id = get_user_option( '_dpa_user_achievement_badge', $achievement_user_id);
    /* Need to figure out how to query custom taxonomy fields from WP_Query, and help is appreciated, this will then retireve the latest achievement from badge-enabled categories
    	if (!$achievement_id)
    	{
    		$latest_achievement = dpa_get_progress(array("author"=>$achievement_user_id, "status"=>"dpa_unlocked", "meta_key"=>"dpa_achievement_category_for_badge", "meta_compare"=>"=", "meta_value"=>"1", "orderby"=>"post_date, ID", "order"=>"DESC"));
    		$achievement_id = $latest_achievement[0]->post_parent;
    	}
    */
    	if (!$achievement_id)
    	{
    		$latest_achievement = dpa_get_progress(array("author"=>$achievement_user_id, "status"=>"dpa_unlocked", "orderby"=>"post_date, ID", "order"=>"DESC"));
    		$achievement_id = $latest_achievement[0]->post_parent;
    	}
    	$image = get_the_post_thumbnail($achievement_id, $size, array("title"=>dpa_get_achievement_title($achievement_id)));
    
    	return apply_filters('dpa_get_achievement_badge', $image, $achievement_id);
    }

    Step 3: Customize dpa_achievement_comment_with_badge() to format your comments as needed.

    Step 4: If you don’t have comments.php in your child-theme, go ahead and copy it there from your current theme.

    Step 5: Edit comments.php in your child-theme by replacing the existing wp_list_comments() with the following:
    <?php wp_list_comments( array( 'callback' => 'dpa_achievement_comment_with_badge', 'style' => 'ol' ) ); ?>

    Conclusion: This code should add the following:

    • Badge Selectors on User Profile admin page for both SuperAdmin and User.
    • Designate achievement categories (if you have them implemented) to be badge-enabled. Do this by modifying existing categories, or making the selection for new categories. The selection will save, but I still need to work on the selection process.
    • Display the most recent achievement as the badge if there is no badge selected for the user.
    • Display badge next to user’s avatar in comments and author info.
    • Added function dpa_get_achievement_badge($user_id, $size) and dpa_achievement_badge($user_id, $size) to allow you to insert the badge whereever you like in your theme.

    Please let me know if you’re having trouble with this — I know it’s a lot of code. Also, I’d be interested in seeing your results, post your links here! πŸ™‚

    Happy coding!
    ~Mike

    Plugin Author Paul Gibbs

    @djpaul

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘[Resolved] Is there any badges system’ is closed to new replies.
Skip to toolbar