forked from learnweb/moodle-block_groups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
237 lines (224 loc) · 9.69 KB
/
externallib.php
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
230
231
232
233
234
235
236
237
<?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/>.
/**
* External block_groups API
*
* @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->libdir/externallib.php");
/**
* block_groups external functions
*
* @copyright 2016/17 N Herrmann
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class block_groups_visibility_change extends external_api{
/**
* Specifies the input parameters.
* @return external_function_parameters
*/
public static function create_output_parameters() {
return new external_function_parameters(
array(
'groups' => new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'id of group'),
'courseid' => new external_value(PARAM_INT, 'id of course'),
)
)
)
);
}
/**
* Specifies the output parameters.
* @return external_single_structure
*/
public static function create_output_returns() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'id of group'),
'courseid' => new external_value(PARAM_INT, 'id of course'),
'newelement' => new external_value(PARAM_RAW, 'replace html-element'),
'visibility' => new external_value(PARAM_INT, 'returns the visibility value')
)
);
}
/**
* Changed the Database and returns the updated html content.
* @param array $groups
* @return array of output_parameters
*/
public static function create_output($groups) {
global $PAGE, $CFG, $DB;
$params = self::validate_parameters(self::create_output_parameters(), array('groups' => $groups));
$PAGE->set_context(context_course::instance($params['groups']['courseid']));
require_capability('moodle/course:managegroups', context_course::instance($params['groups']['courseid']));
require_once($CFG->dirroot.'/blocks/groups/locallib.php');
block_groups_db_transaction_change_visibility($params['groups']['id'], $params['groups']['courseid']);
$renderer = $PAGE->get_renderer('block_groups');
$href = new moodle_url('/blocks/groups/changevisibility.php', array('courseid' => $params['groups']['courseid'],
'groupid' => $params['groups']['id']));
$countmembers = count(groups_get_members($params['groups']['id']));
$group = groups_get_group($params['groups']['id']);
$output = array('id' => $params['groups']['id'], 'courseid' => $params['groups']['courseid']);
// Generates the Output component.
$groupvisible = $DB->get_records('block_groups_hide', array('id' => $params['groups']['id']));
if (empty($groupvisible)) {
$output['newelement'] = $renderer->get_string_group($group, $href, $countmembers, true);
$output['visibility'] = 1;
}
if (!empty($groupvisible)) {
$output['newelement'] = $renderer->get_string_group($group, $href, $countmembers, false);
$output['visibility'] = 0;
}
return $output;
}
}
/**
* Class to implement the External Api to change all groups
*
* @package block_groups
* @copyright 2016/17 N Herrmann
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_groups_visibilityall_change extends external_api{
/**
* Specifies the input parameters.
*/
public static function create_allgroups_output_parameters() {
return new external_function_parameters(
array(
'groups' => new external_single_structure(
array(
// Insert 1 for hide groups 0 for show groups.
'action' => new external_value(PARAM_RAW, 'action'),
'courseid' => new external_value(PARAM_INT, 'id of course'),
)
)
)
);
}
/**
* Specifies the output parameters.
*/
public static function create_allgroups_output_returns() {
return new external_single_structure(
array(
'courseid' => new external_value(PARAM_INT, 'id of course'),
'newelement' => new external_value(PARAM_RAW, 'replace html-element'),
'visibility' => new external_value(PARAM_INT, 'returns the visibility value'),
'changedgroups' => new external_multiple_structure(
new external_single_structure(
array(
'groupid' => new external_value(PARAM_INT, 'group-id', VALUE_OPTIONAL)
)
)
)
)
);
}
/**
* Changed the Database and returns the updated html content.
* create_allgroups_output
* @param array $groups
* @return array
*/
public static function create_allgroups_output($groups) {
global $PAGE, $CFG, $DB;
$params = self::validate_parameters(self::create_allgroups_output_parameters(), array('groups' => $groups));
require_once($CFG->dirroot.'/blocks/groups/locallib.php');
$PAGE->set_context(context_course::instance($params['groups']['courseid']));
require_capability('moodle/course:managegroups', context_course::instance($params['groups']['courseid']));
$groupsuitable = $DB->get_records('groups', array('courseid' => $params['groups']['courseid']), 'id ASC');
// The Course has no groups therefore changing all is not possible.
if (empty($groupsuitable)) {
$output['courseid'] = $params['groups']['courseid'];
$link = html_writer::link(new moodle_url('/group/index.php',
array('id' => $params['groups']['courseid'])), 'modify groups');
$content = html_writer::div(get_string('nogroups', 'block_groups')) . $link;
$output['newelement'] = html_writer::div($content, array('class' => 'content'));
$output['visibility'] = '0';
$output['changedgroups'] = array();
return $output;
}
$groupsvisible = array();
$renderer = $PAGE->get_renderer('block_groups');
foreach ($groupsuitable as $group) {
$entry = $DB->get_records('block_groups_hide', array('id' => $group->id));
// In the Case, that the group of the course has an entry in the 'block_groups_hide' table the group is visible.
if (!empty($entry)) {
$groupsvisible[$group->id] = $group->id;
}
}
$groups = $groupsvisible;
$outputvisibility = '3';
if ($params['groups']['action'] == "show") {
$outputvisibility = '4';
$tempgroup = array();
if (!empty($groupsuitable)) {
foreach ($groupsuitable as $group) {
if (!empty($groupsvisible)) {
if (!(in_array($group->id, $groups))) {
$tempgroup[$group->id] = $group->id;
}
} else {
$tempgroup[$group->id] = $group->id;
}
}
}
$groups = $tempgroup;
}
$output['changedgroups'] = array();
if (!empty($groups)) {
foreach ($groups as $group) {
block_groups_db_transaction_change_visibility($group, $params['groups']['courseid']);
array_push($output['changedgroups'], array('groupid' => $group));
}
}
foreach ($groupsuitable as $group) {
$fullgroup = groups_get_group($group->id);
$href = new moodle_url('/blocks/groups/changevisibility.php',
array('courseid' => $params['groups']['courseid'], 'groupid' => $group->id));
$countmembers = count(groups_get_members($group->id));
if ($params['groups']['action'] == 'hide') {
$visibility = true;
}
if ($params['groups']['action'] == 'show') {
$visibility = false;
}
$groupsarray[] = $renderer->get_string_group($fullgroup, $href, $countmembers, $visibility);
}
$output['newelement'] = html_writer::alist($groupsarray, array('class' => 'wrapperlistgroup'));
$output['courseid'] = $params['groups']['courseid'];
if (empty($groups)) {
$output['visibility'] = $outputvisibility;
return $output;
}
// Parameter $outputvisibility 0->nogroups 1 -> hidden 2->visible 3-> all are hidden 4-> all are visible.
if ($params['groups']['action'] == 'hide') {
$outputvisibility = 1;
}
if ($params['groups']['action'] == 'show') {
$outputvisibility = 2;
}
$output['visibility'] = $outputvisibility;
return $output;
}
}