Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
27
require_once(__DIR__ . '/../config.php');
28
require_once($CFG->libdir . '/badgeslib.php');
29
require_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
$revoke = optional_param('revoke', false, PARAM_BOOL);
35
 
36
require_login();
37
 
38
if (empty($CFG->enablebadges)) {
39
    throw new \moodle_exception('badgesdisabled', 'badges');
40
}
41
 
42
$badge = new badge($badgeid);
43
$context = $badge->get_context();
44
$isadmin = is_siteadmin($USER);
45
 
46
$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
47
 
48
if ($badge->type == BADGE_TYPE_COURSE) {
49
    if (empty($CFG->badges_allowcoursebadges)) {
50
        throw new \moodle_exception('coursebadgesdisabled', 'badges');
51
    }
52
    require_login($badge->courseid);
53
    $course = get_course($badge->courseid);
54
    $heading = format_string($course->fullname, true, ['context' => $context]);
55
    $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
56
    $PAGE->set_pagelayout('standard');
57
    navigation_node::override_active_url($navurl);
58
} else {
59
    $PAGE->set_pagelayout('admin');
60
    $heading = get_string('administrationsite');
61
    navigation_node::override_active_url($navurl, true);
62
}
63
 
64
require_capability('moodle/badges:awardbadge', $context);
65
 
66
$url = new moodle_url('/badges/award.php', array('id' => $badgeid, 'role' => $role));
67
$PAGE->set_url($url);
68
$PAGE->set_context($context);
69
 
70
// Set up navigation and breadcrumbs.
71
$strrecipients = get_string('recipients', 'badges');
72
$PAGE->navbar->add($badge->name, new moodle_url('overview.php', array('id' => $badge->id)))
73
    ->add($strrecipients, new moodle_url('recipients.php', array('id' => $badge->id)))
74
    ->add(get_string('award', 'badges'));
75
$PAGE->set_title($strrecipients);
76
$PAGE->set_heading($heading);
77
 
78
if (!$badge->is_active()) {
79
    echo $OUTPUT->header();
80
    echo $OUTPUT->notification(get_string('donotaward', 'badges'));
81
    echo $OUTPUT->footer();
82
    die();
83
}
84
 
85
$returnurl = new moodle_url('recipients.php', array('id' => $badge->id));
86
$returnlink = html_writer::link($returnurl, $strrecipients);
1441 ariadna 87
$actionbar = new \core_badges\output\standard_action_bar(
88
    page: $PAGE,
89
    type: $badge->type,
90
    showaddbadge: false,
91
    backurl: $returnurl
92
);
1 efrain 93
$output = $PAGE->get_renderer('core', 'badges');
94
$tertiarynav = $output->render_tertiary_navigation($actionbar);
95
 
96
// Roles that can award this badge.
97
$acceptedroles = array_keys($badge->criteria[BADGE_CRITERIA_TYPE_MANUAL]->params);
98
 
99
if (empty($acceptedroles)) {
100
    echo $OUTPUT->header();
101
    echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
102
    echo $OUTPUT->footer();
103
    die();
104
}
105
 
106
// Get groupmode and currentgroup before going further.
107
$groupmode = groups_get_course_groupmode($COURSE);  // Groups are being used.
108
$currentgroup = groups_get_course_group($COURSE, true); // Get active group.
109
 
110
// Check groupmode (SEPARATEGROUPS), currentgroup and capability (or admin).
111
if ($groupmode == SEPARATEGROUPS && empty($currentgroup) &&
112
    !has_capability('moodle/site:accessallgroups', $context) && !is_siteadmin() ) {
113
    echo $OUTPUT->header();
114
    echo $OUTPUT->heading(get_string("notingroup"));
115
    echo $OUTPUT->footer();
116
    die();
117
}
118
 
