Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / enrol / instances.php
blobac4db48e665f5efa586a3537859a8baaad885898
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
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
26 require('../config.php');
28 $id = required_param('id', PARAM_INT); // course id
29 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
30 $instanceid = optional_param('instance', 0, PARAM_INT);
31 $confirm = optional_param('confirm', 0, PARAM_BOOL);
33 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
34 $context = context_course::instance($course->id, MUST_EXIST);
36 if ($course->id == SITEID) {
37 redirect("$CFG->wwwroot/");
40 require_login($course);
41 require_capability('moodle/course:enrolreview', $context);
43 $canconfig = has_capability('moodle/course:enrolconfig', $context);
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 $PAGE->set_heading($course->fullname);
50 $instances = enrol_get_instances($course->id, false);
51 $plugins = enrol_get_plugins(false);
53 if ($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);
73 redirect($PAGE->url);
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);
93 redirect($PAGE->url);
95 } else if ($action === 'delete') {
96 $instance = $instances[$instanceid];
97 $plugin = $plugins[$instance->enrol];
99 if ($confirm) {
100 $plugin->delete_instance($instance);
101 redirect($PAGE->url);
104 echo $OUTPUT->header();
105 $yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'delete', 'instance'=>$instance->id, 'confirm'=>1,'sesskey'=>sesskey()));
106 $displayname = $plugin->get_instance_name($instance);
107 $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
108 if ($users) {
109 $message = markdown_to_html(get_string('deleteinstanceconfirm', 'enrol', array('name'=>$displayname, 'users'=>$users)));
110 } else {
111 $message = markdown_to_html(get_string('deleteinstancenousersconfirm', 'enrol', array('name'=>$displayname)));
113 echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
114 echo $OUTPUT->footer();
115 die();
117 } else if ($action === 'disable') {
118 $instance = $instances[$instanceid];
119 $plugin = $plugins[$instance->enrol];
120 if ($instance->status != ENROL_INSTANCE_DISABLED) {
121 $plugin->update_status($instance, ENROL_INSTANCE_DISABLED);
122 redirect($PAGE->url);
125 } else if ($action === 'enable') {
126 $instance = $instances[$instanceid];
127 $plugin = $plugins[$instance->enrol];
128 if ($instance->status != ENROL_INSTANCE_ENABLED) {
129 $plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
130 redirect($PAGE->url);
137 echo $OUTPUT->header();
138 echo $OUTPUT->heading(get_string('enrolmentinstances', 'enrol'));
140 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
142 // display strings
143 $strup = get_string('up');
144 $strdown = get_string('down');
145 $strdelete = get_string('delete');
146 $strenable = get_string('enable');
147 $strdisable = get_string('disable');
148 $strmanage = get_string('manageinstance', 'enrol');
150 $table = new html_table();
151 $table->head = array(get_string('name'), get_string('users'), $strup.'/'.$strdown, get_string('edit'));
152 $table->align = array('left', 'center', 'center', 'center');
153 $table->width = '100%';
154 $table->data = array();
156 // iterate through enrol plugins and add to the display table
157 $updowncount = 1;
158 $icount = count($instances);
159 $url = new moodle_url('/enrol/instances.php', array('sesskey'=>sesskey(), 'id'=>$course->id));
160 foreach ($instances as $instance) {
161 if (!isset($plugins[$instance->enrol])) {
162 continue;
164 $plugin = $plugins[$instance->enrol];
166 $displayname = $plugin->get_instance_name($instance);
167 if (!enrol_is_enabled($instance->enrol) or $instance->status != ENROL_INSTANCE_ENABLED) {
168 $displayname = html_writer::tag('span', $displayname, array('class'=>'dimmed_text'));
171 $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
173 $updown = array();
174 $edit = array();
176 if ($canconfig) {
177 // up/down link
178 $updown = '';
179 if ($updowncount > 1) {
180 $aurl = new moodle_url($url, array('action'=>'up', 'instance'=>$instance->id));
181 $updown[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/up'), 'alt'=>$strup, 'class'=>'smallicon')));
182 } else {
183 $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'smallicon'));
185 if ($updowncount < $icount) {
186 $aurl = new moodle_url($url, array('action'=>'down', 'instance'=>$instance->id));
187 $updown[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/down'), 'alt'=>$strdown, 'class'=>'smallicon')));
188 } else {
189 $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'smallicon'));
191 ++$updowncount;
193 // edit links
194 if ($plugin->instance_deleteable($instance)) {
195 $aurl = new moodle_url($url, array('action'=>'delete', 'instance'=>$instance->id));
196 $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'alt'=>$strdelete, 'class'=>'smallicon')));
199 if (enrol_is_enabled($instance->enrol)) {
200 if ($instance->status == ENROL_INSTANCE_ENABLED) {
201 $aurl = new moodle_url($url, array('action'=>'disable', 'instance'=>$instance->id));
202 $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/hide'), 'alt'=>$strdisable, 'class'=>'smallicon')));
203 } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
204 $aurl = new moodle_url($url, array('action'=>'enable', 'instance'=>$instance->id));
205 $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>$strenable, 'class'=>'smallicon')));
206 } else {
207 // plugin specific state - do not mess with it!
208 $edit[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>'', 'class'=>'smallicon'));
214 // link to instance management
215 if (enrol_is_enabled($instance->enrol)) {
216 if ($icons = $plugin->get_action_icons($instance)) {
217 $edit = array_merge($edit, $icons);
221 // add a row to the table
222 $table->data[] = array($displayname, $users, implode('&nbsp;', $updown), implode('&nbsp;', $edit));
225 echo html_writer::table($table);
227 // access security is in each plugin
228 $candidates = array();
229 foreach (enrol_get_plugins(true) as $name=>$plugin) {
230 if (!$link = $plugin->get_newinstance_link($course->id)) {
231 continue;
233 $candidates[$link->out(false)] = get_string('pluginname', 'enrol_'.$name);
236 if ($candidates) {
237 $select = new url_select($candidates);
238 $select->set_label(get_string('addinstance', 'enrol'));
239 echo $OUTPUT->render($select);
242 echo $OUTPUT->box_end();
244 echo $OUTPUT->footer();