Skip to content

Commit d738bf6

Browse files
committed
feat: implement is_same_response
1 parent 3b1b16c commit d738bf6

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

classes/utils.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,15 @@ public static function reindex_integer_arrays(array &$array): void {
127127
$array = array_values($array);
128128
}
129129
}
130+
131+
/**
132+
* Given an array as returned by {@see \question_attempt_step::get_qt_data()}, filters out vars starting with `_`.
133+
*
134+
* @param array $qtvars
135+
* @return array
136+
* @see question_attempt_step for the meaning of different step var prefixes
137+
*/
138+
public static function filter_for_response(array $qtvars): array {
139+
return array_filter($qtvars, fn($key) => !str_starts_with($key, '_'), ARRAY_FILTER_USE_KEY);
140+
}
130141
}

question.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
use qtype_questionpy\api\api;
2626
use qtype_questionpy\api\attempt_ui;
27-
use qtype_questionpy\constants;
2827
use qtype_questionpy\api\scoring_code;
28+
use qtype_questionpy\constants;
2929
use qtype_questionpy\question_ui_metadata_extractor;
3030
use qtype_questionpy\utils;
3131

@@ -267,7 +267,7 @@ public function is_complete_response(array $response): bool {
267267
}
268268

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

284286
/**

0 commit comments

Comments
 (0)