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);
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
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]) {
38 $student->timeend
= 0;
41 switch($form->extendbase
[$k]) {
42 case 0: // course start date
43 $student->timeend
= $course->startdate +
$form->extendperiod
[$k];
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];
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];
58 case 3: // current date
59 $student->timeend
= $today +
$form->extendperiod
[$k];
61 case 4: // course enrolment start date
62 if($course->enrolstartdate
> 0) {
63 $student->timeend
= $course->enrolstartdate +
$form->extendperiod
[$k];
66 case 5: // course enrolment end date
67 if($course->enrolenddate
> 0) {
68 $student->timeend
= $course->enrolenddate +
$form->extendperiod
[$k];
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'));
84 if ($course->id
!= SITEID
) {
85 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname
,
86 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
87 get_string('extendenrol'), "", "", true, " ", navmenu($course));
89 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname
,
90 get_string('extendenrol'), "", "", true, " ", navmenu($course));
93 $timeformat = get_string('strftimedate');
94 $unlimited = get_string('unlimited');
95 $periodmenu[-1] = $unlimited;
96 for ($i=1; $i<=365; $i++
) {
97 $seconds = $i * 86400;
98 $periodmenu[$seconds] = get_string('numdays', '', $i);
101 // this will contain all available the based On select options, but we'll disable some on them on a per user basis
102 $basemenu[0] = get_string('startdate') . ' (' . userdate($course->startdate
, $timeformat) . ')';
103 $basemenu[1] = get_string('enrolmentstart');
104 $basemenu[2] = get_string('enrolmentend');
105 if($course->enrollable
!= 2 ||
($course->enrolstartdate
== 0 ||
$course->enrolstartdate
<= $today) && ($course->enrolenddate
== 0 ||
$course->enrolenddate
> $today)) {
106 $basemenu[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
108 if($course->enrollable
== 2) {
109 if($course->enrolstartdate
> 0) {
110 $basemenu[4] = get_string('courseenrolstartdate') . ' (' . userdate($course->enrolstartdate
, $timeformat) . ')';
112 if($course->enrolenddate
> 0) {
113 $basemenu[5] = get_string('courseenrolenddate') . ' (' . userdate($course->enrolenddate
, $timeformat) . ')';
117 $title = get_string('extendenrol');
118 print_heading($title . helpbutton('extendenrol', $title, 'moodle', true, false, '', true));
119 echo "<form method=\"post\" action=\"extendenrol.php\">\n";
120 echo '<input type="hidden" name="id" value="'.$course->id
.'" />';
121 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
122 $table->head
= array (get_string('fullname'), get_string('enrolmentstart'), get_string('enrolmentend'), get_string('extendperiod'), get_string('startingfrom'));
123 $table->align
= array ('left', 'center', 'center', 'center');
124 $table->width
= "600";
125 $nochange = get_string('nochange');
126 $notavailable = get_string('notavailable');
127 foreach ($_POST as $k => $v) {
128 if (preg_match('/^user(\d+)$/',$k,$m)) {
130 if (!($user = get_record_sql("SELECT * FROM {$CFG->prefix}user u
131 INNER JOIN {$CFG->prefix}role_assignments ra ON u.id=ra.userid
132 WHERE u.id={$m[1]} AND ra.contextid = $context->id"))) {
135 $userbasemenu = $basemenu;
136 if ($user->timestart
) {
137 $timestart = userdate($user->timestart
, $timeformat);
139 $timestart = $notavailable;
140 unset($userbasemenu[1]);
142 if ($user->timeend
) {
143 $timeend = userdate($user->timeend
, $timeformat);
145 $timeend = $unlimited;
146 unset($userbasemenu[2]);
148 $checkbox = choose_from_menu($periodmenu, "extendperiod[{$m[1]}]", "0", $nochange, '', '0', true);
149 $checkbox2 = choose_from_menu($userbasemenu, "extendbase[{$m[1]}]", "2", "", '', '0', true);
150 $table->data
[] = array(
151 fullname($user, true),
154 '<input type="hidden" name="userid['.$m[1].']" value="'.$m[1].'" />'.$checkbox,
160 echo "\n<div style=\"width:100%;text-align:center;\"><input type=\"submit\" value=\"".get_string('savechanges')."\" /></div>\n</form>\n";
162 print_footer($course);