Commit | Line | Data |
---|---|---|
2fa35b8d 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_group\output\user_groups_editable | |
19 | * | |
20 | * @package core_group | |
21 | * @copyright 2017 Damyon Wiese | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | namespace core_group\output; | |
26 | ||
27 | use context_system; | |
28 | use context_course; | |
29 | use core_user; | |
30 | use core_external; | |
31 | ||
32 | /** | |
33 | * Class to display list of user groups. | |
34 | * | |
35 | * @package core_group | |
36 | * @copyright 2017 Damyon Wiese | |
37 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
38 | */ | |
39 | class user_groups_editable extends \core\output\inplace_editable { | |
40 | ||
41 | /** @var $coursegroups */ | |
42 | private $coursegroups = null; | |
43 | /** @var $context */ | |
44 | private $context = null; | |
45 | ||
46 | /** | |
47 | * Constructor. | |
48 | * | |
49 | * @param \stdClass|core_tag_tag $tag | |
50 | */ | |
51 | public function __construct($course, $context, $user, $coursegroups, $value) { | |
52 | // Check capabilities to get editable value. | |
53 | $editable = has_capability('moodle/course:managegroups', $context); | |
54 | ||
55 | // Invent an itemid. | |
56 | $itemid = $course->id . ':' . $user->id; | |
57 | ||
58 | $value = json_encode($value); | |
59 | ||
60 | // Remember these for the display value. | |
61 | $this->coursegroups = $coursegroups; | |
62 | $this->context = $context; | |
63 | ||
64 | parent::__construct('core_group', 'user_groups', $itemid, $editable, $value, $value); | |
65 | ||
66 | // Assignable groups. | |
67 | $options = []; | |
68 | ||
69 | foreach ($coursegroups as $group) { | |
70 | $options[$group->id] = $group->name; | |
71 | } | |
72 | ||
73 | $attributes = ['multiple' => true]; | |
74 | $this->set_type_autocomplete($options, $attributes); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Export this data so it can be used as the context for a mustache template. | |
79 | * | |
80 | * @param \renderer_base $output | |
81 | * @return \stdClass | |
82 | */ | |
83 | public function export_for_template(\renderer_base $output) { | |
84 | // Set edithint and display value. | |
85 | $this->edithint = get_string('editusersgroups', 'group'); | |
86 | ||
87 | $listofgroups = []; | |
88 | $groupids = json_decode($this->value); | |
89 | foreach ($groupids as $id) { | |
90 | $listofgroups[] = format_string($this->coursegroups[$id]->name, true, ['context' => $this->context]); | |
91 | } | |
92 | ||
93 | $this->displayvalue = implode($listofgroups, ', '); | |
94 | return parent::export_for_template($output); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Updates the value in database and returns itself, called from inplace_editable callback | |
99 | * | |
100 | * @param int $itemid | |
101 | * @param mixed $newvalue | |
102 | * @return \self | |
103 | */ | |
104 | public static function update($itemid, $newvalue) { | |
105 | // Check caps. | |
106 | // Do the thing. | |
107 | // Return one of me. | |
108 | // Validate the inputs. | |
109 | list($courseid, $userid) = explode(':', $itemid, 2); | |
110 | ||
111 | $courseid = clean_param($courseid, PARAM_INT); | |
112 | $userid = clean_param($userid, PARAM_INT); | |
113 | $groupids = json_decode($newvalue); | |
114 | foreach ($groupids as $index => $groupid) { | |
115 | $groupids[$index] = clean_param($groupid, PARAM_INT); | |
116 | } | |
117 | ||
118 | // Check user is enrolled in the course. | |
119 | $context = context_course::instance($courseid); | |
120 | core_external::validate_context($context); | |
121 | ||
122 | if (!is_enrolled($context, $userid)) { | |
123 | throw new coding_exception('User does not belong to the course'); | |
124 | } | |
125 | ||
126 | // Check that all the groups belong to the course. | |
127 | $coursegroups = groups_get_all_groups($courseid, 0, 0, 'g.*', true); | |
128 | ||
129 | $byid = []; | |
130 | foreach ($groupids as $groupid) { | |
131 | if (!isset($coursegroups[$groupid])) { | |
132 | throw new coding_exception('Group does not belong to the course'); | |
133 | } | |
134 | $byid[$groupid] = $groupid; | |
135 | } | |
136 | $groupids = $byid; | |
137 | // Check permissions. | |
138 | require_capability('moodle/course:managegroups', $context); | |
139 | ||
140 | // Process adds. | |
141 | foreach ($groupids as $groupid) { | |
142 | if (!isset($coursegroups[$groupid]->members[$userid])) { | |
143 | // Add them. | |
144 | groups_add_member($groupid, $userid); | |
145 | // Keep this variable in sync. | |
146 | $coursegroups[$groupid]->members[$userid] = $userid; | |
147 | } | |
148 | } | |
149 | ||
150 | // Process removals. | |
151 | foreach ($coursegroups as $groupid => $group) { | |
152 | if (isset($group->members[$userid]) && !isset($groupids[$groupid])) { | |
153 | if (groups_remove_member_allowed($groupid, $userid)) { | |
154 | groups_remove_member($groupid, $userid); | |
155 | unset($coursegroups[$groupid]->members[$userid]); | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
160 | $course = get_course($courseid); | |
161 | $user = core_user::get_user($userid); | |
5a90a7ca | 162 | return new self($course, $context, $user, $coursegroups, array_values($groupids)); |
2fa35b8d DW |
163 | } |
164 | } |