From 631cf796d4f02de8600e29105b78d506e5aa1c48 Mon Sep 17 00:00:00 2001 From: moodler Date: Thu, 9 Sep 2004 09:43:51 +0000 Subject: [PATCH] Merged enrolment fixes frm stable --- course/enrol.php | 8 +++++--- enrol/enrol.class.php | 18 ++++++++++++------ lib/moodlelib.php | 37 +++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/course/enrol.php b/course/enrol.php index bd2d7ee8cc..43596f6d8b 100644 --- a/course/enrol.php +++ b/course/enrol.php @@ -52,9 +52,11 @@ /// 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 diff --git a/enrol/enrol.class.php b/enrol/enrol.class.php index 96ef284447..1c4261d9c1 100644 --- a/enrol/enrol.class.php +++ b/enrol/enrol.class.php @@ -138,12 +138,11 @@ function print_entry($course) { 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)) { @@ -200,9 +199,16 @@ function check_entry($form, $course) { 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."); } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 643047585b..2468cc9de9 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -858,27 +858,28 @@ function authenticate_user_login($username, $password) { } 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) { -- 2.39.5