82d7a31870b156c19f01312d77748db0c9fbf1e7
[moodle.git] / enrol / manual / manage.php
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/>.
17 /**
18  * Manual user enrolment UI.
19  *
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
23  */
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);
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);
48 }
50 if ($roleid < 0) {
51     $roleid = $instance->roleid;
52 }
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;
59 }
61 if (!$enrol_manual = enrol_get_plugin('manual')) {
62     throw new coding_exception('Can not instantiate enrol_manual');
63 }
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);
85 }
86 // Work out the apropriate default settings.
87 if ($extendperiod) {
88     $defaultperiod = $extendperiod;
89 } else {
90     $defaultperiod = $instance->enrolperiod;
91 }
92 if (!isset($periodmenu[$defaultperiod])) {
93     $periodmenu[$defaultperiod] = get_string('numdays', '', floor($defaultperiod / DAYSECS));
94 }
95 if (empty($extendbase)) {
96     if (!$extendbase = get_config('enrol_manual', 'enrolstart')) {
97         // Default to now if there is no system setting.
98         $extendbase = 4;
99     }
102 // Build the list of options for the starting from dropdown.
103 $now = time();
104 $today = make_timestamp(date('Y', $now), date('m', $now), date('d', $now), 0, 0, 0);
105 $dateformat = get_string('strftimedatefullshort');
107 // Enrolment start.
108 $basemenu = array();
109 if ($course->startdate > 0) {
110     $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $dateformat) . ')';
112 $basemenu[3] = get_string('today') . ' (' . userdate($today, $dateformat) . ')';
113 $basemenu[4] = get_string('now', 'enrol_manual') . ' (' . userdate($now, get_string('strftimedatetimeshort')) . ')';
115 // Process add and removes.
116 if ($canenrol && optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
117     $userstoassign = $potentialuserselector->get_selected_users();
118     if (!empty($userstoassign)) {
119         foreach($userstoassign as $adduser) {
120             switch($extendbase) {
121                 case 2:
122                     $timestart = $course->startdate;
123                     break;
124                 case 4:
125                     // We mimic get_enrolled_sql round(time(), -2) but always floor as we want users to always access their
126                     // courses once they are enrolled.
127                     $timestart = intval(substr($now, 0, 8) . '00') - 1;
128                     break;
129                 case 3:
130                 default:
131                     $timestart = $today;
132                     break;
133             }
135             if ($extendperiod <= 0) {
136                 $timeend = 0;
137             } else {
138                 $timeend = $timestart + $extendperiod;
139             }
140             $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
141         }
143         $potentialuserselector->invalidate_selected_users();
144         $currentuserselector->invalidate_selected_users();
146         //TODO: log
147     }
150 // Process incoming role unassignments.
151 if ($canunenrol && optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
152     $userstounassign = $currentuserselector->get_selected_users();
153     if (!empty($userstounassign)) {
154         foreach($userstounassign as $removeuser) {
155             $enrol_manual->unenrol_user($instance, $removeuser->id);
156         }
158         $potentialuserselector->invalidate_selected_users();
159         $currentuserselector->invalidate_selected_users();
161         //TODO: log
162     }
166 echo $OUTPUT->header();
167 echo $OUTPUT->heading($instancename);
169 $addenabled = $canenrol ? '' : 'disabled="disabled"';
170 $removeenabled = $canunenrol ? '' : 'disabled="disabled"';
172 ?>
173 <form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
174   <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
176   <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
177     <tr>
178       <td id="existingcell">
179           <p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
180           <?php $currentuserselector->display() ?>
181       </td>
182       <td id="buttonscell">
183           <div id="addcontrols">
184               <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 />
186               <div class="enroloptions">
188               <p><label for="menuroleid"><?php print_string('assignrole', 'enrol_manual') ?></label><br />
189               <?php echo html_writer::select($roles, 'roleid', $roleid, false); ?></p>
191               <p><label for="menuextendperiod"><?php print_string('enrolperiod', 'enrol') ?></label><br />
192               <?php echo html_writer::select($periodmenu, 'extendperiod', $defaultperiod, $unlimitedperiod); ?></p>
194               <p><label for="menuextendbase"><?php print_string('startingfrom') ?></label><br />
195               <?php echo html_writer::select($basemenu, 'extendbase', $extendbase, false); ?></p>
197               </div>
198           </div>
200           <div id="removecontrols">
201               <input name="remove" id="remove" <?php echo $removeenabled; ?> type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
202           </div>
203       </td>
204       <td id="potentialcell">
205           <p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
206           <?php $potentialuserselector->display() ?>
207       </td>
208     </tr>
209   </table>
210 </div></form>
211 <?php
214 echo $OUTPUT->footer();