@@ -549,6 +549,96 @@ public function delete()
549
549
return parent ::delete ();
550
550
}
551
551
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
+
552
642
/**
553
643
* Set playlist videos in playlist based on passed entries.
554
644
* This function checks no permissions.
0 commit comments