MDL-53160 tool_lp: Create C(R)UD events for evidence of prior learning

This commit is contained in:
Serge Gauthier 2016-02-23 14:11:10 -05:00 committed by Frederic Massart
parent 9db9cdadb2
commit 8e3eb9f865
7 changed files with 424 additions and 1 deletions

View File

@ -3397,6 +3397,9 @@ class api {
file_save_draft_area_files($draftitemid, $context->id, 'tool_lp', 'userevidence', $itemid, $fileareaoptions);
}
// Trigger an evidence of prior learning created event.
\tool_lp\event\user_evidence_created::create_from_user_evidence($userevidence)->trigger();
return $userevidence;
}
@ -3427,6 +3430,9 @@ class api {
file_save_draft_area_files($draftitemid, $context->id, 'tool_lp', 'userevidence', $itemid, $fileareaoptions);
}
// Trigger an evidence of prior learning updated event.
\tool_lp\event\user_evidence_updated::create_from_user_evidence($userevidence)->trigger();
return $userevidence;
}
@ -3457,6 +3463,10 @@ class api {
foreach ($competencies as $competency) {
static::delete_user_evidence_competency($userevidence, $competency->get_id());
}
// Trigger an evidence of prior learning deleted event.
\tool_lp\event\user_evidence_deleted::create_from_user_evidence($userevidence)->trigger();
$userevidence->set_id(0); // Restore the object.
return true;

View File

@ -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/>.
/**
* Evidence of prior learning created event.
*
* @package tool_lp
* @copyright 2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\event;
use core\event\base;
use tool_lp\user_evidence;
defined('MOODLE_INTERNAL') || die();
/**
* Evidence of prior learning created event class.
*
* @property-read array $other {
* Extra information about event.
* }
*
* @package tool_lp
* @since Moodle 3.1
* @copyright 2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_evidence_created extends base {
/**
* Convenience method to instantiate the event.
*
* @param user_evidence $userevidence The evidence of prior learning.
* @return self
*/
public static final function create_from_user_evidence(user_evidence $userevidence) {
if (!$userevidence->get_id()) {
throw new \coding_exception('The evidence of prior learning ID must be set.');
}
$event = static::create(array(
'contextid' => $userevidence->get_context()->id,
'objectid' => $userevidence->get_id(),
'relateduserid' => $userevidence->get_userid()
));
$event->add_record_snapshot(user_evidence::TABLE, $userevidence->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventuserevidencecreated', 'tool_lp');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' created the evidence of prior learning with id '$this->objectid'.";
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/admin/tool/lp/user_evidence.php',
array('id' => $this->objectid));
}
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = user_evidence::TABLE;
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* 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;
}
}

View File

@ -0,0 +1,100 @@
<?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/>.
/**
* Evidence of prior learning deleted event.
*
* @package tool_lp
* @copyright 2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\event;
use core\event\base;
use tool_lp\user_evidence;
defined('MOODLE_INTERNAL') || die();
/**
* Evidence of prior learning deleted event class.
*
* @property-read array $other {
* Extra information about event.
* }
*
* @package tool_lp
* @since Moodle 3.1
* @copyright 2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_evidence_deleted extends base {
/**
* Convenience method to instantiate the event.
*
* @param user_evidence $userevidence The evidence of prior learning.
* @return self
*/
public static final function create_from_user_evidence(user_evidence $userevidence) {
if (!$userevidence->get_id()) {
throw new \coding_exception('The evidence of prior learning ID must be set.');
}
$event = static::create(array(
'contextid' => $userevidence->get_context()->id,
'objectid' => $userevidence->get_id(),
'relateduserid' => $userevidence->get_userid()
));
$event->add_record_snapshot(user_evidence::TABLE, $userevidence->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventuserevidencedeleted', 'tool_lp');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' deleted the evidence of prior learning with id '$this->objectid'.";
}
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = user_evidence::TABLE;
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* 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;
}
}

View File

