diff --git a/admin/tool/lp/classes/api.php b/admin/tool/lp/classes/api.php index 3edbf0d365d..cfc560ca1a7 100644 --- a/admin/tool/lp/classes/api.php +++ b/admin/tool/lp/classes/api.php @@ -3496,4 +3496,29 @@ class api { $grade, $USER->id); } + + /** + * Template event viewed. + * + * Requires tool/lp:templateread capability at the system context. + * + * @param mixed $templateorid The id or the template. + * @return boolean + */ + public static function template_viewed($templateorid) { + $template = $templateorid; + if (!is_object($template)) { + $template = new template($template); + } + + // First we do a permissions check. + if (!$template->can_read()) { + throw new required_capability_exception($template->get_context(), 'tool/lp:templateread', 'nopermissions', ''); + } + + // Trigger a template viewed event. + \tool_lp\event\template_viewed::create_from_template($template)->trigger(); + + return true; + } } diff --git a/admin/tool/lp/classes/event/template_viewed.php b/admin/tool/lp/classes/event/template_viewed.php new file mode 100644 index 00000000000..56d8c0d1743 --- /dev/null +++ b/admin/tool/lp/classes/event/template_viewed.php @@ -0,0 +1,108 @@ +. + +/** + * Template viewed event. + * + * @package tool_lp + * @copyright 2016 Serge Gauthier + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_lp\event; + +use core\event\base; +use tool_lp\template; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Template viewed event class. + * + * @property-read array $other { + * Extra information about event. + * } + * + * @package tool_lp + * @since Moodle 3.1 + * @copyright 2016 Serge Gauthier + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class template_viewed extends base { + + /** + * Convenience method to instantiate the event. + * + * @param template $template The template. + * @return self + */ + public static function create_from_template(template $template) { + if (!$template->get_id()) { + throw new \coding_exception('The template ID must be set.'); + } + $event = static::create(array( + 'contextid' => $template->get_contextid(), + 'objectid' => $template->get_id() + )); + return $event; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventtemplateviewed', 'tool_lp'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' viewed the template with id '$this->objectid'."; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/admin/tool/lp/templatecompetencies.php', + array('templateid' => $this->objectid, 'pagecontextid' => $this->contextid)); + } + + /** + * Initialise the event data. + */ + protected function init() { + $this->data['objecttable'] = template::TABLE; + $this->data['crud'] = 'r'; + $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; + } + +} diff --git a/admin/tool/lp/classes/external.php b/admin/tool/lp/classes/external.php index 67be84dfe46..1123c4dd045 100644 --- a/admin/tool/lp/classes/external.php +++ b/admin/tool/lp/classes/external.php @@ -4946,4 +4946,50 @@ class external extends external_api { return new external_value(PARAM_BOOL, 'True if the unlink was successful'); } + /** + * Returns description of template_viewed() parameters. + * + * @return \external_function_parameters + */ + public static function template_viewed_parameters() { + $id = new external_value( + PARAM_INT, + 'Data base record id for the template', + VALUE_REQUIRED + ); + + $params = array( + 'id' => $id, + ); + return new external_function_parameters($params); + } + + /** + * Log the template viewed event. + * + * @param int $id the template id + * @return array of warnings and status result + * @throws moodle_exception + */ + public static function template_viewed($id) { + $params = self::validate_parameters(self::view_book_parameters(), + array( + 'id' => $id + )); + + $template = api::read_template($params['id']); + self::validate_context($template->get_context()); + + return api::template_viewed($params['id']); + } + + /** + * Returns description of template_viewed() result value. + * + * @return \external_value + */ + public static function template_viewed_returns() { + return new external_value(PARAM_BOOL, 'True if the log of the view was successful'); + } + } diff --git a/admin/tool/lp/db/services.php b/admin/tool/lp/db/services.php index 65e21551e11..d934650f194 100644 --- a/admin/tool/lp/db/services.php +++ b/admin/tool/lp/db/services.php @@ -818,5 +818,14 @@ $functions = array( 'capabilities' => 'tool/lp:planmanage', 'ajax' => true, ), + 'tool_lp_view_template' => array( + 'classname' => 'tool_lp\external', + 'methodname' => 'template_viewed', + 'classpath' => '', + 'description' => 'Log event template viewed', + 'type' => 'read', + 'capabilities' => 'tool/lp:templateread', + 'ajax' => true, + ), ); diff --git a/admin/tool/lp/lang/en/tool_lp.php b/admin/tool/lp/lang/en/tool_lp.php index 77f296cd187..e104f7a79c3 100644 --- a/admin/tool/lp/lang/en/tool_lp.php +++ b/admin/tool/lp/lang/en/tool_lp.php @@ -99,6 +99,7 @@ $string['eventcompetencyframeworkcreated'] = 'Competency framework created.'; $string['eventcompetencyframeworkdeleted'] = 'Competency framework deleted.'; $string['eventcompetencyframeworkupdated'] = 'Competency framework updated.'; $string['eventcompetencyframeworkviewed'] = 'Competency framework viewed.'; +$string['eventtemplateviewed'] = 'Template viewed.'; $string['evidence'] = 'Evidence'; $string['evidence_competencyrule'] = 'The rule of the competency was met.'; $string['evidence_coursecompleted'] = 'The course \'{$a}\' was completed.'; diff --git a/admin/tool/lp/templatecompetencies.php b/admin/tool/lp/templatecompetencies.php index 8fedf267479..6bcbff453ca 100644 --- a/admin/tool/lp/templatecompetencies.php +++ b/admin/tool/lp/templatecompetencies.php @@ -37,6 +37,9 @@ if (!$template->can_read()) { throw new required_capability_exception($context, 'tool/lp:templateread', 'nopermissions', ''); } +// Trigger a template viewed event. +\tool_lp\api::template_viewed($template); + // Set up the page. $url = new moodle_url('/admin/tool/lp/templatecompetencies.php', array('templateid' => $template->get_id(), 'pagecontextid' => $pagecontextid)); diff --git a/admin/tool/lp/tests/event_test.php b/admin/tool/lp/tests/event_test.php index cf7d33ff0e2..7c2612b9d9f 100644 --- a/admin/tool/lp/tests/event_test.php +++ b/admin/tool/lp/tests/event_test.php @@ -149,4 +149,27 @@ class tool_lp_event_testcase extends advanced_testcase { $this->assertDebuggingNotCalled(); } + /** + * Test the template viewed event. + * + */ + public function test_template_viewed() { + $this->resetAfterTest(true); + $this->setAdminUser(); + $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp'); + $template = $lpg->create_template(); + // Trigger and capture the event. + $sink = $this->redirectEvents(); + api::template_viewed($template); + // Get our event event. + $events = $sink->get_events(); + $event = reset($events); + // Check that the event data is valid. + $this->assertInstanceOf('\tool_lp\event\template_viewed', $event); + $this->assertEquals($template->get_id(), $event->objectid); + $this->assertEquals($template->get_contextid(), $event->contextid); + $this->assertEventContextNotUsed($event); + $this->assertDebuggingNotCalled(); + } + } \ No newline at end of file diff --git a/admin/tool/lp/version.php b/admin/tool/lp/version.php index 1ba9d07b227..3454b2329f5 100644 --- a/admin/tool/lp/version.php +++ b/admin/tool/lp/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015111047; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2015111048; // 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).