lib.php 17.7 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
Penny Leach's avatar
Penny Leach committed
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Penny Leach's avatar
Penny Leach committed
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Penny Leach's avatar
Penny Leach committed
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Penny Leach's avatar
Penny Leach committed
19
20
21
 *
 * @package    mahara
 * @subpackage blocktype-internalmedia
22
 * @author     Catalyst IT Ltd
Penny Leach's avatar
Penny Leach committed
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
Penny Leach's avatar
Penny Leach committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 *
 */

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

class PluginBlocktypeInternalmedia extends PluginBlocktype {

    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() {
41
        return array('fileimagevideo');
Penny Leach's avatar
Penny Leach committed
42
43
44
45
46
47
48
49
    }

    public static function has_config() {
        return true;
    }

    public static function postinst($oldversion) {
        if ($oldversion == 0) {
50
            set_config_plugin('blocktype', 'internalmedia', 'enabledtypes', serialize(array('flv', 'mp3')));
Penny Leach's avatar
Penny Leach committed
51
52
53
        }
    }

54
    public static function render_instance(BlockInstance $instance, $editing=false) {
Penny Leach's avatar
Penny Leach committed
55
56
57
58
59
60
        $configdata = $instance->get('configdata');

        if (empty($configdata['artefactid'])) {
            return '';
        }
        $result = self::get_js_source();
61
        require_once(get_config('docroot') . 'artefact/lib.php');
62
        $artefact = $instance->get_artefact_instance($configdata['artefactid']);
Penny Leach's avatar
Penny Leach committed
63
64
        $width  = (!empty($configdata['width'])) ? hsc($configdata['width']) : '300';
        $height = (!empty($configdata['height'])) ? hsc($configdata['height']) : '300';
65
66
67
        $mimetype = $artefact->get('filetype');
        $mimetypefiletypes = self::get_allowed_mimetype_filetypes();
        if (!isset($mimetypefiletypes[$mimetype])) {
68
            return get_string('typeremoved', 'blocktype.file/internalmedia');
Penny Leach's avatar
Penny Leach committed
69
        }
70
        $callbacks = self::get_all_filetype_players();
71
        $result .= '<div class="mediaplayer-container center"><div class="mediaplayer">' . call_static_method('PluginBlocktypeInternalmedia', $callbacks[$mimetypefiletypes[$mimetype]], $artefact, $instance, $width, $height) . '</div></div>';
Penny Leach's avatar
Penny Leach committed
72
73
74
75
76
77
78
        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

79
    public static function instance_config_form($instance) {
Penny Leach's avatar
Penny Leach committed
80
        $configdata = $instance->get('configdata');
81
82
83
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
        return array(
84
            'artefactid' => self::filebrowser_element($instance, (isset($configdata['artefactid'])) ? array($configdata['artefactid']) : null),
85
            'width' => array(
Penny Leach's avatar
Penny Leach committed
86
87
88
89
                'type' => 'text',
                'title' => get_string('width'),
                'size' => 3,
                'defaultvalue' => (isset($configdata['width'])) ? $configdata['width'] : '',
90
                'rules' => array('minvalue' => 1, 'maxvalue' => 2000),
91
92
            ),
            'height' => array(
Penny Leach's avatar
Penny Leach committed
93
94
95
96
                'type' => 'text',
                'title' => get_string('height'),
                'size' => 3,
                'defaultvalue' => (isset($configdata['height'])) ? $configdata['height'] : '',
97
                'rules' => array('minvalue' => 1, 'maxvalue' => 2000),
98
            ),
Penny Leach's avatar
Penny Leach committed
99
        );
100
101
    }

102
103
    public static function filebrowser_element(&$instance, $default=array()) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
104
        $element['title'] = get_string('media', 'blocktype.file/internalmedia');
105
        $element['name'] = 'artefactid';
106
107
108
109
        $element['config']['selectone'] = true;
        $element['filters'] = array(
            'artefacttype'    => array('file'),
            'filetype'        => self::get_allowed_mimetypes(),
110
111
112
113
        );
        return $element;
    }

114
    public static function artefactchooser_element($default=null) {
Penny Leach's avatar
Penny Leach committed
115
116
117
118
119
120
121
122
123
124
125
126
127
        return array(
            'name' => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('media', 'blocktype.file/internalmedia'),
            'defaultvalue' => $default,
            'blocktype' => 'internalmedia',
            'limit' => 5,
            'selectone' => true,
            'artefacttypes' => array('file'),
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

128
129
130
131
132
    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;
    }

Penny Leach's avatar
Penny Leach committed
133
134
135
    public static function save_config_options($values) {
        $enabledtypes = array();
        foreach ($values as $type => $enabled) {
136
            if (!in_array($type, self::get_all_filetypes())) {
137
138
                continue;
            }
Penny Leach's avatar
Penny Leach committed
139
140
141
142
143
144
145
146
147
148
149
            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();
150
        $currenttypes = self::get_allowed_filetypes();
Penny Leach's avatar
Penny Leach committed
151

152
        foreach (self::get_all_filetypes() as $filetype) {
153
154
155
156
157
            $filetypes[$filetype] = array(
                'type'  => 'checkbox',
                'title' => get_string($filetype, 'artefact.file'),
                'defaultvalue' => in_array($filetype, $currenttypes),
            );
Penny Leach's avatar
Penny Leach committed
158
159
160
161
162
        }
        uasort($filetypes, create_function('$a, $b', 'return $a["title"] > $b["title"];'));
        $filetypes = array_merge(
            array(
                'description' => array(
163
                    'value' => get_string('configdesc', 'blocktype.file/internalmedia'),
Penny Leach's avatar
Penny Leach committed
164
165
166
167
168
169
170
171
172
173
174
                ),
            ),
            $filetypes
        );

        return array(
            'elements' => $filetypes,
            'renderer' => 'table'
        );
    }

175
176

    private static function get_allowed_filetypes() {
Penny Leach's avatar
Penny Leach committed
177
178
179
180
181
182
        if ($data = get_config_plugin('blocktype', 'internalmedia', 'enabledtypes')) {
            return unserialize($data);
        }
        return array();
    }

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

    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;
                }
                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
212
        return array(
213
            'mp3'       => 'flow_player', // tested
214
            'swf'       => 'flash_player', // tested
215
            'flv'       => 'flow_player', // tested
216
217
218
219
            'quicktime' => 'qt_player',  // tested
            'wmv'       => 'wmp_player', // tested
            'mpeg'      => 'qt_player',  // tested
            'avi'       => 'wmp_player', // tested
220
            'mp4_video' => 'flow_player', // tested
221
            /* commenting out for now
Penny Leach's avatar
Penny Leach committed
222
223
224
            'ram'   => 'real_player',
            'rm'    => 'real_player',
            'rpm'   => 'real_player',
225
            */
Penny Leach's avatar
Penny Leach committed
226
227
228
229
230
231
232
233
        );
    }

    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);
