Commit 080c7d47 authored by Marina Glancy's avatar Marina Glancy
Browse files

MDL-44640 enrol: trigger events on instances actions

parent fd57d685
......@@ -96,6 +96,7 @@ if ($mform->is_cancelled()) {
$instance->customint2 = $groupid;
}
$DB->update_record('enrol', $instance);
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
} else {
// Create a new group for the cohort if requested.
if ($data->customint2 == COHORT_CREATE_GROUP) {
......
......@@ -332,6 +332,7 @@ class enrol_guest_plugin extends enrol_plugin {
$instance->password = $data->{'enrol_guest_password_'.$i};
}
$DB->update_record('enrol', $instance);
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
if ($reset) {
$context = context_course::instance($course->id);
......
......@@ -92,13 +92,13 @@ if ($mform->is_cancelled()) {
$instance->notifyall = $data->notifyall;
$instance->expirythreshold = $data->expirythreshold;
$instance->timemodified = time();
$markdirty = ($instance->status != $data->status);
$instance->status = $data->status;
$DB->update_record('enrol', $instance);
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
// Use standard API to update instance status.
if ($instance->status != $data->status) {
$instance = $DB->get_record('enrol', array('id'=>$instance->id));
$plugin->update_status($instance, $data->status);
if ($markdirty) {
$context->mark_dirty();
}
......
......@@ -76,6 +76,7 @@ if ($mform->is_cancelled()) {
$instance->enrolenddate = $data->enrolenddate;
$instance->timemodified = time();
$DB->update_record('enrol', $instance);
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
if ($reset) {
$context->mark_dirty();
......
......@@ -109,6 +109,7 @@ if ($mform->is_cancelled()) {
$instance->enrolenddate = $data->enrolenddate;
$instance->timemodified = time();
$DB->update_record('enrol', $instance);
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
if ($reset) {
$context->mark_dirty();
......
......@@ -362,4 +362,64 @@ class core_enrollib_testcase extends advanced_testcase {
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
/**
* Test enrol_instance_created, enrol_instance_updated and enrol_instance_deleted events.
*/
public function test_instance_events() {
global $DB;
$this->resetAfterTest(true);
$selfplugin = enrol_get_plugin('self');
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$course = $this->getDataGenerator()->create_course();
// Creating enrol instance.
$sink = $this->redirectEvents();
$instanceid = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
'name' => 'Test instance 1',
'customint6' => 1,
'roleid' => $studentrole->id));
$events = $sink->get_events();
$sink->close();
$this->assertCount(1, $events);
$event = array_pop($events);
$this->assertInstanceOf('\core\event\enrol_instance_created', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals('self', $event->other['enrol']);
$this->assertEventContextNotUsed($event);
// Updating enrol instance.
$instance = $DB->get_record('enrol', array('id' => $instanceid));
$sink = $this->redirectEvents();
$selfplugin->update_status($instance, ENROL_INSTANCE_DISABLED);
$events = $sink->get_events();
$sink->close();
$this->assertCount(1, $events);
$event = array_pop($events);
$this->assertInstanceOf('\core\event\enrol_instance_updated', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals('self', $event->other['enrol']);
$this->assertEventContextNotUsed($event);
// Deleting enrol instance.
$instance = $DB->get_record('enrol', array('id' => $instanceid));
$sink = $this->redirectEvents();
$selfplugin->delete_instance($instance);
$events = $sink->get_events();
$sink->close();
$this->assertCount(1, $events);
$event = array_pop($events);
$this->assertInstanceOf('\core\event\enrol_instance_deleted', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals('self', $event->other['enrol']);
$this->assertEventContextNotUsed($event);
}
}
This files describes API changes in /enrol/* - plugins,
information provided here is intended especially for developers.
=== 3.0 ===
* Added new events enrol_instance_created, enrol_instance_updated and
enrol_instance_deleted . Always trigger them when changing records in the
DB table 'enrol'.
=== 2.9 ===
* External function core_enrol_external::get_users_courses now returns additional optional fields:
......
......@@ -48,6 +48,9 @@ $string['enrolcandidates'] = 'Not enrolled users';
$string['enrolcandidatesmatching'] = 'Matching not enrolled users';
$string['enrolcohort'] = 'Enrol cohort';
$string['enrolcohortusers'] = 'Enrol users';
$string['eventenrolinstancecreated'] = 'Enrolment instance created';
$string['eventenrolinstancedeleted'] = 'Enrolment instance deleted';
$string['eventenrolinstanceupdated'] = 'Enrolment instance updated';
$string['enrollednewusers'] = 'Successfully enrolled {$a} new users';
$string['enrolledusers'] = 'Enrolled users';
$string['enrolledusersmatching'] = 'Matching enrolled users';
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Enrol instance created event.
*
* @package core
* @copyright 2015 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* Enrol instance created event class.
*
* @property-read array $other {
* Extra information about event.
*
* - string enrol: name of enrol method
* }
*
* @package core
* @since Moodle 2.9
* @copyright 2015 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_instance_created extends base {
/**
* Api to Create new event from enrol object.
*
* @param \stdClass $enrol record from DB table 'enrol'
* @return \core\event\base returns instance of new event
*/
public static final function create_from_record($enrol) {
$event = static::create(array(
'context' => \context_course::instance($enrol->courseid),
'objectid' => $enrol->id,
'other' => array('enrol' => $enrol->enrol)
));
return $event;
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' created the instance of enrolment method '" .
$this->other['enrol'] . "' with id '$this->objectid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventenrolinstancecreated', 'enrol');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'enrol';
}
/**
* custom validations
*
* Throw \coding_exception notice in case of any problems.
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['enrol'])) {
throw new \coding_exception('The \'enrol\' value must be set in other.');
}
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Enrol instance deleted event.
*
* @package core
* @copyright 2015 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* Enrol instance deleted event class.
*
* @property-read array $other {
* Extra information about event.
*
* - string enrol: name of enrol method
* }
*
* @package core
* @since Moodle 2.9
* @copyright 2015 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_instance_deleted extends base {
/**
* Api to Create new event from enrol object.
*
* @param \stdClass $enrol record from DB table 'enrol'
* @return \core\event\base returns instance of new event
*/
public static final function create_from_record($enrol) {
$event = static::create(array(
'context' => \context_course::instance($enrol->courseid),
'objectid' => $enrol->id,
'other' => array('enrol' => $enrol->enrol)
));
$event->add_record_snapshot('enrol', $enrol);
return $event;
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' deleted the instance of enrolment method '" .
$this->other['enrol'] . "' with id '$this->objectid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventgroupingdeleted', 'group');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'enrol';
}
/**
* custom validations
*
* Throw \coding_exception notice in case of any problems.
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['enrol'])) {
throw new \coding_exception('The \'enrol\' value must be set in other.');
}
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Enrol instance updated event.
*
* @package core
* @copyright 2015 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
/**
* Enrol instance updated event class.
*
* @property-read array $other {
* Extra information about event.
*
* - string enrol: name of enrol method
* }
*
* @package core
* @since Moodle 2.9
* @copyright 2015 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_instance_updated extends base {
/**
* Api to Create new event from enrol object.
*
* @param \stdClass $enrol record from DB table 'enrol'
* @return \core\event\base returns instance of new event
*/
public static final function create_from_record($enrol) {
$event = static::create(array(
'context' => \context_course::instance($enrol->courseid),
'objectid' => $enrol->id,
'other' => array('enrol' => $enrol->enrol)
));
$event->add_record_snapshot('enrol', $enrol);
return $event;
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' updated the instance of enrolment method '" .
$this->other['enrol'] . "' with id '$this->objectid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventenrolinstanceupdated', 'enrol');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'enrol';
}
/**
* custom validations
*
* Throw \coding_exception notice in case of any problems.
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['enrol'])) {
throw new \coding_exception('The \'enrol\' value must be set in other.');
}
}
}
......@@ -1692,7 +1692,11 @@ abstract class enrol_plugin {
$instance->$field = $value;
}
return $DB->insert_record('enrol', $instance);
$instance->id = $DB->insert_record('enrol', $instance);
\core\event\enrol_instance_created::create_from_record($instance)->trigger();
return $instance->id;
}
/**
......@@ -1723,8 +1727,10 @@ abstract class enrol_plugin {
$instance->status = $newstatus;
$DB->update_record('enrol', $instance);
// invalidate all enrol caches
$context = context_course::instance($instance->courseid);
\core\event\enrol_instance_updated::create_from_record($instance)->trigger();
// Invalidate all enrol caches.
$context->mark_dirty();
}
......@@ -1756,8 +1762,10 @@ abstract class enrol_plugin {
// finally drop the enrol row
$DB->delete_records('enrol', array('id'=>$instance->id));
// invalidate all enrol caches
$context = context_course::instance($instance->courseid);
\core\event\enrol_instance_deleted::create_from_record($instance)->trigger();
// Invalidate all enrol caches.
$context->mark_dirty();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment