Merge branch 'w26_MDL-40254_m25_flatfile' of https://github.com/skodak/moodle into...
[moodle.git] / enrol / instances.php
blob0ad5d2c427dad45932ac3d0e8874b9763925acb4
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.
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/>.
17 /**
18 * Main course enrolment management UI.
20 * @package core_enrol
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require('../config.php');
27 $id = required_param('id', PARAM_INT); // course id
28 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
29 $instanceid = optional_param('instance', 0, PARAM_INT);
30 $confirm = optional_param('confirm', 0, PARAM_BOOL);
32 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
33 $context = context_course::instance($course->id, MUST_EXIST);
35 if ($course->id == SITEID) {
36 redirect("$CFG->wwwroot/");
39 require_login($course);
40 require_capability('moodle/course:enrolreview', $context);
42 $canconfig = has_capability('moodle/course:enrolconfig', $context);
44 $PAGE->set_url('/enrol/instances.php', array('id'=>$course->id));
45 $PAGE->set_pagelayout('admin');
46 $PAGE->set_title(get_string('enrolmentinstances', 'enrol'));
47 $PAGE->set_heading($course->fullname);
49 $instances = enrol_get_instances($course->id, false);
50 $plugins = enrol_get_plugins(false);
52 if ($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);
72 redirect($PAGE->url);
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);
92 redirect($PAGE->url);
94 } else if ($action === 'delete') {
95 $instance = $instances[$instanceid];
96 $plugin = $plugins[$instance->enrol];
98 if ($confirm) {
99 $plugin->delete_instance($instance);
100 redirect($PAGE->url);
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 if ($users) {
108 $message = markdown_to_html(get_string('deleteinstanceconfirm', 'enrol', array('name'=>$displayname, 'users'=>$users)));
109 } else {
110 $message = markdown_to_html(get_string('deleteinstancenousersconfirm', 'enrol', array('name'=>$displayname)));
112 echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
113 echo $OUTPUT->footer();
114 die();
116 } else if ($action === 'disable') {
117 $instance = $instances[$instanceid];
118 $plugin = $plugins[$instance->enrol];
119 if ($instance->status != ENROL_INSTANCE_DISABLED) {
120 $plugin->update_status($instance, ENROL_INSTANCE_DISABLED);
121 redirect($PAGE->url);
124 } else if ($action === 'enable') {
125 $instance = $instances[$instanceid];
126 $plugin = $plugins[$instance->enrol];
127 if ($instance->status != ENROL_INSTANCE_ENABLED) {
128 $plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
129 redirect($PAGE->url);
136 echo $OUTPUT->header();
137 echo $OUTPUT->heading(get_string('enrolmentinstances', 'enrol'));
139 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
141 // display strings
142 $strup = get_string('up');
143 $strdown = get_string('down');
144 $strdelete = get_string('delete');
145 $strenable = get_string('enable');
146 $strdisable = get_string('disable');
147 $strmanage = get_string('manageinstance', 'enrol');
149 $table = new html_table();
150 $table->head = array(get_string('name'), get_string('users'), $strup.'/'.$strdown, get_string('edit'));
151 $table->align = array('left', 'center', 'center', 'center');
152 $table->width = '100%';
153 $table->data = array();
155 // iterate through enrol plugins and add to the display table
156 $updowncount = 1;
157 $icount = count($instances);
158 $url = new moodle_url('/enrol/instances.php', array('sesskey'=>sesskey(), 'id'=>$course->id));
159 foreach ($instances as $instance) {
160 if (!isset($plugins[$instance->enrol])) {
161 continue;
163 $plugin = $plugins[$instance->enrol];
165 $displayname = $plugin->get_instance_name($instance);
166 if (!enrol_is_enabled($instance->enrol) or $instance->status != ENROL_INSTANCE_ENABLED) {
167 $displayname = html_writer::tag('span', $displayname, array('class'=>'dimmed_text'));
170 $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
172 $updown = array();
173 $edit = array();
175 if ($canconfig) {
176 // up/down link
177 $updown = '';
178 if ($updowncount > 1) {
179 $aurl = new moodle_url($url, array('action'=>'up', 'instance'=>$instance->id));
180 $updown[] = $OUTPUT->action_icon($aurl, new pix_icon('t/up', $strup, 'core', array('class' => 'iconsmall')));
181 } else {
182 $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'iconsmall'));
184 if ($updowncount < $icount) {
185 $aurl = new moodle_url($url, array('action'=>'down', 'instance'=>$instance->id));
186 $updown[] = $OUTPUT->action_icon($aurl, new pix_icon('t/down', $strdown, 'core', array('class' => 'iconsmall')));
187 } else {
188 $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'iconsmall'));
190 ++$updowncount;
192 // edit links
193 if ($plugin->instance_deleteable($instance)) {
194 $aurl = new moodle_url($url, array('action'=>'delete', 'instance'=>$instance->id));
195 $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/delete', $strdelete, 'core', array('class' => 'iconsmall')));
198 if (enrol_is_enabled($instance->enrol)) {
199 if ($instance->status == ENROL_INSTANCE_ENABLED) {
200 $aurl = new moodle_url($url, array('action'=>'disable', 'instance'=>$instance->id));
201 $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/hide', $strdisable, 'core', array('class' => 'iconsmall')));
202 } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
203 $aurl = new moodle_url($url, array('action'=>'enable', 'instance'=>$instance->id));
204 $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/show', $strenable, 'core', array('class' => 'iconsmall')));
205 } else {
206 // plugin specific state - do not mess with it!
207 $edit[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>'', 'class'=>'iconsmall'));
213 // link to instance management
214 if (enrol_is_enabled($instance->enrol)) {
215 if ($icons = $plugin->get_action_icons($instance)) {
216 $edit = array_merge($edit, $icons);
220 // add a row to the table
221 $table->data[] = array($displayname, $users, implode('&nbsp;', $updown), implode('&nbsp;', $edit));
224 echo html_writer::table($table);
226 // access security is in each plugin
227 $candidates = array();
228 foreach (enrol_get_plugins(true) as $name=>$plugin) {
229 if (!$link = $plugin->get_newinstance_link($course->id)) {
230 continue;
232 $candidates[$link->out(false)] = get_string('pluginname', 'enrol_'.$name);
235 if ($candidates) {
236 $select = new url_select($candidates);
237 $select->set_label(get_string('addinstance', 'enrol'));
238 echo $OUTPUT->render($select);
241 echo $OUTPUT->box_end();
243 echo $OUTPUT->footer();