Skip to content

Commit 2f65a65

Browse files
authored
fixes #1198 (#1199)
1 parent 878d083 commit 2f65a65

File tree

5 files changed

+113
-92
lines changed

5 files changed

+113
-92
lines changed

lib/Models/Playlists.php

+90
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,96 @@ public function delete()
549549
return parent::delete();
550550
}
551551

552+
public function addEntries(Array $videos)
553+
{
554+
$playlist_client = ApiPlaylistsClient::getInstance($this->config_id);
555+
$oc_playlist = $playlist_client->getPlaylist($this->service_playlist_id);
556+
557+
if (!$oc_playlist) {
558+
// something went wrong with playlist creation, try again
559+
$oc_playlist = $playlist_client->createPlaylist([
560+
'title' => $this->title,
561+
'description' => $this->description,
562+
'creator' => $this->creator,
563+
'accessControlEntries' => []
564+
]);
565+
566+
if (!$oc_playlist) {
567+
throw new Error(_('Wiedergabeliste konnte nicht zu Opencast hinzugefügt werden!'), 500);
568+
}
569+
570+
$this->service_playlist_id = $oc_playlist->id;
571+
$this->store();
572+
}
573+
574+
$entries = $oc_playlist->entries;
575+
576+
foreach ($videos as $video) {
577+
if (!$video->episode) continue;
578+
579+
// Only add video if not contained in entries
580+
$entry_exists = current(array_filter($entries, function($e) use ($video) {
581+
return $e->contentId === $video->episode;
582+
}));
583+
584+
if (!$entry_exists) {
585+
$entries[] = (object) [
586+
'contentId' => $video->episode,
587+
'type' => 'EVENT'
588+
];
589+
}
590+
}
591+
592+
// Update videos in playlist of Opencast
593+
$oc_playlist = $playlist_client->updateEntries($oc_playlist->id, $entries);
594+
if (!$oc_playlist) {
595+
throw new Error(_('Die Videos konnten nicht hinzugefügt werden.'), 500);
596+
}
597+
598+
// Update playlist videos in DB
599+
$this->setEntries($oc_playlist->entries);
600+
}
601+
602+
public function removeEntries(Array $videos)
603+
{
604+
// Get playlist entries from Opencast
605+
$playlist_client = ApiPlaylistsClient::getInstance($this->config_id);
606+
$oc_playlist = $playlist_client->getPlaylist($this->service_playlist_id);
607+
608+
$old_entries = (array)$oc_playlist->entries;
609+
$entries = (array)$oc_playlist->entries;
610+
611+
foreach ($videos as $video) {
612+
613+
// Prevent removing video from playlist when it is livestream.
614+
if ((bool) $video->is_livestream) {
615+
continue;
616+
// return $this->createResponse([
617+
// 'message' => [
618+
// 'type' => 'error',
619+
// 'text' => _('Entfernung eines Livestream-Videos aus der Wiedergabeliste ist nicht erlaubt.')
620+
// ],
621+
// ], $response->withStatus(403));
622+
}
623+
624+
// Remove all occurrences of video from entries
625+
$entries = array_values(array_filter($entries, function ($entry) use ($video) {
626+
return $entry->contentId !== $video->episode;
627+
}));
628+
}
629+
630+
if (count($entries) < count($old_entries)) {
631+
// Remove videos in playlist of Opencast
632+
$oc_playlist = $playlist_client->updateEntries($oc_playlist->id, $entries);
633+
if (!$oc_playlist) {
634+
throw new Error(_('Die Videos konnten nicht entfernt werden.'), 500);
635+
}
636+
}
637+
638+
// Update playlist videos in DB
639+
$this->setEntries((array)$oc_playlist->entries);
640+
}
641+
552642
/**
553643
* Set playlist videos in playlist based on passed entries.
554644
* This function checks no permissions.

lib/Models/Videos.php

+3
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,9 @@ public static function addToCoursePlaylist($eventType, $episode, $video)
10981098
$pvideo->video_id = $video->id;
10991099
$pvideo->playlist_id = $playlist->id;
11001100
$pvideo->store();
1101+
1102+
// update playlist in opencast as well
1103+
$playlist->addEntries([$video]);
11011104
}
11021105
}
11031106
}

lib/Routes/Playlist/PlaylistAddVideos.php

+9-52
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function __invoke(Request $request, Response $response, $args)
3131
return Videos::findOneByToken($token);
3232
}, $video_tokens);
3333

34+
/* Permission check */
35+
foreach ($videos as $video) {
36+
// check what permissions the current user has on the playlist and video
37+
if (!Authority::canAddVideoToPlaylist($user, $playlist, $video, $course_id)) {
38+
throw new \AccessDeniedException();
39+
}
40+
}
41+
3442
if (!PlaylistMigration::isConverted()) {
3543
foreach ($videos as $video) {
3644
$plvideo = new PlaylistVideos;
@@ -52,58 +60,7 @@ public function __invoke(Request $request, Response $response, $args)
5260
return $response->withStatus(204);
5361
}
5462

55-
// Get playlist entries from Opencast
56-
$playlist_client = ApiPlaylistsClient::getInstance($playlist->config_id);
57-
$oc_playlist = $playlist_client->getPlaylist($playlist->service_playlist_id);
58-
59-
if (!$oc_playlist) {
60-
// something went wrong with playlist creation, try again
61-
$oc_playlist = $playlist_client->createPlaylist([
62-
'title' => $playlist['title'],
63-
'description' => $playlist['description'],
64-
'creator' => $playlist['creator'],
65-
'accessControlEntries' => []
66-
]);
67-
68-
if (!$oc_playlist) {
69-
throw new Error(_('Wiedergabeliste konnte nicht zu Opencast hinzugefügt werden!'), 500);
70-
}
71-
72-
$playlist->service_playlist_id = $oc_playlist->id;
73-
$playlist->store();
74-
}
75-
76-
$entries = $oc_playlist->entries;
77-
78-
foreach ($videos as $video) {
79-
// check what permissions the current user has on the playlist and video
80-
if (!Authority::canAddVideoToPlaylist($user, $playlist, $video, $course_id)) {
81-
throw new \AccessDeniedException();
82-
}
83-
84-
if (!$video->episode) continue;
85-
86-
// Only add video if not contained in entries
87-
$entry_exists = current(array_filter($entries, function($e) use ($video) {
88-
return $e->contentId === $video->episode;
89-
}));
90-
91-
if (!$entry_exists) {
92-
$entries[] = (object) [
93-
'contentId' => $video->episode,
94-
'type' => 'EVENT'
95-
];
96-
}
97-
}
98-
99-
// Update videos in playlist of Opencast
100-
$oc_playlist = $playlist_client->updateEntries($oc_playlist->id, $entries);
101-
if (!$oc_playlist) {
102-
throw new Error(_('Die Videos konnten nicht hinzugefügt werden.'), 500);
103-
}
104-
105-
// Update playlist videos in DB
106-
$playlist->setEntries($oc_playlist->entries);
63+
$playlist->addEntries($videos);
10764

10865
return $response->withStatus(204);
10966
}

