Skip to content

Commit 68b16e3

Browse files
garemokoryasmi
authored andcommitted
fix: Corrects response formatting for multichoice questions to fix #440. (#447 - Thanks @garemoko)
1 parent 0d9f833 commit 68b16e3

File tree

7 files changed

+26
-20
lines changed

7 files changed

+26
-20
lines changed

lang/en/logstore_xapi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@
4848
$string['sendidnumber'] = 'Send course and activity ID number';
4949
$string['sendidnumber_desc'] = 'Statements will include the ID number (admin defined) for courses and activities in the object extensions';
5050
$string['send_response_choices'] = 'Send response choices';
51-
$string['send_response_choices_desc'] = 'Statements for multiple choice question answers will be sent to the LRS with the correct response and potential choices';
51+
$string['send_response_choices_desc'] = 'Statements for multiple choice and sequencing question answers will be sent to the LRS with the correct response and potential choices';
5252
$string['resendfailedbatches'] = 'Resend failed batches';
5353
$string['resendfailedbatches_desc'] = 'When processing events in batches, try re-sending events in smaller batches if a batch fails. If not selected, the whole batch will not be sent in the event of a failed event.';

src/transformer/events/mod_quiz/question_answered/gapselect.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,11 @@ function gapselect(array $config, \stdClass $event, \stdClass $questionattempt,
4040
],
4141
'object' => [
4242
'id' => utils\get_quiz_question_id($config, $coursemodule->id, $question->id),
43-
'definition' => [
44-
'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction',
45-
'name' => [
46-
$lang => utils\get_string_html_removed($question->questiontext)
47-
],
48-
'interactionType' => 'sequencing',
49-
]
43+
'definition' => utils\get_multichoice_definition($config, $questionattempt, $question, $lang, 'sequencing'),
5044
],
5145
'timestamp' => utils\get_event_timestamp($event),
5246
'result' => [
53-
'response' => $questionattempt->responsesummary,
47+
'response' => implode ('[,]', $selections),
5448
'completion' => $questionattempt->responsesummary !== null,
5549
'success' => $questionattempt->rightanswer === $questionattempt->responsesummary,
5650
'extensions' => [

src/transformer/events/mod_quiz/question_answered/multichoice.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function multichoice(array $config, \stdClass $event, \stdClass $questionattempt
2727
$quiz = $repo->read_record_by_id('quiz', $attempt->quiz);
2828
$coursemodule = $repo->read_record_by_id('course_modules', $event->contextinstanceid);
2929
$lang = utils\get_course_lang($course);
30+
$selections = explode('; ', utils\get_string_html_removed($questionattempt->responsesummary));
3031
return [[
3132
'actor' => utils\get_user($config, $user),
3233
'verb' => [
@@ -41,7 +42,7 @@ function multichoice(array $config, \stdClass $event, \stdClass $questionattempt
4142
],
4243
'timestamp' => utils\get_event_timestamp($event),
4344
'result' => [
44-
'response' => utils\get_string_html_removed($questionattempt->responsesummary),
45+
'response' => implode ('[,]', $selections),
4546
'success' => $questionattempt->rightanswer == $questionattempt->responsesummary,
4647
'completion' => $questionattempt->responsesummary !== '',
4748
'extensions' => [

src/transformer/utils/get_multichoice_definition.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use src\transformer\utils as utils;
1919
defined('MOODLE_INTERNAL') || die();
2020

21-
function get_multichoice_definition(array $config, \stdClass $questionattempt, \stdClass $question, $lang) {
21+
function get_multichoice_definition(array $config, \stdClass $questionattempt, \stdClass $question, $lang, $interactiontype = 'choice') {
2222
if ($config['send_response_choices']) {
2323
$repo = $config['repo'];
2424
$answers = $repo->read_records('question_answers', [
@@ -32,15 +32,26 @@ function get_multichoice_definition(array $config, \stdClass $questionattempt, \
3232
]
3333
];
3434
}, $answers);
35+
36+
$correctresponsepattern;
37+
switch ($interactiontype) {
38+
case 'sequencing':
39+
$selections = explode('} {', rtrim(ltrim($questionattempt->rightanswer, '{'), '}'));
40+
$correctresponsepattern = implode ('[,]', $selections);
41+
break;
42+
default:
43+
$selections = explode('; ', utils\get_string_html_removed($questionattempt->rightanswer));
44+
$correctresponsepattern = implode ('[,]', $selections);
45+
break;
46+
}
47+
3548
return [
3649
'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction',
3750
'name' => [
3851
$lang => utils\get_string_html_removed($question->questiontext),
3952
],
40-
'interactionType' => 'choice',
41-
'correctResponsesPattern' => [
42-
utils\get_string_html_removed($questionattempt->rightanswer),
43-
],
53+
'interactionType' => $interactiontype,
54+
'correctResponsesPattern' => [$correctresponsepattern],
4455
// Need to pull out id's that are appended during array_map so json parses it correctly as an array.
4556
'choices' => array_values($choices)
4657
];
@@ -51,6 +62,6 @@ function get_multichoice_definition(array $config, \stdClass $questionattempt, \
5162
'name' => [
5263
$lang => utils\get_string_html_removed($question->questiontext),
5364
],
54-
'interactionType' => 'choice'
65+
'interactionType' => $interactiontype
5566
];
5667
}

tests/mod_quiz/attempt_submitted/gapselect/statements.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
},
118118
"timestamp": "2015-06-10T15:31:41+01:00",
119119
"result": {
120-
"response": "{spicy} {mango} {milkshake}",
120+
"response": "spicy[,]mango[,]milkshake",
121121
"completion": true,
122122
"success": true,
123123
"extensions": {

tests/mod_quiz/attempt_submitted/multichoiceset/statements.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
},
118118
"timestamp": "2015-06-10T15:31:41+01:00",
119119
"result": {
120-
"response": "answer 1; answer 2",
120+
"response": "answer 1[,]answer 2",
121121
"success": true,
122122
"completion": true,
123123
"extensions": {

tests/mod_quiz/attempt_submitted/multichoiceset_withchoices/statements.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
},
115115
"interactionType": "choice",
116116
"correctResponsesPattern": [
117-
"answer 1; answer 2"
117+
"answer 1[,]answer 2"
118118
],
119119
"choices": [
120120
{
@@ -140,7 +140,7 @@
140140
},
141141
"timestamp": "2015-06-10T15:31:41+01:00",
142142
"result": {
143-
"response": "answer 1; answer 2",
143+
"response": "answer 1[,]answer 2",
144144
"success": true,
145145
"completion": true,
146146
"extensions": {

0 commit comments

Comments
 (0)