/
lib.php
126 lines (113 loc) · 4.02 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
65
66
67
68
69
70
71
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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Media player that generates the HTML embed code to embed support file types
*
* Only local PDFs, ODTs, ODPs, ODSs are supported.
*
* @package filter_viewerjs
* @copyright 2015 Abir Viqar
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/medialib.php');
class filter_viewerjs_media_player extends core_media_player {
/**
* Generates code required to embed the player.
*
* The url contained in $urls must be a local file.
*
* Unlike other core_media_players, the $urls array should only contain
* a single url and the $options array is ignored.
*
* On failure, the function returns an empty string
*
* @see core_media_player::embed
* @param array $urls URL of media file
* @param string $name Display name; '' to use default
* @param int $width Optional width; 0 to use default
* @param int $height Optional height; 0 to use default
* @param array $options Options array
* @return string HTML code for embed
*/
public function embed($urls, $name, $width, $height, $options) {
global $CFG;
// don't expect alternative urls
if (count($urls) !== 1) {
return '';
}
$file_url = new moodle_url($urls[0]);
$viewerjs_player_url = new moodle_url('/filter/viewerjs/lib/viewerjs');
// we assume the filter/viewerjs/lib/viewerjs directory will be four directories away from the initial public directory
$viewerjs_player_url->set_anchor('../../../..' . $file_url->out_as_local_url());
if (!$width) {
$width = '100%';
}
if (!$height) {
$height = 500;
}
$output = html_writer::tag('iframe', '', array(
'src' => $viewerjs_player_url->out(),
'width' => $width,
'height' => $height,
'webkitallowfullscreen' => 'webkitallowfullscreen',
'mozallowfullscreen' => 'mozallowfullscreen',
'allowfullscreen' => 'allowfullscreen'
));
return $output;
}
public function get_supported_extensions() {
return array(
'pdf',
'ods',
'odp',
'odt'
);
}
/**
* Given a list of URLs, returns a reduced array containing only those URLs
* which are supported by this player.
*
* This media player only supports local urls.
*
* @param array $urls Array of moodle_url
* @param array $options Options (same as will be passed to embed)
* @return array Array of supported moodle_url
*/
public function list_supported_urls(array $urls, array $options = array()) {
$extensions = $this->get_supported_extensions();
$result = array();
foreach ($urls as $url) {
try {
$url->out_as_local_url();
}
catch (coding_exception $e) {
continue;
}
if (in_array(core_media::get_extension($url), $extensions)) {
$result[] = $url;
}
}
return $result;
}
public function get_rank() {
return 0;
}
public function is_enabled() {
return true;
}
}