lib.php 8.99 KB
Newer Older
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
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.
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.
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/>.
19
20
21
 *
 * @package    mahara
 * @subpackage blocktype-externalvideo
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 *
 */

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

/**
 * todos before this block type can be considered complete
 *  - document this class and methods
 *  - correct category
 *  - more video url sources, and good default behaviour
 *  - block title editable
 *  - i18n
 *  - minvalue/maxvalue rules
 */
class PluginBlocktypeExternalvideo extends SystemBlocktype {

41
42
43
44
45
    // Default width and height for video players
    private static $default_width = 250;

    private static $default_height = 250;

46
47
48
49
50
51
52
53
54
    public static function get_title() {
        return get_string('title', 'blocktype.externalvideo');
    }

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

    public static function get_categories() {
55
        return array('external');
56
57
    }

58
    public static function render_instance(BlockInstance $instance, $editing=false) {
59
60
        $configdata = $instance->get('configdata');
        $result = '';
61
62
        $width  = (!empty($configdata['width'])) ? hsc($configdata['width']) : self::$default_width;
        $height = (!empty($configdata['height'])) ? hsc($configdata['height']) : self::$default_height;
63
64

        if (isset($configdata['videoid'])) {
65
66
67
68
69
70
71
72
73
74
            // IE seems to wait for all elements on the page to load
            // fully before the onload event goes off.  This means the
            // view editor isn't initialised until all videos have
            // finished loading, and an invalid video URL can stop the
            // editor from loading and result in an uneditable view.

            // Therefore, when this block appears on first load of the
            // view editing page, keep the embed code out of the page
            // initially and add it in after the page has loaded.

75
            $url     = hsc(self::make_video_url($configdata['videoid']));
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

            $embed = '<object width="' . $width . '" height="' . $height . '">';
            $embed .= '<param name="movie" value="' . $url . '"></param>';
            $embed .= '<param name="wmode" value="transparent"></param>';
            $embed .= '<param name="allowscriptaccess" value="never"></param>';
            $embed .= '<embed src="' . $url . '" ';
            $embed .= 'type="application/x-shockwave-flash" wmode="transparent" width="' . $width . '" ';
            $embed .= 'height="' . $height . '" allowscriptaccess="never"></embed></object>';

            $block = $instance->get('id');
            $configuring = $block == param_integer('blockconfig', 0);

            $result .= '<div class="mediaplayer-container center">';
            $result .= '<div id="vid_' . $block . '" class="mediaplayer" style="width: {$width}px; height: {$height}px; margin: 0 auto;">';

            if (!$editing || $configuring) {
                $result .= $embed;
            }

95
            $result .= '</div></div>';
96
97
98
99
100
101

            if ($editing && !$configuring) {
                $result .= '<script>';
                $result .= 'addLoadEvent(function() {$(\'vid_' . $block . "').innerHTML = " . json_encode($embed) . ';});';
                $result .= '</script>';
            }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        }

        return $result;
    }

    public static function has_instance_config() {
        return true;
    }

