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