MDL-52959 tool_lp: Create C(R)UD events for competencies

This commit is contained in:
Issam Taboubi
2016-04-18 10:58:54 +08:00
committed by Frederic Massart
parent 7deba3c0a1
commit d32d64bcbc
9 changed files with 565 additions and 27 deletions
+40 -7
View File
@@ -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;
@@ -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/>.
/**
* Competency created event.
*
* @package tool_lp
* @copyright 2016 Issam Taboubi <[email protected]>
* @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 <[email protected]>
* @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;
}
}
@@ -0,0 +1,115 @@
<?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/>.
/**
* Competency deleted event.
*
* @package tool_lp
* @copyright 2016 Issam Taboubi <[email protected]>
* @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 <[email protected]>
* @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;
}
}
@@ -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/>.
/**
* Competency updated event.
*
* @package tool_lp
* @copyright 2016 Issam Taboubi <[email protected]>
* @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 <[email protected]>
* @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;
}
}
@@ -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
));
}
+18 -8
View File
@@ -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');
+3
View File
@@ -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.';
+166
View File
@@ -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);
}
}
+1 -1
View File
@@ -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).