lib.php 21 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')));
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, 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
            'mp3'       => 'html5audio',
            'flv'       => 'html5video',
            'quicktime' => 'qt',
            'wmv'       => 'wmp',
            'mpeg'      => 'qt',
            'avi'       => 'wmp',
260
            'mp4'       => 'html5video',
Son Nguyen's avatar
Son Nguyen committed
261
            'oga'       => 'html5audio',
262
            'ogg'       => 'html5audio',
Son Nguyen's avatar
Son Nguyen committed
263
            'ogv'       => 'html5video',
264
265
            'webm'      => 'html5video',
            '3gp'       => 'html5video',
Penny Leach's avatar
Penny Leach committed
266
267
268
        );
    }

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
    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
321
322
        }

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

        /**
         * @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;
333
334
335
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

        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
362
363
     * @param int $width @DEPRECATED: Not currently used
     * @param int $height @DEPRECATED: Not currently used
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
     * @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
382
383
     * @param int $width @DEPRECATED: Not currently used
     * @param int $height @DEPRECATED: Not currently used
384
385
386
387
388
389
390
391
392
393
     * @return string|false
     */
    public static function get_js_initjs(ArtefactType $artefact, BlockInstance $block, $width, $height) { return false; }
}

/**
 * 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
394

395
        $url = hsc(PluginBlocktypeInternalmedia::get_download_link($artefact, $block));
Penny Leach's avatar
Penny Leach committed
396
397
398
399
400
401
402

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

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

403
        return '<span class="mediaplugin mediaplugin_wmp">
Penny Leach's avatar
Penny Leach committed
404
405
406
    <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' . $size . '
      standby="Loading Microsoft(R) Windows(R) Media Player components..."
      type="application/x-oleobject">
407
408
409
410
411
412
413
414
415
416
417
418
     <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">
419
     <param name="Wmode" value="opaque">
Penny Leach's avatar
Penny Leach committed
420
421
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
422
423
424
425
426
427
       <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">
428
       <param name="wmode" value="opaque">
Penny Leach's avatar
Penny Leach committed
429
430
431
432
      </object>
    <!--<![endif]-->
    </object></span>';
    }
433
}
Penny Leach's avatar
Penny Leach committed
434

435
436
437
438
439
/**
 * 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
440

441
        $url = PluginBlocktypeInternalmedia::get_download_link($artefact, $block);
Penny Leach's avatar
Penny Leach committed
442
443
444
445

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

        require_once('file.php');
446
        $mimetype = $artefact->get('filetype');
Penny Leach's avatar
Penny Leach committed
447
448
        $autostart = 'false';

449
        return '<span class="mediaplugin mediaplugin_qt">
Penny Leach's avatar
Penny Leach committed
450
451
    <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
      codebase="http://www.apple.com/qtactivex/qtplugin.cab" ' . $size . '>
452
453
     <param name="pluginspage" value="http://www.apple.com/quicktime/download/">
     <param name="src" value="' . $url . '">
454
     <param name="controller" value="true">
455
456
457
458
     <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
459
460
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
461
462
463
464
465
466
467
       <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">
468
       <param name="wmode" value="transparent">
Penny Leach's avatar
Penny Leach committed
469
470
471
472
      </object>
    <!--<![endif]-->
    </object></span>';
    }
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
}


/**
 * 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;

488
    protected static function get_unique_id(ArtefactType $artefact, BlockInstance $block) {
489
490
        return 'audio_' . parent::get_unique_id($artefact, $block);
    }
Penny Leach's avatar
Penny Leach committed
491

492
493
494
495
496
497
498
499
500
    /**
     * Returns html5 code to play the artefact audio
     *
     * @param ArtefactTypeFile $artefact
     * @param BlockInstance $block
     * @param int $width
     * @param int $height
     * @return string
     */
501
    public static function get_html(ArtefactType $artefact, BlockInstance $block, $width, $height) {
502

503
        $url = PluginBlocktypeInternalMedia::get_download_link($artefact, $block);
504
505
506
507
508
509

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

        return '
510
511
512
513
514
515
        <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.'"
        >
516
            <source src="' . $url . '" type="' . $mimetype . '"/>
517
            ' . get_string('browsercannotplay1', 'blocktype.internalmedia') . '
518
519
520
521
522
523
524
        </audio>';
    }

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

525
    public static function get_js_initjs(ArtefactType $artefact, BlockInstance $block, $width, $height) {
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
        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 {

543
    protected static function get_unique_id(ArtefactType $artefact, BlockInstance $block) {
544
        return 'video_' . parent::get_unique_id($artefact, $block);
545
546
547
548
549
550
551
552
553
554
555
    }

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

558
        $url = PluginBlocktypeInternalMedia::get_download_link($artefact, $block);
559
560
561
562
563

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

        return '
564
565
566
567
568
569
        <video
            id="' . self::get_unique_id($artefact, $block) . '"
            class="video-js vjs-default-skin vjs-big-play-centered"
            width="' . $width . '"
            height="' . $height . '"
        >
570
            <source src="' . $url . '" type="' . $mimetype . '"/>
571
            ' . get_string('browsercannotplay1', 'blocktype.internalmedia') . '
572
        </video>';
573
574
    }

575
576
    public static function get_js_library() {
        return 'videojs/video.js';
Penny Leach's avatar
Penny Leach committed
577
578
    }

579
    public static function get_js_initjs(ArtefactType $artefact, BlockInstance $block, $width, $height) {
580
581
582
583
584
585
586
587
588
589
590
591
        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"
            }
        );';
592
    }
Penny Leach's avatar
Penny Leach committed
593
}