MDL-69773 block_section_links: Add an option to display section name
[moodle.git] / badges / award.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 * Handle manual badge award.
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
YB
28require_once($CFG->libdir . '/badgeslib.php');
29require_once($CFG->dirroot . '/badges/lib/awardlib.php');
30
31$badgeid = required_param('id', PARAM_INT);
32$role = optional_param('role', 0, PARAM_INT);
71fd443b 33$award = optional_param('award', false, PARAM_BOOL);
b7374fac 34$revoke = optional_param('revoke', false, PARAM_BOOL);
27806552
YB
35
36require_login();
37
38if (empty($CFG->enablebadges)) {
39 print_error('badgesdisabled', 'badges');
40}
41
42$badge = new badge($badgeid);
43$context = $badge->get_context();
44$isadmin = is_siteadmin($USER);
45
46$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
47
48if ($badge->type == BADGE_TYPE_COURSE) {
71fd443b
YB
49 if (empty($CFG->badges_allowcoursebadges)) {
50 print_error('coursebadgesdisabled', 'badges');
51 }
27806552
YB
52 require_login($badge->courseid);
53 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
e56c107d 54 $PAGE->set_pagelayout('standard');
0d206054 55 navigation_node::override_active_url($navurl);
e56c107d
PS
56} else {
57 $PAGE->set_pagelayout('admin');
0d206054 58 navigation_node::override_active_url($navurl, true);
27806552
YB
59}
60
61require_capability('moodle/badges:awardbadge', $context);
62
9b0f0014 63$url = new moodle_url('/badges/award.php', array('id' => $badgeid, 'role' => $role));
27806552
YB
64$PAGE->set_url($url);
65$PAGE->set_context($context);
66
67// Set up navigation and breadcrumbs.
68$strrecipients = get_string('recipients', 'badges');
27806552
YB
69$PAGE->navbar->add($badge->name, new moodle_url('overview.php', array('id' => $badge->id)))->add($strrecipients);
70$PAGE->set_title($strrecipients);
71$PAGE->set_heading($badge->name);
27806552
YB
72
73if (!$badge->is_active()) {
74 echo $OUTPUT->header();
75 echo $OUTPUT->notification(get_string('donotaward', 'badges'));
76 echo $OUTPUT->footer();
77 die();
78}
79
80$output = $PAGE->get_renderer('core', 'badges');
81
82// Roles that can award this badge.
71fd443b 83$acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
27806552 84
d0053d29
YB
85if (empty($acceptedroles)) {
86 echo $OUTPUT->header();
87 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
88 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
89 echo $OUTPUT->footer();
90 die();
91}
92
b1e63fc7
SP
93// Get groupmode and currentgroup before going further.
94$groupmode = groups_get_course_groupmode($COURSE); // Groups are being used.
95$currentgroup = groups_get_course_group($COURSE, true); // Get active group.
96
97// Check groupmode (SEPARATEGROUPS), currentgroup and capability (or admin).
98if ($groupmode == SEPARATEGROUPS && empty($currentgroup) &&
99 !has_capability('moodle/site:accessallgroups', $context) && !is_siteadmin() ) {
100 echo $OUTPUT->header();
101 echo $OUTPUT->heading(get_string("notingroup"));
102 echo $OUTPUT->footer();
103 die();
104}
105
71fd443b
YB
106if (count($acceptedroles) > 1) {
107 // If there is more than one role that can award a badge, prompt user to make a selection.
108 // If it is an admin, include all accepted roles, otherwise only the ones that current user has in this context.
109 if ($isadmin) {
110 $selection = $acceptedroles;
111 } else {
112 // Get all the roles that user has and use the ones required by this badge.
113 $roles = get_user_roles($context, $USER->id);
484b43f4
MG
114 $roleids = array_map(function($o) {
115 return $o->roleid;
116 }, $roles);
71fd443b
YB
117 $selection = array_intersect($acceptedroles, $roleids);
118 }
19a9f2ea
YB
119
120 if (!empty($selection)) {
121 list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
122 $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
123 foreach ($options as $p) {
124 $select[$p->id] = role_get_name($p);
125 }
126 if (!$role) {
127 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
128 echo $OUTPUT->header();
32bd11cb
BB
129 echo $OUTPUT->box($OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, '', array('' => 'choosedots'),
130 null, array('label' => get_string('selectaward', 'badges'))));
19a9f2ea
YB
131 echo $OUTPUT->footer();
132 die();
133 } else {
134 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
135 $issuerrole = new stdClass();
136 $issuerrole->roleid = $role;
32bd11cb
BB
137 $roleselect = $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role, null, null,
138 array('label' => get_string('selectaward', 'badges')));
19a9f2ea
YB
139 }
140 } else {
27806552 141 echo $OUTPUT->header();
19a9f2ea
YB
142 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
143 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
27806552
YB
144 echo $OUTPUT->footer();
145 die();
27806552
YB
146 }
147} else {
71fd443b 148 // User has to be an admin or the one with the required role.
dafb7910 149 $users = get_role_users($acceptedroles[0], $context, true, 'u.id', 'u.id ASC');
71fd443b
YB
150 $usersids = array_keys($users);
151 if (!$isadmin && !in_array($USER->id, $usersids)) {
27806552 152 echo $OUTPUT->header();
19a9f2ea
YB
153 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
154 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
27806552
YB
155 echo $OUTPUT->footer();
156 die();
71fd443b
YB
157 } else {
158 $issuerrole = new stdClass();
159 $issuerrole->roleid = $acceptedroles[0];
27806552
YB
160 }
161}
71fd443b 162
27806552
YB
163$options = array(
164 'badgeid' => $badge->id,
165 'context' => $context,
166 'issuerid' => $USER->id,
b1e63fc7
SP
167 'issuerrole' => $issuerrole->roleid,
168 'currentgroup' => $currentgroup,
169 'url' => $url,
170 );
27806552
YB
171$existingselector = new badge_existing_users_selector('existingrecipients', $options);
172$recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
173$recipientselector->set_existing_recipients($existingselector->find_users(''));
174
71fd443b 175if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
27806552
YB
176 require_sesskey();
177 $users = $recipientselector->get_selected_users();
178 foreach ($users as $user) {
179 if (process_manual_award($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
180 // If badge was successfully awarded, review manual badge criteria.
181 $data = new stdClass();
182 $data->crit = $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
183 $data->userid = $user->id;
184 badges_award_handle_manual_criteria_review($data);
185 } else {
186 echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
187 }
188 }
189
b7374fac
MD
190 $recipientselector->invalidate_selected_users();
191 $existingselector->invalidate_selected_users();
192 $recipientselector->set_existing_recipients($existingselector->find_users(''));
193} else if ($revoke && data_submitted() && has_capability('moodle/badges:revokebadge', $context)) {
194 require_sesskey();
195 $users = $existingselector->get_selected_users();
196
197 foreach ($users as $user) {
198 if (!process_manual_revoke($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
199 echo $OUTPUT->error_text(get_string('error:cannotrevokebadge', 'badges'));
b7374fac
MD
200 }
201 }
202
27806552
YB
203 $recipientselector->invalidate_selected_users();
204 $existingselector->invalidate_selected_users();
205 $recipientselector->set_existing_recipients($existingselector->find_users(''));
206}
207
208echo $OUTPUT->header();
209echo $OUTPUT->heading($strrecipients);
210
b1e63fc7
SP
211// Print group selector/dropdown menu (find out current groups mode).
212groups_print_course_menu($COURSE, $url);
213
71fd443b 214if (count($acceptedroles) > 1) {
27806552
YB
215 echo $OUTPUT->box($roleselect);
216}
217
218echo $output->recipients_selection_form($existingselector, $recipientselector);
219echo $OUTPUT->footer();