MDL-23502 refactoring towards separate instance config forms
[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 *
20 * @package core
21 * @subpackage enrol
22 * @copyright 2010 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require('../config.php');
27
28$id = required_param('id', PARAM_INT); // course id
29$action = optional_param('action', '', PARAM_ACTION);
30$instanceid = optional_param('instance', 0, PARAM_INT);
31$confirm = optional_param('confirm', 0, PARAM_BOOL);
32
33$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
34$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
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'));
48
49$instances = enrol_get_instances($course->id, false);
50$plugins = enrol_get_plugins(false);
51
52if ($canconfig and $action and confirm_sesskey()) {
53 if (isset($instances[$instanceid]) and isset($plugins[$instances[$instanceid]->enrol])) {
54 if ($action === 'up') {
55 $order = array_keys($instances);
56 $order = array_flip($order);
57 $pos = $order[$instanceid];
58 if ($pos > 0) {
59 $switch = $pos - 1;
60 $resorted = array_values($instances);
61 $temp = $resorted[$pos];
62 $resorted[$pos] = $resorted[$switch];
63 $resorted[$switch] = $temp;
64 // now update db sortorder
65 foreach ($resorted as $sortorder=>$instance) {
66 if ($instance->sortorder != $sortorder) {
67 $instance->sortorder = $sortorder;
68 $DB->update_record('enrol', $instance);
69 }
70 }
71 }
72 redirect($PAGE->url);
73
74 } else if ($action === 'down') {
75 $order = array_keys($instances);
76 $order = array_flip($order);
77 $pos = $order[$instanceid];
78 if ($pos < count($instances) - 1) {
79 $switch = $pos + 1;
80 $resorted = array_values($instances);
81 $temp = $resorted[$pos];
82 $resorted[$pos] = $resorted[$switch];
83 $resorted[$switch] = $temp;
84 // now update db sortorder
85 foreach ($resorted as $sortorder=>$instance) {
86 if ($instance->sortorder != $sortorder) {
87 $instance->sortorder = $sortorder;
88 $DB->update_record('enrol', $instance);
89 }
90 }
91 }
92 redirect($PAGE->url);
93
94 } else if ($action === 'delete') {
95 $instance = $instances[$instanceid];
96 $plugin = $plugins[$instance->enrol];
97
98 if ($confirm) {
99 $plugin->delete_instance($instance);
100 redirect($PAGE->url);
101 }
102
103 echo $OUTPUT->header();
104 $yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'delete', 'instance'=>$instance->id, 'confirm'=>1,'sesskey'=>sesskey()));
105 $displayname = $plugin->get_instance_name($instance);
106 $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
107 $message = get_string('deleteinstanceconfirm', 'enrol', array('name'=>$displayname, 'users'=>$users));
108 echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
109 echo $OUTPUT->footer();
110 die();
111
112 } else if ($action === 'disable') {
113 $instance = $instances[$instanceid];
114 if ($instance->status == ENROL_INSTANCE_ENABLED) {
115 $instance->status = ENROL_INSTANCE_DISABLED;
116 $DB->update_record('enrol', $instance);
117 redirect($PAGE->url);
118 }
119
120 } else if ($action === 'enable') {
121 $instance = $instances[$instanceid];
122 if ($instance->status == ENROL_INSTANCE_DISABLED) {
123 $instance->status = ENROL_INSTANCE_ENABLED;
124 $DB->update_record('enrol', $instance);
125 redirect($PAGE->url);
126 }
127 }
128 }
129}
130
131
132echo $OUTPUT->header();
133echo $OUTPUT->heading(get_string('enrolmentinstances', 'enrol'));
134
135echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
136
137// display strings
138$strup = get_string('up');
139$strdown = get_string('down');
140$strdelete = get_string('delete');
141$strenable = get_string('enable');
142$strdisable = get_string('disable');
143$strmanage = get_string('manageinstance', 'enrol');
144
145$table = new html_table();
146$table->head = array(get_string('name'), get_string('users'), $strup.'/'.$strdown, get_string('edit'));
147$table->align = array('left', 'center', 'center', 'center');
148$table->width = '100%';
149$table->data = array();
150
151// iterate through enrol plugins and add to the display table
152$updowncount = 1;
153$icount = count($instances);
154$url = new moodle_url('/enrol/instances.php', array('sesskey'=>sesskey(), 'id'=>$course->id));
155foreach ($instances as $instance) {
156 if (!isset($plugins[$instance->enrol])) {
157 continue;
158 }
159 $plugin = $plugins[$instance->enrol];
160
161 $displayname = $plugin->get_instance_name($instance);
162 if (!enrol_is_enabled($instance->enrol) or $instance->status != ENROL_INSTANCE_ENABLED) {
163 $displayname = html_writer::tag('span', $displayname, array('class'=>'dimmed_text'));
164 }
165
166 $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
167
2d4b1f3e
PS
168 $updown = array();
169 $edit = array();
df997f84
PS
170
171 if ($canconfig) {
172 // up/down link
173 $updown = '';
174 if ($updowncount > 1) {
175 $aurl = new moodle_url($url, array('action'=>'up', 'instance'=>$instance->id));
2d4b1f3e 176 $updown[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/up'), 'alt'=>$strup, 'class'=>'smallicon')));
df997f84 177 } else {
2d4b1f3e 178 $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'smallicon'));
df997f84
PS
179 }
180 if ($updowncount < $icount) {
181 $aurl = new moodle_url($url, array('action'=>'down', 'instance'=>$instance->id));
2d4b1f3e 182 $updown[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/down'), 'alt'=>$strdown, 'class'=>'smallicon')));
df997f84 183 } else {
2d4b1f3e 184 $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'smallicon'));
df997f84
PS
185 }
186 ++$updowncount;
187
188 // edit links
189 if ($plugin->instance_deleteable($instance)) {
190 $aurl = new moodle_url($url, array('action'=>'delete', 'instance'=>$instance->id));
2d4b1f3e 191 $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'alt'=>$strdelete, 'class'=>'smallicon')));
df997f84
PS
192 }
193
194 if (enrol_is_enabled($instance->enrol)) {
195 if ($instance->status == ENROL_INSTANCE_ENABLED) {
196 $aurl = new moodle_url($url, array('action'=>'disable', 'instance'=>$instance->id));
2d4b1f3e 197 $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/hide'), 'alt'=>$strdisable, 'class'=>'smallicon')));
df997f84
PS
198 } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
199 $aurl = new moodle_url($url, array('action'=>'enable', 'instance'=>$instance->id));
2d4b1f3e 200 $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>$strenable, 'class'=>'smallicon')));
df997f84
PS
201 } else {
202 // plugin specific state - do not mess with it!
2d4b1f3e 203 $edit[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>'', 'class'=>'smallicon'));
df997f84
PS
204 }
205
206 }
207 }
208
209 // link to instance management
2d4b1f3e
PS
210 if (enrol_is_enabled($instance->enrol)) {
211 if ($icons = $plugin->get_action_icons($instance)) {
212 $edit = array_merge($edit, $icons);
213 }
df997f84
PS
214 }
215
216 // add a row to the table
2d4b1f3e 217 $table->data[] = array($displayname, $users, implode('&nbsp;', $updown), implode('&nbsp;', $edit));
df997f84
PS
218
219}
220echo html_writer::table($table);
221
222// access security is in each plugin
223$candidates = array();
224foreach (enrol_get_plugins(true) as $name=>$plugin) {
225 if (!$link = $plugin->get_candidate_link($course->id)) {
226 continue;
227 }
228 $candidates[$link->out(false)] = get_string('pluginname', 'enrol_'.$name);
229}
230
231if ($candidates) {
232 $select = new url_select($candidates);
233 $select->set_label(get_string('addinstance', 'enrol'));
234 echo $OUTPUT->render($select);
235}
236
237echo $OUTPUT->box_end();
238
239echo $OUTPUT->footer();