Skip to content

Commit

Permalink
feat: implement is_same_response
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Nov 15, 2024
1 parent 513fedc commit d77ddb4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 11 additions & 0 deletions classes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,15 @@ public static function reindex_integer_arrays(array &$array): void {
$array = array_values($array);
}
}

/**
* Given an array as returned by {@see \question_attempt_step::get_qt_data()}, filters out vars starting with `_`.
*
* @param array $qtvars
* @return array
* @see question_attempt_step for the meaning of different step var prefixes
*/
public static function filter_for_response(array $qtvars): array {
return array_filter($qtvars, fn($key) => !str_starts_with($key, '_'), ARRAY_FILTER_USE_KEY);
}
}
10 changes: 6 additions & 4 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

use qtype_questionpy\api\api;
use qtype_questionpy\api\attempt_ui;
use qtype_questionpy\constants;
use qtype_questionpy\api\scoring_code;
use qtype_questionpy\constants;
use qtype_questionpy\question_ui_metadata_extractor;
use qtype_questionpy\utils;

Expand Down Expand Up @@ -267,7 +267,7 @@ public function is_complete_response(array $response): bool {
}

/**
* Use by many of the behaviours to determine whether the student's
* Used by many of the behaviours to determine whether the student's
* response has changed. This is normally used to determine that a new set
* of responses can safely be discarded.
*
Expand All @@ -277,8 +277,10 @@ public function is_complete_response(array $response): bool {
* @return bool whether the two sets of responses are the same - that is
* whether the new set of responses can safely be discarded.
*/
public function is_same_response(array $prevresponse, array $newresponse) {
return false;
public function is_same_response(array $prevresponse, array $newresponse): bool {
// The response has already been filtered against get_expected_data, we just need to filter out attempt state
// and scoring state before comparing.
return utils::filter_for_response($prevresponse) == utils::filter_for_response($newresponse);
}

/**
Expand Down

0 comments on commit d77ddb4

Please sign in to comment.