From ed6b7add08db569921a4663781142c9711d7534e Mon Sep 17 00:00:00 2001 From: ferishili Date: Mon, 6 Jan 2025 12:53:24 +0100 Subject: [PATCH] Add support for new "location" metadata field and improve default value handling in forms --- classes/local/addvideo_form.php | 28 +++++++++++++++++- classes/local/batchupload_form.php | 28 +++++++++++++++++- classes/setting_default_manager.php | 4 ++- db/upgrade.php | 45 +++++++++++++++++++++++++++++ version.php | 2 +- 5 files changed, 103 insertions(+), 4 deletions(-) diff --git a/classes/local/addvideo_form.php b/classes/local/addvideo_form.php index 1137d82d..fcd5e627 100644 --- a/classes/local/addvideo_form.php +++ b/classes/local/addvideo_form.php @@ -169,6 +169,14 @@ public function definition() { }); } + // Converting value to default value if exists in params JSON. + $configureddefault = null; + if (isset($param['value'])) { + $configureddefault = $param['value']; + // We unset this here, to make it clean. + unset($param['value']); + } + // Get the created element back from addElement function, in order to further use its attrs. $element = $mform->addElement($field->datatype, $field->name, $this->try_get_string($field->name, 'block_opencast'), $param, $attributes); @@ -194,7 +202,25 @@ public function definition() { } } $mform->setAdvanced($field->name, !$field->required); - $default = (isset($eventdefaults[$field->name]) ? $eventdefaults[$field->name] : null); + + $default = null; + if (!empty($configureddefault)) { + $default = $configureddefault; + // If the default value from the param JSON is used (configured default) and the field is read-only, + // this indicates that the field should be displayed in read-only mode and the default value must + // also be included in the metadata catalog. + if ($field->readonly) { + // We now make sure that the read-only field send its value by form submit. + $element->setPersistantFreeze(true); + } + } + + // We give precedence to the event defaults configured in the course through "Manage default values". + if (isset($eventdefaults[$field->name])) { + $default = $eventdefaults[$field->name]; + } + + // In case we have default value. if ($default) { $mform->setDefault($field->name, $default); } diff --git a/classes/local/batchupload_form.php b/classes/local/batchupload_form.php index b770ac5a..c9fdc276 100644 --- a/classes/local/batchupload_form.php +++ b/classes/local/batchupload_form.php @@ -149,6 +149,14 @@ public function definition() { }); } + // Converting value to default value if exists in params JSON. + $configureddefault = null; + if (isset($param['value'])) { + $configureddefault = $param['value']; + // We unset this here, to make it clean. + unset($param['value']); + } + // Get the created element back from addElement function, in order to further use its attrs. $element = $mform->addElement($field->datatype, $field->name, $this->try_get_string($field->name, 'block_opencast'), $param, $attributes); @@ -174,7 +182,25 @@ public function definition() { } } $mform->setAdvanced($field->name, !$field->required); - $default = (isset($eventdefaults[$field->name]) ? $eventdefaults[$field->name] : null); + + $default = null; + if (!empty($configureddefault)) { + $default = $configureddefault; + // If the default value from the param JSON is used (configured default) and the field is read-only, + // this indicates that the field should be displayed in read-only mode and the default value must + // also be included in the metadata catalog. + if ($field->readonly) { + // We now make sure that the read-only field send its value by form submit. + $element->setPersistantFreeze(true); + } + } + + // We give precedence to the event defaults configured in the course through "Manage default values". + if (isset($eventdefaults[$field->name])) { + $default = $eventdefaults[$field->name]; + } + + // In case we have default value. if ($default) { $mform->setDefault($field->name, $default); } diff --git a/classes/setting_default_manager.php b/classes/setting_default_manager.php index ac402dd1..c6ce3968 100644 --- a/classes/setting_default_manager.php +++ b/classes/setting_default_manager.php @@ -78,7 +78,9 @@ public static function get_default_metadata() { '{"name":"creator","datatype":"autocomplete","required":0,"readonly":0,"param_json":null,"defaultable":0,' . '"batchable":0},' . '{"name":"contributor","datatype":"autocomplete","required":0,"readonly":0,"param_json":null,"defaultable":0,' . - '"batchable":0}]'; + '"batchable":0},' . + '{"name":"location","datatype":"text","required":0,"readonly":1,"param_json":"{\"value\":\"Moodle\"}"' . + ',"defaultable":0,"batchable":0}]'; } /** diff --git a/db/upgrade.php b/db/upgrade.php index db571bb1..93fb084c 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -885,5 +885,50 @@ function xmldb_block_opencast_upgrade($oldversion) { upgrade_block_savepoint(true, 2024093000, 'opencast'); } + // Taking care of new "location" metadata field for events. + if ($oldversion < 2024111103) { + // First, we read all the current "metadata" config settings if exist. + + $params = ['plugin' => 'block_opencast', 'configname' => '%metadata\_%']; + $whereclause = $DB->sql_equal('plugin', ':plugin') . ' AND ' . $DB->sql_like('name', ':configname'); + if ($entries = $DB->get_records_select('config_plugins', $whereclause, $params, '', 'name, value')) { + foreach ($entries as $entry) { + $eventmetadata = $entry->value; + // We only need to add the new location if the metadata config is not empty, + // if it is empty, it means it is not properly saved yet, so we do not need to do anything. + if (!empty($entry->value)) { + // Decode the existing metadata JSON to an array. + $metadata = json_decode($entry->value, true); + // Check if the location metadata already exists. + $locationmetadataexists = array_filter($metadata, function ($metadataentry) { + return $metadataentry['name'] === 'location'; + }); + // If the location metadata already exists, we do not need to add it again. + if (!empty($locationmetadataexists)) { + continue; + } + // If the location metadata does not exist, we create a new entry for it. + // Create a new entry for the location metadata. + $locationmetadataarray = [ + "name" => "location", + "datatype" => "text", + "required" => 0, + "readonly" => 1, + "param_json" => "{\"value\":\"Moodle\"}", // Default value for the location metadata. + "defaultable" => 0, + "batchable" => 0, + ]; + // Add the new location entry to the metadata array. + $metadata[] = $locationmetadataarray; + // Encode the metadata array back to JSON. + $newmetadata = json_encode($metadata); + // Save the new metadata back to the config with added location metadata. + set_config($entry->name, $newmetadata, 'block_opencast'); + } + } + } + upgrade_block_savepoint(true, 2024111103, 'opencast'); + } + return true; } diff --git a/version.php b/version.php index 47ec07f5..4c2ea166 100644 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ $plugin->component = 'block_opencast'; $plugin->release = 'v4.5-r3'; -$plugin->version = 2024111102; +$plugin->version = 2024111103; $plugin->requires = 2024100700; // Requires Moodle 4.5+. $plugin->maturity = MATURITY_STABLE; $plugin->dependencies = [