/** * 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 ''; } } }
<div id="item-header-avatar" style="<?php if (dpa_get_achievement_action_count() <= 1 || dpa_get_achievement_counter() < 1) { echo 'margin-bottom: 25px;'; } ?> "> <a href="<?php dpa_achievement_slug_permalink(); ?> "> <?php dpa_achievement_picture('full'); ?> </a> <?php if (dpa_get_achievement_action_count() > 1 && dpa_get_achievement_counter() >= 1) { ?> <div id="progress-bar" title="<?php dpa_achievement_progress_bar_alt_text(); ?> " alt="<?php dpa_achievement_progress_bar_alt_text(); ?> " style="width: <?php dpa_achievement_picture_width(); ?> px"><div style="width: <?php dpa_achievement_progress_bar_width(); ?> px"></div></div> <?php
/** * 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); } }