lib.php 14.8 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
4
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2007 Catalyst IT Ltd (http://www.catalyst.net.nz)
Penny Leach's avatar
Penny Leach committed
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * 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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 *
 * @package    mahara
 * @subpackage blocktype-internalmedia
 * @author     Penny Leach <penny@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

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() {
        return array('multimedia', 'file');
    }

    public static function has_config() {
        return true;
    }

    public static function postinst($oldversion) {
        if ($oldversion == 0) {
            $enabledtypes = array_map(
                create_function('$a', 'return 1;'),
                self::get_all_extensions());
            unset($enabledtypes['swf']); // disable by default
            $enabledtypes = array_keys($enabledtypes);
            set_config_plugin('blocktype', 'internalmedia', 'enabledtypes', serialize($enabledtypes));
        }
    }

    public static function render_instance(BlockInstance $instance) {
        $configdata = $instance->get('configdata');

        if (empty($configdata['artefactid'])) {
            return '';
        }
        $result = self::get_js_source();
65
        require_once(get_config('docroot') . 'artefact/lib.php');
Penny Leach's avatar
Penny Leach committed
66
67
68
69
70
        $artefact = artefact_instance_from_id($configdata['artefactid']);
        $width  = (!empty($configdata['width'])) ? hsc($configdata['width']) : '300';
        $height = (!empty($configdata['height'])) ? hsc($configdata['height']) : '300';
        $extn = $artefact->get('oldextension');
        if (!in_array($extn, self::get_allowed_extensions())) {
71
            return get_string('typeremoved', 'blocktype.file/internalmedia');
Penny Leach's avatar
Penny Leach committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        }
        $callbacks = self::get_all_extensions();
        $result .= call_static_method('PluginBlocktypeInternalmedia', $callbacks[$extn], $artefact, $instance, $width, $height);
        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

    public static function instance_config_form($instance) {
        $configdata = $instance->get('configdata');
        $form = array(
            self::artefactchooser_element((isset($configdata['artefactid'])) ? $configdata['artefactid'] : null),
        );
        $form['width'] = array(
                'type' => 'text',
                'title' => get_string('width'),
                'size' => 3,
                'defaultvalue' => (isset($configdata['width'])) ? $configdata['width'] : '',
        );
        $form['height'] = array(
                'type' => 'text',
                'title' => get_string('height'),
                'size' => 3,
                'defaultvalue' => (isset($configdata['height'])) ? $configdata['height'] : '',
        );
        return $form;
    }

    public static function get_artefacts(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        if (isset($configdata['artefactid'])) {
            return array($configdata['artefactid']);
        }
        return false;
    }

    public static function artefactchooser_element($default=null) {
        $extraselect = '(' . implode(' OR ', array_map(
            create_function('$a', 'return "title LIKE \'%.$a\'";'),
            self::get_allowed_extensions())
        ) . ')';

        return array(
            'name' => 'artefactid',
            'type'  => 'artefactchooser',
            'title' => get_string('media', 'blocktype.file/internalmedia'),
            'defaultvalue' => $default,
            'rules' => array(
                'required' => true,
            ),
            'blocktype' => 'internalmedia',
            'limit' => 5,
            'selectone' => true,
            'artefacttypes' => array('file'),
            'extraselect' => $extraselect,
            'template' => 'artefact:file:artefactchooser-element.tpl',
        );
    }

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

152
153
154
155
156
        if (!$plugindisabled = get_column_sql('SELECT description
            FROM {artefact_file_file_types} 
            WHERE enabled = 0')) {
            $plugindisabled = array();
        }
Penny Leach's avatar
Penny Leach committed
157
        foreach (array_keys(self::get_all_extensions()) as $filetype) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
            if (in_array($filetype, $plugindisabled)) {
                $filetypes[$filetype] = array(
                    'type'  => 'checkbox',
                    'title' => get_string($filetype, 'artefact.file'),
                    'value' => false,
                    'disabled' => true,
                );
            }
            else {
                $filetypes[$filetype] = array(
                    'type'  => 'checkbox',
                    'title' => get_string($filetype, 'artefact.file'),
                    'defaultvalue' => in_array($filetype, $currenttypes),
                );
            }
Penny Leach's avatar
Penny Leach committed
173
174
175
176
177
        }
        uasort($filetypes, create_function('$a, $b', 'return $a["title"] > $b["title"];'));
        $filetypes = array_merge(
            array(
                'description' => array(
178
                    'value' => get_string('configdesc', 'blocktype.file/internalmedia'),
Penny Leach's avatar
Penny Leach committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
                ),
            ),
            $filetypes
        );

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

    private static function get_allowed_extensions() {
        if ($data = get_config_plugin('blocktype', 'internalmedia', 'enabledtypes')) {
            return unserialize($data);
        }
        return array();
    }

    private static function get_all_extensions() {
        return array(
            'mp3'   => 'flash_player', // tested
            'swf'   => 'flash_player', // tested
            'flv'   => 'flash_player', // tested
            'mov'   => 'qt_player',  // tested
            'wmv'   => 'wmp_player', // tested
            'mpeg'  => 'qt_player',  // tested
205
            'mpg'   => 'qt_player',  // tested
Penny Leach's avatar
Penny Leach committed
206
            'avi'   => 'wmp_player', // tested
207
            /* commenting out for now
Penny Leach's avatar
Penny Leach committed
208
209
210
            'ram'   => 'real_player',
            'rm'    => 'real_player',
            'rpm'   => 'real_player',
211
            */
Penny Leach's avatar
Penny Leach committed
212
213
214
215
216
217
218
219
220
        );
    }

    public static function flash_player($artefact, $block, $width, $height) {
        static $count = 0;
        $count++;
        $extn = $artefact->get('oldextension');
        if ($extn == 'mp3') {
            $height = 0; // one line
221
            $width = 100;
Penny Leach's avatar
Penny Leach committed
222
223
224
225
226
227
228
229
230
231
232
233
234
        }
        $id = 'blocktype_internalmedia_flash_' . time() . $count;
        $url = self::get_download_link($artefact, $block);
        $playerurl =  get_config('wwwroot') . "artefact/file/blocktype/internalmedia/mediaplayer.swf";
        $params = array();
        if ($extn == 'swf') {
            $playerurl = $url;
            $params['play'] = 'false';
        }
        $html =  '<a href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br />
               <span class="blocktype_internalmedia_mp3" id="' . $id . '">(' 
               . get_string('flash', 'blocktype.file/internalmedia') . ')</span>
                <script type="text/javascript">
235
                    var so = new SWFObject(" ' . $playerurl . '","player","' . $width . '","' . ($height + 20). '","7");
Penny Leach's avatar
Penny Leach committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
321
322
323
324
325
326
327
328
329
330
331
332
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
                    so.addParam("allowfullscreen","false");
                    so.addVariable("file","' . urlencode($url) . '");
                    so.addVariable("displayheight"," ' . $height . '");
                    so.addVariable("type", "' . $artefact->get('oldextension') . '");
                    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;

    }

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

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

        require_once('file.php');
        $mimetype = get_mime_type($artefact->get_path());
        $autostart = 'false';

        return '<a href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br />'
    . '<span class="blocktype_internalmedia_real">
    <script type="text/javascript">
    //<![CDATA[
    document.write(\'<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>\');
    //]]>
    </script></span>';
    }

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

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

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

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

        return '<a href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br />'
    . '<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">
     <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" />
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
       <param name="src" value="' . $url . '" />
       <param name="controller" value="true" />
       <param name="autoplay" value="' . $autostart . '" />
       <param name="autostart" value="' . $autostart . '" />
       <param name="resize" value="scale" />
      </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');
        $mimetype = get_mime_type($artefact->get_path());
        $autostart = 'false';

        return '<a href="' . $url . '">' . hsc($artefact->get('title')) . '</a><br />'
    . '<span class="mediaplugin mediaplugin_qt">
    <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
      codebase="http://www.apple.com/qtactivex/qtplugin.cab" ' . $size . '>
     <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
     <param name="src" value="' . $url . '" />
     <param name="coltroller" value="true" />
     <param name="loop" value="false" />
     <param name="autoplay" value="' . $autostart . '" />
     <param name="autostart" value="' . $autostart . '" />
     <param name="scale" value="aspect" />
    <!--[if !IE]>-->
      <object data="' . $url . '" type="' . $mimetype . '" ' . $size . '>
       <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" />
      </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=' 
            . $artefact->get('id') . '&view=' . $instance->get('view')
            . ($wmp ? '&download=1' : '');
    }

    private static function get_js_source() {
        if (defined('BLOCKTYPE_INTERNALMEDIA_JS_INCLUDED')) {
            return '';
        }
        define('BLOCKTYPE_INTERNALMEDIA_JS_INCLUDED', true);
        return '<script src="' . get_config('wwwroot') . 'artefact/file/blocktype/internalmedia/swfobject.js"></script>
             <script defer="true" src="' . get_config('wwwroot') . 'artefact/file/blocktype/internalmedia/eolas_fix.js"></script>';
    }

}

?>