lib.php 21.1 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

    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() {
25
        return array('fileimagevideo' => 7000);
Penny Leach's avatar
Penny Leach committed
26
27
28
29
30
31
32
33
    }

    public static function has_config() {
        return true;
    }

    public static function postinst($oldversion) {
        if ($oldversion == 0) {
34
            set_config_plugin('blocktype', 'internalmedia', 'enabledtypes', serialize(array('flv', 'mp3', 'mp4_video')));
Penny Leach's avatar
Penny Leach committed
35
36
37
        }
    }

38
    public static function render_instance(BlockInstance $instance, $editing=false) {
Penny Leach's avatar
Penny Leach committed
39
        $configdata = $instance->get('configdata');
40
41
42
        $viewid = $instance->get('view');
        $artefactid = isset($configdata['artefactid']) ? $configdata['artefactid'] : null;
        if (empty($artefactid)) {
Penny Leach's avatar
Penny Leach committed
43
44
45
            return '';
        }
        $result = self::get_js_source();
46
        require_once(get_config('docroot') . 'artefact/lib.php');
47
        $artefact = $instance->get_artefact_instance($artefactid);
48
49
50
51
52
53
        $defaultwidth = get_config_plugin('blocktype', 'internalmedia', 'width') ?
                get_config_plugin('blocktype', 'internalmedia', 'width') : 300;
        $defaultheight = get_config_plugin('blocktype', 'internalmedia', 'height') ?
                get_config_plugin('blocktype', 'internalmedia', 'height') : 300;
        $width  = (!empty($configdata['width'])) ? hsc($configdata['width']) : $defaultwidth;
        $height = (!empty($configdata['height'])) ? hsc($configdata['height']) : $defaultheight;
54
55
56
        $mimetype = $artefact->get('filetype');
        $mimetypefiletypes = self::get_allowed_mimetype_filetypes();
        if (!isset($mimetypefiletypes[$mimetype])) {
57
            return get_string('typeremoved', 'blocktype.file/internalmedia');
Penny Leach's avatar
Penny Leach committed
58
        }
59
        $callbacks = self::get_all_filetype_players();
60
        $result .= '<div class="mediaplayer-container panel-body flush"><div class="mediaplayer">' . call_static_method('PluginBlocktypeInternalmedia', $callbacks[$mimetypefiletypes[$mimetype]], $artefact, $instance, $width, $height) . '</div></div>';
61
62
63
64
65

        if ($artefactid) {
            require_once(get_config('docroot') . 'artefact/comment/lib.php');
            require_once(get_config('docroot') . 'lib/view.php');
            $view = new View($viewid);
66
            list($commentcount, $comments) = ArtefactTypeComment::get_artefact_comments_for_view($artefact, $view, $instance->get('id'), true, $editing);
67
68
69
70
71
72
73
74
        }
        $smarty = smarty_core();
        if ($artefactid) {
            $smarty->assign('commentcount', $commentcount);
            $smarty->assign('comments', $comments);
        }
        $smarty->assign('html', $result);
        return $smarty->fetch('blocktype:internalmedia:internalmedia.tpl');
Penny Leach's avatar
Penny Leach committed
75
76
77
78
79
80
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
81
    public static function get_instance_config_javascript(BlockInstance $instance) {
82
83
84
85
86
87
        $result = self::get_js_source(true);
        if (!empty($result)) {
            return $result;
        }
    }

Son Nguyen's avatar
Son Nguyen committed
88
    public static function instance_config_form(BlockInstance $instance) {
Penny Leach's avatar
Penny Leach committed
89
        $configdata = $instance->get('configdata');
90
91
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
92
        $filebrowser = self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null);
93
        return array(
94
95
96
97
98
            'artefactfieldset' => array(
                'type'         => 'fieldset',
                'collapsible'  => true,
                'collapsed'    => true,
                'legend'       => get_string('media', 'blocktype.file/internalmedia'),
99
                'class'        => 'last select-file with-formgroup',
100
101
102
103
                'elements'     => array(
                    'artefactid' => $filebrowser
                )
            ),
104
            'width' => array(
Penny Leach's avatar
Penny Leach committed
105
106
107
108
                'type' => 'text',
                'title' => get_string('width'),
                'size' => 3,
                'defaultvalue' => (isset($configdata['width'])) ? $configdata['width'] : '',
109
                'rules' => array('minvalue' => 1, 'maxvalue' => 2000),
110
111
            ),
            'height' => array(
Penny Leach's avatar
Penny Leach committed
112
113
114
115
                'type' => 'text',
                'title' => get_string('height'),
                'size' => 3,
                'defaultvalue' => (isset($configdata['height'])) ? $configdata['height'] : '',
116
                'rules' => array('minvalue' => 1, 'maxvalue' => 2000),
117
            ),
Penny Leach's avatar
Penny Leach committed
118
        );
119
120
    }