234
        $params = array('play' => 'true');
235
        $html =  '<a href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br>
Penny Leach's avatar
Penny Leach committed
236
               <span class="blocktype_internalmedia_mp3" id="' . $id . '">(' 
237
               . get_string('flashanimation', 'blocktype.file/internalmedia') . ')</span>
Penny Leach's avatar
Penny Leach committed
238
                <script type="text/javascript">
239
                    var so = new SWFObject("' . $url . '","player","' . $width . '","' . ($height + 20). '","7");
Penny Leach's avatar
Penny Leach committed
240
241
                    so.addParam("allowfullscreen","false");
                    so.addVariable("displayheight"," ' . $height . '");
242
                    so.addVariable("type", "swf");
Penny Leach's avatar
Penny Leach committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                    so.addVariable("height", "' . $height . '");
                    so.addVariable("width", "' . $width . '");
                ';
        foreach ($params as $key => $value) {
            $html .= '      so.addParam("' . $key . '", "' . $value . '"); '. "\n";
        }

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

    }

258
259
260
261
262
263
264
    public static function flow_player($artefact, $block, $width, $height) {
        static $count = 0;
        $count++;
        $extn = $artefact->get('oldextension');

        $id = 'blocktype_internalmedia_flow_' . time() . $count;
        $url = self::get_download_link($artefact, $block);
265
        $escapedurl = str_replace('&', '%26', $url); // Flash needs these escaped
266

267
268
269
        $baseurl = get_config('wwwroot') . 'artefact/file/blocktype/internalmedia/';

        $playerurl = $baseurl . 'flowplayer/flowplayer-3.2.4.swf';
270
271
272
273
274
275
276
277
278
279
        $autohide = 'true';
        $type = '';
        $audio = '';
        $buffering = 'true';
        if ($extn == 'mp3') {
            $height = 25; // only show the controls
            $autohide = 'false';
            $type = 'type: "audio",'; // force the player to use the audio plugin
            $buffering = 'false'; // without this autoPlay will also be set to true
            $audio = ', audio: {
280
		                  url: "' . $baseurl . 'flowplayer.audio/flowplayer.audio-3.2.1.swf"
281
282
283
284
		             }';
        }

        $html =  '<a href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br>
