From 147097a11df7b26340f57fd1e75d8c85ef59b228 Mon Sep 17 00:00:00 2001 From: Stefan Hanauska Date: Fri, 16 Feb 2024 10:08:18 +0100 Subject: [PATCH] MBS-8800: Add suffix for default completion --- mod_form.php | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/mod_form.php b/mod_form.php index 17174a3..6c142a5 100644 --- a/mod_form.php +++ b/mod_form.php @@ -163,7 +163,7 @@ public function definition_after_data() { * @return bool */ public function completion_rule_enabled($data): bool { - return (!empty($data['completiontype']) && $data['completiontype'] > 0); + return (!empty($data['completiontype' . $this->get_suffix()])); } /** @@ -181,18 +181,20 @@ public function add_completion_rules(): array { custom_completion::COMPLETION_WITH_ALL_PLACES => get_string('completion_with_all_places', 'mod_learningmap'), ]; + $completiontype = 'completiontype' . $this->get_suffix(); + $mform->addElement( 'select', - 'completiontype', + $completiontype, get_string('completiontype', 'learningmap'), $completionoptions, [] ); - $mform->setType('completiontype', PARAM_INT); - $mform->hideIf('completiontype', 'completion', 'neq', COMPLETION_TRACKING_AUTOMATIC); + $mform->setType($completiontype, PARAM_INT); + $mform->hideIf($completiontype, 'completion', 'neq', COMPLETION_TRACKING_AUTOMATIC); - return(['completiontype']); + return([$completiontype]); } /** @@ -259,11 +261,27 @@ public function data_preprocessing(&$defaultvalues): void { * @return void */ public function data_postprocessing($data): void { - $mapworker = new mapworker( - $data->introeditor['text'], - json_decode($data->placestore, true) - ); - $mapworker->replace_stylesheet(); - $data->introeditor['text'] = $mapworker->get_svgcode(); + // As this form is also used for setting the default completion settings, there might not be an actual learningmap. + if (isset($data->introeditor) && !empty($data->introeditor['text'] === '')) { + $mapworker = new mapworker( + $data->introeditor['text'], + json_decode($data->placestore, true) + ); + $mapworker->replace_stylesheet(); + $data->introeditor['text'] = $mapworker->get_svgcode(); + } + } + + /** + * Get the suffix to be added to the completion elements when creating them. + * This acts as a spare for compatibility with Moodle 4.1 and 4.2. + * + * @return string The suffix + */ + public function get_suffix(): string { + if (method_exists(get_parent_class($this), 'get_suffix')) { + return parent::get_suffix(); + } + return ''; } }