Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Main class for plugin 'media_vimeo'
19
 *
20
 * @package   media_vimeo
21
 * @copyright 2016 Marina Glancy
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
/**
28
 * Player that embeds Vimeo links.
29
 *
30
 * @package   media_vimeo
31
 * @copyright 2016 Marina Glancy
32
 * @author    2011 The Open University
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class media_vimeo_plugin extends core_media_player_external {
36
    protected function embed_external(moodle_url $url, $name, $width, $height, $options) {
37
        global $OUTPUT;
38
 
39
        $donottrack = get_config('media_vimeo', 'donottrack');
40
        $videoid = $this->get_video_id();
41
        $info = s($name);
42
        $params = [];
43
 
44
        // Note: resizing via url is not supported, user can click the fullscreen
45
        // button instead. iframe embedding is not xhtml strict but it is the only
46
        // option that seems to work on most devices.
47
        self::pick_video_size($width, $height);
48
 
49
        // Add do not track parameter.
50
        if ($donottrack) {
51
            $params['dnt'] = 1;
52
        }
53
 
54
        $embedurl = new moodle_url("https://player.vimeo.com/video/$videoid", $params);
55
        // Template context.
56
        $context = [
57
            'width' => $width,
58
            'height' => $height,
59
            'title' => $info,
60
            'embedurl' => $embedurl->out(false),
61
        ];
62
 
63
        // Return the rendered template.
64
        return $OUTPUT->render_from_template('media_vimeo/embed', $context);
65
    }
66
 
67
    /**
68
     * Get Vimeo video ID.
69
     * @return string
70
     */
71
    protected function get_video_id(): string {
72
        return $this->get_video_id_with_code() ?? $this->matches[1] ?? '';
73
    }
74
 
75
    /**
76
     * Get video id with code.
77
     * @return string|null If NULL then the URL does not contain the code.
78
     */
79
    protected function get_video_id_with_code(): ?string {
80
        $id = $this->matches[2] ?? null;
81
 
82
        if (!empty($id)) {
83
            $code = $this->matches[3] ?? null;
84
            if (!empty($code)) {
85
                return "{$id}?h={$code}";
86
            }
87
 
88
            return $id;
89
        }
90
 
91
        return null;
92
    }
93
 
94
    /**
95
     * Returns regular expression to match vimeo URLs.
96
     * @return string
97
     */
98
    protected function get_regex() {
99
        // Initial part of link.
100
        $start = '~^https?://vimeo\.com/';
101
        // Middle bit: either 123456789 or 123456789/abdef12345.
102
        $middle = '(([0-9]+)/([0-9a-f]+)|[0-9]+)';
103
        return $start . $middle . core_media_player_external::END_LINK_REGEX_PART;
104
    }
105
 
106
    public function get_embeddable_markers() {
107
        return array('vimeo.com/');
108
    }
109
 
110
    /**
111
     * Default rank
112
     * @return int
113
     */
114
    public function get_rank() {
115
        return 1010;
116
    }
117
}