From b9b210a6200bde9ee73e32fc52015930af9d292d Mon Sep 17 00:00:00 2001 From: alexanderschmitz Date: Wed, 13 Mar 2024 12:19:33 +0100 Subject: [PATCH] wip feat: check allowshuffle --- classes/question_ui_renderer.php | 9 +++++++-- question.php | 5 +++++ questiontype.php | 11 +++++++++++ version.php | 2 +- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/classes/question_ui_renderer.php b/classes/question_ui_renderer.php index a2237f60..d1131b00 100644 --- a/classes/question_ui_renderer.php +++ b/classes/question_ui_renderer.php @@ -53,6 +53,9 @@ class question_ui_renderer { /** @var question_metadata|null $metadata */ private ?question_metadata $metadata = null; + /** @var boolean Whether the questions answers should be shuffled. */ + public bool $shuffleanswers = false; + /** * Parses the given XML and initializes a new {@see question_ui_renderer} instance. * @@ -282,14 +285,16 @@ private function shuffle_contents(\DOMXPath $xpath): void { $childelements[] = $child; } } - shuffle($childelements); + if ($this->shuffleanswers) { + shuffle($childelements); + } // Iterate over children, replacing elements with random ones while copying everything else. $i = 1; while ($element->hasChildNodes()) { $child = $element->firstChild; if ($child instanceof DOMElement) { - $child = array_pop($childelements); + $child = array_shift($childelements); $newelement->appendChild($child); $this->replace_shuffled_indices($xpath, $child, $i++); } else { diff --git a/question.php b/question.php index 5f629397..6053d9c5 100644 --- a/question.php +++ b/question.php @@ -45,6 +45,8 @@ class qtype_questionpy_question extends question_graded_automatically_with_count private string $packagehash; /** @var string */ private string $questionstate; + /** @var boolean Whether the questions answers should be shuffled. */ + public bool $shuffleanswers = true; // Properties which do change between attempts (i.e. are modified by start_attempt and apply_attempt_state). /** @var string */ @@ -92,6 +94,7 @@ public function start_attempt(question_attempt_step $step, $variant): void { $this->scoringstate = null; $this->ui = new question_ui_renderer($attempt->ui->content, $attempt->ui->placeholders); + $this->ui->shuffleanswers = $this->shuffleanswers; } /** @@ -123,6 +126,7 @@ public function apply_attempt_state(question_attempt_step $step) { $attempt = $this->api->view_attempt($this->packagehash, $this->questionstate, $this->attemptstate, $this->scoringstate); $this->ui = new question_ui_renderer($attempt->ui->content, $attempt->ui->placeholders); + $this->ui->shuffleanswers = $this->shuffleanswers; } /** @@ -223,6 +227,7 @@ public function grade_response(array $response): array { $response ); $this->ui = new question_ui_renderer($attemptscored->ui->content, $attemptscored->ui->placeholders); + $this->ui->shuffleanswers = $this->shuffleanswers; // TODO: Persist scoring state. We need to set a qtvar, but we don't have access to the pending step here. $this->scoringstate = $attemptscored->scoringstate; switch ($attemptscored->scoringcode) { diff --git a/questiontype.php b/questiontype.php index 401d7f62..ae8a7ba0 100644 --- a/questiontype.php +++ b/questiontype.php @@ -135,6 +135,17 @@ public function get_question_options($question): bool { return true; } + protected function initialise_question_instance(question_definition $question, $questiondata) { + parent::initialise_question_instance($question, $questiondata); + + if (isset($questiondata->options->shuffleanswers)) { + // TODO: this is never set... + $question->shuffleanswers = $questiondata->options->shuffleanswers; + } else { + $question->shuffleanswers = true; + } + } + /** * Create an appropriate question_definition for the question of this type * using data loaded from the database. diff --git a/version.php b/version.php index 4ce53cc5..bba3c738 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qtype_questionpy'; -$plugin->version = 2024021900; +$plugin->version = 2024021901; $plugin->requires = 2022041901; $plugin->maturity = MATURITY_ALPHA; $plugin->release = '0.1';