From 2f09bab0922b6afc31e0e1c340e120b3b45d86dd Mon Sep 17 00:00:00 2001 From: Daniel Thies Date: Thu, 8 Feb 2024 11:58:52 -0600 Subject: [PATCH] MED-92: Use Videotime player instead of Media Filter --- classes/media_manager.php | 1 + .../streamio/classes/form/edit_resource.php | 30 ++++++++++-- source/streamio/classes/manager.php | 2 +- .../classes/output/media_resource.php | 30 ++++++++---- source/streamio/templates/video.mustache | 22 ++++++--- .../videotime/classes/form/edit_resource.php | 47 ++++++++++++++++--- source/videotime/classes/manager.php | 4 +- .../classes/output/media_resource.php | 25 +++++++--- source/videotime/templates/video.mustache | 15 +++++- version.php | 3 ++ 10 files changed, 142 insertions(+), 37 deletions(-) diff --git a/classes/media_manager.php b/classes/media_manager.php index 3f16ae9..17da328 100644 --- a/classes/media_manager.php +++ b/classes/media_manager.php @@ -71,6 +71,7 @@ public function __construct(string $source, ?stdClass $record = null, int $page require_capability('tool/mediatime:view', context_system::instance()); if ($record) { $source = $record->source; + $record->content = json_decode($record->content); } $plugins = plugininfo\mediatimesrc::get_enabled_plugins(); diff --git a/source/streamio/classes/form/edit_resource.php b/source/streamio/classes/form/edit_resource.php index 30e27d0..b8eacad 100644 --- a/source/streamio/classes/form/edit_resource.php +++ b/source/streamio/classes/form/edit_resource.php @@ -24,6 +24,10 @@ namespace mediatimesrc_streamio\form; +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->libdir/resourcelib.php"); + use context_system; use moodleform; use mediatimesrc_streamio\api; @@ -89,13 +93,29 @@ public function definition_after_data() { $record = $DB->get_record('tool_mediatime', ['id' => $id]); $context = context_system::instance(); if ($record) { + $record->content = json_decode($record->content); $resource = new media_resource($record); + + $videourl = $resource->video_url($OUTPUT); + $content = [ + 'poster' => $resource->image_url($OUTPUT), + 'elementid' => 'video-' . uniqid(), + 'instance' => json_encode([ + 'vimeo_url' => $videourl, + 'controls' => true, + 'responsive' => true, + 'playsinline' => false, + 'autoplay' => false, + 'option_loop' => false, + 'muted' => true, + 'type' => resourcelib_guess_url_mimetype($videourl), + ]), + ]; $mform->insertElementBefore( - $mform->createElement('html', format_text( - $OUTPUT->render_from_template('mediatimesrc_streamio/video', json_decode($record->content)), - FORMAT_HTML, - ['context' => $context] - )), + $mform->createElement( + 'html', + $OUTPUT->render_from_template('mediatimesrc_streamio/video', $content) + ), 'name' ); $mform->removeElement('filesource'); diff --git a/source/streamio/classes/manager.php b/source/streamio/classes/manager.php index ec76ada..cb84eec 100644 --- a/source/streamio/classes/manager.php +++ b/source/streamio/classes/manager.php @@ -98,7 +98,7 @@ public function __construct($record = null) { $this->form = new form\edit_resource(); if ($record) { - $this->content = json_decode($record->content); + $this->content = $record->content; } if ($edit = optional_param('edit', null, PARAM_INT)) { diff --git a/source/streamio/classes/output/media_resource.php b/source/streamio/classes/output/media_resource.php index f8a13bf..f163f54 100644 --- a/source/streamio/classes/output/media_resource.php +++ b/source/streamio/classes/output/media_resource.php @@ -24,6 +24,10 @@ namespace mediatimesrc_streamio\output; +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->libdir/resourcelib.php"); + use moodle_url; use stdClass; use renderable; @@ -57,21 +61,31 @@ public function __construct(stdClass $record) { */ public function export_for_template(renderer_base $output) { global $DB, $USER; - $content = json_decode($this->record->content); - $api = new \mediatimesrc_streamio\api(); + $content = (array)$this->record->content; $context = \context_system::instance(); + $videourl = $this->video_url($output); + + $content += [ + 'elementid' => 'video-' . uniqid(), + 'instance' => json_encode([ + 'vimeo_url' => $videourl, + 'controls' => true, + 'responsive' => true, + 'playsinline' => false, + 'autoplay' => false, + 'option_loop' => false, + 'muted' => true, + 'type' => resourcelib_guess_url_mimetype($videourl), + ]), + 'poster' => $this->image_url($output), + ]; return [ 'canedit' => has_capability('tool/mediatime:manage', $context) || ($USER->id == $this->record->usermodified), 'id' => $this->record->id, 'libraryhome' => new moodle_url('/admin/tool/mediatime/index.php'), 'resource' => $content, - 'url' => 'https://exampel.com', - 'video' => format_text( - $output->render_from_template('mediatimesrc_streamio/video', $content), - FORMAT_HTML, - ['context' => \context_system::instance()] - ), + 'video' => $output->render_from_template('mediatimesrc_streamio/video', $content), ]; } diff --git a/source/streamio/templates/video.mustache b/source/streamio/templates/video.mustache index 691b334..dc2d297 100644 --- a/source/streamio/templates/video.mustache +++ b/source/streamio/templates/video.mustache @@ -25,19 +25,29 @@ Example context (json): { - "id": "324lk32sK", - "screenshot": { - "normal": "example.com/image.jpg" - } + "elementid": "324lk32sK", + "instance": "[{'vimeo_url': 'https://example.com/video.m3u8'}]", + "screenshot": { + "normal": "example.com/image.jpg" + } } }}

