From 05549cc5d077651f9cab90970268754ad8b8dac7 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Thu, 26 Sep 2013 16:23:25 +0800 Subject: [PATCH] MDL-42004 enrol_cohort: Convert handlers to observers --- enrol/cohort/db/events.php | 31 ++++++++++++++----------------- enrol/cohort/locallib.php | 28 ++++++++++++++-------------- enrol/cohort/version.php | 2 +- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/enrol/cohort/db/events.php b/enrol/cohort/db/events.php index 9de7dcfbde2..7b902fdfc2f 100644 --- a/enrol/cohort/db/events.php +++ b/enrol/cohort/db/events.php @@ -25,26 +25,23 @@ defined('MOODLE_INTERNAL') || die(); -/* List of handlers. */ -$handlers = array ( - 'cohort_member_added' => array ( - 'handlerfile' => '/enrol/cohort/locallib.php', - 'handlerfunction' => array('enrol_cohort_handler', 'member_added'), - 'schedule' => 'instant', - 'internal' => 1, +$observers = array( + + array( + 'eventname' => '\core\event\cohort_member_added', + 'callback' => 'enrol_cohort_handler::member_added', + 'includefile' => '/enrol/cohort/locallib.php' ), - 'cohort_member_removed' => array ( - 'handlerfile' => '/enrol/cohort/locallib.php', - 'handlerfunction' => array('enrol_cohort_handler', 'member_removed'), - 'schedule' => 'instant', - 'internal' => 1, + array( + 'eventname' => '\core\event\cohort_member_removed', + 'callback' => 'enrol_cohort_handler::member_removed', + 'includefile' => '/enrol/cohort/locallib.php' ), - 'cohort_deleted' => array ( - 'handlerfile' => '/enrol/cohort/locallib.php', - 'handlerfunction' => array('enrol_cohort_handler', 'deleted'), - 'schedule' => 'instant', - 'internal' => 1, + array( + 'eventname' => '\core\event\cohort_deleted', + 'callback' => 'enrol_cohort_handler::deleted', + 'includefile' => '/enrol/cohort/locallib.php' ), ); diff --git a/enrol/cohort/locallib.php b/enrol/cohort/locallib.php index 377584e49a9..e07c20bb241 100644 --- a/enrol/cohort/locallib.php +++ b/enrol/cohort/locallib.php @@ -36,10 +36,10 @@ require_once($CFG->dirroot . '/enrol/locallib.php'); class enrol_cohort_handler { /** * Event processor - cohort member added. - * @param stdClass $ca + * @param \core\event\cohort_member_added $event * @return bool */ - public static function member_added($ca) { + public static function member_added(\core\event\cohort_member_added $event) { global $DB, $CFG; require_once("$CFG->dirroot/group/lib.php"); @@ -53,7 +53,7 @@ class enrol_cohort_handler { LEFT JOIN {role} r ON (r.id = e.roleid) WHERE e.customint1 = :cohortid AND e.enrol = 'cohort' ORDER BY e.id ASC"; - if (!$instances = $DB->get_records_sql($sql, array('cohortid'=>$ca->cohortid))) { + if (!$instances = $DB->get_records_sql($sql, array('cohortid'=>$event->objectid))) { return true; } @@ -68,13 +68,13 @@ class enrol_cohort_handler { } unset($instance->roleexists); // No problem if already enrolled. - $plugin->enrol_user($instance, $ca->userid, $instance->roleid, 0, 0, ENROL_USER_ACTIVE); + $plugin->enrol_user($instance, $event->relateduserid, $instance->roleid, 0, 0, ENROL_USER_ACTIVE); // Sync groups. if ($instance->customint2) { - if (!groups_is_member($instance->customint2, $ca->userid)) { + if (!groups_is_member($instance->customint2, $event->relateduserid)) { if ($group = $DB->get_record('groups', array('id'=>$instance->customint2, 'courseid'=>$instance->courseid))) { - groups_add_member($group->id, $ca->userid, 'enrol_cohort', $instance->id); + groups_add_member($group->id, $event->relateduserid, 'enrol_cohort', $instance->id); } } } @@ -85,14 +85,14 @@ class enrol_cohort_handler { /** * Event processor - cohort member removed. - * @param stdClass $ca + * @param \core\event\cohort_member_removed $event * @return bool */ - public static function member_removed($ca) { + public static function member_removed(\core\event\cohort_member_removed $event) { global $DB; // Does anything want to sync with this cohort? - if (!$instances = $DB->get_records('enrol', array('customint1'=>$ca->cohortid, 'enrol'=>'cohort'), 'id ASC')) { + if (!$instances = $DB->get_records('enrol', array('customint1'=>$event->objectid, 'enrol'=>'cohort'), 'id ASC')) { return true; } @@ -100,11 +100,11 @@ class enrol_cohort_handler { $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); foreach ($instances as $instance) { - if (!$ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$ca->userid))) { + if (!$ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$event->relateduserid))) { continue; } if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) { - $plugin->unenrol_user($instance, $ca->userid); + $plugin->unenrol_user($instance, $event->relateduserid); } else { if ($ue->status != ENROL_USER_SUSPENDED) { @@ -120,14 +120,14 @@ class enrol_cohort_handler { /** * Event processor - cohort deleted. - * @param stdClass $cohort + * @param \core\event\cohort_deleted $event * @return bool */ - public static function deleted($cohort) { + public static function deleted(\core\event\cohort_deleted $event) { global $DB; // Does anything want to sync with this cohort? - if (!$instances = $DB->get_records('enrol', array('customint1'=>$cohort->id, 'enrol'=>'cohort'), 'id ASC')) { + if (!$instances = $DB->get_records('enrol', array('customint1'=>$event->objectid, 'enrol'=>'cohort'), 'id ASC')) { return true; } diff --git a/enrol/cohort/version.php b/enrol/cohort/version.php index 2024bbdf0b8..e5df70d9c03 100644 --- a/enrol/cohort/version.php +++ b/enrol/cohort/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2013050100; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2013092600; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2013050100; // Requires this Moodle version $plugin->component = 'enrol_cohort'; // Full name of the plugin (used for diagnostics) $plugin->cron = 60*60; // run cron every hour by default, it is not out-of-sync often