lib/Routes/Playlist/PlaylistRemoveVideos.php

+9-38
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ public function __invoke(Request $request, Response $response, $args)
2929

3030
$videos = array_map(function ($token) { return Videos::findOneByToken($token); }, $video_tokens);
3131

32+
/* Permission check */
33+
foreach ($videos as $video) {
34+
// check what permissions the current user has on the playlist and video
35+
if (!Authority::canAddVideoToPlaylist($user, $playlist, $video, $course_id)) {
36+
throw new \AccessDeniedException();
37+
}
38+
}
39+
3240
if (!PlaylistMigration::isConverted()) {
3341
foreach ($videos as $video) {
3442

@@ -47,44 +55,7 @@ public function __invoke(Request $request, Response $response, $args)
4755
return $response->withStatus(204);
4856
}
4957

50-
// Get playlist entries from Opencast
51-
$playlist_client = ApiPlaylistsClient::getInstance($playlist->config_id);
52-
$oc_playlist = $playlist_client->getPlaylist($playlist->service_playlist_id);
53-
54-
$old_entries = (array)$oc_playlist->entries;
55-
$entries = (array)$oc_playlist->entries;
56-
57-
foreach ($videos as $video) {
58-
if (!Authority::canAddVideoToPlaylist($user, $playlist, $video, $course_id)) {
59-
throw new \AccessDeniedException();
60-
}
61-
62-
// Prevent removing video from playlist when it is livestream.
63-
if ((bool) $video->is_livestream) {
64-
return $this->createResponse([
65-
'message' => [
66-
'type' => 'error',
67-
'text' => _('Entfernung eines Livestream-Videos aus der Wiedergabeliste ist nicht erlaubt.')
68-
],
69-
], $response->withStatus(403));
70-
}
71-
72-
// Remove all occurrences of video from entries
73-
$entries = array_values(array_filter($entries, function ($entry) use ($video) {
74-
return $entry->contentId !== $video->episode;
75-
}));
76-
}
77-
78-
if (count($entries) < count($old_entries)) {
79-
// Remove videos in playlist of Opencast
80-
$oc_playlist = $playlist_client->updateEntries($oc_playlist->id, $entries);
81-
if (!$oc_playlist) {
82-
throw new Error(_('Die Videos konnten nicht entfernt werden.'), 500);
83-
}
84-
}
85-
86-
// Update playlist videos in DB
87-
$playlist->setEntries((array)$oc_playlist->entries);
58+
$playlist->removeEntries($videos);
8859

8960
return $response->withStatus(204);
9061
}

migrations/099_add_playlist_availability.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public function up()
1111
{
1212
$db = DBManager::get();
1313

14-
$stmt = $db->exec('ALTER TABLE oc_playlist_video
14+
$db->exec('ALTER TABLE oc_playlist_video
1515
ADD COLUMN `available` INT NOT NULL DEFAULT 1 AFTER video_id');
1616

17-
$stmt = $db->exec("ALTER TABLE oc_video_sync
17+
$db->exec("ALTER TABLE oc_video_sync
1818
ADD COLUMN `type` ENUM('video', 'playlistvideo') NOT NULL DEFAULT 'video' AFTER `state`,
1919
ADD COLUMN `data` TEXT NULL AFTER `type`
2020
");

0 commit comments

Comments
 (0)