Example #1
0
/**
 * Add nodes to myprofile page.
 *
 * @param \core_user\output\myprofile\tree $tree Tree object
 * @param stdClass $user user object
 * @param bool $iscurrentuser
 * @param stdClass $course Course object
 *
 * @return bool
 */
function core_badges_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course)
{
    global $CFG, $PAGE, $USER, $SITE;
    require_once $CFG->dirroot . '/badges/renderer.php';
    if (empty($CFG->enablebadges) || !empty($course) && empty($CFG->badges_allowcoursebadges)) {
        // Y U NO LIKE BADGES ?
        return true;
    }
    // Add category. This node should appear after 'contact' so that administration block appears towards the end. Refer MDL-49928.
    $category = new core_user\output\myprofile\category('badges', get_string('badges', 'badges'), 'contact');
    $tree->add_category($category);
    $context = context_user::instance($user->id);
    $courseid = empty($course) ? 0 : $course->id;
    if ($USER->id == $user->id || has_capability('moodle/badges:viewotherbadges', $context)) {
        $records = badges_get_user_badges($user->id, $courseid, null, null, null, true);
        $renderer = new core_badges_renderer($PAGE, '');
        // Local badges.
        if ($records) {
            $title = get_string('localbadgesp', 'badges', format_string($SITE->fullname));
            $content = $renderer->print_badges_list($records, $user->id, true);
            $localnode = $mybadges = new core_user\output\myprofile\node('badges', 'localbadges', $title, null, null, $content);
            $tree->add_node($localnode);
        }
        // External badges.
        if ($courseid == 0 && !empty($CFG->badges_allowexternalbackpack)) {
            $backpack = get_backpack_settings($user->id);
            if (isset($backpack->totalbadges) && $backpack->totalbadges !== 0) {
                $title = get_string('externalbadgesp', 'badges');
                $content = $renderer->print_badges_list($backpack->badges, $user->id, true, true);
                $externalnode = $mybadges = new core_user\output\myprofile\node('badges', 'externalbadges', $title, null, null, $content);
                $tree->add_node($externalnode);
            }
        }
    }
}
Example #2
0
 /**
  * Initializes user badge collection.
  *
  * @param array $badges Badges to render
  * @param int $userid Badges owner
  */
 public function __construct($badges, $userid)
 {
     global $CFG;
     parent::__construct($badges);
     if (!empty($CFG->badges_allowexternalbackpack)) {
         $this->backpack = get_backpack_settings($userid);
     }
 }
Example #3
0
/**
 * Print badges on user profile page.
 *
 * @deprecated since Moodle 2.9 MDL-45898 - please do not use this function any more.
 * @param int $userid User ID.
 * @param int $courseid Course if we need to filter badges (optional).
 */
function profile_display_badges($userid, $courseid = 0)
{
    global $CFG, $PAGE, $USER, $SITE;
    require_once $CFG->dirroot . '/badges/renderer.php';
    debugging('profile_display_badges() is deprecated.', DEBUG_DEVELOPER);
    // Determine context.
    if (isloggedin()) {
        $context = context_user::instance($USER->id);
    } else {
        $context = context_system::instance();
    }
    if ($USER->id == $userid || has_capability('moodle/badges:viewotherbadges', $context)) {
        $records = badges_get_user_badges($userid, $courseid, null, null, null, true);
        $renderer = new core_badges_renderer($PAGE, '');
        // Print local badges.
        if ($records) {
            $left = get_string('localbadgesp', 'badges', format_string($SITE->fullname));
            $right = $renderer->print_badges_list($records, $userid, true);
            echo html_writer::tag('dt', $left);
            echo html_writer::tag('dd', $right);
        }
        // Print external badges.
        if ($courseid == 0 && !empty($CFG->badges_allowexternalbackpack)) {
            $backpack = get_backpack_settings($userid);
            if (isset($backpack->totalbadges) && $backpack->totalbadges !== 0) {
                $left = get_string('externalbadgesp', 'badges');
                $right = $renderer->print_badges_list($backpack->badges, $userid, true, true);
                echo html_writer::tag('dt', $left);
                echo html_writer::tag('dd', $right);
            }
        }
    }
}
Example #4
0
$userid = required_param('user', PARAM_INT);
$PAGE->set_url(new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid)));
// Using the same setting as user profile page.
if (!empty($CFG->forceloginforprofiles)) {
    require_login();
    if (isguestuser()) {
        $SESSION->wantsurl = $PAGE->url->out(false);
        redirect(get_login_url());
    }
} else {
    if (!empty($CFG->forcelogin)) {
        require_login();
    }
}
// Get all external badges of a user.
$out = get_backpack_settings($userid);
// If we didn't find any badges then print an error.
if (is_null($out)) {
    print_error('error:externalbadgedoesntexist', 'badges');
}
$badges = $out->badges;
// The variable to store the badge we want.
$badge = '';
// Loop through the badges and check if supplied badge hash exists in user external badges.
foreach ($badges as $b) {
    if ($hash == hash("md5", $b->hostedUrl)) {
        $badge = $b;
        break;
    }
}
// If we didn't find the badge a user might be trying to replace the userid parameter.