Skip to content

Commit

Permalink
Add support for new "location" metadata field and improve default val…
Browse files Browse the repository at this point in the history
…ue handling in forms
  • Loading branch information
ferishili committed Jan 6, 2025
1 parent c47c1a3 commit ed6b7ad
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 4 deletions.
28 changes: 27 additions & 1 deletion classes/local/addvideo_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
28 changes: 27 additions & 1 deletion classes/local/batchupload_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion classes/setting_default_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}]';
}

/**
Expand Down
45 changes: 45 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit ed6b7ad

Please sign in to comment.