Skip to content

Commit d56d79d

Browse files
authored
feat: implement summarise_response
1 parent d27d471 commit d56d79d

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lang/en/qtype_questionpy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
$string['request_error'] = 'The {$a->requestmethod} request to "{$a->uri}" failed with the error code "{$a->errorcode}" and'
7979
. ' status code {$a->statuscode} ({$a->reasonphrase}).';
8080
$string['response'] = 'Response';
81+
$string['response_summary_dynamic_data'] = 'Dynamic Data';
82+
$string['response_summary_files'] = 'Files';
83+
$string['response_summary_form_data'] = 'Form Data';
8184
$string['same_version_different_hash_error'] = 'A package with the same version but different hash already exists.';
8285
$string['scoring_state'] = 'QuestionPy Scoring State';
8386
$string['search_all_header'] = 'All ({$a})';

question.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,44 @@ public function is_same_response(array $prevresponse, array $newresponse): bool
335335
*
336336
* @param array $response a response, as might be passed to {@see grade_response()}.
337337
* @return string a plain text summary of that response, that could be used in reports.
338+
* @throws moodle_exception
338339
*/
339340
public function summarise_response(array $response) {
340-
return '';
341+
$summary = '';
342+
343+
$qpyresponse = utils::get_qpy_response($response);
344+
345+
if ($qpyresponse) {
346+
$qpyresponse = get_object_vars($qpyresponse);
347+
$dynamicdata = get_object_vars($qpyresponse['data'] ?? (object) []);
348+
unset($qpyresponse['data']);
349+
350+
if ($qpyresponse) {
351+
$summary .= get_string('response_summary_form_data', 'qtype_questionpy') . ':';
352+
353+
ksort($qpyresponse);
354+
foreach ($qpyresponse as $key => $value) {
355+
$summary .= $key . ': ' . $value . ';';
356+
}
357+
}
358+
359+
if ($dynamicdata) {
360+
$summary .= get_string('response_summary_dynamic_data', 'qtype_questionpy') . ':';
361+
foreach ($dynamicdata as $key => $value) {
362+
$summary .= $key . ': ' . json_encode($value) . ';';
363+
}
364+
}
365+
}
366+
367+
$fileloader = $response[constants::QT_VAR_RESPONSE_FILES] ?? null;
368+
if ($fileloader) {
369+
$summary .= get_string('response_summary_files', 'qtype_questionpy') . ':';
370+
foreach ($fileloader->get_files() as $file) {
371+
$summary .= $file->get_filename() . ' (' . display_size($file->get_filesize()) . ');';
372+
}
373+
}
374+
375+
return $summary ?: '-';
341376
}
342377

343378
/**

0 commit comments

Comments
 (0)