Skip to content

Commit

Permalink
MDL-52960 tool_lp: Created event for competency viewed
Browse files Browse the repository at this point in the history
  • Loading branch information
taboubi authored and Frederic Massart committed Apr 18, 2016
1 parent a800711 commit 3e6a8e1
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 3 deletions.
2 changes: 1 addition & 1 deletion admin/tool/lp/amd/build/competencyactions.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion admin/tool/lp/amd/build/competencydialogue.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions admin/tool/lp/amd/src/competencyactions.js
Expand Up @@ -55,6 +55,8 @@ define(['jquery',
var taxonomiesConstants;
/** @type {Array} The rules modules. Values are object containing type, namd and amd. */
var rulesModules;
/** @type {Number} the selected competency ID. */
var selectedCompetencyId = null;

/**
* Respond to choosing the "Add" menu item for the selected node in the tree.
Expand Down Expand Up @@ -609,6 +611,23 @@ define(['jquery',

};

/**
* Log the competency viewed event.
*
* @param {Object} The competency.
* @method triggerCompetencyViewedEvent
*/
var triggerCompetencyViewedEvent = function(competency) {
if (competency.id !== selectedCompetencyId) {
// Set the selected competency id.
selectedCompetencyId = competency.id;
ajax.call([{
methodname: 'tool_lp_competency_viewed',
args: { id: competency.id }
}]);
}
};

/**
* Return if the level has a sub level.
*
Expand Down Expand Up @@ -754,6 +773,8 @@ define(['jquery',
actionMenu.show();
$('[data-region="competencyactions"]').data('competency', competency);
renderCompetencySummary(competency);
// Log Competency viewed event.
triggerCompetencyViewedEvent(competency);
}

strSelectedTaxonomy(level).then(function(str) {
Expand Down
21 changes: 21 additions & 0 deletions admin/tool/lp/amd/src/competencydialogue.js
Expand Up @@ -43,6 +43,19 @@ define(['jquery',
$.extend(this.options, options);
};

/**
* Log the competency viewed event.
*
* @param {Number} The competency ID.
* @method triggerCompetencyViewedEvent
*/
Competencydialogue.prototype.triggerCompetencyViewedEvent = function(competencyId) {
ajax.call([{
methodname: 'tool_lp_competency_viewed',
args: { id: competencyId }
}]);
};

/**
* Callback on dialogue display, it apply enhance on competencies dialogue.
*
Expand Down Expand Up @@ -71,6 +84,10 @@ define(['jquery',
// Inner Html in the dialogue content.
templates.render('tool_lp/competency_summary', data)
.done(function(html) {
// Log competency viewed event.
localthis.triggerCompetencyViewedEvent(competencyid);

// Show the dialogue.
new Dialogue(
data.competency.shortname,
html,
Expand All @@ -92,6 +109,10 @@ define(['jquery',
// Inner Html in the dialogue content.
templates.render('tool_lp/competency_summary', dataSource)
.done(function(html) {
// Log competency viewed event.
localthis.triggerCompetencyViewedEvent(dataSource.id);

// Show the dialogue.
new Dialogue(
dataSource.shortname,
html,
Expand Down
20 changes: 20 additions & 0 deletions admin/tool/lp/classes/api.php
Expand Up @@ -626,6 +626,26 @@ public static function competency_framework_viewed($frameworkorid) {
return true;
}

/**
* Logg the competency viewed event.
*
* @param competency|int $competencyorid The competency object or competency id
* @return bool
*/
public static function competency_viewed($competencyorid) {
$competency = $competencyorid;
if (!is_object($competency)) {
$competency = new competency($competency);
}

if (!has_any_capability(array('tool/lp:competencyread', 'tool/lp:competencymanage'), $competency->get_context())) {
throw new required_capability_exception($competency->get_context(), 'tool/lp:competencyread', 'nopermissions', '');
}

\tool_lp\event\competency_viewed::create_from_competency($competency)->trigger();
return true;
}

/**
* Perform a search based on the provided filters and return a paginated list of records.
*
Expand Down
119 changes: 119 additions & 0 deletions admin/tool/lp/classes/event/competency_viewed.php
@@ -0,0 +1,119 @@
<?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 viewed event.
*
* @package tool_lp
* @copyright 2016 Issam Taboubi <issam.taboubi@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\competency;

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
* @copyright 2016 Issam Taboubi <issam.taboubi@umontreal.ca>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class competency_viewed 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(),
'other' => array(
'competencyframeworkid' => $competency->get_competencyframeworkid(),
)
));
return $event;
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' viewed the competency with id '$this->objectid'";
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventcompetencyviewed', '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,
'competencyframeworkid' => $this->other['competencyframeworkid']
));
}

/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$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;
}

}
40 changes: 40 additions & 0 deletions admin/tool/lp/classes/external.php
Expand Up @@ -264,6 +264,46 @@ public static function read_competency_framework_returns() {
return competency_framework_exporter::get_read_structure();
}

/**
* Returns description of competency_viewed() parameters.
*
* @return \external_function_parameters
*/
public static function competency_viewed_parameters() {
$id = new external_value(
PARAM_INT,
'The competency id',
VALUE_REQUIRED
);
$params = array(
'id' => $id
);
return new external_function_parameters($params);
}

/**
* Log event competency viewed.
*
* @param int $id The competency ID.
* @return boolean
*/
public static function competency_viewed($id) {
$params = self::validate_parameters(self::competency_viewed_parameters(),
array(
'id' => $id
));
return api::competency_viewed($params['id']);
}

/**
* Returns description of competency_viewed() result value.
*
* @return \external_description
*/
public static function competency_viewed_returns() {
return new external_value(PARAM_BOOL, 'True if the event competency viewed was logged');
}

/**
* Returns description of duplicate_competency_framework() parameters.
*
Expand Down
9 changes: 9 additions & 0 deletions admin/tool/lp/db/services.php
Expand Up @@ -129,6 +129,15 @@
'capabilities' => 'tool/lp:competencyview',
'ajax' => true,
),
'tool_lp_competency_viewed' => array(
'classname' => 'tool_lp\external',
'methodname' => 'competency_viewed',
'classpath' => '',
'description' => 'Log event competency viewed',
'type' => 'read',
'capabilities' => 'tool/lp:competencyread',
'ajax' => true,
),
'tool_lp_delete_competency' => array(
'classname' => 'tool_lp\external',
'methodname' => 'delete_competency',
Expand Down
1 change: 1 addition & 0 deletions admin/tool/lp/lang/en/tool_lp.php
Expand Up @@ -99,6 +99,7 @@
$string['eventcompetencyframeworkdeleted'] = 'Competency framework deleted.';
$string['eventcompetencyframeworkupdated'] = 'Competency framework updated.';
$string['eventcompetencyframeworkviewed'] = 'Competency framework viewed.';
$string['eventcompetencyviewed'] = 'Competency viewed.';
$string['eventtemplatecreated'] = 'Template created.';
$string['eventtemplatedeleted'] = 'Template deleted.';
$string['eventtemplateupdated'] = 'Template updated.';
Expand Down
25 changes: 25 additions & 0 deletions admin/tool/lp/tests/event_test.php
Expand Up @@ -149,6 +149,31 @@ public function test_competency_framework_viewed() {
$this->assertDebuggingNotCalled();
}

/**
* Test the competency viewed event.
*
*/
public function test_competency_viewed() {
$this->resetAfterTest(true);
$this->setAdminUser();
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$framework = $lpg->create_framework();
$competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));

// Trigger and capture the event.
$sink = $this->redirectEvents();
api::competency_viewed($competency);
// Get our event event.
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\tool_lp\event\competency_viewed', $event);
$this->assertEquals($competency->get_id(), $event->objectid);
$this->assertEquals($competency->get_context()->id, $event->contextid);
$this->assertEventContextNotUsed($event);
$this->assertDebuggingNotCalled();
}

/**
* Test the template viewed event.
*
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/lp/version.php
Expand Up @@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();


$plugin->version = 2015111049; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2015111050; // 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).

0 comments on commit 3e6a8e1

Please sign in to comment.