MDL-51684 badges: Make sure 'moodle/badges:viewbadges' is respected
[moodle.git] / badges / view.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 available badges to a user
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
27require_once(dirname(dirname(__FILE__)) . '/config.php');
28require_once($CFG->libdir . '/badgeslib.php');
29
30$type = required_param('type', PARAM_INT);
31$courseid = optional_param('id', 0, PARAM_INT);
32$sortby = optional_param('sort', 'name', PARAM_ALPHA);
33$sorthow = optional_param('dir', 'DESC', PARAM_ALPHA);
34$page = optional_param('page', 0, PARAM_INT);
35
36require_login();
37
38if (empty($CFG->enablebadges)) {
39 print_error('badgesdisabled', 'badges');
40}
41
85d7399e
YB
42if (empty($CFG->badges_allowcoursebadges) && $courseid != 0) {
43 print_error('coursebadgesdisabled', 'badges');
44}
45
27806552
YB
46if (!in_array($sortby, array('name', 'dateissued'))) {
47 $sortby = 'name';
48}
49
50if ($sorthow != 'ASC' && $sorthow != 'DESC') {
d3754a97 51 $sorthow = 'ASC';
27806552
YB
52}
53
54if ($page < 0) {
55 $page = 0;
56}
57
58if ($course = $DB->get_record('course', array('id' => $courseid))) {
59 $PAGE->set_url('/badges/view.php', array('type' => $type, 'id' => $course->id, 'sort' => $sortby, 'dir' => $sorthow));
60} else {
61 $PAGE->set_url('/badges/view.php', array('type' => $type, 'sort' => $sortby, 'dir' => $sorthow));
62}
63
64if ($type == BADGE_TYPE_SITE) {
27806552
YB
65 $PAGE->set_context(context_system::instance());
66 $PAGE->set_pagelayout('admin');
4e1f6047
DW
67 $PAGE->set_heading($SITE->fullname);
68 $title = get_string('sitebadges', 'badges');
27806552
YB
69} else {
70 require_login($course);
5dcda2f8
YB
71 $coursename = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
72 $title = $coursename . ': ' . get_string('coursebadges', 'badges');
27806552 73 $PAGE->set_context(context_course::instance($course->id));
369484bf 74 $PAGE->set_pagelayout('incourse');
4e1f6047 75 $PAGE->set_heading($coursename);
27806552
YB
76}
77
4bd4d4ee
YB
78require_capability('moodle/badges:viewbadges', $PAGE->context);
79
27806552
YB
80$PAGE->set_title($title);
81$output = $PAGE->get_renderer('core', 'badges');
82
83echo $output->header();
84echo $OUTPUT->heading($title);
85
86$totalcount = count(badges_get_badges($type, $courseid, '', '', '', '', $USER->id));
87$records = badges_get_badges($type, $courseid, $sortby, $sorthow, $page, BADGE_PERPAGE, $USER->id);
88
89if ($totalcount) {
90 echo $output->heading(get_string('badgestoearn', 'badges', $totalcount), 4);
91
92 if ($course && $course->startdate > time()) {
93 echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
94 }
95
96 $badges = new badge_collection($records);
97 $badges->sort = $sortby;
98 $badges->dir = $sorthow;
99 $badges->page = $page;
100 $badges->perpage = BADGE_PERPAGE;
101 $badges->totalcount = $totalcount;
102
103 echo $output->render($badges);
104} else {
105 echo $output->notification(get_string('nobadges', 'badges'));
106}
107
4e1f6047 108echo $output->footer();