MDL-12643 course: fix capabilities used for bulk participant actions.
[moodle.git] / badges / index.php
blob772fa0e9752ef1b74d15397d63fb96d4bf62d63e
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 * Page for badges management
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);
32 $page = optional_param('page', 0, PARAM_INT);
33 $deactivate = optional_param('lock', 0, PARAM_INT);
34 $sortby = optional_param('sort', 'name', PARAM_ALPHA);
35 $sorthow = optional_param('dir', 'ASC', PARAM_ALPHA);
36 $confirm = optional_param('confirm', false, PARAM_BOOL);
37 $delete = optional_param('delete', 0, PARAM_INT);
38 $archive = optional_param('archive', 0, PARAM_INT);
39 $msg = optional_param('msg', '', PARAM_TEXT);
41 if (!in_array($sortby, array('name', 'status'))) {
42 $sortby = 'name';
45 if ($sorthow != 'ASC' and $sorthow != 'DESC') {
46 $sorthow = 'ASC';
49 if ($page < 0) {
50 $page = 0;
53 require_login();
55 if (empty($CFG->enablebadges)) {
56 throw new \moodle_exception('badgesdisabled', 'badges');
59 if (empty($CFG->badges_allowcoursebadges) && ($type == BADGE_TYPE_COURSE)) {
60 throw new \moodle_exception('coursebadgesdisabled', 'badges');
63 $err = '';
64 $urlparams = array('sort' => $sortby, 'dir' => $sorthow, 'page' => $page);
66 if ($course = $DB->get_record('course', array('id' => $courseid))) {
67 $urlparams['type'] = $type;
68 $urlparams['id'] = $course->id;
69 } else {
70 $urlparams['type'] = $type;
73 $hdr = get_string('managebadges', 'badges');
74 $returnurl = new moodle_url('/badges/index.php', $urlparams);
75 $PAGE->set_url($returnurl);
76 $PAGE->add_body_class('limitedwidth');
78 if ($type == BADGE_TYPE_SITE) {
79 $title = get_string('sitebadges', 'badges');
80 $PAGE->set_context(context_system::instance());
81 $PAGE->set_pagelayout('admin');
82 $PAGE->set_heading(get_string('administrationsite'));
83 navigation_node::override_active_url(new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_SITE)), true);
84 } else {
85 require_login($course);
86 $coursecontext = context_course::instance($course->id);
87 $title = get_string('coursebadges', 'badges');
88 $PAGE->set_context($coursecontext);
89 $PAGE->set_pagelayout('incourse');
90 $PAGE->set_heading(format_string($course->fullname, true, array('context' => $coursecontext)));
91 navigation_node::override_active_url(
92 new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_COURSE, 'id' => $course->id))
96 if (!has_any_capability(array(
97 'moodle/badges:viewawarded',
98 'moodle/badges:createbadge',
99 'moodle/badges:awardbadge',
100 'moodle/badges:configurecriteria',
101 'moodle/badges:configuremessages',
102 'moodle/badges:configuredetails',
103 'moodle/badges:deletebadge'), $PAGE->context)) {
104 redirect($CFG->wwwroot);
107 $PAGE->set_title($hdr);
108 $output = $PAGE->get_renderer('core', 'badges');
110 if (($delete || $archive) && has_capability('moodle/badges:deletebadge', $PAGE->context)) {
111 $badgeid = ($archive != 0) ? $archive : $delete;
112 $badge = new badge($badgeid);
113 if (!$confirm) {
114 echo $output->header();
115 // Archive this badge?
116 echo $output->heading(get_string('archivebadge', 'badges', $badge->name));
117 $archivebutton = $output->single_button(
118 new moodle_url($PAGE->url, array('archive' => $badge->id, 'confirm' => 1)),
119 get_string('archiveconfirm', 'badges'));
120 echo $output->box(get_string('archivehelp', 'badges') . $archivebutton, 'generalbox');
122 // Delete this badge?
123 echo $output->heading(get_string('delbadge', 'badges', $badge->name));
124 $deletebutton = $output->single_button(
125 new moodle_url($PAGE->url, array('delete' => $badge->id, 'confirm' => 1)),
126 get_string('delconfirm', 'badges'));
127 echo $output->box(get_string('deletehelp', 'badges') . $deletebutton, 'generalbox');
129 // Go back.
130 echo $output->action_link($returnurl, get_string('cancel'));
132 echo $output->footer();
133 die();
134 } else {
135 require_sesskey();
136 $archiveonly = ($archive != 0) ? true : false;
137 $badge->delete($archiveonly);
138 redirect($returnurl);
142 if ($deactivate && has_capability('moodle/badges:configuredetails', $PAGE->context)) {
143 require_sesskey();
144 $badge = new badge($deactivate);
145 if ($badge->is_locked()) {
146 $badge->set_status(BADGE_STATUS_INACTIVE_LOCKED);
147 } else {
148 $badge->set_status(BADGE_STATUS_INACTIVE);
150 $msg = 'deactivatesuccess';
151 $returnurl->param('msg', $msg);
152 redirect($returnurl);
155 echo $OUTPUT->header();
156 $backurl = $type == BADGE_TYPE_SITE ? null : new moodle_url('/badges/view.php', ['type' => $type, 'id' => $courseid]);
157 $actionbar = new \core_badges\output\standard_action_bar($PAGE, $type, false, true, $backurl);
158 echo $output->render_tertiary_navigation($actionbar);
160 if ($type == BADGE_TYPE_SITE) {
161 echo $OUTPUT->heading_with_help($hdr, 'sitebadges', 'badges');
162 } else {
163 echo $OUTPUT->heading($hdr);
166 echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection');
168 $totalcount = count(badges_get_badges($type, $courseid, '', '' , 0, 0));
169 $records = badges_get_badges($type, $courseid, $sortby, $sorthow, $page, BADGE_PERPAGE);
171 if ($totalcount) {
172 if ($course && $course->startdate > time()) {
173 echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
176 if ($err !== '') {
177 echo $OUTPUT->notification($err, 'notifyproblem');
180 if ($msg !== '') {
181 echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess');
184 $badges = new \core_badges\output\badge_management($records);
185 $badges->sort = $sortby;
186 $badges->dir = $sorthow;
187 $badges->page = $page;
188 $badges->perpage = BADGE_PERPAGE;
189 $badges->totalcount = $totalcount;
191 echo $output->render($badges);
192 } else {
193 echo $output->notification(get_string('nobadges', 'badges'), 'info');
196 echo $OUTPUT->footer();