Merge branch 'MDL-67174' of https://github.com/paulholden/moodle
[moodle.git] / badges / award.php
blob8f8669829f0e19135cf7630e38a58aef98509ff2
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 * Handle manual badge award.
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');
29 require_once($CFG->dirroot . '/badges/lib/awardlib.php');
31 $badgeid = required_param('id', PARAM_INT);
32 $role = optional_param('role', 0, PARAM_INT);
33 $award = optional_param('award', false, PARAM_BOOL);
34 $revoke = optional_param('revoke', false, PARAM_BOOL);
36 require_login();
38 if (empty($CFG->enablebadges)) {
39 print_error('badgesdisabled', 'badges');
42 $badge = new badge($badgeid);
43 $context = $badge->get_context();
44 $isadmin = is_siteadmin($USER);
46 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
48 if ($badge->type == BADGE_TYPE_COURSE) {
49 if (empty($CFG->badges_allowcoursebadges)) {
50 print_error('coursebadgesdisabled', 'badges');
52 require_login($badge->courseid);
53 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
54 $PAGE->set_pagelayout('standard');
55 navigation_node::override_active_url($navurl);
56 } else {
57 $PAGE->set_pagelayout('admin');
58 navigation_node::override_active_url($navurl, true);
61 require_capability('moodle/badges:awardbadge', $context);
63 $url = new moodle_url('/badges/award.php', array('id' => $badgeid, 'role' => $role));
64 $PAGE->set_url($url);
65 $PAGE->set_context($context);
67 // Set up navigation and breadcrumbs.
68 $strrecipients = get_string('recipients', 'badges');
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);
73 if (!$badge->is_active()) {
74 echo $OUTPUT->header();
75 echo $OUTPUT->notification(get_string('donotaward', 'badges'));
76 echo $OUTPUT->footer();
77 die();
80 $output = $PAGE->get_renderer('core', 'badges');
82 // Roles that can award this badge.
83 $acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
85 if (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();
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.
97 // Check groupmode (SEPARATEGROUPS), currentgroup and capability (or admin).
98 if ($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();
106 if (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);
114 $roleids = array_map(function($o) {
115 return $o->roleid;
116 }, $roles);
117 $selection = array_intersect($acceptedroles, $roleids);
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);
126 if (!$role) {
127 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
128 echo $OUTPUT->header();
129 echo $OUTPUT->box($OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, '', array('' => 'choosedots'),
130 null, array('label' => get_string('selectaward', 'badges'))));
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;
137 $roleselect = $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role, null, null,
138 array('label' => get_string('selectaward', 'badges')));
140 } else {
141 echo $OUTPUT->header();
142 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
143 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
144 echo $OUTPUT->footer();
145 die();
147 } else {
148 // User has to be an admin or the one with the required role.
149 $users = get_role_users($acceptedroles[0], $context, true, 'u.id', 'u.id ASC');
150 $usersids = array_keys($users);
151 if (!$isadmin && !in_array($USER->id, $usersids)) {
152 echo $OUTPUT->header();
153 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
154 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
155 echo $OUTPUT->footer();
156 die();
157 } else {
158 $issuerrole = new stdClass();
159 $issuerrole->roleid = $acceptedroles[0];
163 $options = array(
164 'badgeid' => $badge->id,
165 'context' => $context,
166 'issuerid' => $USER->id,
167 'issuerrole' => $issuerrole->roleid,
168 'currentgroup' => $currentgroup,
169 'url' => $url,
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(''));
175 if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
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'));
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();
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'));
203 $recipientselector->invalidate_selected_users();
204 $existingselector->invalidate_selected_users();
205 $recipientselector->set_existing_recipients($existingselector->find_users(''));
208 echo $OUTPUT->header();
209 echo $OUTPUT->heading($strrecipients);
211 // Print group selector/dropdown menu (find out current groups mode).
212 groups_print_course_menu($COURSE, $url);
214 if (count($acceptedroles) > 1) {
215 echo $OUTPUT->box($roleselect);
218 echo $output->recipients_selection_form($existingselector, $recipientselector);
219 echo $OUTPUT->footer();