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