121
122
    public static function filebrowser_element(&$instance, $default=array()) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
123
        $element['title'] = get_string('media', 'blocktype.file/internalmedia');
124
        $element['name'] = 'artefactid';
125
        $element['config']['selectone'] = true;
126
        $element['config']['selectmodal'] = true;
127
        $element['filters'] = array(
128
            'artefacttype'    => array('file', 'audio', 'video'),
129
            'filetype'        => self::get_allowed_mimetypes(),
130
        );
131
        $element['accept'] = implode(',', self::get_allowed_mimetypes());
132
133
134
        return $element;
    }

135
    public static function artefactchooser_element($default=null) {
Penny Leach's avatar
Penny Leach committed
136
137
138
139
140
141
142
143
        return array(
            'name' => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('media', 'blocktype.file/internalmedia'),
            'defaultvalue' => $default,
            'blocktype' => 'internalmedia',
            'limit' => 5,
            'selectone' => true,
144
            'artefacttypes' => array('file', 'audio', 'video'),
Penny Leach's avatar
Penny Leach committed
145
146
147
148
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

149
150
151
152
153
    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
154
    public static function save_config_options(Pieform $form, $values) {
Penny Leach's avatar
Penny Leach committed
155
156
        $enabledtypes = array();
        foreach ($values as $type => $enabled) {
157
            if (!in_array($type, self::get_all_filetypes())) {
158
159
                continue;
            }
Penny Leach's avatar
Penny Leach committed
160
161
162
163
164
            if (!empty($enabled)) {
                $enabledtypes[] = $type;
            }
        }
        set_config_plugin('blocktype', 'internalmedia', 'enabledtypes', serialize($enabledtypes));
165
166
        set_config_plugin('blocktype', 'internalmedia', 'height', $values['height']);
        set_config_plugin('blocktype', 'internalmedia', 'width',  $values['width']);
Penny Leach's avatar
Penny Leach committed
167
168
169
170
171
172
    }

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

175
        foreach (self::get_all_filetypes() as $filetype) {
176
            $filetypes[$filetype] = array(
177
                'type'  => 'switchbox',
178
179
180
                'title' => get_string($filetype, 'artefact.file'),
                'defaultvalue' => in_array($filetype, $currenttypes),
            );
Penny Leach's avatar
Penny Leach committed
181
182
        }
        uasort($filetypes, create_function('$a, $b', 'return $a["title"] > $b["title"];'));
183
        $options = array_merge(
Penny Leach's avatar
Penny Leach committed
184
185
            array(
                'description' => array(
186
                    'value' => get_string('configdesc1', 'blocktype.file/internalmedia'),
Penny Leach's avatar
Penny Leach committed
187
188
189
190
                ),
            ),
            $filetypes
        );
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        $options['height'] = array(
            'type'          => 'text',
            'title'         => get_string('height'),
            'rules'        => array('integer' => true, 'minvalue' => 120, 'maxvalue' => 3072),
            'defaultvalue'  => get_config_plugin('blocktype', 'internalmedia', 'height')
                ? get_config_plugin('blocktype', 'internalmedia', 'height') : 240,
        );
        $options['width'] = array(
            'type'          => 'text',
            'title'         => get_string('width'),
            'rules'        => array('integer' => true, 'minvalue' => 160, 'maxvalue' => 4096),
            'defaultvalue'  => get_config_plugin('blocktype', 'internalmedia', 'width')
                ? get_config_plugin('blocktype', 'internalmedia', 'width') : 320,
        );
Penny Leach's avatar
Penny Leach committed
205
206

        return array(
207
            'class'    => 'panel panel-body',
208
            'elements' => $options,
209
            'renderer' => 'div'
Penny Leach's avatar
Penny Leach committed
210
211
212
        );
    }

213
214

    private static function get_allowed_filetypes() {
Penny Leach's avatar
Penny Leach committed
215
216
217
218
219
220
        if ($data = get_config_plugin('blocktype', 'internalmedia', 'enabledtypes')) {
            return unserialize($data);
        }
        return array();
    }

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

    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;
                }
