lib.php 24.2 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-internalmedia
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
Penny Leach's avatar
Penny Leach committed
9
10
11
12
13
 *
 */

defined('INTERNAL') || die();

14
class PluginBlocktypeInternalmedia extends MaharaCoreBlocktype {
Penny Leach's avatar
Penny Leach committed
15

16
17
18
19
20
21
22
23
24
    /**
     * @DEPRECATED: Default width & height. Not currently used, because we
     * use CSS to make all videos scale to fill the width of their container
     * while maintaining their aspect ratio. Kept here so we don't have to
     * tear out all the width/height display code.
     */
    const DEFAULT_WIDTH = 900;
    const DEFAULT_HEIGHT = 600;

Penny Leach's avatar
Penny Leach committed
25
26
27
28
29
30
31
32
33
    public static function get_title() {
        return get_string('title', 'blocktype.file/internalmedia');
    }

    public static function get_description() {
        return get_string('description', 'blocktype.file/internalmedia');
    }

    public static function get_categories() {
34
        return array('fileimagevideo' => 7000);
Penny Leach's avatar
Penny Leach committed
35
36
37
38
39
40
41
42
    }

    public static function has_config() {
        return true;
    }

    public static function postinst($oldversion) {
        if ($oldversion == 0) {
43
            set_config_plugin('blocktype', 'internalmedia', 'enabledtypes', serialize(array('flv', 'mp3', 'mp4_video')));
Penny Leach's avatar
Penny Leach committed
44
45
46
        }
    }

47
    public static function render_instance(BlockInstance $instance, $editing=false) {
48
49
        list($artefact, $width, $height) = self::get_mediaplayer_details($instance);
        if (!$artefact) {
Penny Leach's avatar
Penny Leach committed
50
51
            return '';
        }
52
53
54

        $playerclass = self::get_player_class_for_artefact($artefact);
        if (!$playerclass) {
55
            return get_string('typeremoved', 'blocktype.file/internalmedia');
Penny Leach's avatar
Penny Leach committed
56
        }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

        $result = '<div class="mediaplayer-container panel-body flush"><div class="mediaplayer">';
        $result .= call_static_method($playerclass, 'get_html', $artefact, $instance, $width, $height);

        // File download link
        $filesize = round($artefact->get('size') / 1000000, 2) . 'MB';
        $url = self::get_download_link($artefact, $instance);
        $result .= '<div class="media-download content-text">
            <span class="icon icon-download left" role="presentation" aria-hidden="true">
            </span><span class="sr-only">' . get_string('Download', 'artefact.internal') . '</span>
            <a class="media-link text-small" href="' . $url . '">' . hsc($artefact->get('title')) . '</a>
            <span class="text-midtone text-small"> [' . $filesize . '] </span>
        </div>';

        $result .= '</div></div>';

        require_once(get_config('docroot') . 'artefact/comment/lib.php');
        require_once(get_config('docroot') . 'lib/view.php');
        $view = new View($instance->get('view'));
        list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($artefact, $view, $instance->get('id'), true, $editing);

78
        $smarty = smarty_core();
79
80
81

        $smarty->assign('commentcount', $commentcount);
        $smarty->assign('comments', $comments);
82
83
        $smarty->assign('html', $result);
        return $smarty->fetch('blocktype:internalmedia:internalmedia.tpl');
Penny Leach's avatar
Penny Leach committed
84
85
86
87
88
89
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
90
    public static function instance_config_form(BlockInstance $instance) {
Penny Leach's avatar
Penny Leach committed
91
        $configdata = $instance->get('configdata');
92
93
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
94
        $filebrowser = self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null);
95
        return array(
96
97
98
99
100
            'artefactfieldset' => array(
                'type'         => 'fieldset',
                'collapsible'  => true,
                'collapsed'    => true,
                'legend'       => get_string('media', 'blocktype.file/internalmedia'),
101
                'class'        => 'last select-file with-formgroup',
102
103
104
105
                'elements'     => array(
                    'artefactid' => $filebrowser
                )
            ),
Penny Leach's avatar
Penny Leach committed
106
        );
107
108
    }

109
110
    public static function filebrowser_element(&$instance, $default=array()) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
111
        $element['title'] = get_string('media', 'blocktype.file/internalmedia');
112
        $element['name'] = 'artefactid';
113
        $element['config']['selectone'] = true;
114
        $element['config']['selectmodal'] = true;
115
        $element['filters'] = array(
116
            'artefacttype'    => array('file', 'audio', 'video'),
117
            'filetype'        => self::get_allowed_mimetypes(),
118
        );
119
        $element['accept'] = implode(',', self::get_allowed_mimetypes());
120
121
122
        return $element;
    }

