]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9983 role assignment related triggers; merged from MOODLE_19_STABLE
authorskodak <skodak>
Sun, 6 Jul 2008 19:30:12 +0000 (19:30 +0000)
committerskodak <skodak>
Sun, 6 Jul 2008 19:30:12 +0000 (19:30 +0000)
lib/accesslib.php
lib/db/events.php

index 70d140e6674a7c257a4b56975cadfb1f70426adb..3ac494dde9d4ca7a2c577fe85c6893a5cbc1e331 100755 (executable)
@@ -2690,64 +2690,62 @@ function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $time
         $ra = $DB->get_record('role_assignments', array('roleid'=>$roleid, 'contextid'=>$context->id, 'groupid'=>$groupid));
     }
 
-
-    $newra = new object;
-
     if (empty($ra)) {             // Create a new entry
-        $newra->roleid = $roleid;
-        $newra->contextid = $context->id;
-        $newra->userid = $userid;
-        $newra->hidden = $hidden;
-        $newra->enrol = $enrol;
+        $ra = new object();
+        $ra->roleid = $roleid;
+        $ra->contextid = $context->id;
+        $ra->userid = $userid;
+        $ra->hidden = $hidden;
+        $ra->enrol = $enrol;
     /// Always round timestart downto 100 secs to help DBs to use their own caching algorithms
     /// by repeating queries with the same exact parameters in a 100 secs time window
-        $newra->timestart = round($timestart, -2);
-        $newra->timeend = $timeend;
-        $newra->timemodified = $timemodified;
-        $newra->modifierid = empty($USER->id) ? 0 : $USER->id;
+        $ra->timestart = round($timestart, -2);
+        $ra->timeend = $timeend;
+        $ra->timemodified = $timemodified;
+        $ra->modifierid = empty($USER->id) ? 0 : $USER->id;
 
-        $success = $DB->insert_record('role_assignments', $newra);
+        if (!$ra->id = $DB->insert_record('role_assignments', $ra)) {
+            return false;
+        }
 
     } else {                      // We already have one, just update it
-
-        $newra->id = $ra->id;
-        $newra->hidden = $hidden;
-        $newra->enrol = $enrol;
+        $ra->id = $ra->id;
+        $ra->hidden = $hidden;
+        $ra->enrol = $enrol;
     /// Always round timestart downto 100 secs to help DBs to use their own caching algorithms
     /// by repeating queries with the same exact parameters in a 100 secs time window
-        $newra->timestart = round($timestart, -2);
-        $newra->timeend = $timeend;
-        $newra->timemodified = $timemodified;
-        $newra->modifierid = empty($USER->id) ? 0 : $USER->id;
+        $ra->timestart = round($timestart, -2);
+        $ra->timeend = $timeend;
+        $ra->timemodified = $timemodified;
+        $ra->modifierid = empty($USER->id) ? 0 : $USER->id;
 
-        $success = $DB->update_record('role_assignments', $newra);
+        if (!$DB->update_record('role_assignments', $ra)) {
+            return false;
+        }
     }
 
-    if ($success) {   /// Role was assigned, so do some other things
-
-    /// mark context as dirty - modules might use has_capability() in xxx_role_assing()
-    /// again expensive, but needed
-        mark_context_dirty($context->path);
+/// mark context as dirty - modules might use has_capability() in xxx_role_assing()
+/// again expensive, but needed
+    mark_context_dirty($context->path);
 
-        if (!empty($USER->id) && $USER->id == $userid) {
-    /// If the user is the current user, then do full reload of capabilities too.
-            load_all_capabilities();
-        }
+    if (!empty($USER->id) && $USER->id == $userid) {
+/// If the user is the current user, then do full reload of capabilities too.
+        load_all_capabilities();
+    }
 
-    /// 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, $roleid);
-                }
+/// 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, $roleid);
             }
         }
     }
 
     /// now handle metacourse role assignments if in course context
-    if ($success and $context->contextlevel == CONTEXT_COURSE) {
+    if ($context->contextlevel == CONTEXT_COURSE) {
         if ($parents = $DB->get_records('course_meta', array('child_course'=>$context->instanceid))) {
             foreach ($parents as $parent) {
                 sync_metacourse($parent->parent_course);
@@ -2755,7 +2753,9 @@ function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $time
         }
     }
 
-    return $success;
+    events_trigger('role_assigned', $ra);
+
+    return true;
 }
 
 
@@ -2793,11 +2793,16 @@ function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0, $enrol=NU
         if ($ras = $DB->get_records_select('role_assignments', implode(' AND ', $select), $params)) {
             $mods = get_list_of_plugins('mod');
             foreach($ras as $ra) {
+                $fireevent = false;
                 /// infinite loop protection when deleting recursively
                 if (!$ra = $DB->get_record('role_assignments', array('id'=>$ra->id))) {
                     continue;
                 }
-                $success = $DB->delete_records('role_assignments', array('id'=>$ra->id)) and $success;
+                if ($DB->delete_records('role_assignments', array('id'=>$ra->id))) {
+                    $fireevent = true;
+                } else {
+                    $success = false;
+                }
 
                 if (!$context = get_context_instance_by_id($ra->contextid)) {
                     // strange error, not much to do
@@ -2849,6 +2854,10 @@ function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0, $enrol=NU
                         }
                     }
                 }
+
+                if ($fireevent) {
+                    events_trigger('role_unassigned', $ra);
+                }
             }
         }
     }
index 95ffa9c8e32afa0a59a771a29ce9f179e4a78983..5995e2fcaf14afea662fa52073e795ed977c780c 100644 (file)
@@ -69,6 +69,11 @@ groups_groupings_groups_removed - int course id - removed all groups from all gr
 groups_groups_deleted           - int course id - deleted all course groups
 groups_groupings_deleted        - int course id - deleted all course groupings
 
+==== role related evetns ====
+
+role_assigned         - object role_assignments table record
+role_unassigned       - object role_assignments table record
+
 
 */