/// Double check just in case they are enrolled to start in the future
if ($student = get_record('user_students', 'userid', $USER->id, 'course', $course->id)) {
- $message = get_string('enrolmentnotyet', '', userdate($student->timestart));
- print_header();
- notice($message, $CFG->wwwroot);
+ if ($course->enrolperiod and $student->timestart and ($student->timestart >= time())) {
+ $message = get_string('enrolmentnotyet', '', userdate($student->timestart));
+ print_header();
+ notice($message, $CFG->wwwroot);
+ }
}
/// Check the submitted enrollment key if there is one
exit;
} else {
- if ($course->enrolperiod == 0) {
- $timestart = 0;
- $timeend = 0;
- } else {
+ if ($course->enrolperiod) {
$timestart = time();
$timeend = time() + $course->enrolperiod;
+ } else {
+ $timestart = $timeend = 0;
}
if (! enrol_student($USER->id, $course->id, $timestart, $timeend)) {
add_to_log($course->id, "course", "guest", "view.php?id=$course->id", $_SERVER['REMOTE_ADDR']);
- } else if (!record_exists("user_students", "userid", $USER->id, "course", $course->id)) {
+ } else { /// Update or add new enrolment
- if (! enrol_student($USER->id, $course->id)) {
+ if ($course->enrolperiod) {
+ $timestart = time();
+ $timeend = $timestart + $course->enrolperiod;
+ } else {
+ $timestart = $timeend = 0;
+ }
+
+ if (! enrol_student($USER->id, $course->id, $timestart, $timeend)) {
error("An error occurred while trying to enrol you.");
}
}
function enrol_student($userid, $courseid, $timestart=0, $timeend=0) {
-/// Enrols a student in a given course
- global $CFG;
-
- $course = get_record("course", "id", $courseid);
-
- if (!record_exists("user_students", "userid", $userid, "course", $courseid)) {
- if (record_exists("user", "id", $userid)) {
+/// Enrols (or re-enrols) a student in a given course
- require_once('../mod/forum/lib.php');
- forum_add_user($userid, $courseid);
-
- $student->userid = $userid;
- $student->course = $courseid;
- $student->timestart = $timestart;
- $student->timeend = $timeend;
- $student->time = time();
- return insert_record("user_students", $student);
- }
+ if (!$course = get_record("course", "id", $courseid)) { // Check course
return false;
}
- return true;
+ if (!$user = get_record("user", "id", $userid)) { // Check user
+ return false;
+ }
+ if ($student = get_record("user_students", "userid", $userid, "course", $courseid)) {
+ $student->timestart = $timestart;
+ $student->timeend = $timeend;
+ $student->time = time();
+ return update_record("user_students", $student);
+
+ } else {
+ $student->userid = $userid;
+ $student->course = $courseid;
+ $student->timestart = $timestart;
+ $student->timeend = $timeend;
+ $student->time = time();
+ return insert_record("user_students", $student);
+ }
}
function unenrol_student($userid, $courseid=0) {