Skip to content

Commit

Permalink
fix: Corrects response formatting for multichoice questions to fix #440
Browse files Browse the repository at this point in the history
…. (#447 - Thanks @garemoko)
  • Loading branch information
garemoko authored and ryasmi committed Feb 26, 2019
1 parent 0d9f833 commit 68b16e3
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lang/en/logstore_xapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
$string['sendidnumber'] = 'Send course and activity ID number';
$string['sendidnumber_desc'] = 'Statements will include the ID number (admin defined) for courses and activities in the object extensions';
$string['send_response_choices'] = 'Send response choices';
$string['send_response_choices_desc'] = 'Statements for multiple choice question answers will be sent to the LRS with the correct response and potential choices';
$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';
$string['resendfailedbatches'] = 'Resend failed batches';
$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.';
10 changes: 2 additions & 8 deletions src/transformer/events/mod_quiz/question_answered/gapselect.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@ function gapselect(array $config, \stdClass $event, \stdClass $questionattempt,
],
'object' => [
'id' => utils\get_quiz_question_id($config, $coursemodule->id, $question->id),
'definition' => [
'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction',
'name' => [
$lang => utils\get_string_html_removed($question->questiontext)
],
'interactionType' => 'sequencing',
]
'definition' => utils\get_multichoice_definition($config, $questionattempt, $question, $lang, 'sequencing'),
],
'timestamp' => utils\get_event_timestamp($event),
'result' => [
'response' => $questionattempt->responsesummary,
'response' => implode ('[,]', $selections),
'completion' => $questionattempt->responsesummary !== null,
'success' => $questionattempt->rightanswer === $questionattempt->responsesummary,
'extensions' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function multichoice(array $config, \stdClass $event, \stdClass $questionattempt
$quiz = $repo->read_record_by_id('quiz', $attempt->quiz);
$coursemodule = $repo->read_record_by_id('course_modules', $event->contextinstanceid);
$lang = utils\get_course_lang($course);
$selections = explode('; ', utils\get_string_html_removed($questionattempt->responsesummary));
return [[
'actor' => utils\get_user($config, $user),
'verb' => [
Expand All @@ -41,7 +42,7 @@ function multichoice(array $config, \stdClass $event, \stdClass $questionattempt
],
'timestamp' => utils\get_event_timestamp($event),
'result' => [
'response' => utils\get_string_html_removed($questionattempt->responsesummary),
'response' => implode ('[,]', $selections),
'success' => $questionattempt->rightanswer == $questionattempt->responsesummary,
'completion' => $questionattempt->responsesummary !== '',
'extensions' => [
Expand Down
23 changes: 17 additions & 6 deletions src/transformer/utils/get_multichoice_definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use src\transformer\utils as utils;
defined('MOODLE_INTERNAL') || die();

function get_multichoice_definition(array $config, \stdClass $questionattempt, \stdClass $question, $lang) {
function get_multichoice_definition(array $config, \stdClass $questionattempt, \stdClass $question, $lang, $interactiontype = 'choice') {
if ($config['send_response_choices']) {
$repo = $config['repo'];
$answers = $repo->read_records('question_answers', [
Expand All @@ -32,15 +32,26 @@ function get_multichoice_definition(array $config, \stdClass $questionattempt, \
]
];
}, $answers);

$correctresponsepattern;
switch ($interactiontype) {
case 'sequencing':
$selections = explode('} {', rtrim(ltrim($questionattempt->rightanswer, '{'), '}'));
$correctresponsepattern = implode ('[,]', $selections);
break;
default:
$selections = explode('; ', utils\get_string_html_removed($questionattempt->rightanswer));
$correctresponsepattern = implode ('[,]', $selections);
break;
}

return [
'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction',
'name' => [
$lang => utils\get_string_html_removed($question->questiontext),
],
'interactionType' => 'choice',
'correctResponsesPattern' => [
utils\get_string_html_removed($questionattempt->rightanswer),
],
'interactionType' => $interactiontype,
'correctResponsesPattern' => [$correctresponsepattern],
// Need to pull out id's that are appended during array_map so json parses it correctly as an array.
'choices' => array_values($choices)
];
Expand All @@ -51,6 +62,6 @@ function get_multichoice_definition(array $config, \stdClass $questionattempt, \
'name' => [
$lang => utils\get_string_html_removed($question->questiontext),
],
'interactionType' => 'choice'
'interactionType' => $interactiontype
];
}
2 changes: 1 addition & 1 deletion tests/mod_quiz/attempt_submitted/gapselect/statements.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
},
"timestamp": "2015-06-10T15:31:41+01:00",
"result": {
"response": "{spicy} {mango} {milkshake}",
"response": "spicy[,]mango[,]milkshake",
"completion": true,
"success": true,
"extensions": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
},
"timestamp": "2015-06-10T15:31:41+01:00",
"result": {
"response": "answer 1; answer 2",
"response": "answer 1[,]answer 2",
"success": true,
"completion": true,
"extensions": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
},
"interactionType": "choice",
"correctResponsesPattern": [
"answer 1; answer 2"
"answer 1[,]answer 2"
],
"choices": [
{
Expand All @@ -140,7 +140,7 @@
},
"timestamp": "2015-06-10T15:31:41+01:00",
"result": {
"response": "answer 1; answer 2",
"response": "answer 1[,]answer 2",
"success": true,
"completion": true,
"extensions": {
Expand Down

0 comments on commit 68b16e3

Please sign in to comment.