2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Handle manual badge award.
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
);
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);
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));
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();
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();
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.
96 $selection = $acceptedroles;
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);
111 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
112 echo $OUTPUT->header();
113 echo $OUTPUT->box($OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, '', array('' => 'choosedots'),
114 null, array('label' => get_string('selectaward', 'badges'))));
115 echo $OUTPUT->footer();
118 $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
119 $issuerrole = new stdClass();
120 $issuerrole->roleid
= $role;
121 $roleselect = $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role, null, null,
122 array('label' => get_string('selectaward', 'badges')));
125 echo $OUTPUT->header();
126 $return = html_writer
::link(new moodle_url('recipients.php', array('id' => $badge->id
)), $strrecipients);
127 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
128 echo $OUTPUT->footer();
132 // User has to be an admin or the one with the required role.
133 $users = get_role_users($acceptedroles[0], $context, true, 'u.id', 'u.id ASC');
134 $usersids = array_keys($users);
135 if (!$isadmin && !in_array($USER->id
, $usersids)) {
136 echo $OUTPUT->header();
137 $return = html_writer
::link(new moodle_url('recipients.php', array('id' => $badge->id
)), $strrecipients);
138 echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $return));
139 echo $OUTPUT->footer();
142 $issuerrole = new stdClass();
143 $issuerrole->roleid
= $acceptedroles[0];
148 'badgeid' => $badge->id
,
149 'context' => $context,
150 'issuerid' => $USER->id
,
151 'issuerrole' => $issuerrole->roleid
153 $existingselector = new badge_existing_users_selector('existingrecipients', $options);
154 $recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
155 $recipientselector->set_existing_recipients($existingselector->find_users(''));
157 if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
159 $users = $recipientselector->get_selected_users();
160 foreach ($users as $user) {
161 if (process_manual_award($user->id
, $USER->id
, $issuerrole->roleid
, $badgeid)) {
162 // If badge was successfully awarded, review manual badge criteria.
163 $data = new stdClass();
164 $data->crit
= $badge->criteria
[BADGE_CRITERIA_TYPE_MANUAL
];
165 $data->userid
= $user->id
;
166 badges_award_handle_manual_criteria_review($data);
168 echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
172 $recipientselector->invalidate_selected_users();
173 $existingselector->invalidate_selected_users();
174 $recipientselector->set_existing_recipients($existingselector->find_users(''));
177 echo $OUTPUT->header();
178 echo $OUTPUT->heading($strrecipients);
180 if (count($acceptedroles) > 1) {
181 echo $OUTPUT->box($roleselect);
184 echo $output->recipients_selection_form($existingselector, $recipientselector);
185 echo $OUTPUT->footer();