Repository navigation
Expand file tree
/
Copy pathblock_groups.php
More file actions
229 lines (212 loc) · 8.19 KB
/
Copy pathblock_groups.php
File metadata and controls
229 lines (212 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The File for the block groups class.
*
* Displays a group and grouping block.
*
* @package block_groups
* @copyright 2016/17 N Herrmann
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/blocks/groups/locallib.php');
/**
* The block_group class
*
* Displays a group and grouping block.
*
* @package block_groups
* @copyright 2016/17 N Herrmann
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_groups extends block_base {
/**
* Initializes the block.
*/
public function init() {
$this->title = get_string('pluginname', 'block_groups');
}
/**
* Returns the content object.
*
* @return object $this->content
*/
public function get_content() {
// Record the current course.
global $COURSE;
// Records the capability to manage courses.
$access = has_capability('moodle/course:managegroups', context_course::instance($COURSE->id));
if ($this->content !== null) {
return $this->content;
}
if (empty($this->instance)) {
$this->content = '';
return $this->content;
}
$this->content = new stdClass();
$this->content->text = '';
if ($access === true) {
$this->content->text .= $this->get_content_teaching();
}
if ($access === false) {
$this->title = get_string('pluginname2', 'block_groups');
}
$this->content->text .= $this->get_content_groupmembers();
return $this->content;
}
/**
* The Block is only available at course-view pages
*
* @return array of applicable formats.
*/
public function applicable_formats() {
return ['course-view' => true, 'mod' => false, 'my' => false];
}
/**
* Returns a html representation of all existing groups and groupings
*
* @return string html-string
*/
private function get_content_teaching() {
global $COURSE, $DB;
$courseid = $COURSE->id;
// Array to save all groups.
$allgroups = groups_get_all_groups($courseid);
// Array to save all groupings.
$allgroupings = groups_get_all_groupings($courseid);
$content = '';
/** @var \block_groups\output\renderer $renderer */
$renderer = $this->page->get_renderer('block_groups');
// Calls Javascript if available.
$this->page->requires->js_call_amd('block_groups/blocks_groups_visibility', 'initialise', [$courseid]);
$groupsarray = [];
foreach ($allgroups as $value) {
// Checks availability of group and requests the content.
if (is_object($value) && property_exists($value, 'name')) {
$countmembers = count(groups_get_members($value->id));
$href = new moodle_url(
'/blocks/groups/changevisibility.php',
['courseid' => $courseid, 'groupid' => $value->id]
);
if (empty($DB->get_records('block_groups_hide', ['id' => $value->id]))) {
$groupsarray[] = $renderer->get_string_group($value, $href, $countmembers, true);
} else {
$groupsarray[] = $renderer->get_string_group($value, $href, $countmembers, false);
}
}
}
// Empty block or block with checkboxes.
$href = new moodle_url('/group/index.php', ['id' => $courseid]);
if (count($groupsarray) == 0) {
$content .= html_writer::div(get_string('nogroups', 'block_groups'));
$content .= $renderer->get_link_modify_groups($href);
} else {
// Since 3.3-r5 shows line with link to change all groups.
$content .= $renderer->change_all_groups();
$groupingsarray = $this->build_grouping_array($allgroupings, $courseid);
if (!empty($groupingsarray)) {
$content .= $renderer->teaching_groups_or_groupings_list($groupingsarray, false);
}
$content .= $renderer->teaching_groups_or_groupings_list($groupsarray, true);
$content .= $renderer->get_link_modify_groups($href);
}
return $content;
}
/**
* Returns all registered groups.
* @return string html-string representing all member groups
*/
private function get_content_groupmembers() {
global $COURSE, $DB;
$enrolledgroups = [];
$allgroups = groups_get_my_groups();
// Necessary to show hidden groups to Course Managers.
$access = has_capability('moodle/course:managegroups', context_course::instance($COURSE->id));
/** @var \block_groups\output\renderer $renderer */
$renderer = $this->page->get_renderer('block_groups');
foreach ($allgroups as $group) {
if (($group->courseid == $COURSE->id)) {
$groupdbentry = $DB->get_records('block_groups_hide', ['id' => $group->id]);
if (!empty($groupdbentry)) {
$enrolledgroups[] = $renderer->get_tag_group($group, true);
} else if ($access === true) {
$enrolledgroups[] = $renderer->get_tag_group($group, false);
}
}
}
// Returns an empty list of groups.
if (empty($enrolledgroups)) {
$content = '';
return $content;
}
$content = $renderer->get_membership_content($enrolledgroups);
return $content;
}
/**
* Generates an array of groupingnames and their members.
*
* @param array $allgroupings
* @param integer $courseid
* @return array every key is a group id and point to a grouping
*/
public function build_grouping_array($allgroupings, $courseid) {
/** @var \block_groups\output\renderer $renderer */
$renderer = $this->page->get_renderer('block_groups');
$groupingsarray = [];
$arrayofmembers = count_grouping_members($courseid);
foreach ($allgroupings as $g => $value) {
if (is_object($value) && property_exists($value, 'name')) {
// Necessary DB query to prohibit multiple ids of grouping members.
$countgroupingmember = 0;
if (isset($arrayofmembers[$value->id])) {
$countgroupingmember = $arrayofmembers[$value->id]->number;
}
$groupnames = $this->get_groups_for_grouping($value->id, $courseid);
$groupingsarray[$g] = $renderer->get_grouping($value, $countgroupingmember, $groupnames);
}
}
return $groupingsarray;
}
/**
* Returns the names of all groups belonging to a grouping.
*
* @param integer $groupingid
* @param integer $courseid
* @return array
* @throws dml_exception
*/
private function get_groups_for_grouping($groupingid, $courseid) {
global $DB;
$sql = "SELECT g.id, g.name
FROM {groups} g
JOIN {groupings_groups} gg ON gg.groupid = g.id
WHERE gg.groupingid = :groupingid
AND g.courseid = :courseid
ORDER BY g.name ASC";
return $DB->get_records_sql($sql, [
'groupingid' => $groupingid,
'courseid' => $courseid,
]);
}
/**
* Tells moodle, that the groups block has a settings file.
* @return bool true
*/
public function has_config() {
return true;
}
}