Merge branch 'MDL-68858-master' of git://github.com/vmdef/moodle
[moodle.git] / badges / lib.php
blob4c2a0ff7a9bffad438d0329ff0dd74857f0eaa6c
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Defines various library functions.
20 * @package core_badges
21 * @copyright 2015 onwards Ankit Agarwal
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Add nodes to myprofile page.
30 * @param \core_user\output\myprofile\tree $tree Tree object
31 * @param stdClass $user user object
32 * @param bool $iscurrentuser
33 * @param stdClass $course Course object
35 * @return bool
37 function core_badges_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
38 global $CFG, $PAGE, $USER, $SITE;
39 require_once($CFG->dirroot . '/badges/renderer.php');
40 if (empty($CFG->enablebadges) || (!empty($course) && empty($CFG->badges_allowcoursebadges))) {
41 // Y U NO LIKE BADGES ?
42 return true;
45 // Add category. This node should appear after 'contact' so that administration block appears towards the end. Refer MDL-49928.
46 $category = new core_user\output\myprofile\category('badges', get_string('badges', 'badges'), 'contact');
47 $tree->add_category($category);
48 $context = context_user::instance($user->id);
49 $courseid = empty($course) ? 0 : $course->id;
51 if ($USER->id == $user->id || has_capability('moodle/badges:viewotherbadges', $context)) {
52 $records = badges_get_user_badges($user->id, $courseid, null, null, null, true);
53 $renderer = new core_badges_renderer($PAGE, '');
55 // Local badges.
56 if ($records) {
57 $title = get_string('localbadgesp', 'badges', format_string($SITE->fullname));
58 $content = $renderer->print_badges_list($records, $user->id, true);
59 $localnode = $mybadges = new core_user\output\myprofile\node('badges', 'localbadges', $title, null, null, $content);
60 $tree->add_node($localnode);
63 // External badges.
64 if ($courseid == 0 && !empty($CFG->badges_allowexternalbackpack)) {
65 $backpack = get_backpack_settings($user->id);
66 if (isset($backpack->totalbadges) && $backpack->totalbadges !== 0) {
67 $title = get_string('externalbadgesp', 'badges');
68 $content = $renderer->print_badges_list($backpack->badges, $user->id, true, true);
69 $externalnode = $mybadges = new core_user\output\myprofile\node('badges', 'externalbadges', $title, null, null,
70 $content);
71 $tree->add_node($externalnode);