236
237
238
239
240
241
242
243
                // 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';
                }
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                return $mimetypes;
            }
        }
        return array();
    }


    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
258
        return array(
259
            'mp3'       => 'html5audio_player', // tested
260
            'swf'       => 'flash_player', // tested
261
            'flv'       => 'html5video_player', // tested
262
263
264
265
            'quicktime' => 'qt_player',  // tested
            'wmv'       => 'wmp_player', // tested
            'mpeg'      => 'qt_player',  // tested
            'avi'       => 'wmp_player', // tested
266
            'mp4_video' => 'html5video_player', //tested
267
            /* commenting out for now
Penny Leach's avatar
Penny Leach committed
268
269
270
            'ram'   => 'real_player',
            'rm'    => 'real_player',
            'rpm'   => 'real_player',
271
            */
Penny Leach's avatar
Penny Leach committed
272
273
274
275
276
277
278
279
        );
    }

    public static function flash_player($artefact, $block, $width, $height) {
        static $count = 0;
        $count++;
        $id = 'blocktype_internalmedia_flash_' . time() . $count;
        $url = self::get_download_link($artefact, $block);
280
281
282
283
        $params = array('play' => 'true',
                        'allowscriptaccess' => 'never',
                        'allownetworking' => 'never'
                       );
284
        $html =  '<a class="media-link text-small" href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br>
285
               <span class="blocktype_internalmedia_mp3" id="' . $id . '">('
286
               . get_string('flashanimation', 'blocktype.file/internalmedia') . ')</span>
287
                <script type="application/javascript">
288
                    var so = new SWFObject("' . $url . '&embedded=1","player","' . $width . '","' . ($height + 20). '","7");
Penny Leach's avatar
Penny Leach committed
289
290
                    so.addParam("allowfullscreen","false");
                    so.addVariable("displayheight"," ' . $height . '");
291
                    so.addVariable("type", "swf");
Penny Leach's avatar
Penny Leach committed
292
293
                    so.addVariable("height", "' . $height . '");
                    so.addVariable("width", "' . $width . '");
294
                    so.addVariable("wmode", "transparent");
Penny Leach's avatar
Penny Leach committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
                ';
        foreach ($params as $key => $value) {
            $html .= '      so.addParam("' . $key . '", "' . $value . '"); '. "\n";
        }

        $html .= '
                    so.write("' . $id . '");
                </script>
        ';
        return $html;

    }

    public static function real_player($artefact, $block, $width, $height) {

        $url = self::get_download_link($artefact, $block);

        require_once('file.php');
313
        $mimetype = $artefact->get('filetype');
Penny Leach's avatar
Penny Leach committed
314
315
        $autostart = 'false';

316
        return '<a class="media-link text-small" href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br>'
Penny Leach's avatar
Penny Leach committed
317
    . '<span class="blocktype_internalmedia_real">
318
    <script type="application/javascript">
Penny Leach's avatar
Penny Leach committed
319
320
    //<![CDATA[
    document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
321
322
323
324
325
326
327
      <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>\\
Penny Leach's avatar
Penny Leach committed
328
      <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
329
330
331
332
333
      <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 . '">\\
Penny Leach's avatar
Penny Leach committed
334
335
336
337
338
339
340
      </object>\');
    //]]>
    </script></span>';
    }

    public static function wmp_player($artefact, $block, $width, $height) {

341
        $url = hsc(self::get_download_link($artefact, $block));
Penny Leach's avatar
Penny Leach committed
342
343
344
345
346
347
348

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

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

349
        return '<a class="media-link text-small" href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br>'
Penny Leach's avatar
Penny Leach committed
350
351
352
353
    . '<span class="mediaplugin mediaplugin_wmp">
    <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' . $size . '
      standby="Loading Microsoft(R) Windows(R) Media Player components..."
      type="application/x-oleobject">
354
355
356
357
358
359
360
361
362
363
364
365
     <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">
366
     <param name="Wmode" value="opaque">
Penny Leach's avatar
Penny Leach committed
367
368
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
369
370
371
372
373
374
       <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">
375
       <param name="wmode" value="opaque">
Penny Leach's avatar
Penny Leach committed
376
377
378
379
380
381
382
383
384
385
386
387
      </object>
    <!--<![endif]-->
    </object></span>';
    }

    public static function qt_player($artefact, $block, $width, $height) {

        $url = self::get_download_link($artefact, $block);

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

        require_once('file.php');
388
        $mimetype = $artefact->get('filetype');
Penny Leach's avatar
Penny Leach committed
389
390
        $autostart = 'false';

391
        return '<a class="media-link text-small" href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br>'
Penny Leach's avatar
Penny Leach committed
392
393
394
    . '<span class="mediaplugin mediaplugin_qt">
    <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
      codebase="http://www.apple.com/qtactivex/qtplugin.cab" ' . $size . '>
395
396
     <param name="pluginspage" value="http://www.apple.com/quicktime/download/">
     <param name="src" value="' . $url . '">
397
     <param name="controller" value="true">
398
399
400
401
     <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
402
403
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
404
405
406
407
408
409
410
       <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">
411
       <param name="wmode" value="transparent">
Penny Leach's avatar
Penny Leach committed
412
413
414
415
416
      </object>
    <!--<![endif]-->
    </object></span>';
    }

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
472
473
474
475
476
477
478
479
480
481
482
483
    /**
     * Returns html5 code to play the artefact audio
     *
     * @param ArtefactTypeFile $artefact
     * @param BlockInstance $block
     * @param int $width
     * @param int $height
     * @return string
     */
    public static function html5audio_player(ArtefactTypeFile $artefact, BlockInstance $block, $width, $height) {

        $url = self::get_download_link($artefact, $block);

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

        return '
        <audio id="audio_' . $block->get('id') . '" class="video-js vjs-default-skin vjs-big-play-centered">
            <source src="' . $url . '" type="' . $mimetype . '"/>
            Your browser does not support the audio element.
        </audio>
        <script type="application/javascript">
             videojs("audio_' . $block->get('id') . '", {"controls": true, "preload": "auto", "width": ' . $width . ', "height": 50}, function() {});
        </script>
        <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>';
    }

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

        $url = self::get_download_link($artefact, $block);

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

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

        return '
        <video id="video_' . $block->get('id') . '" class="video-js vjs-default-skin vjs-big-play-centered">
            <source src="' . $url . '" type="' . $mimetype . '"/>
        </video>
        <script type="application/javascript">
             videojs("video_' . $block->get('id') . '", {"controls": true, "preload": "auto", "width": ' . $width . ', "height": ' . $height . '}, function() {});
        </script>
        <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>';
    }

484
    private static function get_download_link(ArtefactTypeFile $artefact, BlockInstance $instance) {
485
        return get_config('wwwroot') . 'artefact/file/download.php?file='
486
            . $artefact->get('id') . '&view=' . $instance->get('view');
Penny Leach's avatar
Penny Leach committed
487
488
    }

489
    private static function get_js_source($asarray = false) {
Penny Leach's avatar
Penny Leach committed
490
491
492
493
        if (defined('BLOCKTYPE_INTERNALMEDIA_JS_INCLUDED')) {
            return '';
        }
        define('BLOCKTYPE_INTERNALMEDIA_JS_INCLUDED', true);
494
        if ($asarray) {
495
            return array(get_config('wwwroot') . 'artefact/file/blocktype/internalmedia/videojs/video.min.js',
496
497
                         );
        }
498
        return '<script src="'.get_config('wwwroot') . 'artefact/file/blocktype/internalmedia/videojs/video.min.js'.'"></script>';
Penny Leach's avatar
Penny Leach committed
499
500
    }

501
502
503
504
    public static function default_copy_type() {
        return 'full';
    }

Penny Leach's avatar
Penny Leach committed
505
}