-
Notifications
You must be signed in to change notification settings - Fork 7
/
lib.php
393 lines (340 loc) · 14.1 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?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/>.
/**
* repository_opencast class is used to browse opencast files
*
* @package repository_opencast
* @copyright 2017 Andreas Wagner, SYNERGY LEARNING
* @author Andreas Wagner
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
use tool_opencast\local\api;
/**
* repository_opencast class is used to browse opencast files
*
* @package repository_opencast
* @copyright 2017 Andreas Wagner, SYNERGY LEARNING
* @author Andreas Wagner
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_opencast extends repository {
/**
* This method adds a select form and additional information to the settings form..
*
* @param \moodleform $mform Moodle form (passed by reference)
*/
public static function instance_config_form($mform) {
if (!has_capability('moodle/site:config', context_system::instance())) {
$mform->addElement('static', null, '', get_string('nopermissions', 'error', get_string('configplugin',
'repository_opencast')));
return false;
}
$options = [];
foreach (\tool_opencast\local\settings_api::get_ocinstances() as $ocinstance) {
$options[$ocinstance->id] = $ocinstance->name;
}
$mform->addElement('select', 'opencast_instance', get_string('opencastinstance', 'repository_opencast'), $options);
$mform->setType('opencast_instance', PARAM_INT);
$mform->addElement('text', 'opencast_author', get_string('opencastauthor', 'repository_opencast'));
$mform->setType('opencast_author', PARAM_TEXT);
$mform->addElement('text', 'opencast_channelid', get_string('opencastchannelid', 'repository_opencast'));
$mform->setType('opencast_channelid', PARAM_TEXT);
$mform->addHelpButton('opencast_channelid', 'opencastchannelid', 'repository_opencast');
$mform->addRule('opencast_channelid', get_string('required'), 'required', null, 'client');
$mform->addElement('text', 'opencast_thumbnailflavor', get_string('opencastthumbnailflavor', 'repository_opencast'));
$mform->setType('opencast_thumbnailflavor', PARAM_TEXT);
$mform->addHelpButton('opencast_thumbnailflavor', 'opencastthumbnailflavor', 'repository_opencast');
$mform->addElement('text', 'opencast_thumbnailflavorfallback',
get_string('opencastthumbnailflavorfallback', 'repository_opencast'));
$mform->setType('opencast_thumbnailflavorfallback', PARAM_TEXT);
$mform->addHelpButton('opencast_thumbnailflavorfallback', 'opencastthumbnailflavorfallback', 'repository_opencast');
$mform->addElement('checkbox', 'opencast_playerurl', get_string('opencastplayerurl', 'repository_opencast'));
$mform->setType('opencast_playerurl', PARAM_BOOL);
$mform->addHelpButton('opencast_playerurl', 'opencastplayerurl', 'repository_opencast');
$mform->addElement('text', 'opencast_videoflavor', get_string('opencastvideoflavor', 'repository_opencast'));
$mform->setType('opencast_videoflavor', PARAM_TEXT);
$mform->addHelpButton('opencast_videoflavor', 'opencastvideoflavor', 'repository_opencast');
}
/**
* Save settings for repository instance
*
* @param array $options settings
* @return bool
*/
public function set_option($options = []) {
$options['opencast_instance'] = clean_param($options['opencast_instance'], PARAM_INT);
$options['opencast_author'] = clean_param($options['opencast_author'], PARAM_TEXT);
$options['opencast_channelid'] = clean_param($options['opencast_channelid'], PARAM_TEXT);
$options['opencast_playerurl'] = clean_param($options['opencast_playerurl'], PARAM_BOOL);
$options['opencast_thumbnailflavor'] = clean_param($options['opencast_thumbnailflavor'], PARAM_TEXT);
$options['opencast_thumbnailflavorfallback'] = clean_param($options['opencast_thumbnailflavorfallback'], PARAM_TEXT);
$options['opencast_videoflavor'] = clean_param($options['opencast_videoflavor'], PARAM_TEXT);
$ret = parent::set_option($options);
return $ret;
}
/**
* Names of the plugin settings
*
* @return array
*/
public static function get_instance_option_names() {
$instanceoptions = [];
$instanceoptions[] = 'opencast_instance';
$instanceoptions[] = 'opencast_author';
$instanceoptions[] = 'opencast_channelid';
$instanceoptions[] = 'opencast_playerurl';
$instanceoptions[] = 'opencast_thumbnailflavor';
$instanceoptions[] = 'opencast_thumbnailflavorfallback';
$instanceoptions[] = 'opencast_videoflavor';
return $instanceoptions;
}
/**
* Get channel id for this repository type.
* @return string
*/
private function get_channelid() {
return self::get_option('opencast_channelid', 'api');
}
/**
* Get name of author for this repository type.
* @return string
*/
private function get_author() {
return self::get_option('opencast_author');
}
/**
* Get Opencast instance id to which this repository belongs.
* @return string
*/
private function get_ocinstance() {
return self::get_option('opencast_instance');
}
/**
* Select the url for the video based on configuration of preferred flavors.
*
* @param object $publication
* @param object $video
* @return string
*/
private function add_video_thumbnail_url($publication, $video) {
// Try to find a thumbnail url based on configuration.
$thumbnailflavor = self::get_option('opencast_thumbnailflavor');
if (!empty($thumbnailflavor)) {
foreach ($publication->attachments as $attachment) {
if ($attachment->flavor === $thumbnailflavor) {
$video->thumbnail = $attachment->url;
return true;
}
}
}
// Try fallback.
$thumbnailflavorfallback = self::get_option('opencast_thumbnailflavorfallback');
if (!empty($thumbnailflavor)) {
foreach ($publication->attachments as $attachment) {
if ($attachment->flavor === $thumbnailflavorfallback) {
$video->thumbnail = $attachment->url;
return true;
}
}
}
// Automatically try to find the best preview image:
// presentation/search+preview > presenter/search+preview > any other preview.
foreach ($publication->attachments as $attachment) {
if (!empty($attachment->url) && strpos($attachment->flavor, '+preview') > 0) {
if (empty($video->url) || strpos($attachment->flavor, '/search+preview') > 0) {
$video->thumbnail = $attachment->url;
if ($attachment->flavor === 'presentation/search+preview') {
return true;
}
}
}
}
return false;
}
/**
* Select the video url and title.
*
* @param object $publication
* @param object $video
* @return bool
*/
private function add_video_url_and_title($publication, $video) {
// Try to find a video by preferred configuration.
$videoflavor = self::get_option('opencast_videoflavor');
if (!empty($videoflavor)) {
foreach ($publication->media as $media) {
if (!empty($media->has_video) && ($media->flavor === $videoflavor)) {
$video->url = $media->url;
// Check mimetype needed for embedding in moodle.
$ending = pathinfo($media->url, PATHINFO_EXTENSION);
$existingending = pathinfo($video->title, PATHINFO_EXTENSION);
if ($ending !== $existingending) {
$video->title .= '.' . $ending;
}
return true;
}
}
}
// Automatically find a suitable video.
foreach ($publication->media as $media) {
if (!empty($media->has_video)) {
$video->url = $media->url;
// Check mimetype needed for embedding in moodle.
$ending = pathinfo($media->url, PATHINFO_EXTENSION);
$existingending = pathinfo($video->title, PATHINFO_EXTENSION);
if ($ending !== $existingending) {
$video->title .= '.' . $ending;
}
return true;
}
}
return false;
}
/**
* Add data from opencast to the list items.
*
* @param int $ocinstanceid
* @param object $video
* @return boolean true, when it is a valid video published for external api.
*/
private function add_video_published_data($ocinstanceid, $video) {
$channelid = $this->get_channelid();
$published = (count($video->publication_status) > 0 && (in_array($channelid, $video->publication_status)));
if (!$published) {
return false;
}
if (empty($video->publications)) {
return false;
}
$useplayerurl = self::get_option('opencast_playerurl');
foreach ($video->publications as $publication) {
if ($publication->channel == $channelid) {
// Add a suitable thumbnail url.
$this->add_video_thumbnail_url($publication, $video);
// Add a url to video.
if ($useplayerurl) {
$video->url = $publication->url;
return true;
} else {
return $publication->media && $this->add_video_url_and_title($publication, $video);
}
}
}
return false;
}
/**
* Get all the videos published via API for this course
*
* @param int $courseid
* @return array video object suitable for repository listing.
*/
private function get_course_videos($courseid) {
$publishedvideos = [];
// Get all videos from all series.
$ocinstanceid = $this->get_ocinstance();
$videos = [];
foreach (\tool_opencast\seriesmapping::get_records(['courseid' => $courseid,
'ocinstanceid' => $ocinstanceid, ]) as $mapping) {
if (!$mapping || !($seriesid = $mapping->get('series'))) {
continue;
}
$seriesfilter = "series:" . $seriesid;
$query = '/api/events?sign=true&withmetadata=false&withpublications=true&filter=' . urlencode($seriesfilter);
try {
$api = new api($ocinstanceid);
$seriesvideos = $api->oc_get($query);
$seriesvideos = json_decode($seriesvideos);
$videos = array_merge($videos, $seriesvideos);
} catch (\moodle_exception $e) {
continue;
}
}
foreach ($videos as $video) {
if ($this->add_video_published_data($ocinstanceid, $video)) {
$publishedvideos[] = $video;
}
}
return $publishedvideos;
}
/**
* Get file listing
*
* @param string $encodedpath
* @param string $page no paging is used in repository_opencast
* @return mixed
*/
public function get_listing($encodedpath = '', $page = '') {
global $CFG;
require_once($CFG->dirroot . '/lib/accesslib.php');
list($context, $course, $cm) = get_context_info_array($this->context->id);
$videos = $this->get_course_videos($course->id);
$ret = [];
$ret['dynload'] = false;
$ret['nosearch'] = true;
$ret['nologin'] = true;
$ret['list'] = [];
foreach ($videos as $video) {
$listitem = [];
$listitem['title'] = $video->title;
if (!file_extension_in_typegroup($video->title, $this->supported_filetypes())) {
$listitem['title'] = $video->title . '.mp4';
}
$listitem['date'] = strtotime($video->start);
$listitem['thumbnail'] = $video->thumbnail;
$listitem['url'] = $video->url;
$listitem['source'] = $video->url;
$listitem['author'] = (!empty($video->creator)) ? $video->creator : $this->get_author();
$ret['list'][] = $listitem;
}
return $ret;
}
/**
* Supported returntypes of the repository.
* @return int
*/
public function supported_returntypes() {
return FILE_EXTERNAL;
}
/**
* Supported filetypes of the repository.
* @return string[]
*/
public function supported_filetypes() {
return ['video'];
}
/**
* Prepares data for the php unit test.
* @param array $publications
* @param object $video
* @return mixed
*/
public function phpu_adapter_test_listing($publications, $video) {
$channelid = $this->get_channelid();
foreach ($publications as $publication) {
if ($publication->channel == $channelid) {
// Add a suitable thumbnail url.
$this->add_video_thumbnail_url($publication, $video);
// Add a url to video.
if ($publication->media) {
$this->add_video_url_and_title($publication, $video);
}
}
}
return $video;
}
}