Commit b33662e4 authored by Dan Poltawski's avatar Dan Poltawski
Browse files

Merge branch 'wip-MDL-44640-master' of https://github.com/marinaglancy/moodle

parents eb89f7cb 7fdc4690
......@@ -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();
}
......
......@@ -204,4 +204,30 @@ class enrol_meta_observer extends enrol_meta_handler {
return true;
}
/**
* Triggered via enrol_instance_updated event.
*
* @param \core\event\enrol_instance_updated $event
* @return boolean
*/
public static function enrol_instance_updated(\core\event\enrol_instance_updated $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?
$affectedcourses = $DB->get_fieldset_sql('SELECT DISTINCT courseid FROM {enrol} '.
'WHERE customint1 = ? AND enrol = ?',
array($event->courseid, 'meta'));
foreach ($affectedcourses as $courseid) {
enrol_meta_sync($courseid);
}
return true;
}
}
......@@ -52,4 +52,8 @@ $observers = array(
'eventname' => '\core\event\course_deleted',
'callback' => 'enrol_meta_observer::course_deleted',
),
array(
'eventname' => '\core\event\enrol_instance_updated',
'callback' => 'enrol_meta_observer::enrol_instance_updated',
),
);
......@@ -847,6 +847,16 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
// Disable manual enrolment in course1 and make sure all user enrolments in course2 are suspended.
$manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
$allsuspendedenrolemnts = array_combine(array_keys($expectedenrolments), array_fill(0, 5, ENROL_USER_SUSPENDED));
$enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta2id), '', 'userid, status');
$this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
$manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED);
$enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
$this->assertEquals($expectedenrolments, $enrolments);
// Disable events and repeat the same for course3 (testing sync):
$sink = $this->redirectEvents();
$manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
enrol_meta_sync($course3->id);
$enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta3id), '', 'userid, status');
$this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
......@@ -855,5 +865,6 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
enrol_meta_sync($course3->id);
$enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
$this->assertEquals($expectedenrolments, $enrolments);
$sink->close();
}
}
......@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015051100; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2015082400; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2015050500; // Requires this Moodle version
$plugin->component = 'enrol_meta'; // 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
......@@ -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