Merge branch 'MDL-46078-28' of git://github.com/damyon/moodle into MOODLE_28_STABLE
[moodle.git] / badges / award.php
blobf60bf39c876ff53a5ac56a7fa4bc5e3dc1b4de58
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(dirname(dirname(__FILE__)) . '/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);
35 require_login();
37 if (empty($CFG->enablebadges)) {
38 print_error('badgesdisabled', 'badges');
41 $badge = new badge($badgeid);
42 $context = $badge->get_context();
43 $isadmin = is_siteadmin($USER);
45 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
47 if ($badge->type == BADGE_TYPE_COURSE) {
48 if (empty($CFG->badges_allowcoursebadges)) {
49 print_error('coursebadgesdisabled', 'badges');
51 require_login($badge->courseid);
52 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
53 $PAGE->set_pagelayout('standard');
54 navigation_node::override_active_url($navurl);
55 } else {
56 $PAGE->set_pagelayout('admin');
57 navigation_node::override_active_url($navurl, true);
60 require_capability('moodle/badges:awardbadge', $context);
62 $url = new moodle_url('/badges/award.php', array('id' => $badgeid, 'role' => $role));
63 $PAGE->set_url($url);
64 $PAGE->set_context($context);
66 // Set up navigation and breadcrumbs.
67 $strrecipients = get_string('recipients', 'badges');
68 $PAGE->navbar->add($badge->name, new moodle_url('overview.php', array('id' => $badge->id)))->add($strrecipients);
69 $PAGE->set_title($strrecipients);
70 $PAGE->set_heading($badge->name);
72 if (!$badge->is_active()) {
73 echo $OUTPUT->header();
74 echo $OUTPUT->notification(get_string('donotaward', 'badges'));
75 echo $OUTPUT->footer();
76 die();
79 $output = $PAGE->get_renderer('core', 'badges');
81 // Roles that can award this badge.
82 $acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
84 if (empty($acceptedroles)) {
85 echo $OUTPUT->header();
86 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
87 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
88 echo $OUTPUT->footer();
89 die();
92 if (count($acceptedroles) > 1) {
93 // If there is more than one role that can award a badge, prompt user to make a selection.
94 // If it is an admin, include all accepted roles, otherwise only the ones that current user has in this context.
95 if ($isadmin) {
96 $selection = $acceptedroles;
97 } else {
98 // Get all the roles that user has and use the ones required by this badge.
99 $roles = get_user_roles($context, $USER->id);
100 $roleids = array_map(create_function('$o', 'return $o->roleid;'), $roles);
101 $selection = array_intersect($acceptedroles, $roleids);
104 if (!empty($selection)) {
105 list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
106 $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
107 foreach ($options as $p) {
108 $select[$p->id] = role_get_name($p);
110 if (!$role) {
111 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
112 echo $OUTPUT->header();
113 echo $OUTPUT->box(get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select));
114 echo $OUTPUT->footer();
115 die();
116 } else {
117 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
118 $issuerrole = new stdClass();
119 $issuerrole->roleid = $role;
120 $roleselect = get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role, null);
122 } else {
123 echo $OUTPUT->header();
124 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
125 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
126 echo $OUTPUT->footer();
127 die();
129 } else {
130 // User has to be an admin or the one with the required role.
131 $users = get_role_users($acceptedroles[0], $context, true, 'u.id', 'u.id ASC');
132 $usersids = array_keys($users);
133 if (!$isadmin && !in_array($USER->id, $usersids)) {
134 echo $OUTPUT->header();
135 $return = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), $strrecipients);
136 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
137 echo $OUTPUT->footer();
138 die();
139 } else {
140 $issuerrole = new stdClass();
141 $issuerrole->roleid = $acceptedroles[0];
145 $options = array(
146 'badgeid' => $badge->id,
147 'context' => $context,
148 'issuerid' => $USER->id,
149 'issuerrole' => $issuerrole->roleid
151 $existingselector = new badge_existing_users_selector('existingrecipients', $options);
152 $recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
153 $recipientselector->set_existing_recipients($existingselector->find_users(''));
155 if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
156 require_sesskey();
157 $users = $recipientselector->get_selected_users();
158 foreach ($users as $user) {
159 if (process_manual_award($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
160 // If badge was successfully awarded, review manual badge criteria.
161 $data = new stdClass();
162 $data->crit = $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
163 $data->userid = $user->id;
164 badges_award_handle_manual_criteria_review($data);
165 } else {
166 echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
170 $recipientselector->invalidate_selected_users();
171 $existingselector->invalidate_selected_users();
172 $recipientselector->set_existing_recipients($existingselector->find_users(''));
175 echo $OUTPUT->header();
176 echo $OUTPUT->heading($strrecipients);
178 if (count($acceptedroles) > 1) {
179 echo $OUTPUT->box($roleselect);
182 echo $output->recipients_selection_form($existingselector, $recipientselector);
183 echo $OUTPUT->footer();