]> git.mjollnir.org Git - moodle.git/commitdiff
Enrolments working better now. The core part is done, just the plugins to go.
authormoodler <moodler>
Sat, 16 Sep 2006 13:59:38 +0000 (13:59 +0000)
committermoodler <moodler>
Sat, 16 Sep 2006 13:59:38 +0000 (13:59 +0000)
The enrolment plugins are now checked from load_user_capability()

course/enrol.php
lib/moodlelib.php

index 9cfade9817ed0866ee84f56f2780394e9b52c5b5..47ffeee57efb989dee62fa1031697d5086d0524c 100644 (file)
 
     require_login();
 
-    if (! $course = get_record("course", "id", $id) ) {
+    if (! $course = get_record('course', 'id', $id) ) {
         error("That's an invalid course id");
     }
 
-    if (! $site = get_site()) {
-        error("Could not find a site!");
+    if (! $context = get_context_instance(CONTEXT_COURSE, $course->id) ) {
+        error("That's an invalid course id");
     }
 
-
-/// Refreshing enrolment data in the USER session
-    if (!($plugins = explode(',', $CFG->enrol_plugins_enabled))) {
-        $plugins = array($CFG->enrol);
-    }
-    require_once($CFG->dirroot .'/enrol/enrol.class.php');
-    foreach ($plugins as $p) {
-        $enrol = enrolment_factory::factory($p);
-        if (method_exists($enrol, 'get_student_courses')) {
-            $enrol->get_student_courses($USER);
-        }
-        if (method_exists($enrol, 'get_teacher_courses')) {
-            $enrol->get_teacher_courses($USER);
-        }
-        unset($enrol);
+    if (! $enrol = enrolment_factory::factory($course->enrol)) {
+        error("Could not determine course enrolment method!");
     }
 
-    $enrol = enrolment_factory::factory($course->enrol);
+/// Refreshing all current role assignments for the current user
 
-/// Double check just in case they are actually enrolled already 
-/// This might occur if they were enrolled during this session
-/// also happens when course is unhidden after student logs in
+    load_user_capability();
 
-    if ( !empty($USER->student[$course->id]) or !empty($USER->teacher[$course->id]) ) {
+/// Double check just in case they are actually enrolled already and 
+/// thus got to this script by mistake.  This might occur if enrolments 
+/// changed during this session or something
 
+    if (has_capability('moodle/course:view', $context)) {
         if ($SESSION->wantsurl) {
             $destination = $SESSION->wantsurl;
             unset($SESSION->wantsurl);
         } else {
             $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
         }
-
-        redirect($destination);
+        redirect($destination);   // Bye!
     }
 
-/// Check if the course is a meta course
-/// moved here to fix bug 5734
+/// Check if the course is a meta course  (bug 5734)
     if ($course->metacourse) {
         print_header_simple();
         notice(get_string('coursenotaccessible'), "$CFG->wwwroot/index.php");
     }
     
 /// Users can't enroll to site course
-    if (!$course->category) {
+    if ($course->id == SITEID) {
         print_header_simple();
         notice(get_string('enrollfirst'), "$CFG->wwwroot/index.php");
     }
 
 /// 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)) { 
-        if ($course->enrolperiod and $student->timestart and ($student->timestart >= time())) {
-            $message = get_string('enrolmentnotyet', '', userdate($student->timestart));
-            print_header();
-            notice($message, "$CFG->wwwroot/index.php");
+    if ($course->enrolperiod) {   // Only active if the course has an enrolment period in effect
+        if ($roles = get_user_roles($context, $USER->id)) {
+            foreach ($roles as $role) {
+                if ($role->timestart and ($role->timestart >= time())) {
+                    $message = get_string('enrolmentnotyet', '', userdate($student->timestart));
+                    print_header();
+                    notice($message, "$CFG->wwwroot/index.php");
+                }
+            }
         }
     }
 
         notice(get_string('notenrollable'), "$CFG->wwwroot/index.php");
     }
 
-/// Check the submitted enrollment key if there is one
+/// Check the submitted enrolment information if there is any (eg could be enrolment key)
 
     if ($form = data_submitted()) {
-      //User is not enrolled in the course, wants to access course content
-      //as a guest, and course setting allow unlimited guest access
-      //
-      //the original idea was to use "loginas" feature, but require_login() would have to be changed
-      //and we would have to explain it to all users - it is now plain login action
-      if ($loginasguest and !empty($CFG->guestloginbutton) and ($course->guest==1 or $course->guest==2)) {
-        if (isset($SESSION->currentgroup)) {
-            unset($SESSION->currentgroup);
-        }
-        $USER = get_complete_user_data('username', 'guest');    // get full guest user data
-        add_to_log(SITEID, 'user', 'login', "view.php?id=$USER->id&course=".SITEID, $USER->id, 0, $USER->id);
-        if ($SESSION->wantsurl) {
-            $destination = $SESSION->wantsurl;
-            unset($SESSION->wantsurl);
-        } else {
-            $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
-        }
-        redirect($destination);
-      }
-      $enrol->check_entry($form, $course);
+        $enrol->check_entry($form, $course);   // Should terminate/redirect in here if it's all OK
     }
 
+/// Otherwise, we print the entry form.
+
     $enrol->print_entry($course);
 
 /// Easy!
index 137b8934dead114b784b49b2fb011268411bdd43..422356d392b767d8760f23a72d59853a24f29383 100644 (file)
@@ -2455,7 +2455,7 @@ function update_internal_user_password(&$user, $password, $storeindb=true) {
 
 /**
  * Get a complete user record, which includes all the info
- * in the user record, as well as membership information
+ * in the user record
  * Intended for setting as $USER session variable
  *
  * @uses $CFG
@@ -2478,37 +2478,6 @@ function get_complete_user_data($field, $value) {
         return false;
     }
 
-/// Add membership information
-
-    if ($admins = get_records('user_admins', 'userid', $user->id)) {
-        $user->admin = true;
-    }
-
-    $user->student[SITEID] = isstudent(SITEID, $user->id);
-
-/// Load the list of enrolment plugin enabled
-
-    if (!($plugins = explode(',', $CFG->enrol_plugins_enabled))) {
-        $plugins = array($CFG->enrol);
-    }
-
-    require_once($CFG->dirroot .'/enrol/enrol.class.php');
-
-    foreach ($plugins as $p) {
-        $enrol = enrolment_factory::factory($p);
-        if (method_exists($enrol, 'setup_enrolments')) {  /// Plugin supports Roles (Moodle 1.7 and later)
-            $enrol->setup_enrolments($user);
-        } else {                                          /// Run legacy enrolment methods
-            if (method_exists($enrol, 'get_student_courses')) {
-                $enrol->get_student_courses($user);
-            }
-            if (method_exists($enrol, 'get_teacher_courses')) {
-                $enrol->get_teacher_courses($user);
-            }
-        }
-        unset($enrol);
-    }
-
 /// Get various settings and preferences
 
     if ($displays = get_records('course_display', 'userid', $user->id)) {
@@ -2547,10 +2516,10 @@ function get_complete_user_data($field, $value) {
     $user->sessionIP = md5(getremoteaddr());   // Store the current IP in the session
 
     return $user;
-
 }
 
 
+
 /*
  * When logging in, this function is run to set certain preferences
  * for the current SESSION