Skip to content

Commit

Permalink
MDL-42004 enrol_cohort: Convert handlers to observers
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Sep 26, 2013
1 parent 65a4f26 commit 05549cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
31 changes: 14 additions & 17 deletions enrol/cohort/db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
);
28 changes: 14 additions & 14 deletions enrol/cohort/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
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");

Expand All @@ -53,7 +53,7 @@ public static function member_added($ca) {
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;
}

Expand All @@ -68,13 +68,13 @@ public static function member_added($ca) {
}
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);
}
}
}
Expand All @@ -85,26 +85,26 @@ public static function member_added($ca) {

/**
* 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;
}

$plugin = enrol_get_plugin('cohort');
$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) {
Expand All @@ -120,14 +120,14 @@ public static function member_removed($ca) {

/**
* 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;
}

Expand Down
2 changes: 1 addition & 1 deletion enrol/cohort/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 05549cc

Please sign in to comment.