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 |
namespace core_user\output;
|
|
|
18 |
|
|
|
19 |
use context_course;
|
|
|
20 |
use core_user;
|
|
|
21 |
use core_external\external_api;
|
|
|
22 |
use coding_exception;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Class to display list of user roles.
|
|
|
26 |
*
|
|
|
27 |
* @package core_user
|
|
|
28 |
* @copyright 2017 Damyon Wiese
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class user_roles_editable extends \core\output\inplace_editable {
|
|
|
32 |
|
|
|
33 |
/** @var $context */
|
|
|
34 |
private $context = null;
|
|
|
35 |
|
|
|
36 |
/** @var \stdClass[] $courseroles */
|
|
|
37 |
private $courseroles;
|
|
|
38 |
|
|
|
39 |
/** @var \stdClass[] $profileroles */
|
|
|
40 |
private $profileroles;
|
|
|
41 |
|
|
|
42 |
/** @var \stdClass[] $viewableroles */
|
|
|
43 |
private $viewableroles;
|
|
|
44 |
|
|
|
45 |
/** @var \stdClass[] $assignableroles */
|
|
|
46 |
private $assignableroles;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Constructor.
|
|
|
50 |
*
|
|
|
51 |
* @param \stdClass $course The current course
|
|
|
52 |
* @param \context $context The course context
|
|
|
53 |
* @param \stdClass $user The current user
|
|
|
54 |
* @param \stdClass[] $courseroles The list of course roles.
|
|
|
55 |
* @param \stdClass[] $assignableroles The list of assignable roles in this course.
|
|
|
56 |
* @param \stdClass[] $profileroles The list of roles that should be visible in a users profile.
|
|
|
57 |
* @param \stdClass[] $userroles The list of user roles.
|
|
|
58 |
*/
|
|
|
59 |
public function __construct($course, $context, $user, $courseroles, $assignableroles, $profileroles, $userroles, $viewableroles = null) {
|
|
|
60 |
if ($viewableroles === null) {
|
|
|
61 |
debugging('Constructor for user_roles_editable now needs the result of get_viewable_roles passed as viewableroles');
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// Check capabilities to get editable value.
|
|
|
65 |
$editable = has_capability('moodle/role:assign', $context);
|
|
|
66 |
|
|
|
67 |
// Invent an itemid.
|
|
|
68 |
$itemid = $course->id . ':' . $user->id;
|
|
|
69 |
|
|
|
70 |
$getrole = function($role) {
|
|
|
71 |
return $role->roleid;
|
|
|
72 |
};
|
|
|
73 |
$ids = array_values(array_unique(array_map($getrole, $userroles)));
|
|
|
74 |
|
|
|
75 |
$value = json_encode($ids);
|
|
|
76 |
|
|
|
77 |
// Remember these for the display value.
|
|
|
78 |
$this->courseroles = $courseroles;
|
|
|
79 |
$this->profileroles = $profileroles;
|
|
|
80 |
$this->viewableroles = array_keys($viewableroles);
|
|
|
81 |
$this->assignableroles = array_keys($assignableroles);
|
|
|
82 |
$this->context = $context;
|
|
|
83 |
|
|
|
84 |
parent::__construct('core_user', 'user_roles', $itemid, $editable, $value, $value);
|
|
|
85 |
|
|
|
86 |
// Removed the roles that were assigned to the user at a different context.
|
|
|
87 |
$options = $assignableroles;
|
|
|
88 |
foreach ($userroles as $role) {
|
|
|
89 |
if (isset($assignableroles[$role->roleid])) {
|
|
|
90 |
if ($role->contextid != $context->id) {
|
|
|
91 |
unset($options[$role->roleid]);
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$fullname = htmlspecialchars(fullname($user), ENT_QUOTES, 'utf-8');
|
|
|
97 |
$this->edithint = get_string('xroleassignments', 'role', $fullname);
|
|
|
98 |
$this->editlabel = get_string('xroleassignments', 'role', $fullname);
|
|
|
99 |
|
|
|
100 |
$attributes = ['multiple' => true];
|
|
|
101 |
$this->set_type_autocomplete($options, $attributes);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Export this data so it can be used as the context for a mustache template.
|
|
|
106 |
*
|
|
|
107 |
* @param \renderer_base $output
|
|
|
108 |
* @return array
|
|
|
109 |
*/
|
|
|
110 |
public function export_for_template(\renderer_base $output) {
|
|
|
111 |
$listofroles = [];
|
|
|
112 |
$roleids = json_decode($this->value);
|
|
|
113 |
$viewableroleids = array_intersect($roleids, array_merge($this->viewableroles, $this->assignableroles));
|
|
|
114 |
|
|
|
115 |
foreach ($viewableroleids as $id) {
|
|
|
116 |
// If this is a student, we only show a subset of the roles.
|
|
|
117 |
if ($this->editable || array_key_exists($id, $this->profileroles)) {
|
|
|
118 |
$listofroles[] = format_string($this->courseroles[$id]->localname, true, ['context' => $this->context]);
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
if (!empty($listofroles)) {
|
|
|
123 |
$this->displayvalue = implode(', ', $listofroles);
|
|
|
124 |
} else if (!empty($roleids) && empty($viewableroleids)) {
|
|
|
125 |
$this->displayvalue = get_string('novisibleroles', 'role');
|
|
|
126 |
} else {
|
|
|
127 |
$this->displayvalue = get_string('noroles', 'role');
|
|
|
128 |
}
|
|
|
129 |
return parent::export_for_template($output);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Updates the value in database and returns itself, called from inplace_editable callback
|
|
|
134 |
*
|
|
|
135 |
* @param int $itemid
|
|
|
136 |
* @param mixed $newvalue
|
|
|
137 |
* @return \self
|
|
|
138 |
*/
|
|
|
139 |
public static function update($itemid, $newvalue) {
|
|
|
140 |
global $DB;
|
|
|
141 |
|
|
|
142 |
// Check caps.
|
|
|
143 |
// Do the thing.
|
|
|
144 |
// Return one of me.
|
|
|
145 |
// Validate the inputs.
|
|
|
146 |
list($courseid, $userid) = explode(':', $itemid, 2);
|
|
|
147 |
|
|
|
148 |
$courseid = clean_param($courseid, PARAM_INT);
|
|
|
149 |
$userid = clean_param($userid, PARAM_INT);
|
|
|
150 |
$roleids = json_decode($newvalue);
|
|
|
151 |
foreach ($roleids as $index => $roleid) {
|
|
|
152 |
$roleids[$index] = clean_param($roleid, PARAM_INT);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
// Check user is enrolled in the course.
|
|
|
156 |
$context = context_course::instance($courseid);
|
|
|
157 |
external_api::validate_context($context);
|
|
|
158 |
|
|
|
159 |
// Check permissions.
|
|
|
160 |
require_capability('moodle/role:assign', $context);
|
|
|
161 |
|
|
|
162 |
if (!is_enrolled($context, $userid)) {
|
|
|
163 |
throw new coding_exception('User does not belong to the course');
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
// Check that all the groups belong to the course.
|
|
|
167 |
$allroles = role_fix_names(get_all_roles($context), $context, ROLENAME_BOTH);
|
|
|
168 |
$assignableroles = get_assignable_roles($context, ROLENAME_BOTH, false);
|
|
|
169 |
$viewableroles = get_viewable_roles($context);
|
|
|
170 |
$userrolesbyid = get_user_roles($context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC');
|
|
|
171 |
$profileroles = get_profile_roles($context);
|
|
|
172 |
|
|
|
173 |
// Set an array where the index is the roleid.
|
|
|
174 |
$userroles = array();
|
|
|
175 |
foreach ($userrolesbyid as $id => $role) {
|
|
|
176 |
$userroles[$role->roleid] = $role;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
$rolestoprocess = [];
|
|
|
180 |
foreach ($roleids as $roleid) {
|
|
|
181 |
if (!isset($assignableroles[$roleid])) {
|
|
|
182 |
throw new coding_exception('Role cannot be assigned in this course.');
|
|
|
183 |
}
|
|
|
184 |
$rolestoprocess[$roleid] = $roleid;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
// Process adds.
|
|
|
188 |
foreach ($rolestoprocess as $roleid) {
|
|
|
189 |
if (!isset($userroles[$roleid])) {
|
|
|
190 |
// Add them.
|
|
|
191 |
$id = role_assign($roleid, $userid, $context);
|
|
|
192 |
// Keep this variable in sync.
|
|
|
193 |
$role = new \stdClass();
|
|
|
194 |
$role->id = $id;
|
|
|
195 |
$role->roleid = $roleid;
|
|
|
196 |
$role->contextid = $context->id;
|
|
|
197 |
$userroles[$role->roleid] = $role;
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
// Process removals.
|
|
|
202 |
foreach ($assignableroles as $roleid => $rolename) {
|
|
|
203 |
if (isset($userroles[$roleid]) && !isset($rolestoprocess[$roleid])) {
|
|
|
204 |
// Do not remove the role if we are not in the same context.
|
|
|
205 |
if ($userroles[$roleid]->contextid != $context->id) {
|
|
|
206 |
continue;
|
|
|
207 |
}
|
|
|
208 |
$ras = $DB->get_records('role_assignments', ['contextid' => $context->id, 'userid' => $userid,
|
|
|
209 |
'roleid' => $roleid]);
|
|
|
210 |
$allremoved = true;
|
|
|
211 |
foreach ($ras as $ra) {
|
|
|
212 |
if ($ra->component) {
|
|
|
213 |
if (strpos($ra->component, 'enrol_') !== 0) {
|
|
|
214 |
continue;
|
|
|
215 |
}
|
|
|
216 |
if (!$plugin = enrol_get_plugin(substr($ra->component, 6))) {
|
|
|
217 |
continue;
|
|
|
218 |
}
|
|
|
219 |
if ($plugin->roles_protected()) {
|
|
|
220 |
$allremoved = false;
|
|
|
221 |
continue;
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
role_unassign($ra->roleid, $ra->userid, $ra->contextid, $ra->component, $ra->itemid);
|
|
|
225 |
}
|
|
|
226 |
if ($allremoved) {
|
|
|
227 |
unset($userroles[$roleid]);
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
$course = get_course($courseid);
|
|
|
233 |
$user = core_user::get_user($userid);
|
|
|
234 |
return new self($course, $context, $user, $allroles, $assignableroles, $profileroles, $userroles, $viewableroles);
|
|
|
235 |
}
|
|
|
236 |
}
|