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