/** * Submission Form Shortcode. * * @since 1.0.0 * * @param array $atts Shortcode attributes. * @return string HTML markup. */ function badgeos_submission_form($atts = array()) { // Parse our attributes $atts = shortcode_atts(array('achievement_id' => get_the_ID()), $atts, 'badgeos_submission'); // Initialize output $output = ''; // Verify user is logged in to view any submission data if (is_user_logged_in()) { // If submission data was submitted, output success message if (isset($_REQUEST['achievement_id']) && $_REQUEST['achievement_id'] == $atts['achievement_id']) { // Don't award if this achievement was earned in the past 30 seconds $recently_earned = badgeos_get_user_achievements(array('user_id' => get_current_user_id(), 'achievement_id' => $_REQUEST['achievement_id'], 'since' => time() - 30)); if (empty($recently_earned)) { if (badgeos_save_submission_data()) { $output .= sprintf('<p>%s</p>', __('Submission saved successfully.', 'badgeos')); } } } // If user has already submitted something, show their submissions if (badgeos_check_if_user_has_submission(get_current_user_id(), $atts['achievement_id'])) { $output .= badgeos_get_user_submissions('', $atts['achievement_id']); } // Return either the user's submission or the submission form if (badgeos_user_has_access_to_submission_form(get_current_user_id(), $atts['achievement_id'])) { $output .= badgeos_get_submission_form(array('user_id' => get_current_user_id(), 'achievement_id' => $atts['achievement_id'])); } // Logged-out users have no access } else { $output .= sprintf('<p><em>%s</em></p>', __('You must be logged in to post a submission.', 'badgeos')); } return $output; }
/** * Get the UNIX timestamp for the last activity on an achievement for a given user * * @since 1.0.0 * * @param integer $achievement_id The given achievements post ID * @param integer $user_id The given user's ID * @return integer The UNIX timestamp for the last reported badge activity */ function badgeos_achievement_last_user_activity($achievement_id = 0, $user_id = 0) { // Assume the user has no history with this achievement $since = 0; // Attempt to grab the last activity date from active achievement meta if ($achievement = badgeos_user_get_active_achievement($user_id, $achievement_id)) { $since = $achievement->date_started - 1; // Otherwise, attempt to grab the achievement date from earned achievement meta } elseif ($achievements = badgeos_get_user_achievements(array('user_id' => $user_id, 'achievement_id' => $achievement_id))) { $achievement = array_pop($achievements); if (is_object($achievement)) { $since = $achievement->date_earned + 1; } } // Finally, return our time return $since; }
/** * Render an achievement override to include send to Mozilla Backpack * * @since 1.1.0 * @param string $output The output from the original filter * @param integer $achievement The achievement's post ID * @return string Concatenated markup */ public function badgeos_render_openbadge_button($output, $achievement = 0) { global $user_ID; // user earned badge $earned = badgeos_get_user_achievements(array('user_id' => $user_ID, 'achievement_id' => absint($achievement))); // handle case of buddypress achievements tab if (function_exists('bp_displayed_user_id') && bp_displayed_user_id() !== 0 && bp_displayed_user_id() !== $user_ID) { $earned = false; } // if logged in user has earned badge append send to mozilla button to output if ($earned) { // for submissions need to convert achievement id into badge uid $args = array('author' => $user_ID, 'fields' => 'ids', 'post_type' => 'submission', 'meta_query' => array(array('key' => '_badgeos_submission_achievement_id', 'value' => $achievement))); $badges = get_posts($args); // if no submission fallback onto achievement id $achievement_id = !empty($badges) ? $badges[0] : $achievement; //build assertion uri $base_url = site_url() . '/' . get_option('json_api_base', 'api') . '/badge/assertion/?uid='; $uid = $achievement_id . "-" . get_post_time('U', true, $achievement_id) . "-" . $user_ID; // let user know if already attempted to send badge $pushed_badges = ($pushed_items = get_user_meta(absint($user_ID), '_badgeos_backpack_pushed')) ? (array) $pushed_items : array(); $button_text = !in_array($base_url . $uid, $pushed_badges) ? __('Send to Mozilla Backpack', 'badgeos_obi_issuer') : __('Resend to Mozilla Backpack', 'badgeos_obi_issuer'); // append mozilla backpack button to output $output .= '<div class="badgeos_backpack_action">'; $output .= '<a href="" class="badgeos_backpack button" data-uid="' . $base_url . $uid . '">' . $button_text . '</a> '; $output .= '<input type="checkbox" value="' . $base_url . $uid . '" name="badgeos_backpack_issues[]"/>'; $output .= '</div>'; } else { $output .= '<div class="badgeos_backpack_action"></div>'; } return $output; }
function widget($args, $instance) { global $user_ID; extract($args); echo $before_widget; $title = apply_filters('widget_title', $instance['title']); if (!empty($title)) { echo $before_title . $title . $after_title; } //user must be logged in to view earned badges and points if (is_user_logged_in()) { //display user's points if widget option is enabled if ($instance['point_total'] == 'on') { echo '<p class="badgeos-total-points">' . sprintf(__('My Total Points: %s', 'badgeos'), '<strong>' . number_format(badgeos_get_users_points()) . '</strong>') . '</p>'; } $achievements = badgeos_get_user_achievements(); if (is_array($achievements) && !empty($achievements)) { $number_to_show = absint($instance['number']); $thecount = 0; wp_enqueue_script('badgeos-achievements'); wp_enqueue_style('badgeos-widget'); //load widget setting for achievement types to display $set_achievements = isset($instance['set_achievements']) ? $instance['set_achievements'] : ''; //show most recently earned achievement first $achievements = array_reverse($achievements); echo '<ul class="widget-achievements-listing">'; foreach ($achievements as $achievement) { //verify achievement type is set to display in the widget settings //if $set_achievements is not an array it means nothing is set so show all achievements if (!is_array($set_achievements) || in_array($achievement->post_type, $set_achievements)) { //exclude step CPT entries from displaying in the widget if (get_post_type($achievement->ID) != 'step') { $permalink = get_permalink($achievement->ID); $title = get_the_title($achievement->ID); $img = badgeos_get_achievement_post_thumbnail($achievement->ID, array(50, 50), 'wp-post-image'); $thumb = $img ? '<a style="margin-top: -25px;" class="badgeos-item-thumb" href="' . esc_url($permalink) . '">' . $img . '</a>' : ''; $class = 'widget-badgeos-item-title'; $item_class = $thumb ? ' has-thumb' : ''; // Setup credly data if giveable $giveable = credly_is_achievement_giveable($achievement->ID, $user_ID); $item_class .= $giveable ? ' share-credly addCredly' : ''; $credly_ID = $giveable ? 'data-credlyid="' . absint($achievement->ID) . '"' : ''; echo '<li id="widget-achievements-listing-item-' . absint($achievement->ID) . '" ' . $credly_ID . ' class="widget-achievements-listing-item' . esc_attr($item_class) . '">'; echo $thumb; echo '<a class="widget-badgeos-item-title ' . esc_attr($class) . '" href="' . esc_url($permalink) . '">' . esc_html($title) . '</a>'; echo '</li>'; $thecount++; if ($thecount == $number_to_show && $number_to_show != 0) { break; } } } } echo '</ul><!-- widget-achievements-listing -->'; } } else { //user is not logged in so display a message _e('You must be logged in to view earned achievements', 'badgeos'); } echo $after_widget; }
/** * Get an array of unique achievement types a user has earned * * @since 1.0.1 * * @param int $user_id The ID of the user earning the achievement * @return array The array of achievements the user has earned */ function badgeos_get_user_earned_achievement_types($user_id = 0) { $achievements = badgeos_get_user_achievements(array('user_id' => $user_id)); $achievement_types = wp_list_pluck($achievements, 'post_type'); return array_unique($achievement_types); }
/** * Display achievements for a user on their profile screen * * @since 1.0.0 * @param object $user The current user's $user object * @return void */ function badgeos_user_profile_data($user = null) { $achievement_ids = array(); echo '<h2>' . __('BadgeOS Email Notifications', 'badgeos') . '</h2>'; echo '<table class="form-table">'; echo '<tr>'; echo '<th scope="row">' . __('Email Preference', 'badgeos') . '</th>'; echo '<td>'; echo '<label for="_badgeos_can_notify_user"><input type="checkbox" name="_badgeos_can_notify_user" id="_badgeos_can_notify_user" value="1" ' . checked(badgeos_can_notify_user($user->ID), true, false) . '/>' . __('Enable BadgeOS Email Notifications', 'badgeos') . '</label>'; echo '</td>'; echo '</tr>'; echo '</table>'; //verify uesr meets minimum role to view earned badges if (current_user_can(badgeos_get_manager_capability())) { $achievements = badgeos_get_user_achievements(array('user_id' => absint($user->ID))); echo '<h2>' . __('Earned Achievements', 'badgeos') . '</h2>'; echo '<table class="form-table">'; echo '<tr>'; echo '<th><label for="user_points">' . __('Earned Points', 'badgeos') . '</label></th>'; echo '<td>'; echo '<input type="text" name="user_points" id="user_points" value="' . badgeos_get_users_points($user->ID) . '" class="regular-text" /><br />'; echo '<span class="description">' . __("The user's points total. Entering a new total will automatically log the change and difference between totals.", 'badgeos') . '</span>'; echo '</td>'; echo '</tr>'; echo '<tr><td colspan="2">'; // List all of a user's earned achievements if ($achievements) { echo '<table class="widefat badgeos-table">'; echo '<thead><tr>'; echo '<th>' . __('Image', 'badgeos') . '</th>'; echo '<th>' . __('Name', 'badgeos') . '</th>'; echo '<th>' . __('Action', 'badgeos') . '</th>'; echo '</tr></thead>'; foreach ($achievements as $achievement) { // Setup our revoke URL $revoke_url = add_query_arg(array('action' => 'revoke', 'user_id' => absint($user->ID), 'achievement_id' => absint($achievement->ID))); echo '<tr>'; echo '<td>' . badgeos_get_achievement_post_thumbnail($achievement->ID, array(50, 50)) . '</td>'; echo '<td>', edit_post_link(get_the_title($achievement->ID), '', '', $achievement->ID), ' </td>'; echo '<td> <span class="delete"><a class="error" href="' . esc_url(wp_nonce_url($revoke_url, 'badgeos_revoke_achievement')) . '">' . __('Revoke Award', 'badgeos') . '</a></span></td>'; echo '</tr>'; $achievement_ids[] = $achievement->ID; } echo '</table>'; } echo '</td></tr>'; echo '</table>'; // If debug mode is on, output our achievements array if (badgeos_is_debug_mode()) { echo __('DEBUG MODE ENABLED', 'badgeos') . '<br />'; echo __('Metadata value for:', 'badgeos') . ' _badgeos_achievements<br />'; var_dump($achievements); } echo '<br/>'; // Output markup for awarding achievement for user badgeos_profile_award_achievement($user, $achievement_ids); } }
/** * Render an achievement * * @since 1.0.0 * @param integer $achievement The achievement's post ID * @return string Concatenated markup */ function badgeos_render_achievement($achievement = 0) { global $user_ID; // If we were given an ID, get the post if (is_numeric($achievement)) { $achievement = get_post($achievement); } // make sure our JS and CSS is enqueued wp_enqueue_script('badgeos-achievements'); wp_enqueue_style('badgeos-widget'); // check if user has earned this Achievement, and add an 'earned' class $earned_status = badgeos_get_user_achievements(array('user_id' => $user_ID, 'achievement_id' => absint($achievement->ID))) ? 'user-has-earned' : 'user-has-not-earned'; // Setup our credly classes $credly_class = ''; $credly_ID = ''; // If the achievement is earned and givable, override our credly classes if ('user-has-earned' == $earned_status && ($giveable = credly_is_achievement_giveable($achievement->ID, $user_ID))) { $credly_class = ' share-credly addCredly'; $credly_ID = 'data-credlyid="' . absint($achievement->ID) . '"'; } // Each Achievement $output = ''; $output .= '<div id="badgeos-achievements-list-item-' . $achievement->ID . '" class="badgeos-achievements-list-item ' . $earned_status . $credly_class . '"' . $credly_ID . '>'; // Achievement Image $output .= '<div class="badgeos-item-image">'; $output .= '<a href="' . get_permalink($achievement->ID) . '">' . badgeos_get_achievement_post_thumbnail($achievement->ID) . '</a>'; $output .= '</div><!-- .badgeos-item-image -->'; // Achievement Content $output .= '<div class="badgeos-item-description">'; // Achievement Title $output .= '<h2 class="badgeos-item-title"><a href="' . get_permalink($achievement->ID) . '">' . get_the_title($achievement->ID) . '</a></h2>'; // Achievement Short Description $output .= '<div class="badgeos-item-excerpt">'; $output .= badgeos_achievement_points_markup($achievement->ID); $excerpt = !empty($achievement->post_excerpt) ? $achievement->post_excerpt : $achievement->post_content; $output .= wpautop(apply_filters('get_the_excerpt', $excerpt)); $output .= '</div><!-- .badgeos-item-excerpt -->'; // Render our Steps if ($steps = badgeos_get_required_achievements_for_achievement($achievement->ID)) { $output .= '<div class="badgeos-item-attached">'; $output .= '<div id="show-more-' . $achievement->ID . '" class="badgeos-open-close-switch"><a class="show-hide-open" data-badgeid="' . $achievement->ID . '" data-action="open" href="#">' . __('Show Details', 'badgeos') . '</a></div>'; $output .= '<div id="badgeos_toggle_more_window_' . $achievement->ID . '" class="badgeos-extras-window">' . badgeos_get_required_achievements_for_achievement_list_markup($steps, $achievement->ID) . '</div><!-- .badgeos-extras-window -->'; $output .= '</div><!-- .badgeos-item-attached -->'; } $output .= '</div><!-- .badgeos-item-description -->'; $output .= '</div><!-- .badgeos-achievements-list-item -->'; // Return our filterable markup return apply_filters('badgeos_render_achievement', $output, $achievement->ID); }
/** * Count a user's relevant actions for a given step * * @since 1.0.0 * @param integer $user_id The given user's ID * @param integer $step_id The given step's ID * @return integer The total activity count */ function badgeos_get_step_activity_count($user_id = 0, $step_id = 0) { // Assume the user has no relevant activities $activities = array(); // Grab the requirements for this step $step_requirements = badgeos_get_step_requirements($step_id); // Determine which type of trigger we're using and return the corresponding activities switch ($step_requirements['trigger_type']) { case 'specific-achievement': // Get our parent achievement $parent_achievement = badgeos_get_parent_of_achievement($step_id); // If the user has any interaction with this achievement, only get activity since that date if ($parent_achievement && ($date = badgeos_achievement_last_user_activity($parent_achievement->ID, $user_id))) { $since = $date; } else { $since = 0; } // Get our achievement activity $achievements = badgeos_get_user_achievements(array('user_id' => absint($user_id), 'achievement_id' => absint($step_requirements['achievement_post']), 'since' => $since)); $activities = count($achievements); break; case 'any-achievement': $activities = badgeos_get_user_trigger_count($user_id, 'badgeos_unlock_' . $step_requirements['achievement_type']); break; case 'all-achievements': $activities = badgeos_get_user_trigger_count($user_id, 'badgeos_unlock_all_' . $step_requirements['achievement_type']); break; default: $activities = badgeos_get_user_trigger_count($user_id, $step_requirements['trigger_type']); break; } // Available filter for overriding user activity return absint(apply_filters('badgeos_step_activity', $activities, $user_id, $step_id)); }
/** * Update user's earned achievements to reflect a specific acheivement has been sent to Credly. * * @since 1.3.4 * * @param integer $user_id User ID. * @param integer $achievement_id Achievement post ID.= * @return mixed Updated user meta ID on success, otherwise false. */ function badgeos_user_sent_achievement_to_credly($user_id, $achievement_id) { // Get all earned achievements $earned_achievements = badgeos_get_user_achievements(array('user_id' => $user_id)); // Loop through each achievement if (!empty($earned_achievements)) { foreach ($earned_achievements as $key => $achievement) { // If acheivement doesn't match our ID, skip it if ($achievement_id !== $achievement->ID) { continue; } // If this instance has not been sent to credly, mark it as sent and exit if (!badgeos_achievement_has_been_sent_to_credly($achievement)) { $earned_achievements[$key]->sent_to_credly = true; return badgeos_update_user_achievements(array('user_id' => $user_id, 'all_achievements' => $earned_achievements)); } } } return false; }
/** * Output badges on profile * */ function boss_edu_profile_achievements() { global $user_ID; //user must be logged in to view earned badges and points if (is_user_logged_in() && function_exists('badgeos_get_user_achievements')) { $achievements = badgeos_get_user_achievements(array('user_id' => bp_displayed_user_id())); if (is_array($achievements) && !empty($achievements)) { $number_to_show = 5; $thecount = 0; wp_enqueue_script('badgeos-achievements'); wp_enqueue_style('badgeos-widget'); //load widget setting for achievement types to display $set_achievements = isset($instance['set_achievements']) ? $instance['set_achievements'] : ''; //show most recently earned achievement first $achievements = array_reverse($achievements); echo '<ul class="profile-achievements-listing">'; foreach ($achievements as $achievement) { //verify achievement type is set to display in the widget settings //if $set_achievements is not an array it means nothing is set so show all achievements if (!is_array($set_achievements) || in_array($achievement->post_type, $set_achievements)) { //exclude step CPT entries from displaying in the widget if (get_post_type($achievement->ID) != 'step') { $permalink = get_permalink($achievement->ID); $title = get_the_title($achievement->ID); $img = badgeos_get_achievement_post_thumbnail($achievement->ID, array(50, 50), 'wp-post-image'); $thumb = $img ? '<a style="margin-top: -25px;" class="badgeos-item-thumb" href="' . esc_url($permalink) . '">' . $img . '</a>' : ''; $class = 'widget-badgeos-item-title'; $item_class = $thumb ? ' has-thumb' : ''; // Setup credly data if giveable $giveable = credly_is_achievement_giveable($achievement->ID, $user_ID); $item_class .= $giveable ? ' share-credly addCredly' : ''; $credly_ID = $giveable ? 'data-credlyid="' . absint($achievement->ID) . '"' : ''; echo '<li id="widget-achievements-listing-item-' . absint($achievement->ID) . '" ' . $credly_ID . ' class="widget-achievements-listing-item' . esc_attr($item_class) . '">'; echo $thumb; echo '<a class="widget-badgeos-item-title ' . esc_attr($class) . '" href="' . esc_url($permalink) . '">' . esc_html($title) . '</a>'; echo '</li>'; $thecount++; if ($thecount == $number_to_show && $number_to_show != 0) { echo '<li id="widget-achievements-listing-item-more" class="widget-achievements-listing-item">'; echo '<a class="badgeos-item-thumb" href="' . bp_core_get_user_domain(get_current_user_id()) . '/achievements/"><span class="fa fa-ellipsis-h"></span></a>'; echo '<a class="widget-badgeos-item-title ' . esc_attr($class) . '" href="' . bp_core_get_user_domain(get_current_user_id()) . '/achievements/">' . __('See All', 'social-learner') . '</a>'; echo '</li>'; break; } } } } echo '</ul><!-- widget-achievements-listing -->'; } } }