Merge branch 'MDL-51969-master' of https://github.com/dmitriim/moodle
[moodle.git] / badges / award.php
blob18242b98b54c002e5a0f0add70d0fb7684d1aa2e
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 if (count($acceptedroles) > 1) {
94 // If there is more than one role that can award a badge, prompt user to make a selection.
95 // If it is an admin, include all accepted roles, otherwise only the ones that current user has in this context.
96 if ($isadmin) {
97 $selection = $acceptedroles;
98 } else {
99 // Get all the roles that user has and use the ones required by this badge.
100 $roles = get_user_roles($context, $USER->id);
101 $roleids = array_map(function($o) {
102 return $o->roleid;
103 }, $roles);
104 $selection = array_intersect($acceptedroles, $roleids);
107 if (!empty($selection)) {
108 list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
109 $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
110 foreach ($options as $p) {
111 $select[$p->id] = role_get_name($p);
113 if (!$role) {
114 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
115 echo $OUTPUT->header();
116 echo $OUTPUT->box($OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, '', array('' => 'choosedots'),
117 null, array('label' => get_string('selectaward', 'badges'))));
118 echo $OUTPUT->footer();
119 die();
120 } else {
121 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
122 $issuerrole = new stdClass();
123 $issuerrole->roleid = $role;
124 $roleselect = $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role, null, null,
125 array('label' => get_string('selectaward', 'badges')));
127 } else {
128 echo $OUTPUT->header();
129 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
130 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
131 echo $OUTPUT->footer();
132 die();
134 } else {
135 // User has to be an admin or the one with the required role.
136 $users = get_role_users($acceptedroles[0], $context, true, 'u.id', 'u.id ASC');
137 $usersids = array_keys($users);
138 if (!$isadmin && !in_array($USER->id, $usersids)) {
139 echo $OUTPUT->header();
140 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
141 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
142 echo $OUTPUT->footer();
143 die();
144 } else {
145 $issuerrole = new stdClass();
146 $issuerrole->roleid = $acceptedroles[0];
150 $options = array(
151 'badgeid' => $badge->id,
152 'context' => $context,
153 'issuerid' => $USER->id,
154 'issuerrole' => $issuerrole->roleid
156 $existingselector = new badge_existing_users_selector('existingrecipients', $options);
157 $recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
158 $recipientselector->set_existing_recipients($existingselector->find_users(''));
160 if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
161 require_sesskey();
162 $users = $recipientselector->get_selected_users();
163 foreach ($users as $user) {
164 if (process_manual_award($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
165 // If badge was successfully awarded, review manual badge criteria.
166 $data = new stdClass();
167 $data->crit = $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
168 $data->userid = $user->id;
169 badges_award_handle_manual_criteria_review($data);
170 } else {
171 echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
175 $recipientselector->invalidate_selected_users();
176 $existingselector->invalidate_selected_users();
177 $recipientselector->set_existing_recipients($existingselector->find_users(''));
178 } else if ($revoke && data_submitted() && has_capability('moodle/badges:revokebadge', $context)) {
179 require_sesskey();
180 $users = $existingselector->get_selected_users();
182 foreach ($users as $user) {
183 if (!process_manual_revoke($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
184 echo $OUTPUT->error_text(get_string('error:cannotrevokebadge', 'badges'));
188 $recipientselector->invalidate_selected_users();
189 $existingselector->invalidate_selected_users();
190 $recipientselector->set_existing_recipients($existingselector->find_users(''));
193 echo $OUTPUT->header();
194 echo $OUTPUT->heading($strrecipients);
196 if (count($acceptedroles) > 1) {
197 echo $OUTPUT->box($roleselect);
200 echo $output->recipients_selection_form($existingselector, $recipientselector);
201 echo $OUTPUT->footer();