Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / enrol / manual / manage.php
blob30168fd50ffc1557d08f9d33e59588d76b8868e9
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);
42 // Note: manage capability not used here because it is used for editing
43 // of existing enrolments which is not possible here.
45 if (!$canenrol and !$canunenrol) {
46 // No need to invent new error strings here...
47 require_capability('enrol/manual:enrol', $context);
48 require_capability('enrol/manual:unenrol', $context);
51 if ($roleid < 0) {
52 $roleid = $instance->roleid;
54 $roles = get_assignable_roles($context);
55 $roles = array('0'=>get_string('none')) + $roles;
57 if (!isset($roles[$roleid])) {
58 // Weird - security always first!
59 $roleid = 0;
62 if (!$enrol_manual = enrol_get_plugin('manual')) {
63 throw new coding_exception('Can not instantiate enrol_manual');
66 $url = new moodle_url('/enrol/manual/manage.php', ['enrolid' => $instance->id]);
67 $title = get_string('managemanualenrolements', 'enrol_manual');
69 $PAGE->set_url($url);
70 $PAGE->set_pagelayout('admin');
71 $PAGE->set_title($title);
72 $PAGE->set_heading($course->fullname);
73 navigation_node::override_active_url(new moodle_url('/enrol/instances.php', ['id' => $course->id]));
74 $PAGE->navbar->add($title, $url);
76 // Create the user selector objects.
77 $options = array('enrolid' => $enrolid, 'accesscontext' => $context);
79 $potentialuserselector = new enrol_manual_potential_participant('addselect', $options);
80 $currentuserselector = new enrol_manual_current_participant('removeselect', $options);
82 // Build the list of options for the enrolment period dropdown.
83 $unlimitedperiod = get_string('unlimited');
84 $periodmenu = array();
85 for ($i=1; $i<=365; $i++) {
86 $seconds = $i * 86400;
87 $periodmenu[$seconds] = get_string('numdays', '', $i);
89 // Work out the apropriate default settings.
90 if ($extendperiod) {
91 $defaultperiod = $extendperiod;
92 } else {
93 $defaultperiod = $instance->enrolperiod;
95 if ($instance->enrolperiod > 0 && !isset($periodmenu[$instance->enrolperiod])) {
96 $periodmenu[$instance->enrolperiod] = format_time($instance->enrolperiod);
98 if (empty($extendbase)) {
99 if (!$extendbase = get_config('enrol_manual', 'enrolstart')) {
100 // Default to now if there is no system setting.
101 $extendbase = 4;
105 // Build the list of options for the starting from dropdown.
106 $now = time();
107 $today = make_timestamp(date('Y', $now), date('m', $now), date('d', $now), 0, 0, 0);
108 $dateformat = get_string('strftimedatefullshort');
110 // Enrolment start.
111 $basemenu = array();
112 if ($course->startdate > 0) {
113 $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $dateformat) . ')';
115 $basemenu[3] = get_string('today') . ' (' . userdate($today, $dateformat) . ')';
116 $basemenu[4] = get_string('now', 'enrol_manual') . ' (' . userdate($now, get_string('strftimedatetimeshort')) . ')';
118 // Process add and removes.
119 if ($canenrol && optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
120 $userstoassign = $potentialuserselector->get_selected_users();
121 if (!empty($userstoassign)) {
122 foreach($userstoassign as $adduser) {
123 switch($extendbase) {
124 case 2:
125 $timestart = $course->startdate;
126 break;
127 case 4:
128 // We mimic get_enrolled_sql round(time(), -2) but always floor as we want users to always access their
129 // courses once they are enrolled.
130 $timestart = intval(substr($now, 0, 8) . '00') - 1;
131 break;
132 case 3:
133 default:
134 $timestart = $today;
135 break;
138 if ($timeend) {
139 $timeend = make_timestamp($timeend['year'], $timeend['month'], $timeend['day'], $timeend['hour'],
140 $timeend['minute']);
141 } else if ($extendperiod <= 0) {
142 $timeend = 0;
143 } else {
144 $timeend = $timestart + $extendperiod;
146 $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
149 $potentialuserselector->invalidate_selected_users();
150 $currentuserselector->invalidate_selected_users();
152 //TODO: log
156 // Process incoming role unassignments.
157 if ($canunenrol && optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
158 $userstounassign = $currentuserselector->get_selected_users();
159 if (!empty($userstounassign)) {
160 foreach($userstounassign as $removeuser) {
161 $enrol_manual->unenrol_user($instance, $removeuser->id);
164 $potentialuserselector->invalidate_selected_users();
165 $currentuserselector->invalidate_selected_users();
167 //TODO: log
172 echo $OUTPUT->header();
173 echo $OUTPUT->heading($title);
175 $addenabled = $canenrol ? '' : 'disabled="disabled"';
176 $removeenabled = $canunenrol ? '' : 'disabled="disabled"';
179 <form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
180 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
182 <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
183 <tr>
184 <td id="existingcell">
185 <p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
186 <?php $currentuserselector->display() ?>
187 </td>
188 <td id="buttonscell">
189 <div id="addcontrols">
190 <input class="btn btn-secondary" name="add" <?php echo $addenabled; ?> id="add" type="submit"
191 value="<?php echo $OUTPUT->larrow() . '&nbsp;' . get_string('add'); ?>"
192 title="<?php print_string('add'); ?>" /><br />
194 <div class="enroloptions">
196 <p><label for="menuroleid"><?php print_string('assignrole', 'enrol_manual') ?></label><br />
197 <?php echo html_writer::select($roles, 'roleid', $roleid, false); ?></p>
199 <p><label for="menuextendperiod"><?php print_string('enrolperiod', 'enrol') ?></label><br />
200 <?php echo html_writer::select($periodmenu, 'extendperiod', $defaultperiod, $unlimitedperiod); ?></p>
202 <p><label for="menuextendbase"><?php print_string('startingfrom') ?></label><br />
203 <?php echo html_writer::select($basemenu, 'extendbase', $extendbase, false); ?></p>
205 </div>
206 </div>
208 <div id="removecontrols">
209 <input class="btn btn-secondary" name="remove" id="remove" <?php echo $removeenabled; ?> type="submit"
210 value="<?php echo get_string('remove') . '&nbsp;' . $OUTPUT->rarrow(); ?>"
211 title="<?php print_string('remove'); ?>" />
212 </div>
213 </td>
214 <td id="potentialcell">
215 <p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
216 <?php $potentialuserselector->display() ?>
217 </td>
218 </tr>
219 </table>
220 </div></form>
221 <?php
224 echo $OUTPUT->footer();