]> git.mjollnir.org Git - moodle.git/commitdiff
role_assign() and role_unassign() now call these hooks in each module:
authormoodler <moodler>
Wed, 13 Sep 2006 08:07:14 +0000 (08:07 +0000)
committermoodler <moodler>
Wed, 13 Sep 2006 08:07:14 +0000 (08:07 +0000)
    xxx_role_assign()

    xxx_role_unassign()

so that modules can do stuff they might need to do (eg subscriptions etc)

lib/accesslib.php

index 31f24fe9033c6d4e587858edf87cc86a10e96083..fe015e2550423aac3f0e0453221947c13ebe1688 100755 (executable)
@@ -1300,9 +1300,19 @@ function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $time
             load_user_capability();
         }
 
-    /// Make sure the user is subscribed to any appropriate forums in this context
-        require_once($CFG->dirroot.'/mod/forum/lib.php');
-        forum_add_user_default_subscriptions($userid, $context);
+    /// Ask all the modules if anything needs to be done for this user
+        if ($mods = get_list_of_plugins('mod')) {
+            foreach ($mods as $mod) {
+                include_once($CFG->dirroot.'/mod/'.$mod.'/lib.php');
+                $functionname = $mod.'_role_assign';
+                if (function_exists($functionname)) {
+                    $functionname($userid, $context);
+                }
+            }
+        }
+
+    /// Make sure they have an entry in user_lastaccess for courses they can access
+    //    role_add_lastaccess_entries($userid, $context);
     }
 
     return $success;
@@ -1337,9 +1347,24 @@ function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) {
                 load_user_capability();
             }
     
-        /// Make sure the user is unsubscribed from any appropriate forums in this context
-            require_once($CFG->dirroot.'/mod/forum/lib.php');
-            forum_remove_user_subscriptions($userid, $context);
+            if ($contextid) {
+                if ($context = get_record('context', 'id', $contextid)) {
+
+                /// Ask all the modules if anything needs to be done for this user
+                    if ($mods = get_list_of_plugins('mod')) {
+                        foreach ($mods as $mod) {
+                            include_once($CFG->dirroot.'/mod/'.$mod.'/lib.php');
+                            $functionname = $mod.'_role_unassign';
+                            if (function_exists($functionname)) {
+                                $functionname($userid, $context);
+                            }
+                        }
+                    }
+        
+                /// Remove entries from user_lastaccess for courses they can no longer access
+                    //role_add_lastaccess_entries($userid, $context);
+                }
+            }
 
             return true;
         }
@@ -1348,6 +1373,72 @@ function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) {
     return true;
 }
 
+/**
+ * Add last access times to user_lastaccess as required
+ * @param $userid
+ * @param $context
+ * @return boolean - success or failure
+ */
+function role_add_lastaccess_entries($userid, $context) {
+
+    global $USER, $CFG;
+
+    if (empty($context->aggregatelevel)) {
+        return false;
+    }
+
+    $lastaccess = new object;        // Reusable object below
+    $lastaccess->userid = $userid;
+    $lastaccess->timeaccess = 0;
+
+    switch ($context->aggregatelevel) {
+
+        case CONTEXT_SYSTEM:   // For the whole site
+             if ($courses = get_record('course')) {
+                 foreach ($courses as $course) {
+                     $lastaccess->courseid = $course->id;
+                     role_set_lastaccess($lastaccess);
+                 }
+             }
+             break;
+
+        case CONTEXT_CATEGORY:   // For a whole category
+             if ($courses = get_record('course', 'category', $context->instanceid)) {
+                 foreach ($courses as $course) {
+                     $lastaccess->courseid = $course->id;
+                     role_set_lastaccess($lastaccess);
+                 }
+             }
+             if ($categories = get_record('course_categories', 'parent', $context->instanceid)) {
+                 foreach ($categories as $category) {
+                     $subcontext = get_context_instance(CONTEXT_CATEGORY, $category->id);
+                     role_add_lastaccess_entries($userid, $subcontext);
+                 }
+             }
+             break;
+    
+
+        case CONTEXT_COURSE:   // For a whole course
+             if ($course = get_record('course', 'id', $context->instanceid)) {
+                 $lastaccess->courseid = $course->id;
+                 role_set_lastaccess($lastaccess);
+             }
+             break;
+    }
+}
+
+/**
+ * Delete last access times from user_lastaccess as required
+ * @param $userid
+ * @param $context
+ * @return boolean - success or failure
+ */
+function role_remove_lastaccess_entries($userid, $context) {
+
+    global $USER, $CFG;
+
+}
+
 
 /**
  * Loads the capability definitions for the component (from file). If no