コード例 #1
0
 /**
  * Constructor
  *
  * The Achievements template tag loop. It will retrieve results per the criteria passed from the database, apply pagination (if required)
  * and populate the class variables required for the template tag loop to work in the page templates.
  *
  * @access public
  * @global object $bp BuddyPress global settings
  * @param integer $user_id = '0'. User ID
  * @param string $type = 'all'. Either: all | active | inactive | unlocked | locked | single | active_by_action | eventcount
  * @param integer $page = '1'. Which page of results are we on, for pagination? Also, $page = 1 without a per_page will result in no pagination being applied to the results
  * @param integer $per_page = '20'. How many Achievements per page, for pagination?
  * @param integer $max = '0'. How many Achievements to return as a maximum?
  * @param string $slug = ''. The slug of an Achievement. Only required when $type is 'single'
  * @param string $action = ''. Only get Achievements with this action. Only required when $type is 'active_by_action' and $user_id != 0 and $populate_extras is true
  * @param string $search_terms = ''. Search query
  * @param bool $populate_extras = true. If true, fetch additional information about when or if $user_id had unlocked each Achievement
  * @param bool $skip_detail_page_result = true. On a single Achievement page, dpa_setup_nav() will setup the Achievement template objects appropiately. Set this to skip that prefetched value.
  * @see DPA_Achievement::get()
  * @see dpa_setup_nav()
  * @since 2.0
  * @todo Wrap all these arguments into an array, $args. Change bools to strings to better describe what variable is used for.
  * @uses DPA_Achievement_Template
  */
 function DPA_Achievement_Template($user_id, $type, $page, $per_page, $max, $slug, $action, $search_terms, $populate_extras, $skip_detail_page_result)
 {
     global $bp;
     $this->pag_page = !empty($_REQUEST['xpage']) ? intval($_REQUEST['xpage']) : $page;
     $this->pag_num = !empty($_GET['num']) ? intval($_GET['num']) : $per_page;
     $args = array('limit' => $max, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'populate_extras' => $populate_extras, 'search_terms' => $search_terms, 'skip_detail_page_result' => $skip_detail_page_result, 'slug' => $slug, 'action' => $action, 'type' => $type, 'user_id' => $user_id);
     $this->achievements = DPA_Achievement::get($args);
     // Item Requests
     if (!$max || $max >= (int) $this->achievements['total']) {
         $this->total_achievement_count = (int) $this->achievements['total'];
     } else {
         $this->total_achievement_count = (int) $max;
     }
     $this->achievements = $this->achievements['achievements'];
     if ($max) {
         if ($max >= count($this->achievements)) {
             $this->achievement_count = count($this->achievements);
         } else {
             $this->achievement_count = (int) $max;
         }
     } else {
         $this->achievement_count = count($this->achievements);
     }
     if ((int) $this->total_achievement_count && (int) $this->pag_num) {
         $this->pag_links = paginate_links(array('base' => add_query_arg(array('xpage' => '%#%', 'num' => $this->pag_num, 's' => $search_terms)), 'format' => '', 'total' => ceil((int) $this->total_achievement_count / (int) $this->pag_num), 'current' => (int) $this->pag_page, 'prev_text' => '←', 'next_text' => '→', 'mid_size' => 1));
     }
 }
コード例 #2
0
/**
 * Implements the Achievement actions, and unlocks if criteria met.
 *
 * @global DPA_Achievement_Template $achievements_template Achievements template tag object
 * @global int $blog_id Site ID (variable is from NXTClass and hasn't been updated for 3.0; confusing name is confusing)
 * @global object $bp BuddyPress global settings
 * @param string $name Action name
 * @param array $func_args Optional; action's arguments, from func_get_args().
 * @param string $type Optional; if the Achievement's action is called from within the loop in this function (i.e. points awarded), set this to "latent" to avoid a single Achievement being awarded mulitple times.
 * @see dpa_setup_achievements()
 * @since 2.0
 * @uses DPA_Achievement
 */
function dpa_handle_action($name, $func_args = null, $type = '')
{
    global $achievements_template, $blog_id, $bp;
    do_action('dpa_before_handle_action', $name, $func_args, $type);
    if (!($name = apply_filters('dpa_handle_action', $name, $func_args, $type))) {
        return;
    }
    $user_id = apply_filters('dpa_handle_action_user_id', $bp->loggedin_user->id, $name, $func_args, $type);
    if (false === $user_id || empty($user_id)) {
        return;
    }
    if ('latent' == $type) {
        $latent_achievements = DPA_Achievement::get(array('user_id' => $user_id, 'type' => 'active_by_action', 'action' => $name));
        $achievements_template->achievements = array_merge($achievements_template->achievements, $latent_achievements['achievements']);
        $achievements_template->achievement_count += count($latent_achievements['achievements']);
        do_action('dpa_handle_latent_action', $name, $func_args, $is_latent, $latent_achievements);
        // Avoid duplicate invocations of latent Achievement actions
        if ('dpa_points_incremented' == $name) {
            remove_action('dpa_points_incremented', 'dpa_handle_action_dpa_points_incremented', 10, 10);
        }
        if ('dpa_achievement_unlocked' == $name) {
            remove_action('dpa_achievement_unlocked', 'dpa_handle_action_dpa_achievement_unlocked', 10, 10);
        }
        return;
    }
    // This is to support plugins which use the 'dpa_handle_action_user_id' filter to return an array of user IDs.
    if (is_array($user_id)) {
        $user_ids = $user_id;
    } else {
        $user_ids = array($user_id);
    }
    foreach ($user_ids as $user_id) {
        if (dpa_has_achievements(array('user_id' => $user_id, 'type' => 'active_by_action', 'action' => $name))) {
            while (dpa_achievements()) {
                dpa_the_achievement();
                $site_id = apply_filters('dpa_handle_action_site_id', dpa_get_achievement_site_id(), $name, $func_args, $type, $user_id);
                if (false === $site_id) {
                    continue;
                }
                $site_is_valid = false;
                if (!is_multisite() || $site_id < 1 || $blog_id == $site_id) {
                    $site_is_valid = true;
                }
                $group_is_valid = false;
                if (dpa_get_achievement_group_id() < 1 || dpa_is_group_achievement_valid($name, $func_args, $user_id)) {
                    $group_is_valid = true;
                }
                $site_is_valid = apply_filters('dpa_handle_action_site_is_valid', $site_is_valid, $name, $func_args, $type, $user_id);
                $group_is_valid = apply_filters('dpa_handle_action_group_is_valid', $group_is_valid, $name, $func_args, $type, $user_id);
                if ($site_is_valid && $group_is_valid) {
                    dpa_maybe_unlock_achievement($user_id);
                }
            }
        }
    }
    do_action('dpa_after_handle_action', $name, $func_args, $type);
    $achievements_template = null;
}