119
if (count($acceptedroles) > 1) {
120
    // If there is more than one role that can award a badge, prompt user to make a selection.
121
    // If it is an admin, include all accepted roles, otherwise only the ones that current user has in this context.
122
    if ($isadmin) {
123
        $selection = $acceptedroles;
124
    } else {
125
        // Get all the roles that user has and use the ones required by this badge.
126
        $roles = get_user_roles($context, $USER->id);
127
        $roleids = array_map(function($o) {
128
            return $o->roleid;
129
        }, $roles);
130
        $selection = array_intersect($acceptedroles, $roleids);
131
    }
132
 
133
    if (!empty($selection)) {
134
        list($usertest, $userparams) = $DB->get_in_or_equal($selection, SQL_PARAMS_NAMED, 'existing', true);
135
        $options = $DB->get_records_sql('SELECT * FROM {role} WHERE id ' . $usertest, $userparams);
136
        foreach ($options as $p) {
137
            $select[$p->id] = role_get_name($p);
138
        }
139
        if (!$role) {
140
            $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
141
            echo $OUTPUT->header();
142
            echo $tertiarynav;
143
            echo $OUTPUT->box($OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, '', array('' => 'choosedots'),
144
                null, array('label' => get_string('selectaward', 'badges'))));
145
            echo $OUTPUT->footer();
146
            die();
147
        } else {
148
            $pageurl = new moodle_url('/badges/award.php', array('id' => $badgeid));
149
            $issuerrole = new stdClass();
150
            $issuerrole->roleid = $role;
151
            $roleselect = $OUTPUT->single_select(new moodle_url($pageurl), 'role', $select, $role, null, null,
152
                array('label' => get_string('selectaward', 'badges')));
153
        }
154
    } else {
155
        echo $OUTPUT->header();
156
        echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
157
        echo $OUTPUT->footer();
158
        die();
159
    }
160
} else {
161
    // User has to be an admin or the one with the required role.
162
    $users = get_role_users($acceptedroles[0], $context, true, 'u.id', 'u.id ASC');
163
    $usersids = array_keys($users);
164
    if (!$isadmin && !in_array($USER->id, $usersids)) {
165
        echo $OUTPUT->header();
166
        echo $OUTPUT->notification(get_string('notacceptedrole', 'badges', $returnlink));
167
        echo $OUTPUT->footer();
168
        die();
169
    } else {
170
        $issuerrole = new stdClass();
171
        $issuerrole->roleid = $acceptedroles[0];
172
    }
173
}
174
 
175
$options = array(
176
        'badgeid' => $badge->id,
177
        'context' => $context,
178
        'issuerid' => $USER->id,
179
        'issuerrole' => $issuerrole->roleid,
180
        'currentgroup' => $currentgroup,
181
        'url' => $url,
182
    );
183
$existingselector = new badge_existing_users_selector('existingrecipients', $options);
184
$recipientselector = new badge_potential_users_selector('potentialrecipients', $options);
185
$recipientselector->set_existing_recipients($existingselector->find_users(''));
186
 
187
if ($award && data_submitted() && has_capability('moodle/badges:awardbadge', $context)) {
188
    require_sesskey();
189
    $users = $recipientselector->get_selected_users();
190
    foreach ($users as $user) {
191
        if (process_manual_award($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
192
            // If badge was successfully awarded, review manual badge criteria.
193
            $data = new stdClass();
194
            $data->crit = $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
195
            $data->userid = $user->id;
196
            badges_award_handle_manual_criteria_review($data);
197
        } else {
198
            echo $OUTPUT->error_text(get_string('error:cannotawardbadge', 'badges'));
199
        }
200
    }
201
 
202
    $recipientselector->invalidate_selected_users();
203
    $existingselector->invalidate_selected_users();
204
    $recipientselector->set_existing_recipients($existingselector->find_users(''));
205
} else if ($revoke && data_submitted() && has_capability('moodle/badges:revokebadge', $context)) {
206
    require_sesskey();
207
    $users = $existingselector->get_selected_users();
208
 
209
    foreach ($users as $user) {
210
        if (!process_manual_revoke($user->id, $USER->id, $issuerrole->roleid, $badgeid)) {
211
            echo $OUTPUT->error_text(get_string('error:cannotrevokebadge', 'badges'));
212
        }
213
    }
214
 
215
    $recipientselector->invalidate_selected_users();
216
    $existingselector->invalidate_selected_users();
217
    $recipientselector->set_existing_recipients($existingselector->find_users(''));
218
}
219
 
220
echo $OUTPUT->header();
221
echo $tertiarynav;
222
echo $OUTPUT->heading($strrecipients);
223
 
224
// Print group selector/dropdown menu (find out current groups mode).
225
groups_print_course_menu($COURSE, $url);
226
 
227
if (count($acceptedroles) > 1) {
228
    echo $OUTPUT->box($roleselect);
229
}
230
 
231
echo $output->recipients_selection_form($existingselector, $recipientselector);
232
echo $OUTPUT->footer();