From 96608a55bdc84f08e42b390f449f1cfa5abc9308 Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 6 Jul 2008 19:30:12 +0000 Subject: [PATCH] MDL-9983 role assignment related triggers; merged from MOODLE_19_STABLE --- lib/accesslib.php | 93 ++++++++++++++++++++++++++--------------------- lib/db/events.php | 5 +++ 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 70d140e667..3ac494dde9 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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); + } } } } diff --git a/lib/db/events.php b/lib/db/events.php index 95ffa9c8e3..5995e2fcaf 100644 --- a/lib/db/events.php +++ b/lib/db/events.php @@ -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 + */ -- 2.39.5