Merge branch 'MDL-61620_33' of git://github.com/dmonllao/moodle into MOODLE_33_STABLE
[moodle.git] / badges / award.php
blob77195f0fecaa201a122827e5c53b2dca698c3cab
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(create_function('$o', 'return $o->roleid;'), $roles);
102 $selection = array_intersect($acceptedroles, $roleids);
105 if (!empty($selection)) {
106 list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
107 $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
108 foreach ($options as $p) {
109 $select[$p->id] = role_get_name($p);
111 if (!$role) {
112 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
113 echo $OUTPUT->header();
114 echo $OUTPUT->box($OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, '', array('' => 'choosedots'),
115 null, array('label' => get_string('selectaward', 'badges'))));
116 echo $OUTPUT->footer();
117 die();
118 } else {
119 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
120 $issuerrole = new stdClass();
121 $issuerrole->roleid = $role;
122 $roleselect = $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role, null, null,
123 array('label' => get_string('selectaward', 'badges')));
125 } else {
126 echo $OUTPUT->header();
127 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
128 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
129 echo $OUTPUT->footer();
130 die();
132 } else {
133 // User has to be an admin or the one with the required role.
134 $users = get_role_users($acceptedroles[0], $context, true, 'u.id', 'u.id ASC');
135 $usersids = array_keys($users);
136 if (!$isadmin && !in_array($USER->id, $usersids)) {
137 echo $OUTPUT->header();
138 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
139 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
140 echo $OUTPUT->footer();
141 die();
142 } else {
143 $issuerrole = new stdClass();
144 $issuerrole->roleid = $acceptedroles[0];
148 $options = array(
149 'badgeid' => $badge->id,
150 'context' => $context,
151 'issuerid' => $USER->id,
152 'issuerrole' => $issuerrole->roleid
154 $existingselector = new badge_existing_users_selector('existingrecipients', $options);
155 $recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
156 $recipientselector->set_existing_recipients($existingselector->find_users(''));
158 if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
159 require_sesskey();
160 $users = $recipientselector->get_selected_users();
161 foreach ($users as $user) {
162 if (process_manual_award($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
163 // If badge was successfully awarded, review manual badge criteria.
164 $data = new stdClass();
165 $data->crit = $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
166 $data->userid = $user->id;
167 badges_award_handle_manual_criteria_review($data);
168 } else {
169 echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
173 $recipientselector->invalidate_selected_users();
174 $existingselector->invalidate_selected_users();
175 $recipientselector->set_existing_recipients($existingselector->find_users(''));
176 } else if ($revoke && data_submitted() && has_capability('moodle/badges:revokebadge', $context)) {
177 require_sesskey();
178 $users = $existingselector->get_selected_users();
180 foreach ($users as $user) {
181 if (!process_manual_revoke($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
182 echo $OUTPUT->error_text(get_string('error:cannotrevokebadge', 'badges'));
186 $recipientselector->invalidate_selected_users();
187 $existingselector->invalidate_selected_users();
188 $recipientselector->set_existing_recipients($existingselector->find_users(''));
191 echo $OUTPUT->header();
192 echo $OUTPUT->heading($strrecipients);
194 if (count($acceptedroles) > 1) {
195 echo $OUTPUT->box($roleselect);
198 echo $output->recipients_selection_form($existingselector, $recipientselector);
199 echo $OUTPUT->footer();