Skip to content

Commit

Permalink
fix: Removes HTML from quiz responses to close #304. (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
caperneoignis authored and ryasmi committed Nov 27, 2018
1 parent 1c3bef2 commit 1816098
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ 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);

return [[
'actor' => utils\get_user($config, $user),
'verb' => [
Expand All @@ -42,11 +41,12 @@ function multichoice(array $config, \stdClass $event, \stdClass $questionattempt
],
'timestamp' => utils\get_event_timestamp($event),
'result' => [
'response' => $questionattempt->responsesummary,
'response' => utils\get_string_html_removed($questionattempt->responsesummary),
'success' => $questionattempt->rightanswer == $questionattempt->responsesummary,
'completion' => $questionattempt->responsesummary !== '',
'extensions' => [
'http://learninglocker.net/xapi/cmi/choice/response' => $questionattempt->responsesummary,
'http://learninglocker.net/xapi/cmi/choice/response' =>
utils\get_string_html_removed($questionattempt->responsesummary),
],
],
'context' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function truefalse(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);

return [[
'actor' => utils\get_user($config, $user),
'verb' => [
Expand All @@ -42,14 +41,14 @@ function truefalse(array $config, \stdClass $event, \stdClass $questionattempt,
'definition' => [
'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction',
'name' => [
$lang => $question->questiontext,
$lang => utils\get_string_html_removed($question->questiontext),
],
'interactionType' => 'true-false',
]
],
'timestamp' => utils\get_event_timestamp($event),
'result' => [
'response' => $questionattempt->responsesummary,
'response' => utils\get_string_html_removed($questionattempt->responsesummary),
'completion' => $questionattempt->responsesummary !== null,
'success' => $questionattempt->rightanswer === $questionattempt->responsesummary,
'extensions' => [
Expand Down
12 changes: 7 additions & 5 deletions src/transformer/utils/get_multichoice_definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace src\transformer\utils;
use src\transformer\utils as utils;
defined('MOODLE_INTERNAL') || die();

function get_multichoice_definition(array $config, \stdClass $questionattempt, \stdClass $question, $lang) {
Expand All @@ -27,27 +28,28 @@ function get_multichoice_definition(array $config, \stdClass $questionattempt, \
return [
"id" => "$answer->id",
"description" => [
$lang => $answer->answer
$lang => utils\get_string_html_removed($answer->answer)
]
];
}, $answers);
return [
'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction',
'name' => [
$lang => $question->questiontext,
$lang => utils\get_string_html_removed($question->questiontext),
],
'interactionType' => 'choice',
'correctResponsesPattern' => [
$questionattempt->rightanswer,
utils\get_string_html_removed($questionattempt->rightanswer),
],
'choices' => $choices
// Need to pull out id's that are appended during array_map so json parses it correctly as an array.
'choices' => array_values($choices)
];
}

return [
'type' => 'http://adlnet.gov/expapi/activities/cmi.interaction',
'name' => [
$lang => $question->questiontext,
$lang => utils\get_string_html_removed($question->questiontext),
],
'interactionType' => 'choice'
];
Expand Down
25 changes: 25 additions & 0 deletions src/transformer/utils/get_string_html_removed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace src\transformer\utils;
defined('MOODLE_INTERNAL') || die();

function get_string_html_removed($string) {
// For some reason, newlines and &nbsp; is being added to strings,
// in order to remove new lines we have to ensure nbsp is also removed.
$replacestrings = ["\n", "&nbsp;"];
return str_replace($replacestrings, "", strip_tags($string));
}

0 comments on commit 1816098

Please sign in to comment.