-
Notifications
You must be signed in to change notification settings - Fork 20
/
OCCourseModel.class.php
505 lines (424 loc) · 16.8 KB
/
OCCourseModel.class.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?php
use Opencast\Models\OCSeminarWorkflowConfiguration;
use Opencast\Models\OCSeminarSeries;
class OCCourseModel
{
/**
* This is the maximum number of seconds that unread entries are
* marked as new.
*/
const LAST_VISIT_MAX = 7776000; // 90 days
/**
* @param $course_id
*/
public function __construct($course_id)
{
if (!$course_id) {
throw new Exception('Missing course-id!');
}
$this->setCourseID($course_id);
// take care of connected series
$cseries = OCSeminarSeries::getSeries($this->getCourseID());
if (!empty($cseries)) {
$current_seriesdata = array_pop($cseries);
$this->setSeriesMetadata($current_seriesdata);
$this->setSeriesID($current_seriesdata['series_id']);
} else {
$this->setSeriesID(false);
}
}
public function getCourseID()
{
return $this->course_id;
}
public function getSeriesID()
{
return $this->series_id;
}
public function getSeriesMetadata()
{
return $this->seriesMetadata;
}
public function setSeriesID($series_id)
{
$this->series_id = $series_id;
}
public function setCourseID($course_id)
{
$this->course_id = $course_id;
}
public function setSeriesMetadata($seriesMetadata)
{
$this->seriesMetadata = $seriesMetadata;
}
/* */
public function getEpisodes($force_reload = false, $unset_live = false)
{
if ($this->getSeriesID()) {
$search_client = SearchClient::create($this->getCourseID());
$course = Course::find($this->getCourseID());
$role = '';
if (OCPerm::editAllowed($course->id)) {
$role = 'Instructor';
} else if ($GLOBALS['perm']->have_studip_perm('autor', $course->id)) {
$role = 'Learner';
}
$series = $search_client->getEpisodes($this->getSeriesID(), $this->getCourseID(), [$role]);
$stored_episodes = OCModel::getCoursePositions($this->getCourseID());
$ordered_episodes = [];
//check if series' episodes is already stored in studip
if (!empty($series)) {
// add additional episode metadata from opencast
$ordered_episodes = $this->episodeComparison($stored_episodes, $series);
}
if ($unset_live) {
$oc_events = ApiEventsClient::create($this->getCourseID());
$events = $oc_events->getEpisodes(OCSeminarSeries::getSeries($this->getCourseID()));
foreach ($ordered_episodes as $episode) {
if ($events[$episode['id']]->publication_status[0] == 'engage-live')
{
unset($ordered_episodes[$episode['id']]);
}
}
}
// Get the Sort order title = TITLE, start = DATE_PUBLISHED, mkdata = DATE_CREATED (?)
if ($_SESSION['opencast']['sort_order']) {
$sort_str = $_SESSION['opencast']['sort_order'];
}
else if (CourseConfig::get($this->getCourseID())->COURSE_SORT_ORDER) {
$sort_str = CourseConfig::get($this->getCourseID())->COURSE_SORT_ORDER;
}
else {
$sort_str = 'mkdate1';
}
$sort = substr($sort_str, 0, -1);
$reversed = boolval(substr($sort_str, -1));
return $this->order_episodes_by(
[$sort],
[SORT_NATURAL],
[$reversed],
$ordered_episodes
);
} else {
return false;
}
}
/**
* This function sorts an array 'deep'. This means the content is first sorted
* by the first key in the array. If there are more than one entry for this key
* the episodes in this group are sorted by the second key and so on.
*
* @param $keys
* @param $sort_flags
* @param $reversed
* @param $episodes
*
* @return array
*/
private function order_episodes_by($keys, $sort_flags, $reversed, $episodes)
{
$ordered = [];
//Get the current settings for this episode group
$key = array_shift($keys);
$current_reversed = array_shift($reversed);
$current_sort_flags = array_shift($sort_flags);
//Regroup the current episodes bv the key field
foreach ($episodes as $episode) {
$ordered[$episode[$key]][] = $episode;
}
//Sort, reverse if needed
if ($current_reversed) {
krsort($ordered, $current_sort_flags);
} else {
ksort($ordered, $current_sort_flags);
}
//Now remove the grouping but contain the order within
$episodes = [];
foreach ($ordered as $entries) {
if (count($keys) > 0 && count($entries) > 1) {
$entries = $this->order_episodes_by($keys, $sort_flags, $reversed, $entries);
}
foreach ($entries as $entry) {
$episodes[] = $entry;
}
}
//Return really ordered list of episodes
return $episodes;
}
private function episodeComparison($stored_episodes, $remote_episodes)
{
$episodes = [];
$oc_episodes = $this->prepareEpisodes($remote_episodes);
$vis_conf = !is_null(CourseConfig::get($this->course_id)->COURSE_HIDE_EPISODES)
? boolval(CourseConfig::get($this->course_id)->COURSE_HIDE_EPISODES)
: \Config::get()->OPENCAST_HIDE_EPISODES;
$vis = $vis_conf
? 'invisible'
: 'visible';
foreach ($stored_episodes as $key => $stored_episode) {
if ($tmp = $oc_episodes[$stored_episode['episode_id']]) {
$tmp['visibility'] = $stored_episode['visible'];
$tmp['is_retracting'] = $stored_episode['is_retracting'];
$tmp['mkdate'] = $stored_episode['mkdate'];
OCModel::setEpisode(
$stored_episode['episode_id'],
$stored_episode['series_id'],
$this->getCourseID(),
$tmp['visibility'],
$tmp['is_retracting']
);
$episodes[] = $tmp;
unset($oc_episodes[$stored_episode['episode_id']]);
unset($stored_episodes[$key]);
}
}
//add new episodes
if (!empty($oc_episodes)) {
$timestamp = time();
foreach ($oc_episodes as $episode) {
$lastpos++;
$episode['visibility'] = $vis;
$episode['mkdate'] = $timestamp;
OCModel::setEpisode(
$episode['id'],
$episode['series_id'],
$this->getCourseID(),
$vis,
false
);
$episodes[] = $episode;
NotificationCenter::postNotification('NewEpisodeForCourse', [
'episode_id' => $episode['id'],
'course_id' => $this->getCourseID(),
'episode_title' => $episode['title']
]);
}
}
// removed orphaned episodes
if (!empty($stored_episodes)) {
foreach ($stored_episodes as $orphaned_episode) {
// todo log event for this action
OCModel::removeStoredEpisode(
$orphaned_episode['episode_id']
);
}
}
return $episodes;
}
private function prepareEpisodes($oc_episodes)
{
$episodes = [];
if (is_object($oc_episodes)) {
$oc_episodes = [$oc_episodes];
}
if (is_array($oc_episodes)) foreach ($oc_episodes as $episode) {
if (is_object($episode->mediapackage)) {
$presentation_preview = false;
$preview = false;
$presenter_download = [];
$presentation_download = [];
$audio_download = [];
foreach ((array) $episode->mediapackage->attachments->attachment as $attachment) {
if ($attachment->type === "presenter/search+preview") {
$preview = $attachment->url;
}
if ($attachment->type === "presentation/player+preview") {
$presentation_preview = $attachment->url;
}
}
$tracks = (@sizeof($episode->mediapackage->media->track) > 1)
? $episode->mediapackage->media->track
: [$episode->mediapackage->media->track];
foreach ($tracks as $track) {
$parsed_url = parse_url($track->url);
if ($track->type === 'presenter/delivery') {
if (($track->mimetype === 'video/mp4' || $track->mimetype === 'video/avi') && ((in_array('atom', $track->tags->tag) || in_array('engage-download', $track->tags->tag)) && $parsed_url['scheme'] != 'rtmp' && $parsed_url['scheme'] != 'rtmps') && !empty($track->video)) {
$quality = $this->calculate_size(
$track->video->bitrate,
$track->duration
);
$presenter_download[$quality] = [
'url' => $track->url,
'info' => $this->add_px_to_resolution($track->video->resolution)
];
}
if (in_array($track->mimetype, ['audio/aac', 'audio/mp3', 'audio/mpeg', 'audio/m4a', 'audio/ogg', 'audio/opus']) && !empty($track->audio)) {
$quality = $this->calculate_size(
$track->audio->bitrate,
$track->duration
);
$audio_download[$quality] = [
'url' => $track->url,
'info' => round($track->audio->bitrate / 1000, 1) . 'kb/s, ' . explode('/', $track->mimetype)[1]
];
}
}
if ($track->type === 'presentation/delivery' && (($track->mimetype === 'video/mp4' || $track->mimetype === 'video/avi') && ((in_array('atom', $track->tags->tag) || in_array('engage-download', $track->tags->tag)) && $parsed_url['scheme'] != 'rtmp' && $parsed_url['scheme'] != 'rtmps') && !empty($track->video))) {
$quality = $this->calculate_size(
$track->video->bitrate,
$track->duration
);
$presentation_download[$quality] = [
'url' => $track->url,
'info' => $this->add_px_to_resolution($track->video->resolution)
];
}
}
ksort($presenter_download);
ksort($presentation_download);
ksort($audio_download);
$episodes[$episode->id] = [
'id' => $episode->id,
'series_id' => $episode->dcIsPartOf,
'title' => $episode->dcTitle,
'start' => $episode->mediapackage->start,
'duration' => $episode->mediapackage->duration,
'description' => $episode->dcDescription,
'author' => $episode->dcCreator,
'preview' => $preview,
'presentation_preview' => $presentation_preview,
'presenter_download' => $presenter_download,
'presentation_download' => $presentation_download,
'audio_download' => $audio_download,
];
}
}
return $episodes;
}
/**
* return number of new episodes since last visit up to 3 month ago
*
* @param string $visitdate count all entries newer than this timestamp
*
* @return int the number of entries
*/
public function getCount($visitdate)
{
if ($visitdate < time() - OCCourseModel::LAST_VISIT_MAX) {
$visitdate = time() - OCCourseModel::LAST_VISIT_MAX;
}
$stmt = DBManager::get()->prepare("SELECT COUNT(*) FROM oc_seminar_episodes
WHERE seminar_id = :seminar_id AND oc_seminar_episodes.mkdate > :lastvisit");
$stmt->bindParam(':seminar_id', $this->getCourseID());
$stmt->bindParam(':lastvisit', $visitdate);
$stmt->execute();
return $stmt->fetchColumn();
}
public function getEpisodesforREST()
{
$rest_episodes = [];
$is_dozent = $GLOBALS['perm']->have_studip_perm('autor', $this->course_id);
$episodes = $this->getEpisodes();
foreach ($episodes as $episode) {
if ($episode['visibility'] == 'true') {
$rest_episodes[] = $episode;
} else {
if ($is_dozent) {
$rest_episodes[] = $episode;
}
}
}
return $rest_episodes;
}
public function toggleSeriesVisibility()
{
if ($this->getSeriesVisibility() == 'visible') {
$visibility = 'invisible';
} else {
$visibility = 'visible';
}
return OCSeriesModel::updateVisibility($this->course_id, $visibility);
}
/**
* Toggle schedule flag for current series
*
* @return bool true on success, false on failure
*/
public function toggleSeriesSchedule()
{
$series = $this->getSeriesMetadata();
return OCSeriesModel::updateSchedule(
$this->course_id, $series['schedule'] ? 0 : 1
);
}
public function getSeriesVisibility()
{
$visibility = OCSeriesModel::getVisibility($this->course_id);
return $visibility['visibility'];
}
/**
* refine the list of episodes wrt. the visibility of an episode
*
* @param array $ordered_episodes list of all episodes for the given course
*
* @return array episodes refined list of episodes - only visible episodes are considered
*/
public function refineEpisodesForStudents($ordered_episodes)
{
$episodes = [];
foreach ($ordered_episodes as $episode) {
if ($episode['visibility'] != 'invisible') {
$episodes[] = $episode;
}
}
return $episodes;
}
public function getWorkflow($target)
{
$workflow = static::getWorkflowWithCustomCourseID($this->getCourseID(), $target);
if (!$workflow) {
$workflow = static::getWorkflowWithCustomCourseID('default_workflow', $target);
}
return $workflow;
}
public static function getWorkflowWithCustomCourseID($course_id, $target)
{
return OCSeminarWorkflowConfiguration::findOneBySql(
'seminar_id = ? AND target = ?',
[$course_id, $target]
);
}
public function setWorkflow($workflow_id, $target)
{
return static::setWorkflowWithCustomCourseID(
$this->getCourseID(), $workflow_id, $target
);
}
public static function setWorkflowWithCustomCourseID($course_id, $workflow_id, $target)
{
if (!$ocw = OCSeminarWorkflowConfiguration::findOneBySql(
'seminar_id = ? AND target = ?',
[$course_id, $target]
)) {
$ocw = new OCSeminarWorkflowConfiguration();
}
$ocw->setData([
'seminar_id' => $course_id,
'workflow_id' => $workflow_id,
'target' => $target
]);
return $ocw->store();
}
public static function removeWorkflowsWithoutCustomCourseID($course_id, $target)
{
return OCSeminarWorkflowConfiguration::deleteBySql(
'seminar_id = ? AND target = ?',
[$course_id, $target]
);
}
public function setWorkflowForDate($termin_id, $workflow_id)
{
$stmt = DBManager::get()->prepare("UPDATE
oc_scheduled_recordings SET workflow_id = ?
WHERE seminar_id = ? AND date_id = ?");
return $stmt->execute([$workflow_id, $this->getCourseID(), $termin_id]);
}
private function calculate_size($bitrate, $duration)
{
return ($bitrate / 8) * ($duration / 1000);
}
private function add_px_to_resolution($resolution)
{
return $resolution . 'px';
}
}