285
286
               <span class="blocktype_internalmedia_mp3" id="' . $id . '" style="display:block;width:'.$width.'px;height:'.$height.'px;"></span>
               <span id="' . $id . '_h">' . get_string('flashanimation', 'blocktype.file/internalmedia') . '</span>
287
288
289
               <script type="text/javascript">
               flowplayer("'.$id.'", "'.$playerurl.'", {
                   clip:  {
290
                       url: "'.$escapedurl.'",
291
292
293
294
295
296
297
298
299
300
301
302
303
                       '.$type.'
                       autoPlay: false,
                       autoBuffering: '.$buffering.'
                   },
                   plugins: {
	                  controls: {
                          url: "flowplayer.controls-3.2.2.swf",
                          play:true,
                          volume:true,
                          mute:true,
                          time:false,
                          stop:false,
                          playlist:false,
304
                          fullscreen:true,
305
306
307
308
309
                          scrubber: true,
                          autoHide: '.$autohide.'
                      }'.$audio.'
                   }
               }).load();
310
               addElementClass("' . $id . '_h", "hidden");
311
312
313
314
315
316
               </script>
';
        return $html;

    }

Penny Leach's avatar
Penny Leach committed
317
318
319
320
321
    public static function real_player($artefact, $block, $width, $height) {

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

        require_once('file.php');
322
        $mimetype = $artefact->get('filetype');
Penny Leach's avatar
Penny Leach committed
323
324
        $autostart = 'false';

325
        return '<a href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br>'
Penny Leach's avatar
Penny Leach committed
326
327
328
329
    . '<span class="blocktype_internalmedia_real">
    <script type="text/javascript">
    //<![CDATA[
    document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
330
331
332
333
334
335
336
      <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
337
      <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
338
339
340
341
342
      <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
343
344
345
346
347
348
349
      </object>\');
    //]]>
    </script></span>';
    }

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

350
        $url = hsc(self::get_download_link($artefact, $block, true));
Penny Leach's avatar
Penny Leach committed
351
352
353
354
355
356
357

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

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

358
        return '<a href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br>'
Penny Leach's avatar
Penny Leach committed
359
360
361
362
    . '<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">
363
364
365
366
367
368
369
370
371
372
373
374
     <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">
Penny Leach's avatar
Penny Leach committed
375
376
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
377
378
379
380
381
382
       <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">
Penny Leach's avatar
Penny Leach committed
383
384
385
386
387
388
389
390
391
392
393
394
      </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');
395
        $mimetype = $artefact->get('filetype');
Penny Leach's avatar
Penny Leach committed
396
397
        $autostart = 'false';

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

    private static function get_download_link(ArtefactTypeFile $artefact, BlockInstance $instance, $wmp=false) {
        return get_config('wwwroot') . 'artefact/file/download.php?file=' 
425
            . $artefact->get('id') . '&view=' . $instance->get('view')
Penny Leach's avatar
Penny Leach committed
426
427
428
429
430
431
432
433
            . ($wmp ? '&download=1' : '');
    }

    private static function get_js_source() {
        if (defined('BLOCKTYPE_INTERNALMEDIA_JS_INCLUDED')) {
            return '';
        }
        define('BLOCKTYPE_INTERNALMEDIA_JS_INCLUDED', true);
434
        return '<script src="'.get_config('wwwroot').'artefact/file/blocktype/internalmedia/flowplayer/flowplayer-3.2.4.js"></script>
435
             <script src="' . get_config('wwwroot') . 'artefact/file/blocktype/internalmedia/swfobject.js" type="text/javascript"></script>
Nigel McNie's avatar
Nigel McNie committed
436
             <script defer="defer" src="' . get_config('wwwroot') . 'artefact/file/blocktype/internalmedia/eolas_fix.js" type="text/javascript"></script>';
Penny Leach's avatar
Penny Leach committed
437
438
    }

439
440
441
442
    public static function default_copy_type() {
        return 'full';
    }

Penny Leach's avatar
Penny Leach committed
443
}