Skip to content

Commit

Permalink
wip feat: check allowshuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderschmitz committed Mar 13, 2024
1 parent 720d98a commit b9b210a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
9 changes: 7 additions & 2 deletions classes/question_ui_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit b9b210a

Please sign in to comment.