Merge branch 'MDL-40255_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / badges / award.php
blob3d3aba7ec97da477a51f03f6b04084eb19bbbf61
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));
55 require_capability('moodle/badges:awardbadge', $context);
57 $url = new moodle_url('/badges/award.php', array('id' => $badgeid, 'role' => $role));
58 $PAGE->set_url($url);
59 $PAGE->set_context($context);
61 // Set up navigation and breadcrumbs.
62 $strrecipients = get_string('recipients', 'badges');
63 navigation_node::override_active_url($navurl);
64 $PAGE->navbar->add($badge->name, new moodle_url('overview.php', array('id' => $badge->id)))->add($strrecipients);
65 $PAGE->set_title($strrecipients);
66 $PAGE->set_heading($badge->name);
67 $PAGE->set_pagelayout('standard');
69 if (!$badge->is_active()) {
70 echo $OUTPUT->header();
71 echo $OUTPUT->notification(get_string('donotaward', 'badges'));
72 echo $OUTPUT->footer();
73 die();
76 $output = $PAGE->get_renderer('core', 'badges');
78 // Roles that can award this badge.
79 $acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
81 if (count($acceptedroles) > 1) {
82 // If there is more than one role that can award a badge, prompt user to make a selection.
83 // If it is an admin, include all accepted roles, otherwise only the ones that current user has in this context.
84 if ($isadmin) {
85 $selection = $acceptedroles;
86 } else {
87 // Get all the roles that user has and use the ones required by this badge.
88 $roles = get_user_roles($context, $USER->id);
89 $roleids = array_map(create_function('$o', 'return $o->roleid;'), $roles);
90 $selection = array_intersect($acceptedroles, $roleids);
92 list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
93 $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
94 foreach ($options as $p) {
95 $select[$p->id] = role_get_name($p);
97 if (!$role) {
98 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
99 echo $OUTPUT->header();
100 echo $OUTPUT->box(get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select));
101 echo $OUTPUT->footer();
102 die();
103 } else {
104 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
105 $issuerrole = new stdClass();
106 $issuerrole->roleid = $role;
107 $roleselect = get_string('selectaward', 'badges') . $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role);
109 } else {
110 // User has to be an admin or the one with the required role.
111 $users = get_role_users($acceptedroles[0], $context, false, 'u.id', 'u.id ASC');
112 $usersids = array_keys($users);
113 if (!$isadmin && !in_array($USER->id, $usersids)) {
114 echo $OUTPUT->header();
115 $rlink = html_writer::link(new moodle_url('recipients.php', array('id' => $badge->id)), get_string('recipients', 'badges'));
116 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $rlink));
117 echo $OUTPUT->footer();
118 die();
119 } else {
120 $issuerrole = new stdClass();
121 $issuerrole->roleid = $acceptedroles[0];
125 $options = array(
126 'badgeid' => $badge->id,
127 'context' => $context,
128 'issuerid' => $USER->id,
129 'issuerrole' => $issuerrole->roleid
131 $existingselector = new badge_existing_users_selector('existingrecipients', $options);
132 $recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
133 $recipientselector->set_existing_recipients($existingselector->find_users(''));
135 if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
136 require_sesskey();
137 $users = $recipientselector->get_selected_users();
138 foreach ($users as $user) {
139 if (process_manual_award($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
140 // If badge was successfully awarded, review manual badge criteria.
141 $data = new stdClass();
142 $data->crit = $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
143 $data->userid = $user->id;
144 badges_award_handle_manual_criteria_review($data);
145 } else {
146 echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
150 $recipientselector->invalidate_selected_users();
151 $existingselector->invalidate_selected_users();
152 $recipientselector->set_existing_recipients($existingselector->find_users(''));
155 echo $OUTPUT->header();
156 echo $OUTPUT->heading($strrecipients);
158 if (count($acceptedroles) > 1) {
159 echo $OUTPUT->box($roleselect);
162 echo $output->recipients_selection_form($existingselector, $recipientselector);
163 echo $OUTPUT->footer();