14
14
// You should have received a copy of the GNU General Public License
15
15
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
16
17
- /**
18
- * Smart media filtering
19
- *
20
- * This filter will replace any links to a compatible media file with
21
- * a smart media plugin that plays that media inline and uses AI/ML
22
- * techniques to improve user experience.
23
- *
24
- * @package filter_smartmedia
25
- * @copyright 2019 Matt Porritt <mattp@catalyst-au.net>V
26
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
- */
28
-
17
+ use local_smartmedia \conversion ;
18
+ use core \plugininfo \media ;
19
+ use core \url ;
20
+ use core \output \html_writer ;
21
+ use core \context \module ;
22
+ use core \context \course ;
29
23
use local_smartmedia \aws_api ;
30
24
use local_smartmedia \aws_elastic_transcoder ;
31
25
@@ -62,7 +56,7 @@ class filter_smartmedia extends moodle_text_filter {
62
56
/**
63
57
* Enabled status of the video JS player.
64
58
*
65
- * @var integer
59
+ * @var int
66
60
*/
67
61
private $ videojsenabled = self ::VIDEOJS_ENABLED_NOT_SET ;
68
62
@@ -131,15 +125,15 @@ class filter_smartmedia extends moodle_text_filter {
131
125
* @param context $context The current context.
132
126
* @param array $localconfig Any context-specific configuration for this filter.
133
127
*/
134
- public function __construct ($ context , array $ localconfig , \ local_smartmedia \ conversion $ conversion = null ) {
128
+ public function __construct ($ context , array $ localconfig , conversion $ conversion = null ) {
135
129
parent ::__construct ($ context , $ localconfig );
136
130
137
131
if (!empty ($ conversion )) {
138
132
$ this ->conversion = $ conversion ;
139
133
} else {
140
134
$ api = new aws_api ();
141
135
$ transcoder = new aws_elastic_transcoder ($ api ->create_elastic_transcoder_client ());
142
- $ this ->conversion = new \ local_smartmedia \ conversion ($ transcoder );
136
+ $ this ->conversion = new conversion ($ transcoder );
143
137
}
144
138
}
145
139
@@ -176,7 +170,7 @@ private function videojs_enabled() {
176
170
if ($ this ->videojsenabled == self ::VIDEOJS_ENABLED_NOT_SET ) {
177
171
// If we haven't already determined Videjos plugin enabled status
178
172
// do so now.
179
- $ enabledplayes = \ core \ plugininfo \ media::get_enabled_plugins ();
173
+ $ enabledplayes = media::get_enabled_plugins ();
180
174
if (in_array ('videojs ' , $ enabledplayes ) && class_exists ('media_videojs_plugin ' )) {
181
175
$ this ->videojsenabled = self ::VIDEOJS_ENABLED ;
182
176
} else {
@@ -193,7 +187,7 @@ private function videojs_enabled() {
193
187
*
194
188
* @return string $typestring String of supported types.
195
189
*/
196
- private function get_browser_native_types () : string {
190
+ private function get_browser_native_types (): string {
197
191
$ typestring = '\\' . implode ('| \\' , $ this ->browsernative );
198
192
199
193
return $ typestring ;
@@ -206,11 +200,11 @@ private function get_browser_native_types() : string {
206
200
* @param string $linkhref The href to the source file.
207
201
* @return array $elements The smart media elements to embed.
208
202
*/
209
- private function get_smart_elements (string $ linkhref ) : array {
203
+ private function get_smart_elements (string $ linkhref ): array {
210
204
$ urls = [];
211
205
$ options = [];
212
206
$ elements = [];
213
- $ moodleurl = new \ moodle_url ($ linkhref );
207
+ $ moodleurl = new url ($ linkhref );
214
208
215
209
$ smartmedia = $ this ->conversion ->get_smart_media ($ moodleurl );
216
210
@@ -241,7 +235,7 @@ private function get_smart_elements(string $linkhref) : array {
241
235
* @param string $needle The string to search for.
242
236
* @return bool Result of string check.
243
237
*/
244
- private function string_ends_with (string $ haystack , string $ needle ) : bool {
238
+ private function string_ends_with (string $ haystack , string $ needle ): bool {
245
239
$ length = strlen ($ needle );
246
240
if ($ length == 0 ) {
247
241
return true ;
@@ -261,7 +255,7 @@ private function string_ends_with(string $haystack, string $needle) : bool {
261
255
* @param bool $hasdata Whether there is metadata associated with this smartmedia.
262
256
* @return string $newtext Rendered VideoJS markup.
263
257
*/
264
- private function get_embed_markup (string $ linkhref , array $ urls , array $ options , array $ download , bool $ hasdata ) : string {
258
+ private function get_embed_markup (string $ linkhref , array $ urls , array $ options , array $ download , bool $ hasdata ): string {
265
259
global $ OUTPUT ;
266
260
267
261
$ name = $ options ['name ' ];
@@ -270,7 +264,7 @@ private function get_embed_markup(string $linkhref, array $urls, array $options,
270
264
$ embedoptions = [];
271
265
$ downloaddata = '<video ' ;
272
266
273
- $ videojs = new \ media_videojs_plugin ();
267
+ $ videojs = new media_videojs_plugin ();
274
268
$ newtext = $ videojs ->embed ($ urls , $ name , $ width , $ height , $ embedoptions );
275
269
// TODO: Deal with fallback link.
276
270
@@ -315,7 +309,7 @@ private function get_embed_markup(string $linkhref, array $urls, array $options,
315
309
// Explode the url to get the filename component for naming.
316
310
$ components = explode ('/ ' , $ linkhref );
317
311
$ newtext .= $ OUTPUT ->single_button (
318
- new moodle_url ('/filter/smartmedia/download_metadata.php ' , [
312
+ new url ('/filter/smartmedia/download_metadata.php ' , [
319
313
'sesskey ' => sesskey (),
320
314
'conv ' => base64_encode ($ linkhref ),
321
315
'title ' => base64_encode (end ($ components )),
@@ -335,9 +329,9 @@ private function get_embed_markup(string $linkhref, array $urls, array $options,
335
329
* @param string $fulltext The full text of the element.
336
330
* @return string $markup The placeholder markup.
337
331
*/
338
- private function get_placeholder_markup (string $ linkhref , string $ fulltext ) : string {
332
+ private function get_placeholder_markup (string $ linkhref , string $ fulltext ): string {
339
333
global $ OUTPUT ;
340
- $ moodleurl = new \ moodle_url ($ linkhref );
334
+ $ moodleurl = new url ($ linkhref );
341
335
$ path = $ moodleurl ->get_path ();
342
336
$ args = explode ('/ ' , $ path );
343
337
$ filename = array_pop ($ args );
@@ -349,7 +343,7 @@ private function get_placeholder_markup(string $linkhref, string $fulltext) : st
349
343
}
350
344
351
345
$ markup = $ fulltext ;
352
- $ context = new \ stdClass ();
346
+ $ context = new stdClass ();
353
347
354
348
// If file is of type that is browser native,
355
349
// don't show placeholder.
@@ -379,7 +373,7 @@ private function get_placeholder_markup(string $linkhref, string $fulltext) : st
379
373
* @param array $matches An array of link matches.
380
374
* @return array Array of newtext and whether the text was replaced
381
375
*/
382
- private function replace ($ target , $ fulltext ) : array {
376
+ private function replace ($ target , $ fulltext ): array {
383
377
global $ OUTPUT , $ SESSION ;
384
378
385
379
list ($ context , $ elements ) = $ this ->get_smart_elements ($ target ); // Get the smartmedia elements if they exist.
@@ -390,7 +384,7 @@ private function replace($target, $fulltext) : array {
390
384
// The placeholder should only be displayed if this file will actually be converted.
391
385
// We need to verify that the file will be queued for conversion.
392
386
// Timecreated check.
393
- $ file = $ this ->conversion ->get_file_from_url (new \ moodle_url ($ target ));
387
+ $ file = $ this ->conversion ->get_file_from_url (new url ($ target ));
394
388
if (!empty ($ file ) && $ file ->get_timecreated () < time () - $ lookback ) {
395
389
$ placeholder = false ;
396
390
}
@@ -421,12 +415,12 @@ private function replace($target, $fulltext) : array {
421
415
if ($ usesource && has_capability ('filter/smartmedia:viewsource ' , $ context )) {
422
416
// Return the original markup, along with a button to swap back to smartmedia.
423
417
$ url ->param ('sm ' , $ current );
424
- $ button = new \ single_button (
418
+ $ button = new single_button (
425
419
$ url ,
426
420
get_string ('viewoptimised ' , 'filter_smartmedia ' ),
427
421
'get '
428
422
);
429
- $ button = \ html_writer::div ($ OUTPUT ->render ($ button ), 'local-smartmedia-view-optimised ' );
423
+ $ button = html_writer::div ($ OUTPUT ->render ($ button ), 'local-smartmedia-view-optimised ' );
430
424
431
425
// Output the original source media and return.
432
426
if (!array_key_exists ($ current , $ viewsource )) {
@@ -439,7 +433,7 @@ private function replace($target, $fulltext) : array {
439
433
$ fulltext = str_replace (' ' , '' , $ fulltext );
440
434
441
435
// Put in the smartmedia wrapper to keep styling consistent.
442
- $ html = \ html_writer::div ($ fulltext . $ button , 'local-smartmedia-wrapper ' );
436
+ $ html = html_writer::div ($ fulltext . $ button , 'local-smartmedia-wrapper ' );
443
437
return [$ html , false ];
444
438
}
445
439
// Now store the state back into the session.
@@ -459,20 +453,20 @@ private function replace($target, $fulltext) : array {
459
453
if (has_capability ('filter/smartmedia:viewsource ' , $ context )) {
460
454
// Add a button to view source.
461
455
$ url ->param ('source ' , $ current );
462
- $ button = new \ single_button (
456
+ $ button = new single_button (
463
457
$ url ,
464
458
get_string ('viewsource ' , 'filter_smartmedia ' ),
465
459
'get '
466
460
);
467
461
// Wrap just smartmedia content inside a wrapper div for styling targeting.
468
- $ replacedlink = \ html_writer::div ($ replacedlink . $ OUTPUT ->render ($ button ), 'local-smartmedia-wrapper ' );
462
+ $ replacedlink = html_writer::div ($ replacedlink . $ OUTPUT ->render ($ button ), 'local-smartmedia-wrapper ' );
469
463
} else {
470
- $ replacedlink = \ html_writer::div ($ replacedlink , 'local-smartmedia-wrapper ' );
464
+ $ replacedlink = html_writer::div ($ replacedlink , 'local-smartmedia-wrapper ' );
471
465
}
472
466
$ replaced = true ;
473
467
} else if ($ placeholder ) {
474
468
// If no smartmedia found add the correct placeholder markup.
475
- $ replacedlink = \ html_writer::div ($ this ->get_placeholder_markup ($ target , $ fulltext ), 'local-smartmedia-wrapper ' );
469
+ $ replacedlink = html_writer::div ($ this ->get_placeholder_markup ($ target , $ fulltext ), 'local-smartmedia-wrapper ' );
476
470
$ replaced = true ;
477
471
} else {
478
472
// Do nothing, no replacement candidate.
@@ -487,7 +481,7 @@ private function replace($target, $fulltext) : array {
487
481
* Gets the course/page url from the context.
488
482
*
489
483
* @param context $context
490
- * @return moodle_url $url
484
+ * @return \core\url $url
491
485
*/
492
486
private function url_from_context ($ context ) {
493
487
global $ PAGE ;
@@ -499,11 +493,11 @@ private function url_from_context($context) {
499
493
$ course = null ;
500
494
$ cm = null ;
501
495
502
- if ($ context instanceof \context_module ) {
496
+ if ($ context instanceof module ) {
503
497
list ($ course , $ cm ) = get_course_and_cm_from_cmid ($ context ->instanceid );
504
498
}
505
499
506
- if ($ context instanceof \context_course ) {
500
+ if ($ context instanceof course ) {
507
501
$ course = get_course ($ context ->instanceid );
508
502
}
509
503
@@ -512,7 +506,7 @@ private function url_from_context($context) {
512
506
513
507
// Start with course, as its the most likely to exist.
514
508
if ($ isajax && !empty ($ course )) {
515
- $ url = new moodle_url ('/course/view.php ' , ['id ' => $ course ->id ]);
509
+ $ url = new url ('/course/view.php ' , ['id ' => $ course ->id ]);
516
510
}
517
511
518
512
// Then check the course module has a URL, if so then use that instead.
@@ -521,7 +515,7 @@ private function url_from_context($context) {
521
515
}
522
516
523
517
// Setup a page anchor if on the course page and viewing a section.
524
- if ($ context instanceof \context_module && strpos ($ url , '/course/view.php ' ) !== false && !empty ($ cm )) {
518
+ if ($ context instanceof module && strpos ($ url , '/course/view.php ' ) !== false && !empty ($ cm )) {
525
519
$ url ->set_anchor ('section- ' . $ cm ->sectionnum );
526
520
}
527
521
@@ -657,7 +651,7 @@ public function filter($text, array $options = []) {
657
651
if ($ link ->isSameNode ($ newlink )) {
658
652
$ exists = true ;
659
653
}
660
- } catch (\ Throwable $ e ) {
654
+ } catch (Throwable $ e ) {
661
655
// Some error, likely when the $link is no longer a valid DOMElement.
662
656
continue ;
663
657
}
0 commit comments