@ -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/>.
/**
* Evidence of prior learning updated event.
*
* @package tool_lp
* @copyright 2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\event;
use core\event\base;
use tool_lp\user_evidence;
defined('MOODLE_INTERNAL') || die();
/**
* Evidence of prior learning updated event class.
*
* @property-read array $other {
* Extra information about event.
* }
*
* @package tool_lp
* @since Moodle 3.1
* @copyright 2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_evidence_updated extends base {
/**
* Convenience method to instantiate the event.
*
* @param user_evidence $userevidence The evidence of prior learning.
* @return self
*/
public static final function create_from_user_evidence(user_evidence $userevidence) {
if (!$userevidence->get_id()) {
throw new \coding_exception('The evidence of prior learning ID must be set.');
}
$event = static::create(array(
'contextid' => $userevidence->get_context()->id,
'objectid' => $userevidence->get_id(),
'relateduserid' => $userevidence->get_userid()
));
$event->add_record_snapshot(user_evidence::TABLE, $userevidence->to_record());
return $event;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventuserevidenceupdated', 'tool_lp');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' updated the evidence of prior learning with id '$this->objectid'.";
}
/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/admin/tool/lp/user_evidence.php',
array('id' => $this->objectid));
}
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = user_evidence::TABLE;
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* 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;
}
}

View File

@ -116,6 +116,9 @@ $string['eventtemplatecreated'] = 'Template created.';
$string['eventtemplatedeleted'] = 'Template deleted.';
$string['eventtemplateupdated'] = 'Template updated.';
$string['eventtemplateviewed'] = 'Template viewed.';
$string['eventuserevidencecreated'] = 'Evidence of prior learning created.';
$string['eventuserevidencedeleted'] = 'Evidence of prior learning deleted.';
$string['eventuserevidenceupdated'] = 'Evidence of prior learning updated.';
$string['evidence'] = 'Evidence';
$string['evidence_competencyrule'] = 'The rule of the competency was met.';
$string['evidence_coursecompleted'] = 'The course \'{$a}\' was completed.';

View File

@ -670,4 +670,94 @@ class tool_lp_event_testcase extends advanced_testcase {
$this->assertEventContextNotUsed($event);
$this->assertDebuggingNotCalled();
}
/**
* Test the evidence of prior learning created event.
*
*/
public function test_user_evidence_created() {
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$user = $dg->create_user();
// Use DataGenerator to have a user_evidence record with the right format.
$record = $userevidence = $lpg->create_user_evidence(array('userid' => $user->id))->to_record();
$record->id = 0;
$record->name = "New name";
// Trigger and capture the event.
$sink = $this->redirectEvents();
$userevidence = api::create_user_evidence((object) $record);
// Get our event event.
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\tool_lp\event\user_evidence_created', $event);
$this->assertEquals($userevidence->get_id(), $event->objectid);
$this->assertEquals($userevidence->get_context()->id, $event->contextid);
$this->assertEventContextNotUsed($event);
$this->assertDebuggingNotCalled();
}
/**
* Test the evidence of prior learning deleted event.
*
*/
public function test_user_evidence_deleted() {
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$user = $dg->create_user();
$userevidence = $lpg->create_user_evidence(array('userid' => $user->id));
// Trigger and capture the event.
$sink = $this->redirectEvents();
api::delete_user_evidence($userevidence->get_id());
// Get our event event.
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\tool_lp\event\user_evidence_deleted', $event);
$this->assertEquals($userevidence->get_id(), $event->objectid);
$this->assertEquals($userevidence->get_context()->id, $event->contextid);
$this->assertEventContextNotUsed($event);
$this->assertDebuggingNotCalled();
}
/**
* Test the evidence of prior learning updated event.
*
*/
public function test_user_evidence_updated() {
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$user = $dg->create_user();
$userevidence = $lpg->create_user_evidence(array('userid' => $user->id));
// Trigger and capture the event.
$sink = $this->redirectEvents();
$userevidence->set_name('Name modified');
api::update_user_evidence($userevidence->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\user_evidence_updated', $event);
$this->assertEquals($userevidence->get_id(), $event->objectid);
$this->assertEquals($userevidence->get_context()->id, $event->contextid);
$this->assertEventContextNotUsed($event);
$this->assertDebuggingNotCalled();
}
}

View File

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016020903; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2016020904; // 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).