Skip to content

Commit

Permalink
Check backup consistency before importing workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
justusdieckmann committed Feb 20, 2024
1 parent 421cc45 commit 038ab1b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
34 changes: 34 additions & 0 deletions classes/local/backup/restore_lifecycle_workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use tool_lifecycle\local\entity\step_subplugin;
use tool_lifecycle\local\entity\trigger_subplugin;
use tool_lifecycle\local\entity\workflow;
use tool_lifecycle\local\manager\lib_manager;
use tool_lifecycle\local\manager\workflow_manager;
use tool_lifecycle\local\manager\step_manager;
use tool_lifecycle\local\manager\trigger_manager;
Expand Down Expand Up @@ -76,6 +77,7 @@ public function execute() {
// If the workflow could be loaded continue with the subplugins.
if ($this->workflow) {
$this->load_subplugins();
$this->check_subplugin_validity();
// Validate the subplugin data.
if (empty($this->errors) && $this->all_subplugins_installed()) {
// If all loaded data is valid, the new workflow and the steps can be stored in the database.
Expand Down Expand Up @@ -174,6 +176,38 @@ private function all_subplugins_installed() {
return true;
}

private function check_subplugin_validity() {
foreach ($this->steps as $step) {
$steplib = lib_manager::get_step_lib($step->subpluginname);
$filteredsettings = [];
foreach ($this->settings as $setting) {
if ($setting->pluginid === $step->id) {
$filteredsettings[$setting->name] = $setting->value;
}
}
$errors = array_map(
fn($x) => get_string('restore_error_in_step', 'tool_lifecycle', $step->instancename) . $x,
$steplib->ensure_validity($filteredsettings)
);
$this->errors = array_merge($this->errors, $errors);
}

foreach ($this->trigger as $trigger) {
$steplib = lib_manager::get_trigger_lib($trigger->subpluginname);
$filteredsettings = [];
foreach ($this->settings as $setting) {
if ($setting->pluginid === $trigger->id) {
$filteredsettings[$setting->name] = $setting->value;
}
}
$errors = array_map(
fn($x) => get_string('restore_error_in_trigger', 'tool_lifecycle', $trigger->instancename) . $x,
$steplib->ensure_validity($filteredsettings)
);
$this->errors = array_merge($this->errors, $errors);
}
}

/**
* Stores all loaded data in the database.
* @throws \moodle_exception
Expand Down
2 changes: 2 additions & 0 deletions lang/en/tool_lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@
$string['restore_subplugins_invalid'] = 'Wrong format of the backup file. The structure of the subplugin elements is not as expected.';
$string['restore_step_does_not_exist'] = 'The step {$a} is not installed, but is included in the backup file. Please installed it first and try again.';
$string['restore_trigger_does_not_exist'] = 'The trigger {$a} is not installed, but is included in the backup file. Please installed it first and try again.';
$string['restore_error_in_step'] = 'An error occurred when importing step "{$a}": ';
$string['restore_error_in_trigger'] = 'An error occurred when importing trigger "{$a}": ';

// Events.
$string['process_triggered_event'] = 'A process has been triggered';
Expand Down
10 changes: 10 additions & 0 deletions step/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public function extend_add_instance_form_definition_after_data($mform, $settings
public function abort_course($process) {
}


/**
* Ensure validity of settings upon backup restoration.
* @param array $settings
* @return array List of errors with settings. If empty, the given settings are valid.
*/
public function ensure_validity(array $settings) : array {
return [];
}

}

/**
Expand Down
1 change: 1 addition & 0 deletions trigger/categories/lang/de/lifecycletrigger_categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
$string['categories'] = 'Kategorien, für die der Workflow ausgelöst werden soll.';
$string['categories_noselection'] = 'Bitte wählen sie mindestens eine Kategorie aus.';
$string['exclude'] = 'Falls ausgewählt, werden gerade die Kurse der angegebenen Kategorien nicht ausgelöst.';
$string['category_does_not_exist'] = 'Es gibt keine Kurskategorie mit der ID {$a}.';
1 change: 1 addition & 0 deletions trigger/categories/lang/en/lifecycletrigger_categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
$string['categories'] = 'Categories, for which the workflow should be triggered';
$string['categories_noselection'] = 'Please choose at least one category.';
$string['exclude'] = 'If ticked, the named categories are excluded from triggering instead.';
$string['category_does_not_exist'] = 'There is no course category with id {$a}.';
13 changes: 13 additions & 0 deletions trigger/categories/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,17 @@ public function extend_add_instance_form_definition($mform) {
$mform->setType('exclude', PARAM_BOOL);
}

public function ensure_validity(array $settings): array {
$errors = [];
$categories = explode(',', $settings['categories']);
// Use core_course_category for moodle 3.6 and higher.
$categoryobjects = \core_course_category::get_many($categories);
foreach ($categories as $category) {
if(!$categoryobjects[$category]) {
$errors[] = get_string('category_does_not_exist', 'lifecycletrigger_categories', $category);
}
}
return $errors;
}

}
9 changes: 9 additions & 0 deletions trigger/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ public function get_status_message() {
return get_string("workflow_started", "tool_lifecycle");
}

/**
* Ensure validity of settings upon backup restoration.
* @param array $settings
* @return array List of errors with settings. If empty, the given settings are valid.
*/
public function ensure_validity(array $settings) : array {
return [];
}

}

/**
Expand Down

0 comments on commit 038ab1b

Please sign in to comment.