Skip to content

Commit

Permalink
MDL-61876 question: apply format_text to input only
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Apr 17, 2018
1 parent 20bf0c4 commit b197da8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion question/type/gapselect/renderer.php
Expand Up @@ -57,7 +57,8 @@ protected function embedded_element(question_attempt $qa, $place,
$orderedchoices = $question->get_ordered_choices($group);
$selectoptions = array();
foreach ($orderedchoices as $orderedchoicevalue => $orderedchoice) {
$selectoptions[$orderedchoicevalue] = $orderedchoice->text;
$selectoptions[$orderedchoicevalue] = $question->format_text($orderedchoice->text,
$question->questiontextformat, $qa, 'question', 'questiontext', $question->id);
}

$feedbackimage = '';
Expand Down
42 changes: 39 additions & 3 deletions question/type/gapselect/rendererbase.php
Expand Up @@ -40,16 +40,26 @@ public function formulation_and_controls(question_attempt $qa,
$question = $qa->get_question();

$questiontext = '';
// Glue question fragments together using unique placeholders, apply format_text to the result
// and then substitute each placeholder with the embedded element.
// This will ensure that format_text() is applied to the whole question but not to the embedded elements.
$placeholders = $this->get_fragments_glue_placeholders($question->textfragments);
foreach ($question->textfragments as $i => $fragment) {
if ($i > 0) {
$questiontext .= $this->embedded_element($qa, $i, $options);
$questiontext .= $placeholders[$i];
$embeddedelements[$placeholders[$i]] = $this->embedded_element($qa, $i, $options);
}
$questiontext .= $fragment;
}
$questiontext = $question->format_text($questiontext,
$question->questiontextformat, $qa, 'question', 'questiontext', $question->id);
foreach ($placeholders as $i => $placeholder) {
$questiontext = preg_replace('/'. preg_quote($placeholder, '/') . '/',
$embeddedelements[$placeholder], $questiontext);
}

$result = '';
$result .= html_writer::tag('div', $question->format_text($questiontext,
$question->questiontextformat, $qa, 'question', 'questiontext', $question->id),
$result .= html_writer::tag('div', $questiontext,
array('class' => $this->qtext_classname(), 'id' => $this->qtext_id($qa)));

$result .= $this->post_qtext_elements($qa, $options);
Expand All @@ -63,6 +73,32 @@ public function formulation_and_controls(question_attempt $qa,
return $result;
}

/**
* Find strings that we can use to glue the fragments with
*
* These strings have to be all different and neither of them can be present in the text
*
* @param array $fragments
* @return array array with indexes from 1 to count($fragments)-1
*/
protected function get_fragments_glue_placeholders($fragments) {
$fragmentscount = count($fragments);
if ($fragmentscount <= 1) {
return [];
}
$prefix = '[[$';
$postfix = ']]';
$text = join('', $fragments);
while (preg_match('/' . preg_quote($prefix, '/') . '\\d+' . preg_quote($postfix, '/') . '/', $text)) {
$prefix .= '$';
}
$glues = [];
for ($i = 1; $i < $fragmentscount; $i++) {
$glues[$i] = $prefix . $i . $postfix;
}
return $glues;
}

protected function qtext_classname() {
return 'qtext';
}
Expand Down
3 changes: 3 additions & 0 deletions question/type/upgrade.txt
Expand Up @@ -8,6 +8,9 @@ This files describes API changes for question type plugins.
question_manually_gradable.
+ The default implementation of is_gradable_response has been moved from question_graded_automatically to
question_with_responses.
+ Note that format_text() is no longer applied to the results of
qtype_elements_embedded_in_question_text_renderer::embedded_element(). If question type overrides
this method make sure you apply format_text() to any text that came from a user.

=== 3.1.5, 3.2.2, 3.3 ===

Expand Down

0 comments on commit b197da8

Please sign in to comment.