+{{#js}} + require(['videotimeplugin_videojs/videotime'], function(VideoTime) { + VideoTime.prototype.view = function() { + return true; + }; + var v = new VideoTime('{{ elementid }}', 0, 0, 0, {{{instance}}}); + v.initialize(); + }); +{{/js}} diff --git a/source/videotime/classes/form/edit_resource.php b/source/videotime/classes/form/edit_resource.php index cf21ba0..f2a6069 100644 --- a/source/videotime/classes/form/edit_resource.php +++ b/source/videotime/classes/form/edit_resource.php @@ -24,6 +24,10 @@ namespace mediatimesrc_videotime\form; +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->libdir/resourcelib.php"); + use moodleform; use mediatimesrc_videotime\output\media_resource; @@ -71,11 +75,30 @@ public function definition() { get_string('videofile', 'mediatimesrc_videotime'), null, [ + 'accepted_types' => [ + '.flac', + '.mp3', + '.mp4', + '.mov', + '.movie', + '.m3u', + '.m3u8', + '.m4a', + '.m4v', + '.mp4', + '.mpeg', + '.ogg', + '.oga', + '.ogv', + '.wav', + '.webm', + '.wma', + '.wmv', + ], 'subdirs' => 0, 'maxbytes' => $maxbytes, 'areamaxbytes' => $maxbytes, 'maxfiles' => 1, - 'accepted_types' => ['video'], 'return_types' => FILE_INTERNAL, ] ); @@ -114,16 +137,26 @@ public function definition_after_data() { $record = $DB->get_record('tool_mediatime', ['id' => $id]); if ($record) { $resource = new media_resource($record); + $content = [ 'poster' => $resource->image_url($OUTPUT), - 'videourl' => $resource->video_url($OUTPUT), + 'elementid' => 'video-' . uniqid(), + 'instance' => json_encode([ + 'vimeo_url' => $resource->video_url($OUTPUT), + 'controls' => true, + 'responsive' => true, + 'playsinline' => false, + 'autoplay' => false, + 'option_loop' => false, + 'muted' => true, + 'type' => resourcelib_guess_url_mimetype($videourl), + ]), ]; $mform->insertElementBefore( - $mform->createElement('html', format_text( - $OUTPUT->render_from_template('mediatimesrc_videotime/video', $content), - FORMAT_HTML, - ['context' => \context_system::instance()] - )), + $mform->createElement( + 'html', + $OUTPUT->render_from_template('mediatimesrc_videotime/video', $content) + ), 'name' ); } diff --git a/source/videotime/classes/manager.php b/source/videotime/classes/manager.php index 853693e..efc7fc8 100644 --- a/source/videotime/classes/manager.php +++ b/source/videotime/classes/manager.php @@ -62,7 +62,7 @@ public function __construct($record = null) { $this->form = new form\edit_resource(); if ($record) { - $this->content = json_decode($record->content); + $this->content = $record->content; } $maxbytes = get_config('mediatimesrc_videotime', 'maxbytes'); @@ -80,6 +80,7 @@ public function __construct($record = null) { 'maxfiles' => 1, ] ); + $this->content->videofile = $draftitemid; $draftitemid = file_get_submitted_draft_itemid('posterimage'); file_prepare_draft_area( @@ -94,6 +95,7 @@ public function __construct($record = null) { 'maxfiles' => 1, ] ); + $this->content->posterimage = $draftitemid; $this->content->tags = core_tag_tag::get_item_tags_array('tool_mediatime', 'tool_mediatime', $edit); diff --git a/source/videotime/classes/output/media_resource.php b/source/videotime/classes/output/media_resource.php index 73e76c8..66192a4 100644 --- a/source/videotime/classes/output/media_resource.php +++ b/source/videotime/classes/output/media_resource.php @@ -24,6 +24,10 @@ namespace mediatimesrc_videotime\output; +defined('MOODLE_INTERNAL') || die(); + +require_once("$CFG->libdir/resourcelib.php"); + use moodle_url; use stdClass; use renderable; @@ -58,22 +62,29 @@ public function __construct(stdClass $record) { */ public function export_for_template(renderer_base $output) { global $DB, $USER; + $videourl = $this->video_url($output); $content = [ + 'elementid' => 'video-' . uniqid(), + 'instance' => json_encode([ + 'autoplay' => false, + 'controls' => true, + 'playsinline' => false, + 'muted' => true, + 'option_loop' => false, + 'responsive' => true, + 'type' => resourcelib_guess_url_mimetype($videourl), + 'vimeo_url' => $videourl, + ]), 'poster' => $this->image_url($output), - 'videourl' => $this->video_url($output), ]; return [ 'canedit' => has_capability('moodle/tag:edit', $this->context) || $USER->id == $this->record->usermodified, 'id' => $this->record->id, 'libraryhome' => new moodle_url('/admin/tool/mediatime/index.php'), - 'resource' => json_decode($this->record->content), - 'video' => format_text( - $output->render_from_template('mediatimesrc_videotime/video', $content), - FORMAT_HTML, - ['context' => $this->context] - ), + 'resource' => $this->record->content, + 'video' => $output->render_from_template('mediatimesrc_videotime/video', $content), ]; } diff --git a/source/videotime/templates/video.mustache b/source/videotime/templates/video.mustache index 8e8dfdf..bed4121 100644 --- a/source/videotime/templates/video.mustache +++ b/source/videotime/templates/video.mustache @@ -25,17 +25,28 @@ Example context (json): { + "elementid": "324lk32sK", "videourl": "https://example.com/video.mp4", + "instance": "[{'vimeo_url': 'https://example.com/video.mp4'}]", "poster": "https://example.com/image.jpg" } }}

+{{#js}} + require(['videotimeplugin_videojs/videotime'], function(VideoTime) { + VideoTime.prototype.view = function() { + return true; + }; + var v = new VideoTime('{{ elementid }}', 0, 0, 0, {{{instance}}}); + v.initialize(); + }); +{{/js}} diff --git a/version.php b/version.php index 75c8d33..e80d82e 100644 --- a/version.php +++ b/version.php @@ -29,3 +29,6 @@ $plugin->version = 2024010804; $plugin->requires = 2022112800; $plugin->maturity = MATURITY_ALPHA; +$plugin->dependencies = [ + 'mod_videotime' => 2023011200, +];