MDL-43175 events: Base event unit tests for JSON encoding / decoding comparison.
[moodle.git] / enrol / manual / manage.php
blob7d91e4b6017ba9d8068df916a11cc28556b4ce26
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', 3, PARAM_INT);
33 $instance = $DB->get_record('enrol', array('id'=>$enrolid, 'enrol'=>'manual'), '*', MUST_EXIST);
34 $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
35 $context = context_course::instance($course->id, MUST_EXIST);
37 require_login($course);
38 $canenrol = has_capability('enrol/manual:enrol', $context);
39 $canunenrol = has_capability('enrol/manual:unenrol', $context);
41 // Note: manage capability not used here because it is used for editing
42 // of existing enrolments which is not possible here.
44 if (!$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);
50 if ($roleid < 0) {
51 $roleid = $instance->roleid;
53 $roles = get_assignable_roles($context);
54 $roles = array('0'=>get_string('none')) + $roles;
56 if (!isset($roles[$roleid])) {
57 // Weird - security always first!
58 $roleid = 0;
61 if (!$enrol_manual = enrol_get_plugin('manual')) {
62 throw new coding_exception('Can not instantiate enrol_manual');
65 $instancename = $enrol_manual->get_instance_name($instance);
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));
70 $PAGE->set_heading($course->fullname);
71 navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
73 // Create the user selector objects.
74 $options = array('enrolid' => $enrolid, 'accesscontext' => $context);
76 $potentialuserselector = new enrol_manual_potential_participant('addselect', $options);
77 $currentuserselector = new enrol_manual_current_participant('removeselect', $options);
79 // Build the list of options for the enrolment period dropdown.
80 $unlimitedperiod = get_string('unlimited');
81 $periodmenu = array();
82 for ($i=1; $i<=365; $i++) {
83 $seconds = $i * 86400;
84 $periodmenu[$seconds] = get_string('numdays', '', $i);
86 // Work out the apropriate default setting.
87 if ($extendperiod) {
88 $defaultperiod = $extendperiod;
89 } else {
90 $defaultperiod = $instance->enrolperiod;
93 // Build the list of options for the starting from dropdown.
94 $timeformat = get_string('strftimedatefullshort');
95 $today = time();
96 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
98 // Enrolment start.
99 $basemenu = array();
100 if ($course->startdate > 0) {
101 $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $timeformat) . ')';
103 $basemenu[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
105 // Process add and removes.
106 if ($canenrol && optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
107 $userstoassign = $potentialuserselector->get_selected_users();
108 if (!empty($userstoassign)) {
109 foreach($userstoassign as $adduser) {
110 switch($extendbase) {
111 case 2:
112 $timestart = $course->startdate;
113 break;
114 case 3:
115 default:
116 $timestart = $today;
117 break;
120 if ($extendperiod <= 0) {
121 $timeend = 0;
122 } else {
123 $timeend = $timestart + $extendperiod;
125 $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
126 add_to_log($course->id, 'course', 'enrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere!
129 $potentialuserselector->invalidate_selected_users();
130 $currentuserselector->invalidate_selected_users();
132 //TODO: log
136 // Process incoming role unassignments.
137 if ($canunenrol && optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
138 $userstounassign = $currentuserselector->get_selected_users();
139 if (!empty($userstounassign)) {
140 foreach($userstounassign as $removeuser) {
141 $enrol_manual->unenrol_user($instance, $removeuser->id);
142 add_to_log($course->id, 'course', 'unenrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere!
145 $potentialuserselector->invalidate_selected_users();
146 $currentuserselector->invalidate_selected_users();
148 //TODO: log
153 echo $OUTPUT->header();
154 echo $OUTPUT->heading($instancename);
156 $addenabled = $canenrol ? '' : 'disabled="disabled"';
157 $removeenabled = $canunenrol ? '' : 'disabled="disabled"';
160 <form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
161 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
163 <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
164 <tr>
165 <td id="existingcell">
166 <p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
167 <?php $currentuserselector->display() ?>
168 </td>
169 <td id="buttonscell">
170 <div id="addcontrols">
171 <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 />
173 <div class="enroloptions">
175 <p><label for="menuroleid"><?php print_string('assignrole', 'enrol_manual') ?></label><br />
176 <?php echo html_writer::select($roles, 'roleid', $roleid, false); ?></p>
178 <p><label for="menuextendperiod"><?php print_string('enrolperiod', 'enrol') ?></label><br />
179 <?php echo html_writer::select($periodmenu, 'extendperiod', $defaultperiod, $unlimitedperiod); ?></p>
181 <p><label for="menuextendbase"><?php print_string('startingfrom') ?></label><br />
182 <?php echo html_writer::select($basemenu, 'extendbase', $extendbase, false); ?></p>
184 </div>
185 </div>
187 <div id="removecontrols">
188 <input name="remove" id="remove" <?php echo $removeenabled; ?> type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
189 </div>
190 </td>
191 <td id="potentialcell">
192 <p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
193 <?php $potentialuserselector->display() ?>
194 </td>
195 </tr>
196 </table>
197 </div></form>
198 <?php
201 echo $OUTPUT->footer();