Skip to content

Commit

Permalink
MED-92: Use Videotime player instead of Media Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Thies committed Feb 8, 2024
1 parent db77fb1 commit 2f09bab
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 37 deletions.
1 change: 1 addition & 0 deletions classes/media_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
30 changes: 25 additions & 5 deletions source/streamio/classes/form/edit_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion source/streamio/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
30 changes: 22 additions & 8 deletions source/streamio/classes/output/media_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
];
}

Expand Down
22 changes: 16 additions & 6 deletions source/streamio/templates/video.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

}}
<p>
<video
class="vjs-fluid"
id="{{ elementid }}"
controls
src="https://streamio.com/api/v1/videos/{{ id }}/public_show.m3u8"
class="video-js"
poster="https://{{ screenshot.normal }}"
>
</video>
</p>
{{#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}}
47 changes: 40 additions & 7 deletions source/videotime/classes/form/edit_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
]
);
Expand Down Expand Up @@ -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'
);
}
Expand Down
4 changes: 3 additions & 1 deletion source/videotime/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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(
Expand All @@ -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);

Expand Down
25 changes: 18 additions & 7 deletions source/videotime/classes/output/media_resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
];
}

Expand Down
15 changes: 13 additions & 2 deletions source/videotime/templates/video.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

}}
<p>
<video
id="{{ elementid }}"
controls
class="vjs-fluid"
src="{{ videourl }}"
class="video-js"
poster="{{ poster }}"
>
</video>
</p>
{{#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}}
3 changes: 3 additions & 0 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@
$plugin->version = 2024010804;
$plugin->requires = 2022112800;
$plugin->maturity = MATURITY_ALPHA;
$plugin->dependencies = [
'mod_videotime' => 2023011200,
];

0 comments on commit 2f09bab

Please sign in to comment.