MDL-10873 If both site default and user pref are empty for studentsperpage, we assume...
[moodle-pu.git] / user / groupextendenrol.php
blob18b6c78087fddbe00ef376e52c0235776605fe18
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);
19 if ((count($users) > 0) and ($form = data_submitted()) and confirm_sesskey()) {
21 foreach ($form->userid as $k => $v) {
22 // find all roles this student have in this course
23 if ($students = get_records_sql("SELECT ra.id, ra.roleid, ra.timestart, ra.timeend
24 FROM {$CFG->prefix}role_assignments ra
25 WHERE userid = $v
26 AND contextid = $context->id")) {
27 // enrol these users again, with time extension
28 // not that this is not necessarily a student role
29 foreach ($students as $student) {
30 // only extend if the user can make role assignments on this role
31 if (user_can_assign($context, $student->roleid)) {
32 switch($form->extendperiod) {
33 case 0: // No change (currently this option is not available in dropdown list, but it might be ...)
34 break;
35 case -1: // unlimited
36 $student->timeend = 0;
37 break;
38 default: // extend
39 switch($form->extendbase) {
40 case 0: // course start date
41 $student->timeend = $course->startdate + $form->extendperiod;
42 break;
43 case 1: // student enrolment start date
44 // we check for student enrolment date because Moodle versions before 1.9 did not set this for
45 // unlimited enrolment courses, so it might be 0
46 if($student->timestart > 0) {
47 $student->timeend = $student->timestart + $form->extendperiod;
49 break;
50 case 2: // student enrolment start date
51 // enrolment end equals 0 means Unlimited, so adding some time to that will still yield Unlimited
52 if($student->timeend > 0) {
53 $student->timeend = $student->timeend + $form->extendperiod;
55 break;
56 case 3: // current date
57 $student->timeend = $today + $form->extendperiod;
58 break;
59 case 4: // course enrolment start date
60 if($course->enrolstartdate > 0) {
61 $student->timeend = $course->enrolstartdate + $form->extendperiod;
63 break;
64 case 5: // course enrolment end date
65 if($course->enrolenddate > 0) {
66 $student->timeend = $course->enrolenddate + $form->extendperiod;
68 break;
71 role_assign($student->roleid, $v, 0, $context->id, $student->timestart, $student->timeend, 0);
77 redirect("$CFG->wwwroot/user/index.php?id=$id", get_string('changessaved'));
80 /// Print headers
82 if ($course->id != SITEID) {
83 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname,
84 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
85 get_string('extendenrol'), "", "", true, "&nbsp;", navmenu($course));
86 } else {
87 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname,
88 get_string('extendenrol'), "", "", true, "&nbsp;", navmenu($course));
91 $timeformat = get_string('strftimedate');
92 $unlimited = get_string('unlimited');
93 $periodmenu[-1] = $unlimited;
94 for ($i=1; $i<=365; $i++) {
95 $seconds = $i * 86400;
96 $periodmenu[$seconds] = get_string('numdays', '', $i);
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('groupextendenrol');
115 print_heading($title . helpbutton('groupextendenrol', $title, 'moodle', true, false, '', true));
116 echo '<form method="post" action="groupextendenrol.php">';
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'));
120 $table->align = array ('left', 'center', 'center', 'center');
121 $table->width = "600";
122 $nochange = get_string('nochange');
123 $notavailable = get_string('notavailable');
125 foreach ($_POST as $k => $v) {
126 if (preg_match('/^user(\d+)$/',$k,$m)) {
128 if (!($user = get_record_sql("SELECT * FROM {$CFG->prefix}user u
129 INNER JOIN {$CFG->prefix}role_assignments ra ON u.id=ra.userid
130 WHERE u.id={$m[1]} AND ra.contextid = $context->id"))) {
131 continue;
133 if ($user->timestart) {
134 $timestart = userdate($user->timestart, $timeformat);
135 } else {
136 $timestart = $notavailable;
138 if ($user->timeend) {
139 $timeend = userdate($user->timeend, $timeformat);
140 } else {
141 $timeend = $unlimited;
143 $table->data[] = array(
144 fullname($user, true),
145 $timestart,
146 $timeend . '<input type="hidden" name="userid['.$m[1].']" value="'.$m[1].'" />'
150 print_table($table);
151 echo '<div style="width:100%;text-align:center;"><strong>';
152 echo get_string('extendperiod') . ' ';
153 choose_from_menu($periodmenu, "extendperiod");
154 echo ' ' . get_string('startingfrom') . ' ';
155 choose_from_menu($basemenu, "extendbase", "2", "");
156 echo '</strong><br />';
157 echo '<input type="submit" value="'.get_string('savechanges').'" />';
158 echo '</div></form>';
160 print_footer($course);