forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instances.php
316 lines (277 loc) · 13.7 KB
/
instances.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?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/>.
/**
* Main course enrolment management UI.
*
* @package core_enrol
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../config.php');
$id = required_param('id', PARAM_INT); // course id
$action = optional_param('action', '', PARAM_ALPHANUMEXT);
$instanceid = optional_param('instance', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$confirm2 = optional_param('confirm2', 0, PARAM_BOOL);
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
$context = context_course::instance($course->id, MUST_EXIST);
if ($course->id == SITEID) {
redirect("$CFG->wwwroot/");
}
require_login($course);
require_capability('moodle/course:enrolreview', $context);
$canconfig = has_capability('moodle/course:enrolconfig', $context);
$PAGE->set_url('/enrol/instances.php', array('id'=>$course->id));
$PAGE->set_pagelayout('admin');
$PAGE->set_title(get_string('enrolmentinstances', 'enrol'));
$PAGE->set_heading($course->fullname);
$instances = enrol_get_instances($course->id, false);
$plugins = enrol_get_plugins(false);
if ($canconfig and $action and confirm_sesskey()) {
if (isset($instances[$instanceid]) and isset($plugins[$instances[$instanceid]->enrol])) {
if ($action === 'up') {
$order = array_keys($instances);
$order = array_flip($order);
$pos = $order[$instanceid];
if ($pos > 0) {
$switch = $pos - 1;
$resorted = array_values($instances);
$temp = $resorted[$pos];
$resorted[$pos] = $resorted[$switch];
$resorted[$switch] = $temp;
// now update db sortorder
foreach ($resorted as $sortorder=>$instance) {
if ($instance->sortorder != $sortorder) {
$instance->sortorder = $sortorder;
$DB->update_record('enrol', $instance);
}
}
}
redirect($PAGE->url);
} else if ($action === 'down') {
$order = array_keys($instances);
$order = array_flip($order);
$pos = $order[$instanceid];
if ($pos < count($instances) - 1) {
$switch = $pos + 1;
$resorted = array_values($instances);
$temp = $resorted[$pos];
$resorted[$pos] = $resorted[$switch];
$resorted[$switch] = $temp;
// now update db sortorder
foreach ($resorted as $sortorder=>$instance) {
if ($instance->sortorder != $sortorder) {
$instance->sortorder = $sortorder;
$DB->update_record('enrol', $instance);
}
}
}
redirect($PAGE->url);
} else if ($action === 'delete') {
$instance = $instances[$instanceid];
$plugin = $plugins[$instance->enrol];
if ($plugin->can_delete_instance($instance)) {
if ($confirm) {
if (enrol_accessing_via_instance($instance)) {
if (!$confirm2) {
$yesurl = new moodle_url('/enrol/instances.php',
array('id' => $course->id,
'action' => 'delete',
'instance' => $instance->id,
'confirm' => 1,
'confirm2' => 1,
'sesskey' => sesskey()));
$displayname = $plugin->get_instance_name($instance);
$message = markdown_to_html(get_string('deleteinstanceconfirmself',
'enrol',
array('name' => $displayname)));
echo $OUTPUT->header();
echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
echo $OUTPUT->footer();
die();
}
}
// Update communication for instance and given action.
if (core_communication\api::is_available() && $instance->enrol !== 'guest') {
$plugin->update_communication($instance->id, 'remove', $course->id);
}
$plugin->delete_instance($instance);
redirect($PAGE->url);
}
echo $OUTPUT->header();
$yesurl = new moodle_url('/enrol/instances.php',
array('id' => $course->id,
'action' => 'delete',
'instance' => $instance->id,
'confirm' => 1,
'sesskey' => sesskey()));
$displayname = $plugin->get_instance_name($instance);
$users = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
if ($users) {
$message = markdown_to_html(get_string('deleteinstanceconfirm', 'enrol',
array('name' => $displayname,
'users' => $users)));
} else {
$message = markdown_to_html(get_string('deleteinstancenousersconfirm', 'enrol',
array('name' => $displayname)));
}
echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
echo $OUTPUT->footer();
die();
}
} else if ($action === 'disable') {
$instance = $instances[$instanceid];
$plugin = $plugins[$instance->enrol];
if ($plugin->can_hide_show_instance($instance)) {
if ($instance->status != ENROL_INSTANCE_DISABLED) {
if (enrol_accessing_via_instance($instance)) {
if (!$confirm2) {
$yesurl = new moodle_url('/enrol/instances.php',
array('id' => $course->id,
'action' => 'disable',
'instance' => $instance->id,
'confirm2' => 1,
'sesskey' => sesskey()));
$displayname = $plugin->get_instance_name($instance);
$message = markdown_to_html(get_string('disableinstanceconfirmself',
'enrol',
array('name' => $displayname)));
echo $OUTPUT->header();
echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
echo $OUTPUT->footer();
die();
}
}
// Update communication for instance and given action.
if (core_communication\api::is_available() && $instance->enrol !== 'guest') {
$plugin->update_communication($instance->id, 'remove', $course->id);
}
$plugin->update_status($instance, ENROL_INSTANCE_DISABLED);
redirect($PAGE->url);
}
}
} else if ($action === 'enable') {
$instance = $instances[$instanceid];
$plugin = $plugins[$instance->enrol];
if ($plugin->can_hide_show_instance($instance)) {
if ($instance->status != ENROL_INSTANCE_ENABLED) {
// Update communication for instance and given action.
if (core_communication\api::is_available() && $instance->enrol !== 'guest') {
$plugin->update_communication($instance->id, 'add', $course->id);
}
$plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
redirect($PAGE->url);
}
}
}
}
}
echo $OUTPUT->header();
echo $OUTPUT->render_participants_tertiary_nav($course);
echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
// display strings
$strup = get_string('up');
$strdown = get_string('down');
$strdelete = get_string('delete');
$strenable = get_string('enable');
$strdisable = get_string('disable');
$strmanage = get_string('manageinstance', 'enrol');
$table = new html_table();
$table->head = array(get_string('name'), get_string('users'), $strup.'/'.$strdown, get_string('edit'));
$table->align = array('left', 'center', 'center', 'center');
$table->width = '100%';
$table->data = array();
// iterate through enrol plugins and add to the display table
$updowncount = 1;
$icount = count($instances);
$url = new moodle_url('/enrol/instances.php', array('sesskey'=>sesskey(), 'id'=>$course->id));
foreach ($instances as $instance) {
if (!isset($plugins[$instance->enrol])) {
continue;
}
$plugin = $plugins[$instance->enrol];
$displayname = $plugin->get_instance_name($instance);
if (!enrol_is_enabled($instance->enrol) or $instance->status != ENROL_INSTANCE_ENABLED) {
$displayname = html_writer::tag('span', $displayname, array('class'=>'dimmed_text'));
}
$users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
$updown = array();
$edit = array();
if ($canconfig) {
if ($updowncount > 1) {
$aurl = new moodle_url($url, array('action'=>'up', 'instance'=>$instance->id));
$updown[] = $OUTPUT->action_icon($aurl, new pix_icon('t/up', $strup, 'core', array('class' => 'iconsmall')));
} else {
$updown[] = $OUTPUT->spacer();
}
if ($updowncount < $icount) {
$aurl = new moodle_url($url, array('action'=>'down', 'instance'=>$instance->id));
$updown[] = $OUTPUT->action_icon($aurl, new pix_icon('t/down', $strdown, 'core', array('class' => 'iconsmall')));
} else {
$updown[] = $OUTPUT->spacer();
}
++$updowncount;
if ($plugin->can_delete_instance($instance)) {
$aurl = new moodle_url($url, array('action'=>'delete', 'instance'=>$instance->id));
$edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/delete', $strdelete, 'core', array('class' => 'iconsmall')));
}
if (enrol_is_enabled($instance->enrol) && $plugin->can_hide_show_instance($instance)) {
if ($instance->status == ENROL_INSTANCE_ENABLED) {
$aurl = new moodle_url($url, array('action'=>'disable', 'instance'=>$instance->id));
$edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/hide', $strdisable, 'core', array('class' => 'iconsmall')));
} else if ($instance->status == ENROL_INSTANCE_DISABLED) {
$aurl = new moodle_url($url, array('action'=>'enable', 'instance'=>$instance->id));
$edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/show', $strenable, 'core', array('class' => 'iconsmall')));
} else {
// plugin specific state - do not mess with it!
$edit[] = $OUTPUT->pix_icon('t/show', get_string('show'));
}
}
}
// link to instance management
if (enrol_is_enabled($instance->enrol) && $canconfig) {
if ($icons = $plugin->get_action_icons($instance)) {
$edit = array_merge($edit, $icons);
}
}
// Add a row to the table.
$table->data[] = array($displayname, $users, implode('', $updown), implode('', $edit));
}
echo html_writer::table($table);
// access security is in each plugin
$candidates = array();
foreach (enrol_get_plugins(true) as $name=>$plugin) {
if ($plugin->use_standard_editing_ui()) {
if ($plugin->can_add_instance($course->id)) {
// Standard add/edit UI.
$params = array('type' => $name, 'courseid' => $course->id);
$url = new moodle_url('/enrol/editinstance.php', $params);
$link = $url->out(false);
$candidates[$link] = get_string('pluginname', 'enrol_'.$name);
}
} else if ($url = $plugin->get_newinstance_link($course->id)) {
// Old custom UI.
$link = $url->out(false);
$candidates[$link] = get_string('pluginname', 'enrol_'.$name);
}
}
if ($candidates) {
$select = new url_select($candidates);
$select->set_label(get_string('addinstance', 'enrol'));
echo $OUTPUT->render($select);
}
echo $OUTPUT->box_end();
echo $OUTPUT->footer();