"MDL-14932, move the course section"
[moodle-linuxchix.git] / user / groupextendenrol.php
blobe941141f3825f6a4f66b2c558a21174463d1ce59
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 $navlinks = array();
83 $navlinks[] = array('name' => get_string('extendenrol'), 'link' => null, 'type' => 'misc');
84 $navigation = build_navigation($navlinks);
86 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname, $navigation, "", "", true, "&nbsp;", navmenu($course));
88 $timeformat = get_string('strftimedate');
89 $unlimited = get_string('unlimited');
90 $periodmenu[-1] = $unlimited;
91 for ($i=1; $i<=365; $i++) {
92 $seconds = $i * 86400;
93 $periodmenu[$seconds] = get_string('numdays', '', $i);
96 $basemenu[0] = get_string('startdate') . ' (' . userdate($course->startdate, $timeformat) . ')';
97 $basemenu[1] = get_string('enrolmentstart');
98 $basemenu[2] = get_string('enrolmentend');
99 if($course->enrollable != 2 || ($course->enrolstartdate == 0 || $course->enrolstartdate <= $today) && ($course->enrolenddate == 0 || $course->enrolenddate > $today)) {
100 $basemenu[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
102 if($course->enrollable == 2) {
103 if($course->enrolstartdate > 0) {
104 $basemenu[4] = get_string('courseenrolstartdate') . ' (' . userdate($course->enrolstartdate, $timeformat) . ')';
106 if($course->enrolenddate > 0) {
107 $basemenu[5] = get_string('courseenrolenddate') . ' (' . userdate($course->enrolenddate, $timeformat) . ')';
111 $title = get_string('groupextendenrol');
112 print_heading($title . helpbutton('groupextendenrol', $title, 'moodle', true, false, '', true));
113 echo '<form method="post" action="groupextendenrol.php">';
114 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
115 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
116 $table->head = array (get_string('fullname'), get_string('enrolmentstart'), get_string('enrolmentend'));
117 $table->align = array ('left', 'center', 'center', 'center');
118 $table->width = "600";
119 $nochange = get_string('nochange');
120 $notavailable = get_string('notavailable');
122 foreach ($_POST as $k => $v) {
123 if (preg_match('/^user(\d+)$/',$k,$m)) {
125 if (!($user = get_record_sql("SELECT * FROM {$CFG->prefix}user u
126 INNER JOIN {$CFG->prefix}role_assignments ra ON u.id=ra.userid
127 WHERE u.id={$m[1]} AND ra.contextid = $context->id"))) {
128 continue;
130 if ($user->timestart) {
131 $timestart = userdate($user->timestart, $timeformat);
132 } else {
133 $timestart = $notavailable;
135 if ($user->timeend) {
136 $timeend = userdate($user->timeend, $timeformat);
137 } else {
138 $timeend = $unlimited;
140 $table->data[] = array(
141 fullname($user, true),
142 $timestart,
143 $timeend . '<input type="hidden" name="userid['.$m[1].']" value="'.$m[1].'" />'
147 print_table($table);
148 echo '<div style="width:100%;text-align:center;"><strong>';
149 echo get_string('extendperiod') . ' ';
150 choose_from_menu($periodmenu, "extendperiod");
151 echo ' ' . get_string('startingfrom') . ' ';
152 choose_from_menu($basemenu, "extendbase", "2", "");
153 echo '</strong><br />';
154 echo '<input type="submit" value="'.get_string('savechanges').'" />';
155 echo '</div></form>';
157 print_footer($course);