MDL-37717 warn teachers before disabling or deleting their enrolment method
[moodle.git] / enrol / instances.php
CommitLineData
df997f84
PS
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 * Main course enrolment management UI.
19 *
31ac2aef 20 * @package core_enrol
df997f84
PS
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require('../config.php');
26
27$id = required_param('id', PARAM_INT); // course id
405aca35 28$action = optional_param('action', '', PARAM_ALPHANUMEXT);
df997f84
PS
29$instanceid = optional_param('instance', 0, PARAM_INT);
30$confirm = optional_param('confirm', 0, PARAM_BOOL);
12c92bca 31$confirm2 = optional_param('confirm2', 0, PARAM_BOOL);
df997f84 32
74df2951 33$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
55bcef29 34$context = context_course::instance($course->id, MUST_EXIST);
df997f84
PS
35
36if ($course->id == SITEID) {
37 redirect("$CFG->wwwroot/");
38}
39
40require_login($course);
41require_capability('moodle/course:enrolreview', $context);
42
43$canconfig = has_capability('moodle/course:enrolconfig', $context);
44
45$PAGE->set_url('/enrol/instances.php', array('id'=>$course->id));
46$PAGE->set_pagelayout('admin');
47$PAGE->set_title(get_string('enrolmentinstances', 'enrol'));
ae715bbd 48$PAGE->set_heading($course->fullname);
df997f84
PS
49
50$instances = enrol_get_instances($course->id, false);
51$plugins = enrol_get_plugins(false);
52
53if ($canconfig and $action and confirm_sesskey()) {
54 if (isset($instances[$instanceid]) and isset($plugins[$instances[$instanceid]->enrol])) {
55 if ($action === 'up') {
56 $order = array_keys($instances);
57 $order = array_flip($order);
58 $pos = $order[$instanceid];
59 if ($pos > 0) {
60 $switch = $pos - 1;
61 $resorted = array_values($instances);
62 $temp = $resorted[$pos];
63 $resorted[$pos] = $resorted[$switch];
64 $resorted[$switch] = $temp;
65 // now update db sortorder
66 foreach ($resorted as $sortorder=>$instance) {
67 if ($instance->sortorder != $sortorder) {
68 $instance->sortorder = $sortorder;
69 $DB->update_record('enrol', $instance);
70 }
71 }
72 }
73 redirect($PAGE->url);
74
75 } else if ($action === 'down') {
76 $order = array_keys($instances);
77 $order = array_flip($order);
78 $pos = $order[$instanceid];
79 if ($pos < count($instances) - 1) {
80 $switch = $pos + 1;
81 $resorted = array_values($instances);
82 $temp = $resorted[$pos];
83 $resorted[$pos] = $resorted[$switch];
84 $resorted[$switch] = $temp;
85 // now update db sortorder
86 foreach ($resorted as $sortorder=>$instance) {
87 if ($instance->sortorder != $sortorder) {
88 $instance->sortorder = $sortorder;
89 $DB->update_record('enrol', $instance);
90 }
91 }
92 }
93 redirect($PAGE->url);
94
95 } else if ($action === 'delete') {
96 $instance = $instances[$instanceid];
97 $plugin = $plugins[$instance->enrol];
98
99 if ($confirm) {
12c92bca
PS
100 if (enrol_accessing_via_instance($instance)) {
101 if (!$confirm2) {
102 $yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'delete', 'instance'=>$instance->id, 'confirm'=>1, 'confirm2'=>1, 'sesskey'=>sesskey()));
103 $displayname = $plugin->get_instance_name($instance);
104 $message = markdown_to_html(get_string('deleteinstanceconfirmself', 'enrol', array('name'=>$displayname)));
105 echo $OUTPUT->header();
106 echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
107 echo $OUTPUT->footer();
108 die();
109 }
110 }
df997f84
PS
111 $plugin->delete_instance($instance);
112 redirect($PAGE->url);
113 }
114
115 echo $OUTPUT->header();
116 $yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'delete', 'instance'=>$instance->id, 'confirm'=>1,'sesskey'=>sesskey()));
117 $displayname = $plugin->get_instance_name($instance);
118 $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
19434214
PS
119 if ($users) {
120 $message = markdown_to_html(get_string('deleteinstanceconfirm', 'enrol', array('name'=>$displayname, 'users'=>$users)));
121 } else {
122 $message = markdown_to_html(get_string('deleteinstancenousersconfirm', 'enrol', array('name'=>$displayname)));
123 }
df997f84
PS
124 echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
125 echo $OUTPUT->footer();
126 die();
127
128 } else if ($action === 'disable') {
129 $instance = $instances[$instanceid];
af7177db
PS
130 $plugin = $plugins[$instance->enrol];
131 if ($instance->status != ENROL_INSTANCE_DISABLED) {
12c92bca
PS
132 if (enrol_accessing_via_instance($instance)) {
133 if (!$confirm2) {
134 $yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'disable', 'instance'=>$instance->id, 'confirm2'=>1, 'sesskey'=>sesskey()));
135 $displayname = $plugin->get_instance_name($instance);
136 $message = markdown_to_html(get_string('disableinstanceconfirmself', 'enrol', array('name'=>$displayname)));
137 echo $OUTPUT->header();
138 echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
139 echo $OUTPUT->footer();
140 die();
141 }
142 }
af7177db 143 $plugin->update_status($instance, ENROL_INSTANCE_DISABLED);
df997f84
PS
144 redirect($PAGE->url);
145 }
146
147 } else if ($action === 'enable') {
148 $instance = $instances[$instanceid];
af7177db
PS
149 $plugin = $plugins[$instance->enrol];
150 if ($instance->status != ENROL_INSTANCE_ENABLED) {
151 $plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
df997f84
PS
152 redirect($PAGE->url);
153 }
154 }
155 }
156}
157
158
159echo $OUTPUT->header();
160echo $OUTPUT->heading(get_string('enrolmentinstances', 'enrol'));
161
162echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
163
164// display strings
165$strup = get_string('up');
166$strdown = get_string('down');
167$strdelete = get_string('delete');
168$strenable = get_string('enable');
169$strdisable = get_string('disable');
170$strmanage = get_string('manageinstance', 'enrol');
171
172$table = new html_table();
173$table->head = array(get_string('name'), get_string('users'), $strup.'/'.$strdown, get_string('edit'));
174$table->align = array('left', 'center', 'center', 'center');
175$table->width = '100%';
176$table->data = array();
177
178// iterate through enrol plugins and add to the display table
179$updowncount = 1;
180$icount = count($instances);
181$url = new moodle_url('/enrol/instances.php', array('sesskey'=>sesskey(), 'id'=>$course->id));
182foreach ($instances as $instance) {
183 if (!isset($plugins[$instance->enrol])) {
184 continue;
185 }
186 $plugin = $plugins[$instance->enrol];
187
188 $displayname = $plugin->get_instance_name($instance);
189 if (!enrol_is_enabled($instance->enrol) or $instance->status != ENROL_INSTANCE_ENABLED) {
190 $displayname = html_writer::tag('span', $displayname, array('class'=>'dimmed_text'));
191 }
192
193 $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
194
2d4b1f3e
PS
195 $updown = array();
196 $edit = array();
df997f84
PS
197
198 if ($canconfig) {
199 // up/down link
200 $updown = '';
201 if ($updowncount > 1) {
202 $aurl = new moodle_url($url, array('action'=>'up', 'instance'=>$instance->id));
8ef3d514 203 $updown[] = $OUTPUT->action_icon($aurl, new pix_icon('t/up', $strup, 'core', array('class' => 'iconsmall')));
df997f84 204 } else {
8ef3d514 205 $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'iconsmall'));
df997f84
PS
206 }
207 if ($updowncount < $icount) {
208 $aurl = new moodle_url($url, array('action'=>'down', 'instance'=>$instance->id));
8ef3d514 209 $updown[] = $OUTPUT->action_icon($aurl, new pix_icon('t/down', $strdown, 'core', array('class' => 'iconsmall')));
df997f84 210 } else {
8ef3d514 211 $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'iconsmall'));
df997f84
PS
212 }
213 ++$updowncount;
214
215 // edit links
216 if ($plugin->instance_deleteable($instance)) {
217 $aurl = new moodle_url($url, array('action'=>'delete', 'instance'=>$instance->id));
8ef3d514 218 $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/delete', $strdelete, 'core', array('class' => 'iconsmall')));
df997f84
PS
219 }
220
221 if (enrol_is_enabled($instance->enrol)) {
222 if ($instance->status == ENROL_INSTANCE_ENABLED) {
223 $aurl = new moodle_url($url, array('action'=>'disable', 'instance'=>$instance->id));
8ef3d514 224 $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/hide', $strdisable, 'core', array('class' => 'iconsmall')));
df997f84
PS
225 } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
226 $aurl = new moodle_url($url, array('action'=>'enable', 'instance'=>$instance->id));
8ef3d514 227 $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/show', $strenable, 'core', array('class' => 'iconsmall')));
df997f84
PS
228 } else {
229 // plugin specific state - do not mess with it!
8ef3d514 230 $edit[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>'', 'class'=>'iconsmall'));
df997f84
PS
231 }
232
233 }
234 }
235
236 // link to instance management
2d4b1f3e
PS
237 if (enrol_is_enabled($instance->enrol)) {
238 if ($icons = $plugin->get_action_icons($instance)) {
239 $edit = array_merge($edit, $icons);
240 }
df997f84
PS
241 }
242
243 // add a row to the table
2d4b1f3e 244 $table->data[] = array($displayname, $users, implode('&nbsp;', $updown), implode('&nbsp;', $edit));
df997f84
PS
245
246}
247echo html_writer::table($table);
248
249// access security is in each plugin
250$candidates = array();
251foreach (enrol_get_plugins(true) as $name=>$plugin) {
e25f2466 252 if (!$link = $plugin->get_newinstance_link($course->id)) {
df997f84
PS
253 continue;
254 }
255 $candidates[$link->out(false)] = get_string('pluginname', 'enrol_'.$name);
256}
257
258if ($candidates) {
259 $select = new url_select($candidates);
260 $select->set_label(get_string('addinstance', 'enrol'));
261 echo $OUTPUT->render($select);
262}
263
264echo $OUTPUT->box_end();
265
266echo $OUTPUT->footer();