Merge branch 'master_MDL-78571' of https://github.com/mattporritt/moodle

This commit is contained in:
Ilya Tregubov 2023-08-02 12:11:44 +08:00
commit 70de576893
No known key found for this signature in database
GPG Key ID: 0F58186F748E55C1
8 changed files with 185 additions and 29 deletions

View File

@ -34,23 +34,34 @@ defined('MOODLE_INTERNAL') || die();
*/
class media_vimeo_plugin extends core_media_player_external {
protected function embed_external(moodle_url $url, $name, $width, $height, $options) {
global $OUTPUT;
$donottrack = get_config('media_vimeo', 'donottrack');
$videoid = $this->get_video_id();
$info = s($name);
$params = [];
// Note: resizing via url is not supported, user can click the fullscreen
// button instead. iframe embedding is not xhtml strict but it is the only
// option that seems to work on most devices.
self::pick_video_size($width, $height);
$output = <<<OET
<span class="mediaplugin mediaplugin_vimeo">
<iframe title="$info" src="https://player.vimeo.com/video/$videoid"
width="$width" height="$height" frameborder="0"
webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</span>
OET;
// Add do not track parameter.
if ($donottrack) {
$params['dnt'] = 1;
}
return $output;
$embedurl = new moodle_url("https://player.vimeo.com/video/$videoid", $params);
// Template context.
$context = [
'width' => $width,
'height' => $height,
'title' => $info,
'embedurl' => $embedurl->out(false),
];
// Return the rendered template.
return $OUTPUT->render_from_template('media_vimeo/embed', $context);
}
/**

View File

@ -25,3 +25,5 @@
$string['pluginname'] = 'Vimeo';
$string['pluginname_help'] = 'The video-sharing website vimeo.com.';
$string['privacy:metadata'] = 'The Vimeo media plugin does not store any personal data.';
$string['donottrack'] = 'Enable do not track Vimeo links';
$string['donottrack_desc'] = 'If enabled, the player will not track any session data, including all cookies and analytics.';

View File

@ -0,0 +1,32 @@
<?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/>.
/**
* Settings file for plugin 'media_vimeo'
*
* @package media_vimeo
* @copyright 2023 Matt Porritt <matt.porritt@moodle.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if ($ADMIN->fulltree) {
// Add a settings checkbox to enable or disable do not track vimeo links.
$settings->add(new admin_setting_configcheckbox('media_vimeo/donottrack',
new lang_string('donottrack', 'media_vimeo'),
new lang_string('donottrack_desc', 'media_vimeo'), 0));
}

View File

@ -0,0 +1,53 @@
{{!
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 media_vimeo/embed
This template will render the Vimeo embeded player
in a special format for the Moodle mobile app.
Variables required for this template:
* lang: The language of the user.
* width: The width of the video.
* height: The height of the video.
* style: The style info for iframe.
* embedurl: The URL to the video.
Example context (json):
{
"lang": "en",
"width": 640,
"height": 360,
"style": "position:absolute; top:0; left:0; width:100%; height:100%;",
"embedurl": "https://player.vimeo.com/video/226053498"
}
}}
<!DOCTYPE html>
<html lang="{{lang}}">
<head>
<title>{{#str}} pluginname, media_vimeo {{/str}}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body style="margin:0; padding:0">
<iframe {{#style}}style="{{style}}" {{/style}}
{{#width}}width="{{width}}" {{/width}}
{{#height}}height="{{height}}" {{/height}}
src="{{{embedurl}}}" allow="fullscreen" loading="lazy">
</iframe>
</body>
</html>

View File

@ -0,0 +1,41 @@
{{!
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 media_vimeo/embed
This template will render the Vimeo embeded player.
Variables required for this template:
* title: The title of the video.
* width: The width of the video.
* height: The height of the video.
* embedurl: The URL to the video.
Example context (json):
{
"title": "Vimeo video",
"width": 640,
"height": 360,
"embedurl": "https://player.vimeo.com/video/226053498"
}
}}
<span class="mediaplugin mediaplugin_vimeo">
<iframe title="{{title}}" width="{{width}}" height="{{height}}" style="border:0;"
src="{{{embedurl}}}" allow="fullscreen" loading="lazy"></iframe>
</span>

View File

@ -225,4 +225,21 @@ class player_test extends \advanced_testcase {
$this->assertDoesNotMatchRegularExpression('~mediaplugin_vimeo~', $content);
$this->assertDoesNotMatchRegularExpression('~</iframe>~', $content);
}
/**
* Test that Vimeo media plugin renders embed code correctly
* when the "do not track" config options is set to true.
*
* @covers \media_vimeo_plugin::embed_external
*/
public function test_vimeo_donottrack(): void {
// Turn on the do not track option.
set_config('donottrack', true, 'media_vimeo');
// Test that the embed code contains the do not track param in the url.
$url = new \moodle_url('https://vimeo.com/226053498');
$text = \html_writer::link($url, 'Watch this one');
$content = format_text($text, FORMAT_HTML);
$this->assertMatchesRegularExpression('~dnt=1~', $content);
}
}

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2023042400; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2023062500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2023041800; // Requires this Moodle version.
$plugin->component = 'media_vimeo'; // Full name of the plugin (used for diagnostics).

View File

@ -36,6 +36,8 @@ define('NO_MOODLE_COOKIES', true);
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot . '/webservice/lib.php');
global $OUTPUT;
$token = required_param('token', PARAM_ALPHANUM);
$video = required_param('video', PARAM_ALPHANUM); // Video ids are numeric, but it's more solid to expect things like 00001.
$width = optional_param('width', 0, PARAM_INT);
@ -46,28 +48,26 @@ $h = optional_param('h', '', PARAM_ALPHANUM); // Security hash for restricted vi
$webservicelib = new webservice();
$webservicelib->authenticate_user($token);
$params = ['lang' => current_language()];
if (!empty($h)) {
$params['h'] = $h;
}
// Add do not track parameter.
if (get_config('media_vimeo', 'donottrack')) {
$params['dnt'] = 1;
}
$embedurl = new moodle_url("https://player.vimeo.com/video/$video", $params);
$context = ['embedurl' => $embedurl->out(false)]; // Template context.
if (empty($width) && empty($height)) {
// Use the full page. The video will keep the ratio.
$display = 'style="position:absolute; top:0; left:0; width:100%; height:100%;"';
$context['display'] = "position:absolute; top:0; left:0; width:100%; height:100%;";
} else {
$display = "width=\"$width\" height=\"$height\"";
$context['width'] = $width;
$context['height'] = $height;
}
if (!empty($h)) {
$video .= '?h=' . $h;
}
$output = <<<OET
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body style="margin:0; padding:0">
<iframe src="https://player.vimeo.com/video/$video"
$display frameborder="0"
webkitallowfullscreen mozallowfullscreen allowfullscreen>
</iframe>
</body>
</html>
OET;
echo $output;
// Output the rendered template.
echo $OUTPUT->render_from_template('media_vimeo/appembed', $context);