@@ -719,10 +719,11 @@ public function haveCoursePerm(string $course_perm, string $user_id = null)
719719 * Updates the metadata related to this video in both opencast and local and runs republish-metadata workflow
720720 *
721721 * @param object $event the updated version of the event
722+ * @param boolean $check_dirty_state whether to check for dirty state, meaning if any of the fields that need the opencast update has changed!
722723 *
723724 * @return boolean the result of updating process
724725 */
725- public function updateMetadata ($ event )
726+ public function updateMetadata ($ event, $ check_dirty_state = true )
726727 {
727728 $ api_event_client = ApiEventsClient::getInstance ($ this ->config_id );
728729
@@ -737,21 +738,34 @@ public function updateMetadata($event)
737738 'subject ' , 'language ' , 'description ' , 'startDate ' ];
738739 $ metadata = [];
739740
741+ // A dirty state flag to check if any of the fields that need the opencast update has changed!
742+ $ is_dirty = false ;
743+
740744 foreach ($ allowed_metadata_fields as $ field_name ) {
741745 if (isset ($ event [$ field_name ])) {
746+ $ current_value = $ this ->getValue ($ field_name ) ?? null ;
742747 $ value = $ event [$ field_name ];
743748 $ id = $ field_name ;
744749 if ($ field_name == 'subject ' ) {
745750 $ id = 'subjects ' ;
746751 $ value = [$ value ];
752+ $ current_value = [$ current_value ];
747753 }
748754 if ($ field_name == 'presenters ' ) {
749755 $ id = 'creator ' ;
750- $ value = array_map ('trim ' , explode (', ' , $ value ));
756+ $ value = array_filter (array_map ('trim ' , explode (', ' , $ value )));
757+ $ current_value = array_filter (array_map ('trim ' , explode (', ' , $ current_value )));
751758 }
752759 if ($ field_name == 'contributors ' ) {
753760 $ id = 'contributor ' ;
754- $ value = array_map ('trim ' , explode (', ' , $ value ));
761+ $ value = array_filter (array_map ('trim ' , explode (', ' , $ value )));
762+ $ current_value = array_filter (array_map ('trim ' , explode (', ' , $ current_value )));
763+ }
764+
765+ // If it is not yet marked dirty, check for dirty state!
766+ if (!$ is_dirty ) {
767+ // Assuming that the current value is not the same as the requested value!
768+ $ is_dirty = !(json_encode ($ current_value ) === json_encode ($ value ));
755769 }
756770
757771 $ metadata [] = [
@@ -761,6 +775,11 @@ public function updateMetadata($event)
761775 }
762776 }
763777
778+ if ($ check_dirty_state && !$ is_dirty ) {
779+ // nothing changed, no need to update!
780+ return true ;
781+ }
782+
764783 $ success = false ;
765784 $ response = $ api_event_client ->updateMetadata ($ this ->episode , $ metadata );
766785 $ republish = in_array ($ response ['code ' ], [200 , 204 ]) === true ;
0 commit comments