    public static function instance_config_form($instance) {
        $configdata = $instance->get('configdata');
        return array(
            'videoid' => array(
                'type'  => 'text',
Clare Lenihan's avatar
Clare Lenihan committed
116
                'title' => get_string('videourl','blocktype.externalvideo'),
117
                'description' => get_string('videourldescription2','blocktype.externalvideo') . self::get_html_of_supported_websites(),
118
                'width' => '90%',
119
                'defaultvalue' => isset($configdata['videoid']) ? $configdata['videoid'] : null,
120
121
122
123
124
125
                'rules' => array(
                    'required' => true
                ),
            ),
            'width' => array(
                'type' => 'text',
Clare Lenihan's avatar
Clare Lenihan committed
126
                'title' => get_string('width','blocktype.externalvideo'),
127
                'size' => 3,
128
129
130
131
132
133
134
                'rules' => array(
                    'required' => true,
                    'integer'  => true,
                    'minvalue' => 100,
                    'maxvalue' => 800,
                ),
                'defaultvalue' => (!empty($configdata['width'])) ? $configdata['width'] : self::$default_width,
135
136
137
            ),
            'height' => array(
                'type' => 'text',
Clare Lenihan's avatar
Clare Lenihan committed
138
                'title' => get_string('height','blocktype.externalvideo'),
139
                'size' => 3,
140
141
142
143
144
145
146
                'rules' => array(
                    'required' => true,
                    'integer'  => true,
                    'minvalue' => 100,
                    'maxvalue' => 800,
                ),
                'defaultvalue' => (!empty($configdata['height'])) ? $configdata['height'] : self::$default_height,
147
148
149
150
            ),
        );
    }

151
152
153
154
155
156
157
158
159
    public static function instance_config_validate(Pieform $form, $values) {
        if ($values['videoid']) {
            $urlparts = parse_url($values['videoid']);
            if (empty($urlparts['host'])) {
                $form->set_error('videoid', get_string('invalidurl', 'blocktype.externalvideo'));
            }
        }
    }

160
161
    private static function make_video_url($url) {
        static $embedsources = array(
162
            // www.youtube.com (old style)
163
            array(
164
165
                'match' => '#.*youtube\.com.*(v|(cp))(=|\/)([a-zA-Z0-9_=-]+).*#',
                'url'   => 'http://www.youtube.com/$1/$4'
166
            ),
167
168
169
170
171
            // www.youtube.com (iframe)
            array(
                'match' => '#.*youtube\.com.*(embed\/)([a-zA-Z0-9_=-]+).*#',
                'url' => 'http://www.youtube.com/v/$2'
            ),
172
            // video.google.com
173
174
175
            array(
                'match' => '#.*video.google.com.*docid=(\-?[0-9]+).*#',
                'url'   => 'http://video.google.com/googleplayer.swf?docId=$1',
176
177
178
179
180
181
            ),
            // www.teachertube.com
            array(
                'match' => '#.*teachertube.com/flvideo/([0-9]+)\.flv.*#',
                'url'   => 'http://www.teachertube.com/skin-p/mediaplayer.swf?file=http://www.teachertube.com/flvideo/$1.flv'
            ),
182
183
184
185
            array(
                'match' => '#.*teachertube\.com/viewVideo\.php\?video_id=(\d+).*#',
                'url'   => 'http://www.teachertube.com/embed/player.swf?file=http://www.teachertube.com/embedFLV.php?pg=video_$1'
            ),
Nigel McNie's avatar
Nigel McNie committed
186
            // www.scivee.tv
187
188
189
190
191
192
193
194
            array(
                'match' => '#.*scivee.tv/node/([0-9]+).*#',
                'url'   => 'http://scivee.tv/flash/embedPlayer.swf?id=$1&type=3',
            ),
            array(
                'match' => '#.*scivee.tv.*id=([0-9]+).*#',
                'url'   => 'http://scivee.tv/flash/embedPlayer.swf?id=$1&type=3',
            ),
195
196
197
198
199
200
201
202
203
        );

        foreach ($embedsources as $source) {
            if (preg_match($source['match'], $url)) {
                return preg_replace($source['match'], $source['url'], $url);
            }
        }
        // TODO handle failure case
    }
204
205
206
207
208
209
210

    /**
     * Returns a block of HTML that the external video block can use to list 
     * which video sites are supported.
     */
    private static function get_html_of_supported_websites() {
        return <<<EOF
Evonne Cheung's avatar
Evonne Cheung committed
211
<ul style="list-style-type: none;" class="inlinelist">
212
213
    <li><a href="http://www.youtube.com/" target="_blank"><img src="http://www.youtube.com/favicon.ico" alt="YouTube"> youtube.com</a></li>
    <li><a href="http://video.google.com/" target="_blank"><img src="http://video.google.com/favicon.ico" alt="Google Video"> video.google.com</a></li>
214
    <li><a href="http://www.teachertube.com/" target="_blank"><img src="http://www.teachertube.com/favicon.ico" alt="TeacherTube"> teachertube.com</a></li>
215
    <li><a href="http://www.scivee.tv/" target="_blank"><img src="http://www.scivee.tv/files/favicon.ico" alt="SciVee"> scivee.tv</a></li>
216
</ul>
217
218
EOF;
    }
219
220
221
222
223

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

224
}