MDL-69773 block_section_links: Add an option to display section name
[moodle.git] / badges / mybadges.php
CommitLineData
27806552
YB
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.
13//
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/>.
16
17/**
18 * Displays user badges for badges management in own profile
19 *
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>
25 */
26
1fcf0ca8 27require_once(__DIR__ . '/../config.php');
27806552 28require_once($CFG->libdir . '/badgeslib.php');
62fef3cf 29require_once($CFG->libdir . '/filelib.php');
27806552
YB
30
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);
39
40require_login();
41
42if (empty($CFG->enablebadges)) {
43 print_error('badgesdisabled', 'badges');
44}
45
a2a114bd
YB
46$url = new moodle_url('/badges/mybadges.php');
47$PAGE->set_url($url);
48
27806552 49if (isguestuser()) {
a2a114bd
YB
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();
27806552
YB
54 die();
55}
56
57if ($page < 0) {
58 $page = 0;
59}
60
61if ($clearsearch) {
62 $search = '';
63}
64
65if ($hide) {
66 require_sesskey();
100ec861 67 $DB->set_field('badge_issued', 'visible', 0, array('id' => $hide, 'userid' => $USER->id));
27806552
YB
68} else if ($show) {
69 require_sesskey();
100ec861 70 $DB->set_field('badge_issued', 'visible', 1, array('id' => $show, 'userid' => $USER->id));
27806552
YB
71} else if ($download && $hash) {
72 require_sesskey();
73 $badge = new badge($download);
74 $name = str_replace(' ', '_', $badge->name) . '.png';
d77a6026 75 $name = clean_param($name, PARAM_FILE);
62fef3cf
YB
76 $filehash = badges_bake($hash, $download, $USER->id, true);
77 $fs = get_file_storage();
78 $file = $fs->get_file_by_hash($filehash);
79 send_stored_file($file, 0, 0, true, array('filename' => $name));
27806552
YB
80} else if ($downloadall) {
81 require_sesskey();
27806552 82 badges_download($USER->id);
27806552
YB
83}
84
85$context = context_user::instance($USER->id);
86require_capability('moodle/badges:manageownbadges', $context);
87
27806552
YB
88$PAGE->set_context($context);
89
4887d152 90$title = get_string('badges', 'badges');
27806552 91$PAGE->set_title($title);
880c5073 92$PAGE->set_heading(fullname($USER));
ab1ca45e 93$PAGE->set_pagelayout('standard');
27806552 94
2c910861
YB
95// Include JS files for backpack support.
96badges_setup_backpack_js();
27806552
YB
97
98$output = $PAGE->get_renderer('core', 'badges');
99$badges = badges_get_user_badges($USER->id);
100
101echo $OUTPUT->header();
aae219ac
DW
102$success = optional_param('success', '', PARAM_ALPHA);
103$warning = optional_param('warning', '', PARAM_ALPHA);
104if (!empty($success)) {
105 echo $OUTPUT->notification(get_string($success, 'core_badges'), 'notifysuccess');
106} else if (!empty($warning)) {
107 echo $OUTPUT->notification(get_string($warning, 'core_badges'), 'warning');
108}
27806552
YB
109$totalcount = count($badges);
110$records = badges_get_user_badges($USER->id, null, $page, BADGE_PERPAGE, $search);
111
aae219ac 112$userbadges = new \core_badges\output\badge_user_collection($records, $USER->id);
27806552
YB
113$userbadges->sort = 'dateissued';
114$userbadges->dir = 'DESC';
115$userbadges->page = $page;
116$userbadges->perpage = BADGE_PERPAGE;
117$userbadges->totalcount = $totalcount;
118$userbadges->search = $search;
119
120echo $output->render($userbadges);
121
60d72efb 122echo $OUTPUT->footer();