diff --git a/classes/catquiz_handler.php b/classes/catquiz_handler.php index 113d0cd46..df4198ef8 100644 --- a/classes/catquiz_handler.php +++ b/classes/catquiz_handler.php @@ -153,7 +153,9 @@ public static function instance_form_definition(MoodleQuickForm &$mform) { // ... after this function has finished execution. submitted form. // But the submitted via post, so we can access the variable via the superglobal $POST. - if ($chosentemplate = optional_param('choosetemplate', 0, PARAM_INT)) { + $reloadtemplate = optional_param('reloadtemplate', true, PARAM_BOOL); + $template = null; + if ($reloadtemplate && $chosentemplate = optional_param('choosetemplate', 0, PARAM_INT)) { // Get parent scale ID from template. $cattest = (object)[ 'id' => $chosentemplate, @@ -161,8 +163,8 @@ public static function instance_form_definition(MoodleQuickForm &$mform) { ]; // Pass on the values as stdClass. - $test = new testenvironment($cattest); - $selectedparentscale = $test->return_as_array()['catscaleid']; + $template = new testenvironment($cattest); + $selectedparentscale = $template->return_as_array()['catscaleid']; } else { $selectedparentscale = optional_param('catquiz_catscales', 0, PARAM_INT); } @@ -189,7 +191,7 @@ public static function instance_form_definition(MoodleQuickForm &$mform) { $mform->addHelpButton('catquiz_passinglevel', 'passinglevel', 'local_catquiz'); $mform->setType('catquiz_passinglevel', PARAM_INT); - info::instance_form_definition($mform, $elements); + info::instance_form_definition($mform, $elements, $template); return $elements; } diff --git a/classes/feedback/feedbackclass.php b/classes/feedback/feedbackclass.php index 13f68a10a..18a9235ec 100644 --- a/classes/feedback/feedbackclass.php +++ b/classes/feedback/feedbackclass.php @@ -25,6 +25,7 @@ namespace local_catquiz\feedback; use local_catquiz\data\dataapi; +use local_catquiz\testenvironment; use MoodleQuickForm; use stdClass; @@ -41,16 +42,23 @@ class feedbackclass { * Add Form elements to form. * @param MoodleQuickForm $mform * @param array $elements + * @param ?testenvironment $template + * * @return void */ - public static function instance_form_definition(MoodleQuickForm &$mform, array &$elements) { + public static function instance_form_definition(MoodleQuickForm &$mform, array &$elements, ?testenvironment $template) { global $CFG, $PAGE, $OUTPUT; require_once($CFG->libdir .'/datalib.php'); - // Get all Values from the form. - $data = $mform->getSubmitValues(); + // If the user just selected a template, get the values from there. + if ($template) { + $data = (array) $template->return_settings(); + } else { + // Otherwise: Get all values from the existing form. + $data = $mform->getSubmitValues(); + } $defaultvalues = $mform->_defaultValues; // phpcs:ignore @@ -65,6 +73,9 @@ public static function instance_form_definition(MoodleQuickForm &$mform, array & ]; $selectedparentscale = optional_param('catquiz_catscales', 0, PARAM_INT); + if ($template) { + $selectedparentscale = $template->return_as_array()['catscaleid']; + } if (!empty($selectedparentscale)) { $scales = dataapi::get_catscale_and_children($selectedparentscale, true); @@ -185,6 +196,9 @@ public static function instance_form_definition(MoodleQuickForm &$mform, array & if (is_array($feedback)) { $feedbacktext = $feedback['text']; } + if (is_object($feedback)) { + $feedbacktext = $feedback->text; + } $feedbacktext = strip_tags($feedbacktext ?? ''); } diff --git a/classes/teststrategy/info.php b/classes/teststrategy/info.php index 11e4d4f9e..8692c90df 100644 --- a/classes/teststrategy/info.php +++ b/classes/teststrategy/info.php @@ -27,6 +27,7 @@ use cache; use core_component; use local_catquiz\feedback\feedbackclass; +use local_catquiz\testenvironment; use local_catquiz\teststrategy\context\contextcreator; use local_catquiz\teststrategy\preselect_task\firstquestionselector; use MoodleQuickForm; @@ -144,9 +145,10 @@ public static function get_teststrategy(int $id, bool $onlyactive = true) { * * @param MoodleQuickForm $mform * @param array $elements + * @param ?testenvironment $template * @return void */ - public static function instance_form_definition(MoodleQuickForm &$mform, array &$elements) { + public static function instance_form_definition(MoodleQuickForm &$mform, array &$elements, ?testenvironment $template) { $data = $mform->getSubmitValues(); $defaultvalues = $mform->_defaultValues; @@ -433,7 +435,7 @@ public static function instance_form_definition(MoodleQuickForm &$mform, array & ); $mform->hideIf('catquiz_questionfeedbacksettings', 'catquiz_showquestion', 'neq', 1); - feedbackclass::instance_form_definition($mform, $elements); + feedbackclass::instance_form_definition($mform, $elements, $template); } /**