Skip to content

Commit

Permalink
MDL-41413 Events: replaced course_deleted event handler with observer
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Sep 3, 2013
1 parent 8525a8f commit 9077fc6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 59 deletions.
49 changes: 49 additions & 0 deletions enrol/meta/classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,53 @@ public static function role_unassigned(\core\event\role_unassigned $event) {

return true;
}

/**
* Triggered via course_deleted event.
*
* @param \core\event\course_deleted $event
* @return bool true on success
*/
public static function course_deleted(\core\event\course_deleted $event) {
global $DB;

if (!enrol_is_enabled('meta')) {
// This is slow, let enrol_meta_sync() deal with disabled plugin.
return true;
}

// Does anything want to sync with this parent?
if (!$enrols = $DB->get_records('enrol', array('customint1' => $event->objectid, 'enrol' => 'meta'),
'courseid ASC, id ASC')) {
return true;
}

$plugin = enrol_get_plugin('meta');
$unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);

if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// Simple, just delete this instance which purges all enrolments,
// admins were warned that this is risky setting!
foreach ($enrols as $enrol) {
$plugin->delete_instance($enrol);
}
return true;
}

foreach ($enrols as $enrol) {
$enrol->customint = 0;
$DB->update_record('enrol', $enrol);

if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND or $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
// This makes all enrolments suspended very quickly.
$plugin->update_status($enrol, ENROL_INSTANCE_DISABLED);
}
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
$context = context_course::instance($enrol->courseid);
role_unassign_all(array('contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id));
}
}

return true;
}
}
14 changes: 4 additions & 10 deletions enrol/meta/db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@

defined('MOODLE_INTERNAL') || die();

/* List of handlers */
$handlers = array (
'course_deleted' => array (
'handlerfile' => '/enrol/meta/locallib.php',
'handlerfunction' => array('enrol_meta_handler', 'course_deleted'),
'schedule' => 'instant',
'internal' => 1,
),
);

// List of observers.
$observers = array(

Expand All @@ -58,4 +48,8 @@
'eventname' => '\core\event\role_unassigned',
'callback' => 'enrol_meta_observer::role_unassigned',
),
array(
'eventname' => '\core\event\course_deleted',
'callback' => 'enrol_meta_observer::course_deleted',
),
);
49 changes: 0 additions & 49 deletions enrol/meta/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,57 +233,8 @@ protected static function user_not_supposed_to_be_here($instance, $ue, context_c
debugging('Unknown unenrol action '.$unenrolaction);
}
}

/**
* Triggered via course_deleted event.
* @static
* @param stdClass $course
* @return bool success
*/
public static function course_deleted($course) {
global $DB;

if (!enrol_is_enabled('meta')) {
// This is slow, let enrol_meta_sync() deal with disabled plugin.
return true;
}

// does anything want to sync with this parent?
if (!$enrols = $DB->get_records('enrol', array('customint1'=>$course->id, 'enrol'=>'meta'), 'courseid ASC, id ASC')) {
return true;
}

$plugin = enrol_get_plugin('meta');
$unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);

if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// Simple, just delete this instance which purges all enrolments,
// admins were warned that this is risky setting!
foreach ($enrols as $enrol) {
$plugin->delete_instance($enrol);
}
return true;
}

foreach ($enrols as $enrol) {
$enrol->customint = 0;
$DB->update_record('enrol', $enrol);

if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND or $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
// This makes all enrolments suspended very quickly.
$plugin->update_status($enrol, ENROL_INSTANCE_DISABLED);
}
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
$context = context_course::instance($enrol->courseid);
role_unassign_all(array('contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id));
}
}

return true;
}
}


/**
* Sync all meta course links.
*
Expand Down

0 comments on commit 9077fc6

Please sign in to comment.