/**
 * Returns the 'title' text for a user's Achievement progress bar, for Achievements with an Action Count > 0
 *
 * @since 2.0
 * @see dpa_get_achievement_progress_bar_width()
 * @global object $bp BuddyPress global settings
 * @return string
 */
function dpa_get_achievement_progress_bar_alt_text()
{
    global $bp;
    if (!$bp->displayed_user->id && !$bp->loggedin_user->id) {
        return '';
    }
    if (dpa_is_achievement_unlocked()) {
        return __("You've unlocked this Achievement! Way to go!", 'dpa');
    } else {
        $achievement_count = dpa_get_achievement_action_count();
        if ($achievement_count > 1) {
            if (!($counter = dpa_get_achievement_counter())) {
                $percentage = 0;
            } else {
                $percentage = min(ceil($counter / $achievement_count * 100), 100);
            }
            if (100 == $percentage) {
                return sprintf(__("You've very nearly unlocked this Achievement, keep going!", 'dpa'), $percentage);
            } else {
                return sprintf(__("You're about %d%% of the way towards unlocking this Achievement, keep going!", 'dpa'), $percentage);
            }
        } else {
            return '';
        }
    }
}
Exemplo n.º 2
0
/**
 * Checks if an Achievement's criteria has been met, and if it has, unlock the Achievement.
 * Achievements with an action count of 0 are, effectively, unlocked each time but only
 * the points are added to user's total.
 *
 * @global object $bp BuddyPress global settings
 * @param int $user_id
 * @param string $skip_validation Set to 'force' to skip Achievement validation, e.g. the Achievement is unlocked regardless of its criteria.
 * @since 2.0
 */
function dpa_maybe_unlock_achievement($user_id, $skip_validation = '')
{
    global $bp;
    $action_count = dpa_get_achievement_action_count();
    if (dpa_is_achievement_unlocked() && $action_count > 0) {
        return;
    }
    $counters = get_user_meta($user_id, 'achievements_counters', true);
    $achievement_id = dpa_get_achievement_id();
    $skip_validation = 'force' == $skip_validation;
    // No point saving a count of 1 if the action_count is 1.
    $unlocked = false;
    if (0 === $action_count || 1 == $action_count) {
        $unlocked = true;
    } elseif (!$skip_validation) {
        if (!$counters && !is_array($counters)) {
            $counters = array();
        }
        $counters[$achievement_id] += apply_filters('dpa_achievement_counter_increment_by', 1);
        update_user_meta($user_id, 'achievements_counters', $counters);
        do_action('dpa_achievement_counter_incremented');
    }
    if (!$unlocked && ($skip_validation || $counters[$achievement_id] >= $action_count)) {
        if (isset($counters[$achievement_id])) {
            unset($counters[$achievement_id]);
            update_user_meta($user_id, 'achievements_counters', $counters);
        }
        $unlocked = true;
    }
    // Update points, insert unlocked record into DB and send notifications.
    if ($unlocked) {
        dpa_points_increment(dpa_get_achievement_points(), $user_id);
        // Let Achievements with action_count == 0, which have already been unlocked, only increment points.
        if (dpa_is_achievement_unlocked() && 0 === $action_count) {
            return;
        }
        dpa_unlock_achievement($user_id);
        if (apply_filters('dpa_achievement_unlocked_tell_user', true, $achievement_id, $user_id)) {
            bp_core_add_notification($achievement_id, $user_id, $bp->achievements->id, 'new_achievement', $user_id);
            dpa_record_activity($user_id, dpa_format_activity($user_id, $achievement_id), $achievement_id);
        }
        do_action('dpa_achievement_unlocked', $achievement_id, $user_id);
    }
}
"><?php 
        dpa_achievement_points();
        ?>
</p>
			</div>

			<div class="item">
				<div class="item-title"><a href="<?php 
        dpa_achievement_slug_permalink();
        ?>
"><?php 
        dpa_achievement_name();
        ?>
</a></div>
				<?php 
        if (dpa_is_achievement_unlocked()) {
            ?>
					<div class="item-meta"><span class="activity"><?php 
            printf(__("Unlocked %s", 'dpa'), dpa_get_achievement_unlocked_ago());
            ?>
</span></div>
				<?php 
        }
        ?>

				<div class="item-desc"><?php 
        dpa_achievement_description_excerpt();
        ?>
</div>

				<?php 
Exemplo n.º 4
0
        /**
         * Add the 'User Points' box to Edit User page, and a list of the user's current achievements.
         *
         * @param WP_User $user
         * @since Achievements (3.0)
         */
        public function add_profile_fields(WP_User $user)
        {
            if (!is_super_admin()) {
                return;
            }
            ?>

		<h3><?php 
            _e('Achievements Settings', 'achievements');
            ?>
</h3>
		<table class="form-table">
			<tr>
				<th><label for="dpa_achievements"><?php 
            _ex('Total Points', "User&rsquo;s total points from unlocked Achievements", 'achievements');
            ?>
</label></th>
				<td><input type="number" name="dpa_achievements" id="dpa_achievements" value="<?php 
            echo (int) dpa_get_user_points($user->ID);
            ?>
" class="regular-text" />
				</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))) {
                ?>
				<tr>
					<th scope="row"><?php 
                _e('Unlocked Achievements', 'achievements');
                ?>
</th>
					<td>
						<fieldset>
							<legend class="screen-reader-text"><?php 
                _e('Assign or remove achievements from this user', 'achievements');
                ?>
</legend>

							<?php 
                while (dpa_achievements()) {
                    ?>
								<?php 
                    dpa_the_achievement();
                    ?>

								<label><input type="checkbox" name="dpa_user_achievements[]" value="<?php 
                    echo absint(dpa_get_the_achievement_ID());
                    ?>
" <?php 
                    checked(dpa_is_achievement_unlocked(), true);
                    ?>
> <?php 
                    dpa_achievement_title();
                    ?>
</label><br>
							<?php 
                }
                ?>

						</fieldset>
					</td>
				</tr>
			<?php 
            }
            ?>

		</table>

	<?php 
        }