Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding standard logging #353

Open
wants to merge 8 commits into
base: MOODLE_311_DEV
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions filter/kaltura/classes/event/kaltura_media_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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/>.

/**
* Kaltura media viewed.
*
* @package filter_kaltura
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace filter_kaltura\event;

defined('MOODLE_INTERNAL') || die();

/**
* View kaltura media in filter
*
* @property-read array $other {
* Extra information about event.
* - string sourceurl: (required) source media url.
* }
*
* @package filter_kaltura
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class kaltura_media_viewed extends \core\event\base {

/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('kalturamediaviewed', 'filter_kaltura');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return 'The user with id ' . $this->userid . ' viewed video with source url' . $this->other['sourceurl']
. 'embed in context ' . $this->contextid;
}

protected function validate_data() {
if (empty($this->other['sourceurl'])) {
throw new \coding_exception('Missing source url');
}
}

/**
* Gets data for use when logging to old log table.
*
* @return array Data to be passed to the legacy add_to_log function
*/
public function get_legacy_logdata() {
$context = $this->get_context();

$info = $this->userid . ':' . $this->other['sourceurl'] . ':' .
$this->context->id;
if (\core_text::strlen($info) > 255) {
$info = \core_text::substr($info, 0, 255);
}
return [$context->id, 'filter_kaltura', 'kalturamediaviewed',
$this->other['sourceurl'], $info, $context->instanceid];
}
}
33 changes: 33 additions & 0 deletions filter/kaltura/db/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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/>.

/**
* @package filter_kaltura
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$capabilities = [
'filter/kaltura:view' => [
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => [
'student' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
]
]
];
28 changes: 3 additions & 25 deletions filter/kaltura/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,6 @@ public function filter($text, array $options = array()) {
$kafuri = str_replace(array('http://', 'https://', '.', '/'), array('https?://', 'https?://', '\.', '\/'), $kafuri);

$search = $search = '/<a\s[^>]*href="(((https?:\/\/'.KALTURA_URI_TOKEN.')|('.$kafuri.')))\/browseandembed\/index\/media\/entryid\/([\d]+_[a-z0-9]+)(\/([a-zA-Z0-9]+\/[a-zA-Z0-9]+\/)*)"[^>]*>([^>]*)<\/a>/is';

if (!empty($CFG->filter_kaltura_uris)) {
$altkafuriconfig = $CFG->filter_kaltura_uris;
$altkafuris = explode(PHP_EOL, $altkafuriconfig);

$search = $search = '/<a\s[^>]*href="(((https?:\/\/'.KALTURA_URI_TOKEN.')|('.$kafuri.')';

foreach ($altkafuris as $altkafuri) {
$altkafuri = rtrim($altkafuri);
if ($altkafuri != '') {
// If a https url is needed for kaf_uri it should be entered into the kaf_uri setting as https://.
if (!preg_match('#^https?://#', $altkafuri)) {
$altkafuri = 'http://' . $altkafuri;
}

$altkafuri = str_replace(array('http://', 'https://', '.', '/'), array('https?://', 'https?://', '\.', '\/'), $altkafuri);
$search .= '|('.$altkafuri.')';
}
}

$search .= '))\/browseandembed\/index\/media\/entryid\/([\d]+_[a-z0-9]+)(\/([a-zA-Z0-9]+\/[a-zA-Z0-9]+\/)*)"[^>]*>([^>]*)<\/a>/is';
}

$newtext = preg_replace_callback($search, 'filter_kaltura_callback', $newtext);

if (empty($newtext) || $newtext === $text) {
Expand All @@ -169,6 +146,7 @@ public function filter($text, array $options = array()) {
* @return string Kaltura embed video markup.
*/
function filter_kaltura_callback($link) {
global $PAGE;
$width = filter_kaltura::$defaultwidth;
$height = filter_kaltura::$defaultheight;
$source = '';
Expand Down Expand Up @@ -200,8 +178,8 @@ function filter_kaltura_callback($link) {
'height' => $height,
'width' => $width,
'withblocks' => 0,
'source' => $source

'source' => $source,
'embedcontextid' => $PAGE->context->id
);

$url = new moodle_url('/filter/kaltura/lti_launch.php', $params);
Expand Down
6 changes: 3 additions & 3 deletions filter/kaltura/filtersettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configcheckbox('filter_kaltura_enable', get_string('enable', 'filter_kaltura'), get_string('enable_help', 'filter_kaltura'), 1));

$settings->add(new admin_setting_configtextarea('filter_kaltura_uris',
get_string('uris', 'filter_kaltura'),
get_string('uris_help', 'filter_kaltura'), ''));
$adminsetting = new admin_setting_configcheckbox('enable_moodle_logging', get_string('moodle_trace_log', 'filter_kaltura'),
get_string('moodle_trace_log_desc', 'filter_kaltura'), 0);
$settings->add($adminsetting);
}
5 changes: 5 additions & 0 deletions filter/kaltura/lang/en/filter_kaltura.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
*/

