MDL-73975 course: Remove course_search_form template
[moodle.git] / badges / view.php
blobff734c9eaf7abcc5324bfc07484205411426a450
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 available badges to a user
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(__DIR__ . '/../config.php');
28 require_once($CFG->libdir . '/badgeslib.php');
30 $type = required_param('type', PARAM_INT);
31 $courseid = optional_param('id', 0, PARAM_INT);
33 require_login();
35 if (empty($CFG->enablebadges)) {
36 throw new \moodle_exception('badgesdisabled', 'badges');
39 if (empty($CFG->badges_allowcoursebadges) && $courseid != 0) {
40 throw new \moodle_exception('coursebadgesdisabled', 'badges');
43 if ($course = $DB->get_record('course', array('id' => $courseid))) {
44 $PAGE->set_url('/badges/view.php', ['type' => $type, 'id' => $course->id]);
45 } else {
46 $PAGE->set_url('/badges/view.php', ['type' => $type]);
49 $PAGE->add_body_class('limitedwidth');
51 if ($type == BADGE_TYPE_SITE) {
52 $PAGE->set_context(context_system::instance());
53 $PAGE->set_pagelayout('admin');
54 $PAGE->set_heading(get_string('administrationsite'));
55 $title = get_string('sitebadges', 'badges');
56 $eventotherparams = array('badgetype' => BADGE_TYPE_SITE);
57 } else {
58 require_login($course);
59 $coursename = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
60 $title = get_string('coursebadges', 'badges');
61 $PAGE->set_context(context_course::instance($course->id));
62 $PAGE->set_pagelayout('incourse');
63 $PAGE->set_heading($coursename);
64 $eventotherparams = array('badgetype' => BADGE_TYPE_COURSE, 'courseid' => $course->id);
67 require_capability('moodle/badges:viewbadges', $PAGE->context);
69 $PAGE->set_title($title);
70 $output = $PAGE->get_renderer('core', 'badges');
72 // Display "Manage badges" button to users with proper capabilities.
73 $isfrontpage = (empty($courseid) || $courseid == $SITE->id);
74 if ($isfrontpage) {
75 $context = context_system::instance();
76 } else {
77 $context = context_course::instance($courseid);
79 $canmanage = has_any_capability(array('moodle/badges:viewawarded',
80 'moodle/badges:createbadge',
81 'moodle/badges:awardbadge',
82 'moodle/badges:configurecriteria',
83 'moodle/badges:configuremessages',
84 'moodle/badges:configuredetails',
85 'moodle/badges:deletebadge'), $context);
87 if ($canmanage) {
88 // Check there are non archived badges on the course.
89 $allbadgescount = count(badges_get_badges($type, $courseid));
90 $canmanage = ($allbadgescount > 0);
92 $actionbar = new \core_badges\output\standard_action_bar($PAGE, $type, $canmanage);
93 echo $output->header();
94 echo $output->render_tertiary_navigation($actionbar);
95 echo $OUTPUT->heading($title);
97 if ($course && $course->startdate > time()) {
98 echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
101 $report = \core_reportbuilder\system_report_factory::create(\core_badges\reportbuilder\local\systemreports\course_badges::class,
102 $PAGE->context, '', '', 0, ['type' => $type, 'courseid' => $courseid]);
103 $report->set_default_no_results_notice(new lang_string('nobadges', 'badges'));
104 echo $report->output();
106 // Trigger event, badge listing viewed.
107 $eventparams = array('context' => $PAGE->context, 'other' => $eventotherparams);
108 $event = \core\event\badge_listing_viewed::create($eventparams);
109 $event->trigger();
111 echo $output->footer();