source: moodle/trunk/fuentes/enrol/manual/manage.php

Last change on this file was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 7.8 KB
Line 
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 *
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 */
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);
31$extendbase   = optional_param('extendbase', 0, PARAM_INT);
32
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);
36
37require_login($course);
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}
49
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])) {
57    // Weird - security always first!
58    $roleid = 0;
59}
60
61if (!$enrol_manual = enrol_get_plugin('manual')) {
62    throw new coding_exception('Can not instantiate enrol_manual');
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));
70$PAGE->set_heading($course->fullname);
71navigation_node::override_active_url(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
72
73// Create the user selector objects.
74$options = array('enrolid' => $enrolid, 'accesscontext' => $context);
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}
86// Work out the apropriate default settings.
87if ($extendperiod) {
88    $defaultperiod = $extendperiod;
89} else {
90    $defaultperiod = $instance->enrolperiod;
91}
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}
98
99// Build the list of options for the starting from dropdown.
100$now = time();
101$today = make_timestamp(date('Y', $now), date('m', $now), date('d', $now), 0, 0, 0);
102$dateformat = get_string('strftimedatefullshort');
103
104// Enrolment start.
105$basemenu = array();
106if ($course->startdate > 0) {
107    $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $dateformat) . ')';
108}
109$basemenu[3] = get_string('today') . ' (' . userdate($today, $dateformat) . ')';
110$basemenu[4] = get_string('now', 'enrol_manual') . ' (' . userdate($now, get_string('strftimedatetimeshort')) . ')';
111
112// Process add and removes.
113if ($canenrol && optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
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;
121                case 4:
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;
125                    break;
126                case 3:
127                default:
128                    $timestart = $today;
129                    break;
130            }
131
132            if ($extendperiod <= 0) {
133                $timeend = 0;
134            } else {
135                $timeend = $timestart + $extendperiod;
136            }
137            $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
138        }
139
140        $potentialuserselector->invalidate_selected_users();
141        $currentuserselector->invalidate_selected_users();
142
143        //TODO: log
144    }
145}
146
147// Process incoming role unassignments.
148if ($canunenrol && optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
149    $userstounassign = $currentuserselector->get_selected_users();
150    if (!empty($userstounassign)) {
151        foreach($userstounassign as $removeuser) {
152            $enrol_manual->unenrol_user($instance, $removeuser->id);
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
166$addenabled = $canenrol ? '' : 'disabled="disabled"';
167$removeenabled = $canunenrol ? '' : 'disabled="disabled"';
168
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">
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 />
182
183              <div class="enroloptions">
184
185              <p><label for="menuroleid"><?php print_string('assignrole', 'enrol_manual') ?></label><br />
186              <?php echo html_writer::select($roles, 'roleid', $roleid, false); ?></p>
187
188              <p><label for="menuextendperiod"><?php print_string('enrolperiod', 'enrol') ?></label><br />
189              <?php echo html_writer::select($periodmenu, 'extendperiod', $defaultperiod, $unlimitedperiod); ?></p>
190
191              <p><label for="menuextendbase"><?php print_string('startingfrom') ?></label><br />
192              <?php echo html_writer::select($basemenu, 'extendbase', $extendbase, false); ?></p>
193
194              </div>
195          </div>
196
197          <div id="removecontrols">
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'); ?>" />
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();
Note: See TracBrowser for help on using the repository browser.