123
    public static function artefactchooser_element($default=null) {
Penny Leach's avatar
Penny Leach committed
124
125
126
127
128
129
130
131
        return array(
            'name' => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('media', 'blocktype.file/internalmedia'),
            'defaultvalue' => $default,
            'blocktype' => 'internalmedia',
            'limit' => 5,
            'selectone' => true,
132
            'artefacttypes' => array('file', 'audio', 'video'),
Penny Leach's avatar
Penny Leach committed
133
134
135
136
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

137
138
139
140
141
    public static function artefactchooser_get_element_data($artefact) {
        $artefact->icon = call_static_method(generate_artefact_class_name($artefact->artefacttype), 'get_icon', array('id' => $artefact->id));
        return $artefact;
    }

Son Nguyen's avatar
Son Nguyen committed
142
    public static function save_config_options(Pieform $form, $values) {
Penny Leach's avatar
Penny Leach committed
143
144
        $enabledtypes = array();
        foreach ($values as $type => $enabled) {
145
            if (!in_array($type, self::get_all_filetypes())) {
146
147
                continue;
            }
Penny Leach's avatar
Penny Leach committed
148
149
150
151
152
153
154
155
156
157
158
            if (!empty($enabled)) {
                $enabledtypes[] = $type;
            }
        }
        set_config_plugin('blocktype', 'internalmedia', 'enabledtypes', serialize($enabledtypes));
    }

    public static function get_config_options() {
        $elements = array();
        // Allowed file types
        $filetypes = array();
159
        $currenttypes = self::get_allowed_filetypes();
Penny Leach's avatar
Penny Leach committed
160

161
        foreach (self::get_all_filetypes() as $filetype) {
162
            $filetypes[$filetype] = array(
163
                'type'  => 'switchbox',
164
165
166
                'title' => get_string($filetype, 'artefact.file'),
                'defaultvalue' => in_array($filetype, $currenttypes),
            );
Penny Leach's avatar
Penny Leach committed
167
168
        }
        uasort($filetypes, create_function('$a, $b', 'return $a["title"] > $b["title"];'));
169
        $options = array_merge(
Penny Leach's avatar
Penny Leach committed
170
171
            array(
                'description' => array(
172
                    'value' => get_string('configdesc1', 'blocktype.file/internalmedia'),
Penny Leach's avatar
Penny Leach committed
173
174
175
176
177
178
                ),
            ),
            $filetypes
        );

        return array(
179
            'elements' => $options,
Penny Leach's avatar
Penny Leach committed
180
181
182
        );
    }

183
184

    private static function get_allowed_filetypes() {
Penny Leach's avatar
Penny Leach committed
185
186
187
188
189
190
        if ($data = get_config_plugin('blocktype', 'internalmedia', 'enabledtypes')) {
            return unserialize($data);
        }
        return array();
    }

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

    private static function get_allowed_mimetypes() {
        return array_keys(self::get_allowed_mimetype_filetypes());
    }


    private static function get_allowed_mimetype_filetypes() {
        if ($data = self::get_allowed_filetypes()) {
            if ($mimetypes = get_records_sql_assoc('
                SELECT mimetype, description
                FROM {artefact_file_mime_types}
                WHERE description IN (' . join(',', array_map('db_quote', $data)) . ')', array())) {
                foreach ($mimetypes as &$m) {
                    $m = $m->description;
                }
206
207
208
209
210
211
212
213
                // Hack to allow .wmv and .wma files to also use the .asf mimetype as well
                // See http://en.wikipedia.org/wiki/Advanced_Systems_Format
                if (in_array('wmv', $data)) {
                    $mimetypes['video/x-ms-asf'] = 'wmv';
                }
                if (in_array('wma', $data)) {
                    $mimetypes['video/x-ms-asf'] = 'wma';
                }
214
215
216
217
218
219
220
                return $mimetypes;
            }
        }
        return array();
    }


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    /**
     * Get the MaharaMediaPlayer class appropriate for this artefact.
     * Or boolean false if there is none.
     *
     * @param ArtefactType $artefact
     * @return string|false
     */
    private static function get_player_class_for_artefact($artefact) {
        $mimetype = $artefact->get('filetype');
        $mimetypefiletypes = self::get_allowed_mimetype_filetypes();
        if (!isset($mimetypefiletypes[$mimetype])) {
            return false;
        }
        else {
            $callbacks = self::get_all_filetype_players();
            $classname = 'MaharaMediaPlayer_' . $callbacks[$mimetypefiletypes[$mimetype]];
            if (class_exists($classname)) {
                return $classname;
            }
            else {
                return false;
            }
        }
    }

246
247
248
249
250
251
252
    private static function get_all_filetypes() {
        return array_keys(self::get_all_filetype_players());
    }


    private static function get_all_filetype_players() {
        /* Keyed by the file type descriptions from the artefact_file_mime_types table */
Penny Leach's avatar
Penny Leach committed
253
        return array(
254
255
256
257
258
259
260
261
            'mp3'       => 'html5audio',
            'swf'       => 'flash',
            'flv'       => 'html5video',
            'quicktime' => 'qt',
            'wmv'       => 'wmp',
            'mpeg'      => 'qt',
            'avi'       => 'wmp',
            'mp4_video' => 'html5video',
Son Nguyen's avatar
Son Nguyen committed
262
263
            'oga'       => 'html5audio',
            'ogv'       => 'html5video',
264
            /* commenting out for now
Penny Leach's avatar
Penny Leach committed
265
266
267
            'ram'   => 'real_player',
            'rm'    => 'real_player',
            'rpm'   => 'real_player',
268
            */
Penny Leach's avatar
Penny Leach committed
269
270
271
        );
    }

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    public static function get_download_link(ArtefactTypeFile $artefact, BlockInstance $instance) {
        return get_config('wwwroot') . 'artefact/file/download.php?file='
            . $artefact->get('id') . '&view=' . $instance->get('view');
    }

    public static function get_instance_javascript(BlockInstance $instance) {
        list($artefact, $width, $height) = self::get_mediaplayer_details($instance);
        if (!$artefact) {
            return array();
        }

        $playerclass = self::get_player_class_for_artefact($artefact);
        if (!$playerclass) {
            return array();
        }

        $jsfile = call_static_method($playerclass, 'get_js_library');
        $jsblock = call_static_method($playerclass, 'get_js_initjs', $artefact, $instance, $width, $height);

        $js = array();
        if ($jsfile) {
            $js['file'] = $jsfile;
        }
        if ($jsblock) {
            $js['initjs'] = $jsblock;
        }
        if ($js) {
            return array($js);
        }
        else {
            return array();
        }
    }

    public static function default_copy_type() {
        return 'full';
    }

    /**
     * Fetches the details needed by the mediaplayer renderers
     *
     * @param BlockInstance $instance
     * @return mixed array(ArtefactType $artefact, width, height) or false if no artefact on this block
     */
    public static function get_mediaplayer_details(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        $viewid = $instance->get('view');
        $artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;

        // If there is no artefact, then return false
        if (empty($artefactid)) {
            return array(false, false, false);
Penny Leach's avatar
Penny Leach committed
324
325
        }

326
327
        require_once(get_config('docroot') . 'artefact/lib.php');
        $artefact = $instance->get_artefact_instance($artefactid);
328
329
330
331
332
333
334
335

        /**
         * @DEPRECATED: Not currently using configurable heights; instead height & width
         * are dynamically scaled using CSS. Keeping these as placeholders so we don't
         * have to tear out all the width & height display code.
         */
        $width  = self::DEFAULT_WIDTH;
        $height = self::DEFAULT_HEIGHT;
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

        return array($artefact, $width, $height);
    }
}


/**
 * Hierarchy of classes that hold the code for the different media players
 * we use. See BlocktypeInternalMedia::get_all_filetype_players() for how
 * we map mimetypes to media players.
 */
abstract class MaharaMediaPlayer {

    /**
     * Returns a unique identifier to use in the "id" attributes of the media player. Should be
     * deterministic so that the HTML function can print it, and the script can then find it.
     *
     * @param ArtefactType $artefact
     * @param BlockInstance $block
     */
    protected static function get_unique_id(ArtefactType $artefact, BlockInstance $block) {
        return $block->get('id') . '_' . $artefact->get('id');
    }

    /**
     * Returns the HTML to display the a mediaplayer of this type
     *
     * @param ArtefactType $artefact
     * @param BlockInstance $block
365
366
     * @param int $width @DEPRECATED: Not currently used
     * @param int $height @DEPRECATED: Not currently used
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
     * @return string
     */
    abstract public static function get_html(ArtefactType $artefact, BlockInstance $block, $width, $height);

    /**
     * Returns JS library used to display a mediaplayer of this type.
     * Because Mahara currently ties one init block to one Javascript library loading, this can
     * only currently support one file per player type.
     *
     * @return array
     */
    public static function get_js_library() { return false; }

    /**
     * Returns a JS snippet needed to initialize a mediaplayer of this type (or boolean false if none)
     *
     * @param ArtefactType $artefact
     * @param BlockInstance $block
385
386
     * @param int $width @DEPRECATED: Not currently used
     * @param int $height @DEPRECATED: Not currently used
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
     * @return string|false
     */
    public static function get_js_initjs(ArtefactType $artefact, BlockInstance $block, $width, $height) { return false; }
}


/**
 * Flash browser plugin (used only for playing actual Flash files, i.e. ".swf" files)
 */
class MaharaMediaPlayer_flash extends MaharaMediaPlayer {

    /**
     * Calculates some items shared by the JS and the HTML
     *
     * @param ArtefactType $artefact
     * @param BlockInstance $block
     * @return array
     */
    protected static function get_unique_id($artefact, $block) {
        return 'blocktype_internalmedia_flash_' . parent::get_unique_id($artefact, $block);
    }

    public static function get_html(ArtefactType $artefact, BlockInstance $block, $width, $height) {
        $id = self::get_unique_id($artefact, $block);
        $html =  '<span class="blocktype_internalmedia_mp3" id="' . $id . '">('
               . get_string('flashanimation', 'blocktype.file/internalmedia') . ')</span>';
Penny Leach's avatar
Penny Leach committed
413
414
415
416
        return $html;

    }

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    public static function get_js_initjs(ArtefactType $artefact, BlockInstance $block, $width, $height) {
        $id = self::get_unique_id($artefact, $block);
        $url = PluginBlocktypeInternalmedia::get_download_link($artefact, $block);
        $swfheight = $height+20;

        return <<<JS
    var so = new SWFObject("{$url}&embedded=1","player","{$width}","{$swfheight}","7");
    so.addParam("allowfullscreen","false");
    so.addVariable("displayheight","{$height}");
    so.addVariable("type", "swf");
    so.addVariable("height", "{$height}");
    so.addVariable("width", "{$width}");
    so.addVariable("wmode", "transparent");
    so.addParam("play", "true");
    so.addParam("allowscriptaccess", "never");
    so.addParam("allownetworking", "never");
    so.write("{$id}");
JS;
    }

    public static function get_js_library() {
        return 'swfobject.js';
    }
}
Penny Leach's avatar
Penny Leach committed
441

442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// /**
//  * RealMedia browser plugin
//  */
// class MaharaMediaPlayer_real extends MaharaMediaPlayer {
//     public static function get_html($artefact, $block, $width, $height) {

//         $url = PluginBlocktypeInternalmedia::get_download_link($artefact, $block);

//         $mimetype = $artefact->get('filetype');
//         $autostart = 'false';

//         return '<span class="blocktype_internalmedia_real">
//     <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">
//         <param name="src" value="' . $url . '">
//         <param name="autostart" value="' . $autostart . '">
//         <param name="controls" value="imagewindow">
//         <param name="console" value="video">
//         <param name="loop" value="true">
//         <embed src="' . $url . '" width=240" height="180" loop="true" type="' . $mimetype . '" controls="imagewindow" console="video" autostart="' . $autostart . '">
//     </object><br>
//     <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">
//         <param name="src" value="' . $url . '">
//         <param name="autostart" value="' . $autostart . '">
//         <param name="controls" value="ControlPanel">
//         <param name="console" value="video">
//         <embed src="' . $url . '" width="240" height="30" controls="ControlPanel" type="' . $mimetype . '" console="video" autostart="' . $autostart . '">
//     </object>
// </span>';
//     }
// }
Penny Leach's avatar
Penny Leach committed
472

473
474
475
476
477
/**
 * Windows Media Player browser plugin
 */
class MaharaMediaPlayer_wmp extends MaharaMediaPlayer {
    public static function get_html(ArtefactType $artefact, BlockInstance $block, $width, $height) {
Penny Leach's avatar
Penny Leach committed
478

479
        $url = hsc(PluginBlocktypeInternalmedia::get_download_link($artefact, $block));
Penny Leach's avatar
Penny Leach committed
480
481
482
483
484
485
486

        $size = 'width="' . $width . '" height="' . $height . '"';
        $autosize = 'false';

        $mimetype = 'video/x-ms-wmv'; // hardcode this
        $autostart = 'false';

487
        return '<span class="mediaplugin mediaplugin_wmp">
Penny Leach's avatar
Penny Leach committed
488
489
490
    <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' . $size . '
      standby="Loading Microsoft(R) Windows(R) Media Player components..."
      type="application/x-oleobject">
491
492
493
494
495
496
497
498
499
500
501
502
     <param name="Filename" value="' . $url . '">' /* hsc($artefact->get('title')) . '"> */ . '
     <param name="src" value="' . $url . '">
     <param name="url" value="' . $url . '">
     <param name="ShowControls" value="true">
     <param name="AutoRewind" value="true">
     <param name="AutoStart" value="' . $autostart . '">
     <param name="Autosize" value="' . $autosize . '">
     <param name="EnableContextMenu" value="true">
     <param name="TransparentAtStart" value="false">
     <param name="AnimationAtStart" value="false">
     <param name="ShowGotoBar" value="false">
     <param name="EnableFullScreenControls" value="true">
503
     <param name="Wmode" value="opaque">
Penny Leach's avatar
Penny Leach committed
504
505
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
506
507
508
509
510
511
       <param name="src" value="' . $url . '">
       <param name="url" value="' . $url . '">
       <param name="controller" value="true">
       <param name="autoplay" value="' . $autostart . '">
       <param name="autostart" value="' . $autostart . '">
       <param name="resize" value="scale">
512
       <param name="wmode" value="opaque">
Penny Leach's avatar
Penny Leach committed
513
514
515
516
      </object>
    <!--<![endif]-->
    </object></span>';
    }
517
}
Penny Leach's avatar
Penny Leach committed
518

519
520
521
522
523
/**
 * Quicktime browser plugin
 */
class MaharaMediaPlayer_qt extends MaharaMediaPlayer {
    public static function get_html(ArtefactType $artefact, BlockInstance $block, $width, $height) {
Penny Leach's avatar
Penny Leach committed
524

525
        $url = PluginBlocktypeInternalmedia::get_download_link($artefact, $block);
Penny Leach's avatar
Penny Leach committed
526
527
528
529

        $size = 'width="' . $width . '" height="' . $height . '"';

        require_once('file.php');
530
        $mimetype = $artefact->get('filetype');
Penny Leach's avatar
Penny Leach committed
531
532
        $autostart = 'false';

533
        return '<span class="mediaplugin mediaplugin_qt">
Penny Leach's avatar
Penny Leach committed
534
535
    <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
      codebase="http://www.apple.com/qtactivex/qtplugin.cab" ' . $size . '>
536
537
     <param name="pluginspage" value="http://www.apple.com/quicktime/download/">
     <param name="src" value="' . $url . '">
538
     <param name="controller" value="true">
539
540
541
542
     <param name="loop" value="false">
     <param name="autoplay" value="' . $autostart . '">
     <param name="autostart" value="' . $autostart . '">
     <param name="scale" value="aspect">
Penny Leach's avatar
Penny Leach committed
543
544
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
545
546
547
548
549
550
551
       <param name="src" value="'.$url.'">
       <param name="pluginurl" value="http://www.apple.com/quicktime/download/">
       <param name="controller" value="true">
       <param name="loop" value="false">
       <param name="autoplay" value="' . $autostart . '">
       <param name="autostart" value="' . $autostart . '">
       <param name="scale" value="aspect">
552
       <param name="wmode" value="transparent">
Penny Leach's avatar
Penny Leach committed
553
554
555
556
      </object>
    <!--<![endif]-->
    </object></span>';
    }
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
}


/**
 * HTLM5 audio player
 */
class MaharaMediaPlayer_html5audio extends MaharaMediaPlayer {

    /**
     * The (current) height, in pixels, of the VideoJS controls.
     * HACK: This height was just found through observation. There's probably
     * a more reliable way to get or control this.
     */
    const VIDEOJS_CONTROL_HEIGHT = 30;

    protected static function get_unique_id($artefact, $block) {
        return 'audio_' . parent::get_unique_id($artefact, $block);
    }
Penny Leach's avatar
Penny Leach committed
575

576
577
578
579
580
581
582
583
584
    /**
     * Returns html5 code to play the artefact audio
     *
     * @param ArtefactTypeFile $artefact
     * @param BlockInstance $block
     * @param int $width
     * @param int $height
     * @return string
     */
585
    public static function get_html(ArtefactType $artefact, BlockInstance $block, $width, $height) {
586

587
        $url = PluginBlocktypeInternalMedia::get_download_link($artefact, $block);
588
589
590
591
592
593

        require_once('file.php');
        $mimetype = $artefact->get('filetype');
        $filesize = round($artefact->get('size') / 1000000, 2) . 'MB';

        return '
594
595
596
597
598
599
        <audio
            id="' . self::get_unique_id($artefact, $block) . '"
            class="video-js vjs-default-skin vjs-big-play-centered vjs-audio"
            width="' . $width . '"
            height="'.self::VIDEOJS_CONTROL_HEIGHT.'"
        >
600
            <source src="' . $url . '" type="' . $mimetype . '"/>
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
            ' . get_string('browsercannotplay', 'blocktype.internalmedia') . '
        </audio>';
    }

    public static function get_js_library() {
        return 'videojs/video.js';
    }

    public static function get_js_initjs(ArtefactTypeFile $artefact, BlockInstance $block, $width, $height) {
        return 'videojs(
            "' . self::get_unique_id($artefact, $block) . '",
            {
                "controls": true,
                "preload": "auto",
                "width": "1000",
                "height": "'.self::VIDEOJS_CONTROL_HEIGHT.'"
            }
        );';
    }
}

/**
 * HTML5 video player
 */
class MaharaMediaPlayer_html5video extends MaharaMediaPlayer {

    protected static function get_unique_id($artefact, $block) {
        return 'video_' . parent::get_unique_id($artefact, $block);
629
630
631
632
633
634
635
636
637
638
639
    }

    /**
     * Returns html5 code to play the artefact video
     *
     * @param ArtefactTypeFile $artefact
     * @param BlockInstance $block
     * @param int $width
     * @param int $height
     * @return string
     */
640
    public static function get_html(ArtefactType $artefact, BlockInstance $block, $width, $height) {
641

642
        $url = PluginBlocktypeInternalMedia::get_download_link($artefact, $block);
643
644
645
646
647

        require_once('file.php');
        $mimetype = $artefact->get('filetype');

        return '
648
649
650
651
652
653
        <video
            id="' . self::get_unique_id($artefact, $block) . '"
            class="video-js vjs-default-skin vjs-big-play-centered"
            width="' . $width . '"
            height="' . $height . '"
        >
654
            <source src="' . $url . '" type="' . $mimetype . '"/>
655
656
            ' . get_string('browsercannotplay', 'blocktype.internalmedia') . '
        </video>';
657
658
    }

659
660
    public static function get_js_library() {
        return 'videojs/video.js';
Penny Leach's avatar
Penny Leach committed
661
662
    }

663
664
665
666
667
668
669
670
671
672
673
674
675
    public static function get_js_initjs(ArtefactTypeFile $artefact, BlockInstance $block, $width, $height) {
        global $CFG;
        return 'videojs(
            "' . self::get_unique_id($artefact, $block) . '",
            {
                "controls": true,
                "preload": "auto",
                "fluid": true,
                "width": ' . $width . ',
                "height": ' . $height . ',
                "swf": "' . $CFG->wwwroot . '/artefact/file/blocktype/internalmedia/videojs/video-js.swf"
            }
        );';
676
    }
Penny Leach's avatar
Penny Leach committed
677
}