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 if ((count($users) > 0) and ($form = data_submitted()) and confirm_sesskey()) {
18 if (count($form->userid
) != count($form->extendperiod
)) {
19 error('Parameters malformation', $CFG->wwwroot
.'/user/index.php?id='.$id);
22 foreach ($form->userid
as $k => $v) {
23 // find all roles this student have in this course
24 if ($students = get_records_sql("SELECT ra.id, ra.roleid, ra.timestart, ra.timeend
25 FROM {$CFG->prefix}role_assignments ra
27 AND contextid = $context->id")) {
28 // enrol these users again, with time extension
29 // not that this is not necessarily a student role
30 foreach ($students as $student) {
31 // only extend if the user can make role assignments on this role
32 if (user_can_assign($context, $student->roleid
)) {
33 role_assign($student->roleid
, $v, 0, $context->id
, $student->timestart
, $student->timeend +
$form->extendperiod
[$k], 0);
39 redirect("$CFG->wwwroot/user/index.php?id=$id", get_string('changessaved'));
44 if ($course->id
!= SITEID
) {
45 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname
,
46 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
47 get_string('extendenrol'), "", "", true, " ", navmenu($course));
49 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname
,
50 get_string('extendenrol'), "", "", true, " ", navmenu($course));
53 for ($i=1; $i<=365; $i++
) {
54 $seconds = $i * 86400;
55 $periodmenu[$seconds] = get_string('numdays', '', $i);
58 print_heading(get_string('extendenrol'));
59 echo "<form method=\"post\" action=\"extendenrol.php\">\n";
60 echo '<input type="hidden" name="id" value="'.$course->id
.'" />';
61 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
62 $table->head
= array (get_string('fullname'), get_string('enrolmentstart'), get_string('enrolmentend'), get_string('extendperiod'));
63 $table->align
= array ('left', 'center', 'center', 'center');
64 $table->width
= "600";
65 $timeformat = get_string('strftimedate');
66 $nochange = get_string('nochange');
67 $notavailable = get_string('notavailable');
68 $unlimited = get_string('unlimited');
69 foreach ($_POST as $k => $v) {
70 if (preg_match('/^user(\d+)$/',$k,$m)) {
72 if (!($user = get_record_sql("SELECT * FROM {$CFG->prefix}user u
73 INNER JOIN {$CFG->prefix}role_assignments ra ON u.id=ra.userid
74 WHERE u.id={$m[1]} AND ra.contextid = $context->id"))) {
77 if ($user->timestart
) {
78 $timestart = userdate($user->timestart
, $timeformat);
80 $timestart = $notavailable;
83 $timeend = userdate($user->timeend
, $timeformat);
84 $checkbox = choose_from_menu($periodmenu, "extendperiod[{$m[1]}]", "0", $nochange, '', '0', true);
86 $timeend = $unlimited;
87 $checkbox = '<input type="hidden" name="extendperiod['.$m[1].']" value="0" />'.$nochange;
89 $table->data
[] = array(
90 fullname($user, true),
93 '<input type="hidden" name="userid['.$m[1].']" value="'.$m[1].'" />'.$checkbox
98 echo "\n<div style=\"width:100%;text-align:center;\"><input type=\"submit\" value=\"".get_string('savechanges')."\" /></div>\n</form>\n";
100 print_footer($course);