Skip to content

Commit

Permalink
MDL-47494 gapselect: Bug fixes: #2009
Browse files Browse the repository at this point in the history
1. Automatic display of right answer did not work if question text
contained images.

2. A choice of '0' was rejected by the form validation.

3. Hard coded strings moved to lang strings.
  • Loading branch information
timhunt committed Nov 24, 2011
1 parent 0491945 commit a719f4d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
20 changes: 10 additions & 10 deletions question/type/gapselect/edit_form_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,40 +215,40 @@ public function validation($data, $files) {
private function validate_slots($questiontext, $choices) {
$error = 'Please check the Question text: ';
if (!$questiontext) {
return $error . 'The question text is empty!';
return get_string('errorquestiontextblank', 'qtype_gapselect');
}

$matches = array();
preg_match_all($this->squarebracketsregex, $questiontext, $matches);
$slots = $matches[0];

if (!$slots) {
return $error . 'The question text is not in the correct format!';
return get_string('errornoslots', 'qtype_gapselect');
}

$output = array();
$cleanedslots = array();
foreach ($slots as $slot) {
// The 2 is for'[[' and 4 is for '[[]]'.
$output[] = substr($slot, 2, (strlen($slot)-4));
$cleanedslots[] = substr($slot, 2, (strlen($slot)-4));
}
$slots = $cleanedslots;

$slots = $output;
$found = false;
foreach ($slots as $slot) {
$found = false;
foreach ($choices as $key => $choice) {
if ($slot == $key + 1) {
if (!$choice['answer']) {
return " Please check Choices: The choice <b>$slot</b> empty.";
if ($choice['answer'] === '') {
return get_string('errorblankchoice', 'qtype_gapselect',
html_writer::tag('b', $slot));
}
$found = true;
break;
}
}
if (!$found) {
return $error . '<b>' . $slot . '</b> was not found in Choices! ' .
'(only the choice numbers that exist in choices are allowed ' .
'to be used a place holders!';
return get_string('errormissingchoice', 'qtype_gapselect',
html_writer::tag('b', $slot));
}
}
return false;
Expand Down
4 changes: 4 additions & 0 deletions question/type/gapselect/lang/en/qtype_gapselect.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
$string['choicex'] = 'Choice {no}';
$string['correctansweris'] = 'The correct answer is: {$a}';
$string['editinggapselect'] = 'Editing a select missing words question';
$string['errorblankchoice'] = 'Please check the Choices: Choice {$a} is empty.';
$string['errormissingchoice'] = 'Please check the Question text: {$a} was not found in Choices! Only the choice numbers that exist in choices are allowed to be used a place holders.';
$string['errornoslots'] = 'The question text must contain placeholders like [[1]] to show where the missing words go.';
$string['errorquestiontextblank'] = 'You must enter some question text.';
$string['gapselect'] = 'Select missing words';
$string['gapselect_help'] = 'Type in some question text like "The [[1]] jumped over the [[2]]", then enter the possible words to go in gaps 1 and 2 underneath.';
$string['gapselectsummary'] = 'Missing words in some text are filled in using dropdown menus.';
Expand Down
13 changes: 11 additions & 2 deletions question/type/gapselect/questionbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public function summarise_response(array $response) {
foreach ($this->places as $place => $group) {
if (array_key_exists($this->field($place), $response) &&
$response[$this->field($place)]) {
$choices[] = '{' . html_to_text($this->get_selected_choice(
$group, $response[$this->field($place)])->text, 0, false) . '}';
$choices[] = '{' . $this->summarise_choice(
$this->get_selected_choice($group, $response[$this->field($place)])) . '}';
$allblank = false;
} else {
$choices[] = '{}';
Expand All @@ -132,6 +132,15 @@ public function summarise_response(array $response) {
return implode(' ', $choices);
}

/**
* Convert a choice to plain text.
* @param qtype_gapselect_choice $choice one of the choices for a place.
* @return a plain text summary of the choice.
*/
public function summarise_choice($choice) {
return $this->html_to_text($choice->text, FORMAT_PLAIN);
}

public function get_random_guess_score() {
$accum = 0;

Expand Down
4 changes: 3 additions & 1 deletion question/type/gapselect/rendererbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public function correct_response(question_attempt $qa) {
}

if (!empty($correctanswer)) {
return get_string('correctansweris', 'qtype_gapselect', $correctanswer);
return get_string('correctansweris', 'qtype_gapselect',
$question->format_text($correctanswer, $question->questiontextformat,
$qa, 'question', 'questiontext', $question->id));
}
}
}

0 comments on commit a719f4d

Please sign in to comment.