source: moodle/trunk/fuentes/badges/award.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 7.2 KB
Line 
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.
13//
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/>.
16
17/**
18 * Handle manual badge award.
19 *
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>
25 */
26
27require_once(dirname(dirname(__FILE__)) . '/config.php');
28require_once($CFG->libdir . '/badgeslib.php');
29require_once($CFG->dirroot . '/badges/lib/awardlib.php');
30
31$badgeid = required_param('id', PARAM_INT);
32$role = optional_param('role', 0, PARAM_INT);
33$award = optional_param('award', false, PARAM_BOOL);
34
35require_login();
36
37if (empty($CFG->enablebadges)) {
38    print_error('badgesdisabled', 'badges');
39}
40
41$badge = new badge($badgeid);
42$context = $badge->get_context();
43$isadmin = is_siteadmin($USER);
44
45$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
46
47if ($badge->type == BADGE_TYPE_COURSE) {
48    if (empty($CFG->badges_allowcoursebadges)) {
49        print_error('coursebadgesdisabled', 'badges');
50    }
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);
58}
59
60require_capability('moodle/badges:awardbadge', $context);
61
62$url = new moodle_url('/badges/award.php', array('id' => $badgeid, 'role' => $role));
63$PAGE->set_url($url);
64$PAGE->set_context($context);
65
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);
71
72if (!$badge->is_active()) {
73    echo $OUTPUT->header();
74    echo $OUTPUT->notification(get_string('donotaward', 'badges'));
75    echo $OUTPUT->footer();
76    die();
77}
78
79$output = $PAGE->get_renderer('core', 'badges');
80
81// Roles that can award this badge.
82$acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
83
84if (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();
90}
91
92if (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);
102    }
103
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);
109        }
110        if (!$role) {
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();
116            die();
117        } else {
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')));
123        }
124    } else {
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();
129        die();
130    }
131} else {
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();
140        die();
141    } else {
142        $issuerrole = new stdClass();
143        $issuerrole->roleid = $acceptedroles[0];
144    }
145}
146
147$options = array(
148        'badgeid' => $badge->id,
149        'context' => $context,
150        'issuerid' => $USER->id,
151        'issuerrole' => $issuerrole->roleid
152        );
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(''));
156
157if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
158    require_sesskey();
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);
167        } else {
168            echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
169        }
170    }
171
172    $recipientselector->invalidate_selected_users();
173    $existingselector->invalidate_selected_users();
174    $recipientselector->set_existing_recipients($existingselector->find_users(''));
175}
176
177echo $OUTPUT->header();
178echo $OUTPUT->heading($strrecipients);
179
180if (count($acceptedroles) > 1) {
181    echo $OUTPUT->box($roleselect);
182}
183
184echo $output->recipients_selection_form($existingselector, $recipientselector);
185echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.