Merge branch 'MDL-50805_master' of https://github.com/crazyserver/moodle
[moodle.git] / badges / mybadges.php
blobe523b3ea1c1ab499009798787efbc4335ca9ee05
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 * Displays user badges for badges management in own profile
20 * @package core
21 * @subpackage badges
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
27 require_once(dirname(dirname(__FILE__)) . '/config.php');
28 require_once($CFG->libdir . '/badgeslib.php');
29 require_once($CFG->libdir . '/filelib.php');
31 $page = optional_param('page', 0, PARAM_INT);
32 $search = optional_param('search', '', PARAM_CLEAN);
33 $clearsearch = optional_param('clearsearch', '', PARAM_TEXT);
34 $download = optional_param('download', 0, PARAM_INT);
35 $hash = optional_param('hash', '', PARAM_ALPHANUM);
36 $downloadall = optional_param('downloadall', false, PARAM_BOOL);
37 $hide = optional_param('hide', 0, PARAM_INT);
38 $show = optional_param('show', 0, PARAM_INT);
40 require_login();
42 if (empty($CFG->enablebadges)) {
43 print_error('badgesdisabled', 'badges');
46 $url = new moodle_url('/badges/mybadges.php');
47 $PAGE->set_url($url);
49 if (isguestuser()) {
50 $PAGE->set_context(context_system::instance());
51 echo $OUTPUT->header();
52 echo $OUTPUT->box(get_string('error:guestuseraccess', 'badges'), 'notifyproblem');
53 echo $OUTPUT->footer();
54 die();
57 if ($page < 0) {
58 $page = 0;
61 if ($clearsearch) {
62 $search = '';
65 if ($hide) {
66 require_sesskey();
67 $DB->set_field('badge_issued', 'visible', 0, array('id' => $hide, 'userid' => $USER->id));
68 } else if ($show) {
69 require_sesskey();
70 $DB->set_field('badge_issued', 'visible', 1, array('id' => $show, 'userid' => $USER->id));
71 } else if ($download && $hash) {
72 require_sesskey();
73 $badge = new badge($download);
74 $name = str_replace(' ', '_', $badge->name) . '.png';
75 $filehash = badges_bake($hash, $download, $USER->id, true);
76 $fs = get_file_storage();
77 $file = $fs->get_file_by_hash($filehash);
78 send_stored_file($file, 0, 0, true, array('filename' => $name));
79 } else if ($downloadall) {
80 require_sesskey();
81 badges_download($USER->id);
84 $context = context_user::instance($USER->id);
85 require_capability('moodle/badges:manageownbadges', $context);
87 $PAGE->set_context($context);
89 $title = get_string('badges', 'badges');
90 $PAGE->set_title($title);
91 $PAGE->set_heading(fullname($USER));
92 $PAGE->set_pagelayout('mydashboard');
94 // Include JS files for backpack support.
95 badges_setup_backpack_js();
97 $output = $PAGE->get_renderer('core', 'badges');
98 $badges = badges_get_user_badges($USER->id);
100 echo $OUTPUT->header();
101 $totalcount = count($badges);
102 $records = badges_get_user_badges($USER->id, null, $page, BADGE_PERPAGE, $search);
104 $userbadges = new badge_user_collection($records, $USER->id);
105 $userbadges->sort = 'dateissued';
106 $userbadges->dir = 'DESC';
107 $userbadges->page = $page;
108 $userbadges->perpage = BADGE_PERPAGE;
109 $userbadges->totalcount = $totalcount;
110 $userbadges->search = $search;
112 echo $output->render($userbadges);
114 echo $OUTPUT->footer();