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