MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / user / extendenrol.php
blob1adc0344bacbdf0b520ff55695b0aa8aff956529
1 <?php // $Id$
2 require_once("../config.php");
4 $id = required_param('id', PARAM_INT); // course id
5 $users = optional_param('userid', array(), PARAM_INT); // array of user id
7 if (! $course = get_record('course', 'id', $id)) {
8 error("Course ID is incorrect");
11 $context = get_context_instance(CONTEXT_COURSE, $id);
12 require_login($course->id);
14 // to extend enrolments current user needs to be able to do role assignments
15 require_capability('moodle/role:assign', $context);
16 $today = time();
17 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
18 if ((count($users) > 0) and ($form = data_submitted()) and confirm_sesskey()) {
19 if (count($form->userid) != count($form->extendperiod) || count($form->userid) != count($form->extendbase)) {
20 error('Parameters malformation', $CFG->wwwroot.'/user/index.php?id='.$id);
23 foreach ($form->userid as $k => $v) {
24 // find all roles this student have in this course
25 if ($students = get_records_sql("SELECT ra.id, ra.roleid, ra.timestart, ra.timeend
26 FROM {$CFG->prefix}role_assignments ra
27 WHERE userid = $v
28 AND contextid = $context->id")) {
29 // enrol these users again, with time extension
30 // not that this is not necessarily a student role
31 foreach ($students as $student) {
32 // only extend if the user can make role assignments on this role
33 if (user_can_assign($context, $student->roleid)) {
34 switch($form->extendperiod[$k]) {
35 case 0: // No change
36 break;
37 case -1: // unlimited
38 $student->timeend = 0;
39 break;
40 default: // extend
41 switch($form->extendbase[$k]) {
42 case 0: // course start date
43 $student->timeend = $course->startdate + $form->extendperiod[$k];
44 break;
45 case 1: // student enrolment start date
46 // we check for student enrolment date because Moodle versions before 1.9 did not set this for
47 // unlimited enrolment courses, so it might be 0
48 if($student->timestart > 0) {
49 $student->timeend = $student->timestart + $form->extendperiod[$k];
51 break;
52 case 2: // student enrolment start date
53 // enrolment end equals 0 means Unlimited, so adding some time to that will still yield Unlimited
54 if($student->timeend > 0) {
55 $student->timeend = $student->timeend + $form->extendperiod[$k];
57 break;
58 case 3: // current date
59 $student->timeend = $today + $form->extendperiod[$k];
60 break;
61 case 4: // course enrolment start date
62 if($course->enrolstartdate > 0) {
63 $student->timeend = $course->enrolstartdate + $form->extendperiod[$k];
65 break;
66 case 5: // course enrolment end date
67 if($course->enrolenddate > 0) {
68 $student->timeend = $course->enrolenddate + $form->extendperiod[$k];
70 break;
73 role_assign($student->roleid, $v, 0, $context->id, $student->timestart, $student->timeend, 0);
79 redirect("$CFG->wwwroot/user/index.php?id=$id", get_string('changessaved'));
82 /// Print headers
84 $navlinks = array();
85 $navlinks[] = array('name' => get_string('extendenrol'), 'link' => null, 'type' => 'misc');
86 $navigation = build_navigation($navlinks);
88 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname, $navigation, "", "", true, "&nbsp;", navmenu($course));
90 $timeformat = get_string('strftimedate');
91 $unlimited = get_string('unlimited');
92 $periodmenu[-1] = $unlimited;
93 for ($i=1; $i<=365; $i++) {
94 $seconds = $i * 86400;
95 $periodmenu[$seconds] = get_string('numdays', '', $i);
98 // this will contain all available the based On select options, but we'll disable some on them on a per user basis
99 $basemenu[0] = get_string('startdate') . ' (' . userdate($course->startdate, $timeformat) . ')';
100 $basemenu[1] = get_string('enrolmentstart');
101 $basemenu[2] = get_string('enrolmentend');
102 if($course->enrollable != 2 || ($course->enrolstartdate == 0 || $course->enrolstartdate <= $today) && ($course->enrolenddate == 0 || $course->enrolenddate > $today)) {
103 $basemenu[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
105 if($course->enrollable == 2) {
106 if($course->enrolstartdate > 0) {
107 $basemenu[4] = get_string('courseenrolstartdate') . ' (' . userdate($course->enrolstartdate, $timeformat) . ')';
109 if($course->enrolenddate > 0) {
110 $basemenu[5] = get_string('courseenrolenddate') . ' (' . userdate($course->enrolenddate, $timeformat) . ')';
114 $title = get_string('extendenrol');
115 print_heading($title . helpbutton('extendenrol', $title, 'moodle', true, false, '', true));
116 echo "<form method=\"post\" action=\"extendenrol.php\">\n";
117 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
118 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
119 $table->head = array (get_string('fullname'), get_string('enrolmentstart'), get_string('enrolmentend'), get_string('extendperiod'), get_string('startingfrom'));
120 $table->align = array ('left', 'center', 'center', 'center');
121 $table->width = "600";
122 $nochange = get_string('nochange');
123 $notavailable = get_string('notavailable');
124 foreach ($_POST as $k => $v) {
125 if (preg_match('/^user(\d+)$/',$k,$m)) {
127 if (!($user = get_record_sql("SELECT * FROM {$CFG->prefix}user u
128 INNER JOIN {$CFG->prefix}role_assignments ra ON u.id=ra.userid
129 WHERE u.id={$m[1]} AND ra.contextid = $context->id"))) {
130 continue;
132 $userbasemenu = $basemenu;
133 if ($user->timestart) {
134 $timestart = userdate($user->timestart, $timeformat);
135 } else {
136 $timestart = $notavailable;
137 unset($userbasemenu[1]);
139 if ($user->timeend) {
140 $timeend = userdate($user->timeend, $timeformat);
141 } else {
142 $timeend = $unlimited;
143 unset($userbasemenu[2]);
145 $checkbox = choose_from_menu($periodmenu, "extendperiod[{$m[1]}]", "0", $nochange, '', '0', true);
146 $checkbox2 = choose_from_menu($userbasemenu, "extendbase[{$m[1]}]", "2", "", '', '0', true);
147 $table->data[] = array(
148 fullname($user, true),
149 $timestart,
150 $timeend,
151 '<input type="hidden" name="userid['.$m[1].']" value="'.$m[1].'" />'.$checkbox,
152 $checkbox2
156 print_table($table);
157 echo "\n<div style=\"width:100%;text-align:center;\"><input type=\"submit\" value=\"".get_string('savechanges')."\" /></div>\n</form>\n";
159 print_footer($course);