MDL-52712 tool_lp: Create event for template viewed

This commit is contained in:
Serge Gauthier
2016-04-18 10:58:51 +08:00
committed by Frederic Massart
parent b74bdcc33d
commit 305eaf3845
8 changed files with 216 additions and 1 deletions
+25
View File
@@ -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;
}
}
@@ -0,0 +1,108 @@
<?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/>.
/**
* Template viewed event.
*
* @package tool_lp
* @copyright 2016 Serge Gauthier <[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\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 <[email protected]>
* @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;
}
}
+46
View File
@@ -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');
}
}
+9
View File
@@ -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,
),
);
+1
View File
@@ -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.';
+3
View File
@@ -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));
+23
View File
@@ -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();
}
}
+1 -1
View File
@@ -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).