|
8 | 8 | use Opencast\VersionHelper; |
9 | 9 | use Opencast\Providers\Perm; |
10 | 10 | use Opencast\Models\Videos; |
| 11 | +use Opencast\Models\REST\ApiEventsClient; |
| 12 | +use Opencast\Models\REST\ApiWorkflowsClient; |
11 | 13 |
|
12 | 14 | class Helpers |
13 | 15 | { |
| 16 | + const RECORDED_SERIES_ID_CACHE_ID = 'OpencastV3/series/recorded_series_ids'; |
| 17 | + |
14 | 18 | static function getConfigurationstate() |
15 | 19 | { |
16 | 20 | $stmt = DBManager::get()->prepare("SELECT COUNT(*) AS c FROM oc_config WHERE active = 1"); |
@@ -426,4 +430,131 @@ public static function isWorldReadable($oc_acls) |
426 | 430 |
|
427 | 431 | return $has_anonymous_role; |
428 | 432 | } |
| 433 | + |
| 434 | + /** |
| 435 | + * Retrieves all known recorded Opencast series IDs from the cache or database. |
| 436 | + * |
| 437 | + * This method returns an array of all series IDs that are known to the system, |
| 438 | + * combining both user-specific and seminar-specific series. It first attempts |
| 439 | + * to read the list from the cache. If the cache is empty or if the $force |
| 440 | + * parameter is set to true, it queries the database for all user and seminar |
| 441 | + * series IDs, merges and deduplicates them, and then updates the cache. |
| 442 | + * |
| 443 | + * @param bool $force If true, forces a refresh from the database instead of using the cache. |
| 444 | + * @return array List of unique recorded series IDs. |
| 445 | + */ |
| 446 | + public static function getAllRecordedSeriesIds(bool $force = false) |
| 447 | + { |
| 448 | + $cache = \StudipCacheFactory::getCache(); |
| 449 | + $all_known_seriesids = $cache->read(self::RECORDED_SERIES_ID_CACHE_ID); |
| 450 | + if ($force || empty($all_known_seriesids)) { |
| 451 | + $combined_records = []; |
| 452 | + $user_series_ids =\SimpleCollection::createFromArray( |
| 453 | + UserSeries::findBySql('1') |
| 454 | + )->toArray(); |
| 455 | + $seminar_series_ids =\SimpleCollection::createFromArray( |
| 456 | + SeminarSeries::findBySql('1') |
| 457 | + )->toArray(); |
| 458 | + $combined_records = array_merge($user_series_ids, $seminar_series_ids); |
| 459 | + $all_known_seriesids = array_column($combined_records, 'series_id'); |
| 460 | + $all_known_seriesids = array_unique($all_known_seriesids); |
| 461 | + $cache->write(self::RECORDED_SERIES_ID_CACHE_ID, $all_known_seriesids); |
| 462 | + } |
| 463 | + return $all_known_seriesids; |
| 464 | + } |
| 465 | + |
| 466 | + /** |
| 467 | + * Determines whether a given Opencast event belongs to any know series in this Stud.IP instance. |
| 468 | + * |
| 469 | + * This method checks if the provided Opencast event's series ID (`is_part_of`) |
| 470 | + * is known to the current Stud.IP system. If the event does not have a series ID, |
| 471 | + * it is considered valid for this Stud.IP instance. Otherwise, the method checks |
| 472 | + * if the series ID exists in the list of all recorded series IDs known to Stud.IP. |
| 473 | + * |
| 474 | + * @param object $oc_event The Opencast event object to check. |
| 475 | + * @return bool True if the event belongs to this Stud.IP instance, false otherwise. |
| 476 | + */ |
| 477 | + public static function isEventInAnyKnownSeries($oc_event) |
| 478 | + { |
| 479 | + if (empty($oc_event->is_part_of)) { |
| 480 | + // No series id, so we consider it as a valid event for this studip to be processed! |
| 481 | + return true; |
| 482 | + } |
| 483 | + |
| 484 | + $all_known_seriesids = self::getAllRecordedSeriesIds(); |
| 485 | + |
| 486 | + if (in_array($oc_event->is_part_of, $all_known_seriesids)) { |
| 487 | + return true; |
| 488 | + } |
| 489 | + |
| 490 | + return false; |
| 491 | + } |
| 492 | + |
| 493 | + /** |
| 494 | + * Invalidates the cache for recorded series IDs. |
| 495 | + * |
| 496 | + * This method clears the cache entry that stores all known recorded series IDs. |
| 497 | + * It should be called whenever a series is created or deleted to ensure |
| 498 | + * that the cache remains consistent with the database. |
| 499 | + * @see \Opencast\Models\UserSeries |
| 500 | + * @see \Opencast\Models\SeminarSeries |
| 501 | + */ |
| 502 | + public static function invalidateRecordedSeriesIdsCache() |
| 503 | + { |
| 504 | + $cache = \StudipCacheFactory::getCache(); |
| 505 | + $cache->expire(self::RECORDED_SERIES_ID_CACHE_ID); |
| 506 | + } |
| 507 | + |
| 508 | + /** |
| 509 | + * Gives the events without series id a chance of getting one by mapping user perms and user series. |
| 510 | + * |
| 511 | + * @Notification OpencastVideoSync |
| 512 | + * |
| 513 | + * @param string $eventType |
| 514 | + * @param object $event |
| 515 | + * @param Opencast\Models\Videos $video |
| 516 | + */ |
| 517 | + public static function mapEventUserSeriesUserPerms($eventType, $event, $video) |
| 518 | + { |
| 519 | + if (!empty($event->is_part_of)) { |
| 520 | + // Already has a series id, then we are done here! |
| 521 | + return; |
| 522 | + } |
| 523 | + |
| 524 | + // Get the (a) video owner. |
| 525 | + $video_owner = VideosUserPerms::findOneBySQL('video_id = ? AND perm = ?', [$video->id, 'owner']); |
| 526 | + if (empty($video_owner)) { |
| 527 | + // No owner, then we have nothing to do here! |
| 528 | + return; |
| 529 | + } |
| 530 | + |
| 531 | + // Make sure the owner has a user series! |
| 532 | + $user_series = null; |
| 533 | + |
| 534 | + $all_user_series = UserSeries::getSeries($video_owner->user_id); |
| 535 | + // Enforce user series creation! |
| 536 | + if (empty($all_user_series)) { |
| 537 | + $user_series = UserSeries::createSeries($video_owner->user_id); |
| 538 | + } else { |
| 539 | + $user_series = $all_user_series[0]; |
| 540 | + } |
| 541 | + |
| 542 | + // Update the event with the new series id. |
| 543 | + $api_event_client = ApiEventsClient::getInstance($video->config_id); |
| 544 | + |
| 545 | + $metadata[] = [ |
| 546 | + 'id' => 'isPartOf', |
| 547 | + 'value' => $user_series['series_id'] |
| 548 | + ]; |
| 549 | + $response = $api_event_client->updateMetadata($video->episode, $metadata); |
| 550 | + $republish = in_array($response['code'], [200, 204]) === true; |
| 551 | + |
| 552 | + if ($republish) { |
| 553 | + $api_wf_client = ApiWorkflowsClient::getInstance($video->config_id); |
| 554 | + |
| 555 | + if ($api_wf_client->republish($video->episode)) { |
| 556 | + echo 'Event metadata has been updated by the owner specific series id: ' . $video->episode . "\n"; |
| 557 | + } |
| 558 | + } |
| 559 | + } |
429 | 560 | } |
0 commit comments