$string['filtername'] = 'Kaltura Media';
$string['pluginname'] = 'Kaltura Media Filter';
$string['enable'] = 'Embed Kaltura Video Links';
$string['enable_help'] = 'Convert Kaltura video links to embed code';
$string['moodle_trace_log'] = 'Enable moodle trace logging';
$string['moodle_trace_log_desc'] = 'If enabled, all requests and responses to and from Kaltura are logged into Moodle logging system.';
$string['kalturamediaviewed'] = 'Kaltura media viewed';
$string['uris'] = 'Alternate KAF URIs';
$string['uris_help'] = 'Enter alternate KAF URIs to filter, one per line';
$string['unable'] = 'Unable to convert video at this time';
$string['privacy:metadata'] = 'The Kaltura Media filter does not store any personal data.';
$string['kaltura:view'] = 'Permission for user can filter Embed Kaltura Video Links';
13 changes: 10 additions & 3 deletions filter/kaltura/lti_launch.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
$width = required_param('width', PARAM_INT);
$withblocks = optional_param('withblocks', 0, PARAM_INT);
$source = optional_param('source', '', PARAM_URL);
$embedcontextid = optional_param('embedcontextid', 0, PARAM_INT);

// If a course id of zero is passed, then we must be in the system context.
if (0 != $courseid) {
Expand All @@ -47,8 +48,8 @@
if ($context instanceof context_course) {
$course = get_course($courseid);

// Check if the user has the capability to view comments in Moodle.
if (!has_capability('moodle/comment:view', $context)) {
// Check if the user has the capability to view kaltura media.
if (!has_capability('filter/kaltura:view', $context)) {
echo get_string('nocapabilitytousethisservice', 'error');
die();
}
Expand Down Expand Up @@ -76,6 +77,12 @@
if (local_kaltura_validate_browseembed_required_params($launch)) {
$content = local_kaltura_request_lti_launch($launch, $withblocks);
echo $content;
// Check if Moodle logging is enabled.
$enablemoodlelogging = get_config(KALTURA_PLUGIN_NAME, 'enable_moodle_logging');
if (!empty($enablemoodlelogging) && $embedcontextid) {
$event = filter_kaltura\event\kaltura_media_viewed::create(['sourceurl' => $source, 'contextid' => $embedcontextid]);
$event->trigger();
}
} else {
echo get_string('invalid_launch_parameters', 'mod_kalvidres');
}
}
67 changes: 67 additions & 0 deletions filter/kaltura/tests/events_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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/>.

/**
* Contains the event tests for the plugin.
*
* @package filter_kaltura
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

global $CFG;

/**
* Contains the event tests for the plugin.
*
* @package filter_kaltura
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter_kaltura_events_testcase extends advanced_testcase {

/**
* Tests (media_gallery_viewed)
*/
public function test_media_gallery_viewed() {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$this->setUser($user);

$context = context_course::instance($course->id);

// Create a basic event.
$event = \local_kalturamediagallery\event\media_gallery_viewed::create([
'context' => $context,
'other' => [
'coursename' => $course->fullname,
'courseid' => $course->id,
]]);

// Check name (generic).
$this->assertEquals(get_string('eventviewmediagallry', 'local_kalturamediagallery'), $event->get_name());
$this->assertEquals('The user with id ' . $user->id . ' viewed ' . $course->fullname . ' media gallery in context ' . $context->id
. ' with courseid ' . $course->id, $event->get_description());
$info = $user->id . ':' . $course->fullname . ':' . $context->id . ':' . $course->id;
$this->assertEquals([$context->id, 'local_kalturamediagallery', 'eventviewmediagallry',
'local/kalturamediagallery/index.php?courseid=' . $course->id, $info, $context->instanceid], $event->get_legacy_logdata());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
$string['embedbuttontext'] = 'Embed';
$string['browse_and_embed'] = 'Browse and Embed';
$string['privacy:metadata'] = 'The atto_kalturamedia plugin does not store any personal data.';
$string['kalturamedia:view'] = 'Permission to use Embed Kaltura Media atto plugin';
5 changes: 4 additions & 1 deletion lib/editor/atto/plugins/kalturamedia/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ function atto_kalturamedia_params_for_js($elementid, $options, $fpoptions) {
if (!$context) {
$context = context_system::instance();
}


$active = has_capability('atto/kalturamedia:view', $context);

return array(
'active' => $active,
'contextid' => $context->id,
'kafuri' => local_kaltura_get_config()->kaf_uri
);
Expand Down
2 changes: 1 addition & 1 deletion lib/editor/atto/plugins/kalturamedia/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2021051701; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2021051702; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 'Kaltura release 4.3.1';
$plugin->requires = 2018120300; // Requires this Moodle version.
$plugin->component = 'atto_kalturamedia'; // Full name of the plugin (used for diagnostics).
Expand Down
Loading