Skip to content

Commit

Permalink
MDL-44640 enrol: trigger events on instances actions
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Sep 27, 2015
1 parent fd57d68 commit 080c7d4
Show file tree
Hide file tree
Showing 12 changed files with 417 additions and 7 deletions.
1 change: 1 addition & 0 deletions enrol/cohort/edit.php
Expand Up @@ -96,6 +96,7 @@
$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) {
Expand Down
1 change: 1 addition & 0 deletions enrol/guest/lib.php
Expand Up @@ -332,6 +332,7 @@ public function course_updated($inserted, $course, $data) {
$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);
Expand Down
8 changes: 4 additions & 4 deletions enrol/manual/edit.php
Expand Up @@ -92,13 +92,13 @@
$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();
}

Expand Down
1 change: 1 addition & 0 deletions enrol/paypal/edit.php
Expand Up @@ -76,6 +76,7 @@
$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();
Expand Down
1 change: 1 addition & 0 deletions enrol/self/edit.php
Expand Up @@ -109,6 +109,7 @@
$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();
Expand Down
60 changes: 60 additions & 0 deletions enrol/tests/enrollib_test.php
Expand Up @@ -362,4 +362,64 @@ public function test_user_enrolment_deleted_event() {
$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);
}
}
6 changes: 6 additions & 0 deletions enrol/upgrade.txt
@@ -1,6 +1,12 @@
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:
Expand Down
3 changes: 3 additions & 0 deletions lang/en/enrol.php
Expand Up @@ -48,6 +48,9 @@
$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';
Expand Down
109 changes: 109 additions & 0 deletions lib/classes/event/enrol_instance_created.php
@@ -0,0 +1,109 @@
<?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.');
}
}
}
110 changes: 110 additions & 0 deletions lib/classes/event/enrol_instance_deleted.php
@@ -0,0 +1,110 @@
<?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.');
}
}
}

0 comments on commit 080c7d4

Please sign in to comment.