Merge branch 'MDL-47474' of https://github.com/bostelm/moodle
[moodle.git] / badges / mybadges.php
blob3f15a93a8e3dfbe0754afc29641d6da01a4e3401
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');
30 $page = optional_param('page', 0, PARAM_INT);
31 $search = optional_param('search', '', PARAM_CLEAN);
32 $clearsearch = optional_param('clearsearch', '', PARAM_TEXT);
33 $download = optional_param('download', 0, PARAM_INT);
34 $hash = optional_param('hash', '', PARAM_ALPHANUM);
35 $downloadall = optional_param('downloadall', false, PARAM_BOOL);
36 $hide = optional_param('hide', 0, PARAM_INT);
37 $show = optional_param('show', 0, PARAM_INT);
39 require_login();
41 if (empty($CFG->enablebadges)) {
42 print_error('badgesdisabled', 'badges');
45 $url = new moodle_url('/badges/mybadges.php');
46 $PAGE->set_url($url);
48 if (isguestuser()) {
49 $PAGE->set_context(context_system::instance());
50 echo $OUTPUT->header();
51 echo $OUTPUT->box(get_string('error:guestuseraccess', 'badges'), 'notifyproblem');
52 echo $OUTPUT->footer();
53 die();
56 if ($page < 0) {
57 $page = 0;
60 if ($clearsearch) {
61 $search = '';
64 if ($hide) {
65 require_sesskey();
66 $DB->set_field('badge_issued', 'visible', 0, array('id' => $hide, 'userid' => $USER->id));
67 } else if ($show) {
68 require_sesskey();
69 $DB->set_field('badge_issued', 'visible', 1, array('id' => $show, 'userid' => $USER->id));
70 } else if ($download && $hash) {
71 require_sesskey();
72 $badge = new badge($download);
73 $name = str_replace(' ', '_', $badge->name) . '.png';
74 ob_start();
75 $file = badges_bake($hash, $download);
76 header('Content-Type: image/png');
77 header('Content-Disposition: attachment; filename="'. $name .'"');
78 readfile($file);
79 ob_flush();
80 } else if ($downloadall) {
81 require_sesskey();
82 ob_start();
83 badges_download($USER->id);
84 ob_flush();
87 $context = context_user::instance($USER->id);
88 require_capability('moodle/badges:manageownbadges', $context);
90 $PAGE->set_context($context);
92 $title = get_string('mybadges', 'badges');
93 $PAGE->set_title($title);
94 $PAGE->set_heading($title);
95 $PAGE->set_pagelayout('mydashboard');
97 // Include JS files for backpack support.
98 badges_setup_backpack_js();
100 $output = $PAGE->get_renderer('core', 'badges');
101 $badges = badges_get_user_badges($USER->id);
103 echo $OUTPUT->header();
104 $totalcount = count($badges);
105 $records = badges_get_user_badges($USER->id, null, $page, BADGE_PERPAGE, $search);
107 $userbadges = new badge_user_collection($records, $USER->id);
108 $userbadges->sort = 'dateissued';
109 $userbadges->dir = 'DESC';
110 $userbadges->page = $page;
111 $userbadges->perpage = BADGE_PERPAGE;
112 $userbadges->totalcount = $totalcount;
113 $userbadges->search = $search;
115 echo $output->render($userbadges);
117 echo $OUTPUT->footer();