From d32d64bcbcbcb59d14483e33247577047224415f Mon Sep 17 00:00:00 2001 From: Issam Taboubi Date: Wed, 10 Feb 2016 15:20:04 -0500 Subject: [PATCH] MDL-52959 tool_lp: Create C(R)UD events for competencies --- admin/tool/lp/classes/api.php | 47 ++++- .../lp/classes/event/competency_created.php | 110 ++++++++++++ .../lp/classes/event/competency_deleted.php | 115 ++++++++++++ .../lp/classes/event/competency_updated.php | 110 ++++++++++++ .../lp/classes/event/competency_viewed.php | 13 +- admin/tool/lp/editcompetency.php | 26 ++- admin/tool/lp/lang/en/tool_lp.php | 3 + admin/tool/lp/tests/event_test.php | 166 ++++++++++++++++++ admin/tool/lp/version.php | 2 +- 9 files changed, 565 insertions(+), 27 deletions(-) create mode 100644 admin/tool/lp/classes/event/competency_created.php create mode 100644 admin/tool/lp/classes/event/competency_deleted.php create mode 100644 admin/tool/lp/classes/event/competency_updated.php diff --git a/admin/tool/lp/classes/api.php b/admin/tool/lp/classes/api.php index a50227ad768..8c189013def 100644 --- a/admin/tool/lp/classes/api.php +++ b/admin/tool/lp/classes/api.php @@ -118,6 +118,8 @@ class api { $competency->set_sortorder(null); $competency->create(); + \tool_lp\event\competency_created::create_from_competency($competency)->trigger(); + // Reset the rule of the parent. $parent = $competency->get_parent(); if ($parent) { @@ -143,7 +145,9 @@ class api { // First we do a permissions check. require_capability('tool/lp:competencymanage', $competency->get_context()); + $events = array(); $competencyids = array(intval($competency->get_id())); + $contextid = $competency->get_context()->id; $competencyids = array_merge(competency::get_descendants_ids($competency), $competencyids); if (!competency::can_all_be_deleted($competencyids)) { return false; @@ -171,12 +175,20 @@ class api { // Delete competency evidences. user_evidence_competency::delete_by_competencyids($competencyids); - $transaction->allow_commit(); - return true; + // Register the competencies deleted events. + $events = \tool_lp\event\competency_deleted::create_multiple_from_competencyids($competencyids, $contextid); } catch (\Exception $e) { $transaction->rollback($e); } + + $transaction->allow_commit(); + // Trigger events. + foreach ($events as $event) { + $event->trigger(); + } + + return true; } /** @@ -216,8 +228,9 @@ class api { } // OK - all set. - $current->update(); + $result = $current->update(); + return $result; } /** @@ -252,7 +265,9 @@ class api { } // OK - all set. - return $current->update(); + $result = $current->update(); + + return $result; } /** @@ -315,12 +330,12 @@ class api { // Do the actual move. $current->set_parentid($newparentid); - $current->update(); + $result = $current->update(); // All right, let's commit this. $transaction->allow_commit(); - return true; + return $result; } /** @@ -348,7 +363,12 @@ class api { require_capability('tool/lp:competencymanage', $competency->get_context()); // OK - all set. - return $competency->update(); + $result = $competency->update(); + + // Trigger the update event. + \tool_lp\event\competency_updated::create_from_competency($competency)->trigger(); + + return $result; } /** @@ -563,7 +583,9 @@ class api { $framework = new competency_framework($id); require_capability('tool/lp:competencymanage', $framework->get_context()); + $events = array(); $competenciesid = competency::get_ids_by_frameworkid($id); + $contextid = $framework->get_contextid(); if (!competency::can_all_be_deleted($competenciesid)) { return false; } @@ -584,6 +606,9 @@ class api { $event = \tool_lp\event\competency_framework_deleted::create_from_framework($framework); $result = $framework->delete(); + // Register the deleted events competencies. + $events = \tool_lp\event\competency_deleted::create_multiple_from_competencyids($competenciesid, $contextid); + } catch (\Exception $e) { $transaction->rollback($e); } @@ -594,6 +619,11 @@ class api { // If all operations are successfull then trigger the delete event. $event->trigger(); + // Trigger deleted event competencies. + foreach ($events as $event) { + $event->trigger(); + } + return $result; } @@ -3489,6 +3519,9 @@ class api { $competency->reset_rule(); $competency->create(); + // Trigger the created event competency. + \tool_lp\event\competency_created::create_from_competency($competency)->trigger(); + // Match the old id with the new one. $matchids[$parentid] = $competency; diff --git a/admin/tool/lp/classes/event/competency_created.php b/admin/tool/lp/classes/event/competency_created.php new file mode 100644 index 00000000000..4e7072d8324 --- /dev/null +++ b/admin/tool/lp/classes/event/competency_created.php @@ -0,0 +1,110 @@ +. + +/** + * Competency created event. + * + * @package tool_lp + * @copyright 2016 Issam Taboubi + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_lp\event; + +use core\event\base; +use tool_lp\competency; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Competency created event class. + * + * + * @package tool_lp + * @since Moodle 3.1 + * @copyright 2016 Issam Taboubi + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class competency_created extends base { + + /** + * Convenience method to instantiate the event. + * + * @param competency $competency The competency. + * @return self + */ + public static function create_from_competency(competency $competency) { + if (!$competency->get_id()) { + throw new \coding_exception('The competency ID must be set.'); + } + $event = static::create(array( + 'contextid' => $competency->get_context()->id, + 'objectid' => $competency->get_id() + )); + return $event; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' created the competency with id '$this->objectid'"; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcompetencycreated', 'tool_lp'); + } + + /** + * Get URL related to the action + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/admin/tool/lp/editcompetency.php', array( + 'id' => $this->objectid, + 'pagecontextid' => $this->contextid + )); + } + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'c'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->data['objecttable'] = competency::TABLE; + } + + /** + * Get_objectid_mapping method. + * + * @return string the name of the restore mapping the objectid links to + */ + public static function get_objectid_mapping() { + return base::NOT_MAPPED; + } + +} diff --git a/admin/tool/lp/classes/event/competency_deleted.php b/admin/tool/lp/classes/event/competency_deleted.php new file mode 100644 index 00000000000..f916b89fc5b --- /dev/null +++ b/admin/tool/lp/classes/event/competency_deleted.php @@ -0,0 +1,115 @@ +. + +/** + * Competency deleted event. + * + * @package tool_lp + * @copyright 2016 Issam Taboubi + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_lp\event; + +use core\event\base; +use tool_lp\competency; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Competency deleted event class. + * + * @package tool_lp + * @since Moodle 3.1 + * @copyright 2016 Issam Taboubi + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class competency_deleted extends base { + + /** + * Convenience method to instantiate the event. + * + * @param competency $competency The competency. + * @return self + */ + public static function create_from_competency(competency $competency) { + if (!$competency->get_id()) { + throw new \coding_exception('The competency ID must be set.'); + } + $event = static::create(array( + 'contextid' => $competency->get_context()->id, + 'objectid' => $competency->get_id() + )); + return $event; + } + + /** + * Instantiate events from competency ids. + * + * @param array $competencyids Array of competency ids. + * @param int $contextid The context id. + * @return self[] Array of events. + */ + public static function create_multiple_from_competencyids($competencyids, $contextid) { + $events = array(); + foreach ($competencyids as $id) { + $events[$id] = static::create(array( + 'contextid' => $contextid, + 'objectid' => $id + )); + } + return $events; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' deleted the competency with id '$this->objectid'"; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcompetencydeleted', 'tool_lp'); + } + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'd'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->data['objecttable'] = competency::TABLE; + } + + /** + * Get_objectid_mapping method. + * + * @return string the name of the restore mapping the objectid links to + */ + public static function get_objectid_mapping() { + return base::NOT_MAPPED; + } + +} diff --git a/admin/tool/lp/classes/event/competency_updated.php b/admin/tool/lp/classes/event/competency_updated.php new file mode 100644 index 00000000000..f993fe80a4f --- /dev/null +++ b/admin/tool/lp/classes/event/competency_updated.php @@ -0,0 +1,110 @@ +. + +/** + * Competency updated event. + * + * @package tool_lp + * @copyright 2016 Issam Taboubi + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_lp\event; + +use core\event\base; +use tool_lp\competency; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Competency updated event class. + * + * + * @package tool_lp + * @since Moodle 3.1 + * @copyright 2016 Issam Taboubi + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class competency_updated extends base { + + /** + * Convenience method to instantiate the event. + * + * @param competency $competency The competency. + * @return self + */ + public static function create_from_competency(competency $competency) { + if (!$competency->get_id()) { + throw new \coding_exception('The competency ID must be set.'); + } + $event = static::create(array( + 'contextid' => $competency->get_context()->id, + 'objectid' => $competency->get_id() + )); + return $event; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' updated the competency with id '$this->objectid'"; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcompetencyupdated', 'tool_lp'); + } + + /** + * Get URL related to the action + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/admin/tool/lp/editcompetency.php', array( + 'id' => $this->objectid, + 'pagecontextid' => $this->contextid + )); + } + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->data['objecttable'] = competency::TABLE; + } + + /** + * Get_objectid_mapping method. + * + * @return string the name of the restore mapping the objectid links to + */ + public static function get_objectid_mapping() { + return base::NOT_MAPPED; + } + +} diff --git a/admin/tool/lp/classes/event/competency_viewed.php b/admin/tool/lp/classes/event/competency_viewed.php index e010c52a8b4..032cca7f9f8 100644 --- a/admin/tool/lp/classes/event/competency_viewed.php +++ b/admin/tool/lp/classes/event/competency_viewed.php @@ -32,11 +32,6 @@ defined('MOODLE_INTERNAL') || die(); /** * Competency viewed event class. * - * @property-read array $other { - * Extra information about event. - * - * - int competencyframeworkid: the competency framework ID. - * } * * @package tool_lp * @since Moodle 3.1 @@ -57,10 +52,7 @@ class competency_viewed extends base { } $event = static::create(array( 'contextid' => $competency->get_context()->id, - 'objectid' => $competency->get_id(), - 'other' => array( - 'competencyframeworkid' => $competency->get_competencyframeworkid(), - ) + 'objectid' => $competency->get_id() )); return $event; } @@ -91,8 +83,7 @@ class competency_viewed extends base { public function get_url() { return new \moodle_url('/admin/tool/lp/editcompetency.php', array( 'id' => $this->objectid, - 'pagecontextid' => $this->contextid, - 'competencyframeworkid' => $this->other['competencyframeworkid'] + 'pagecontextid' => $this->contextid )); } diff --git a/admin/tool/lp/editcompetency.php b/admin/tool/lp/editcompetency.php index 07398ae1de6..e3f2f1550cb 100644 --- a/admin/tool/lp/editcompetency.php +++ b/admin/tool/lp/editcompetency.php @@ -27,24 +27,24 @@ require_once($CFG->libdir.'/adminlib.php'); $title = get_string('competencies', 'tool_lp'); $id = optional_param('id', 0, PARAM_INT); -$competencyframeworkid = required_param('competencyframeworkid', PARAM_INT); +$competencyframeworkid = optional_param('competencyframeworkid', 0, PARAM_INT); $pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to the context we came from. $parentid = optional_param('parentid', 0, PARAM_INT); require_login(); $pagecontext = context::instance_by_id($pagecontextid); -// Set up the page. -$url = new moodle_url("/admin/tool/lp/editcompetency.php", array('id' => $id, 'competencyframeworkid' => $competencyframeworkid, - 'parentid' => $parentid, 'pagecontextid' => $pagecontextid)); -$frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $pagecontextid)); -$frameworkurl = new moodle_url('/admin/tool/lp/competencies.php', array('competencyframeworkid' => $competencyframeworkid, - 'pagecontextid' => $pagecontextid)); +if (empty($competencyframeworkid) && empty($id)) { + throw new coding_exception('Competencyframeworkid param is required'); +} +if (!empty($competencyframeworkid)) { + $competencyframework = \tool_lp\api::read_framework($competencyframeworkid); +} $competency = null; -$competencyframework = \tool_lp\api::read_framework($competencyframeworkid); if (!empty($id)) { $competency = \tool_lp\api::read_competency($id); + $competencyframework = $competency->get_framework(); } $parent = null; @@ -61,6 +61,16 @@ if (empty($id)) { $pagetitle = get_string('taxonomy_edit_' . $competencyframework->get_taxonomy($competency->get_level()), 'tool_lp'); } +// Set up the page. +$url = new moodle_url("/admin/tool/lp/editcompetency.php", array( + 'id' => $id, + 'competencyframeworkid' => $competencyframework->get_id(), + 'parentid' => $parentid, 'pagecontextid' => $pagecontextid) +); +$frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $pagecontextid)); +$frameworkurl = new moodle_url('/admin/tool/lp/competencies.php', array('competencyframeworkid' => $competencyframework->get_id(), + 'pagecontextid' => $pagecontextid)); + $PAGE->navigation->override_active_url($frameworksurl); $PAGE->set_context($pagecontext); $PAGE->set_pagelayout('admin'); diff --git a/admin/tool/lp/lang/en/tool_lp.php b/admin/tool/lp/lang/en/tool_lp.php index 21e1a729d43..a861fe9051c 100644 --- a/admin/tool/lp/lang/en/tool_lp.php +++ b/admin/tool/lp/lang/en/tool_lp.php @@ -104,6 +104,9 @@ $string['eventcompetencyframeworkcreated'] = 'Competency framework created.'; $string['eventcompetencyframeworkdeleted'] = 'Competency framework deleted.'; $string['eventcompetencyframeworkupdated'] = 'Competency framework updated.'; $string['eventcompetencyframeworkviewed'] = 'Competency framework viewed.'; +$string['eventcompetencycreated'] = 'Competency created.'; +$string['eventcompetencydeleted'] = 'Competency deleted.'; +$string['eventcompetencyupdated'] = 'Competency updated.'; $string['eventcompetencyviewed'] = 'Competency viewed.'; $string['eventtemplatecreated'] = 'Template created.'; $string['eventtemplatedeleted'] = 'Template deleted.'; diff --git a/admin/tool/lp/tests/event_test.php b/admin/tool/lp/tests/event_test.php index f7ac469a660..96b0f20c4ff 100644 --- a/admin/tool/lp/tests/event_test.php +++ b/admin/tool/lp/tests/event_test.php @@ -281,4 +281,170 @@ class tool_lp_event_testcase extends advanced_testcase { $this->assertDebuggingNotCalled(); } + /** + * Test the competency updated event. + * + */ + public function test_competency_updated() { + $this->resetAfterTest(true); + $this->setAdminUser(); + $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); + + $f1 = $lpg->create_framework(); + $competency = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id())); + $c13 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id())); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $competency->set_shortname('Shortname modified'); + api::update_competency($competency->to_record()); + + // Get our event event. + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\tool_lp\event\competency_updated', $event); + $this->assertEquals($competency->get_id(), $event->objectid); + $this->assertEquals($competency->get_context()->id, $event->contextid); + $this->assertEventContextNotUsed($event); + $this->assertDebuggingNotCalled(); + } + + /** + * Test the competency created event. + * + */ + public function test_competency_created() { + $this->resetAfterTest(true); + $this->setAdminUser(); + $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); + + $f1 = $lpg->create_framework(); + $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $record = $c1->to_record(); + $record->id = 0; + $record->idnumber = 'comp idnumber'; + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + // Create competency should trigger a created event. + $competency = api::create_competency($record); + + // Get our event event. + $events = $sink->get_events(); + $event = reset($events); + + $this->assertInstanceOf('\tool_lp\event\competency_created', $event); + $this->assertEquals($competency->get_id(), $event->objectid); + $this->assertEquals($competency->get_context()->id, $event->contextid); + $this->assertEventContextNotUsed($event); + $this->assertDebuggingNotCalled(); + } + + /** + * Test the competency created event by duplicate framework. + * + */ + public function test_competency_created_by_duplicateframework() { + $this->resetAfterTest(true); + $this->setAdminUser(); + $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); + + $f1 = $lpg->create_framework(); + $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id())); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + // Create framework should trigger a created event for competencies. + api::duplicate_framework($f1->get_id()); + + // Get our event event. + $events = $sink->get_events(); + $this->assertEquals(4, count($events)); + + $event = array_shift($events); + $this->assertInstanceOf('\tool_lp\event\competency_created', $event); + + $event = array_shift($events); + $this->assertInstanceOf('\tool_lp\event\competency_created', $event); + + $event = array_shift($events); + $this->assertInstanceOf('\tool_lp\event\competency_created', $event); + + $event = array_shift($events); + $this->assertInstanceOf('\tool_lp\event\competency_framework_created', $event); + } + + /** + * Test the competency deleted event. + * + */ + public function test_competency_deleted() { + $this->resetAfterTest(true); + $this->setAdminUser(); + $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); + + $f1 = $lpg->create_framework(); + $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $c1id = $c1->get_id(); + $contextid = $c1->get_context()->id; + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + // Delete competency should trigger a deleted event. + api::delete_competency($c1id); + + // Get our event event. + $events = $sink->get_events(); + $event = reset($events); + + $this->assertInstanceOf('\tool_lp\event\competency_deleted', $event); + $this->assertEquals($c1id, $event->objectid); + $this->assertEquals($contextid, $event->contextid); + $this->assertEventContextNotUsed($event); + $this->assertDebuggingNotCalled(); + } + + /** + * Test the competency deleted event by delete framework. + * + */ + public function test_competency_deleted_by_deleteframework() { + $this->resetAfterTest(true); + $this->setAdminUser(); + $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); + + $f1 = $lpg->create_framework(); + $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); + $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id())); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + // Delete framework should trigger a deleted event for competencies. + api::delete_framework($f1->get_id()); + + // Get our event event. + $events = $sink->get_events(); + $this->assertEquals(4, count($events)); + + $event = array_shift($events); + $this->assertInstanceOf('\tool_lp\event\competency_framework_deleted', $event); + + $event = array_shift($events); + $this->assertInstanceOf('\tool_lp\event\competency_deleted', $event); + + $event = array_shift($events); + $this->assertInstanceOf('\tool_lp\event\competency_deleted', $event); + + $event = array_shift($events); + $this->assertInstanceOf('\tool_lp\event\competency_deleted', $event); + } + } diff --git a/admin/tool/lp/version.php b/admin/tool/lp/version.php index d77581126fd..766ff84c07e 100644 --- a/admin/tool/lp/version.php +++ b/admin/tool/lp/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016020901; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2016020902; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2014110400; // Requires this Moodle version. $plugin->component = 'tool_lp'; // Full name of the plugin (used for diagnostics).