MDL-64032 enrol_manual: Make consistent the UI for setting enrolment
[moodle.git] / enrol / manual / manage.php
blob0df80cf1fcb68b85d45c5a27a37c1ddcfad352ed
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 * Manual user enrolment UI.
20 * @package enrol_manual
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');
26 require_once($CFG->dirroot.'/enrol/manual/locallib.php');
28 $enrolid = required_param('enrolid', PARAM_INT);
29 $roleid = optional_param('roleid', -1, PARAM_INT);
30 $extendperiod = optional_param('extendperiod', 0, PARAM_INT);
31 $extendbase = optional_param('extendbase', 0, PARAM_INT);
32 $timeend = optional_param_array('timeend', [], PARAM_INT);
34 $instance = $DB->get_record('enrol', array('id'=>$enrolid, 'enrol'=>'manual'), '*', MUST_EXIST);
35 $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
36 $context = context_course::instance($course->id, MUST_EXIST);
38 require_login($course);
39 $canenrol = has_capability('enrol/manual:enrol', $context);
40 $canunenrol = has_capability('enrol/manual:unenrol', $context);
41 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
43 // Note: manage capability not used here because it is used for editing
44 // of existing enrolments which is not possible here.
46 if (!$canenrol and !$canunenrol) {
47 // No need to invent new error strings here...
48 require_capability('enrol/manual:enrol', $context);
49 require_capability('enrol/manual:unenrol', $context);
52 if ($roleid < 0) {
53 $roleid = $instance->roleid;
55 $roles = get_assignable_roles($context);
56 $roles = array('0'=>get_string('none')) + $roles;
58 if (!isset($roles[$roleid])) {
59 // Weird - security always first!
60 $roleid = 0;
63 if (!$enrol_manual = enrol_get_plugin('manual')) {
64 throw new coding_exception('Can not instantiate enrol_manual');
67 $instancename = $enrol_manual->get_instance_name($instance);
69 $PAGE->set_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id));
70 $PAGE->set_pagelayout('admin');
71 $PAGE->set_title($enrol_manual->get_instance_name($instance));
72 $PAGE->set_heading($course->fullname);
73 navigation_node::override_active_url(new moodle_url('/user/index.php', array('id'=>$course->id)));
75 // Create the user selector objects.
76 $options = array('enrolid' => $enrolid, 'accesscontext' => $context);
78 $potentialuserselector = new enrol_manual_potential_participant('addselect', $options);
79 $potentialuserselector->viewfullnames = $viewfullnames;
80 $currentuserselector = new enrol_manual_current_participant('removeselect', $options);
81 $currentuserselector->viewfullnames = $viewfullnames;
83 // Build the list of options for the enrolment period dropdown.
84 $unlimitedperiod = get_string('unlimited');
85 $periodmenu = array();
86 for ($i=1; $i<=365; $i++) {
87 $seconds = $i * 86400;
88 $periodmenu[$seconds] = get_string('numdays', '', $i);
90 // Work out the apropriate default settings.
91 if ($extendperiod) {
92 $defaultperiod = $extendperiod;
93 } else {
94 $defaultperiod = $instance->enrolperiod;
96 if ($instance->enrolperiod > 0 && !isset($periodmenu[$instance->enrolperiod])) {
97 $periodmenu[$instance->enrolperiod] = format_time($instance->enrolperiod);
99 if (empty($extendbase)) {
100 if (!$extendbase = get_config('enrol_manual', 'enrolstart')) {
101 // Default to now if there is no system setting.
102 $extendbase = 4;
106 // Build the list of options for the starting from dropdown.
107 $now = time();
108 $today = make_timestamp(date('Y', $now), date('m', $now), date('d', $now), 0, 0, 0);
109 $dateformat = get_string('strftimedatefullshort');
111 // Enrolment start.
112 $basemenu = array();
113 if ($course->startdate > 0) {
114 $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $dateformat) . ')';
116 $basemenu[3] = get_string('today') . ' (' . userdate($today, $dateformat) . ')';
117 $basemenu[4] = get_string('now', 'enrol_manual') . ' (' . userdate($now, get_string('strftimedatetimeshort')) . ')';
119 // Process add and removes.
120 if ($canenrol && optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
121 $userstoassign = $potentialuserselector->get_selected_users();
122 if (!empty($userstoassign)) {
123 foreach($userstoassign as $adduser) {
124 switch($extendbase) {
125 case 2:
126 $timestart = $course->startdate;
127 break;
128 case 4:
129 // We mimic get_enrolled_sql round(time(), -2) but always floor as we want users to always access their
130 // courses once they are enrolled.
131 $timestart = intval(substr($now, 0, 8) . '00') - 1;
132 break;
133 case 3:
134 default:
135 $timestart = $today;
136 break;
139 if ($timeend) {
140 $timeend = make_timestamp($timeend['year'], $timeend['month'], $timeend['day'], $timeend['hour'],
141 $timeend['minute']);
142 } else if ($extendperiod <= 0) {
143 $timeend = 0;
144 } else {
145 $timeend = $timestart + $extendperiod;
147 $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
150 $potentialuserselector->invalidate_selected_users();
151 $currentuserselector->invalidate_selected_users();
153 //TODO: log
157 // Process incoming role unassignments.
158 if ($canunenrol && optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
159 $userstounassign = $currentuserselector->get_selected_users();
160 if (!empty($userstounassign)) {
161 foreach($userstounassign as $removeuser) {
162 $enrol_manual->unenrol_user($instance, $removeuser->id);
165 $potentialuserselector->invalidate_selected_users();
166 $currentuserselector->invalidate_selected_users();
168 //TODO: log
173 echo $OUTPUT->header();
174 echo $OUTPUT->heading($instancename);
176 $addenabled = $canenrol ? '' : 'disabled="disabled"';
177 $removeenabled = $canunenrol ? '' : 'disabled="disabled"';
180 <form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
181 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
183 <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
184 <tr>
185 <td id="existingcell">
186 <p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
187 <?php $currentuserselector->display() ?>
188 </td>
189 <td id="buttonscell">
190 <div id="addcontrols">
191 <input name="add" <?php echo $addenabled; ?> id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
193 <div class="enroloptions">
195 <p><label for="menuroleid"><?php print_string('assignrole', 'enrol_manual') ?></label><br />
196 <?php echo html_writer::select($roles, 'roleid', $roleid, false); ?></p>
198 <p><label for="menuextendperiod"><?php print_string('enrolperiod', 'enrol') ?></label><br />
199 <?php echo html_writer::select($periodmenu, 'extendperiod', $defaultperiod, $unlimitedperiod); ?></p>
201 <p><label for="menuextendbase"><?php print_string('startingfrom') ?></label><br />
202 <?php echo html_writer::select($basemenu, 'extendbase', $extendbase, false); ?></p>
204 </div>
205 </div>
207 <div id="removecontrols">
208 <input name="remove" id="remove" <?php echo $removeenabled; ?> type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
209 </div>
210 </td>
211 <td id="potentialcell">
212 <p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
213 <?php $potentialuserselector->display() ?>
214 </td>
215 </tr>
216 </table>
217 </div></form>
218 <?php
221 echo $OUTPUT->footer();