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