From 9fe710159f60087da808ded720f2ffc2f2e71fb6 Mon Sep 17 00:00:00 2001 From: mindforge Date: Wed, 7 Sep 2005 13:59:28 +0000 Subject: [PATCH] Fix for the issue described in http://moodle.org/mod/forum/discuss.php?d=30192 --- .../multianswer/editquestion.php | 4 +- .../multianswer/questiontype.php | 44 ++++++++++++++----- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/mod/quiz/questiontypes/multianswer/editquestion.php b/mod/quiz/questiontypes/multianswer/editquestion.php index 4d54cae51fb..1cb6ad9bfd3 100644 --- a/mod/quiz/questiontypes/multianswer/editquestion.php +++ b/mod/quiz/questiontypes/multianswer/editquestion.php @@ -42,10 +42,8 @@ // Fix the questiontext fields of old questions set_field('quiz_questions', 'questiontext', addslashes($parsableanswerdef), 'id', $wrapped->id); } else { - $parsableanswerdef = str_replace('&#', '&\#', $wrapped->questiontext); + $parsableanswerdef = $wrapped->questiontext; } - //echo "{#$key}
"; - //echo $parsableanswerdef.'
'; $question->questiontext = str_replace("{#$key}", $parsableanswerdef, $question->questiontext); } } diff --git a/mod/quiz/questiontypes/multianswer/questiontype.php b/mod/quiz/questiontypes/multianswer/questiontype.php index 55c57594af3..5c58c493317 100644 --- a/mod/quiz/questiontypes/multianswer/questiontype.php +++ b/mod/quiz/questiontypes/multianswer/questiontype.php @@ -308,6 +308,7 @@ class quiz_embedded_cloze_qtype extends quiz_default_questiontype { $teststate = clone($state); $state->raw_grade = 0; foreach($question->options->questions as $key => $wrapped) { + $state->responses[$key] = html_entity_decode($state->responses[$key]); $teststate->responses = array('' => $state->responses[$key]); $teststate->raw_grade = 0; if (false === $QUIZ_QTYPES[$wrapped->qtype] @@ -356,20 +357,32 @@ function quiz_qtype_multianswer_extract_question($text) { //// quiz/format/multianswer/format.php //////////////////////////////////////////////// + + // Undo the automatic addslashes, because we want to analyze the text - we need to remember this later and addslashes again! + $text = stripslashes($text); + + // We need to allow entities (e.g. н) in answers. This is difficulty, + // because the '#' character is used as delimiter between answer and + // feedback as well as inside entities. The HTML editor automatically + // replaces '&' with '&', so we undo this to get back the entities we + // originally wanted. However, this code leaves all & alone, if they + // are not followed by 2 to 9 characters and a final semicolon. This allows + // to have an answer end on '&' with the feedback (e.g. answer&#feedback). + // When the plain text editor is used, the & needs to be typed out + // explicitly in this case. + $text = preg_replace('/&(#[0-9a-fx]{2,6}?);/', '&$1;', $text); + // REGULAR EXPRESSION CONSTANTS // I do not know any way to make this easier // Regexes are always awkard when defined but more comprehensible // when used as constants in the executive code - // Handle the entity encoded ampersand in entities (e.g. &lt; -> <) - $text = preg_replace('/&(.{2,9}?;)/', '&${1}', $text); - $text = stripslashes($text); - // ANSWER_ALTERNATIVE regexes define("ANSWER_ALTERNATIVE_FRACTION_REGEX", '=|%(-?[0-9]+)%'); + // for the syntax '(?answer = array(); $wrapped->fraction = array(); $wrapped->feedback = array(); - $wrapped->questiontext = addslashes(str_replace('&\#', '&#', $answerregs[0])); + $wrapped->questiontext = addslashes($answerregs[0]); // here we don't want multianswer_escape, because this is editing time information $wrapped->questiontextformat = 0; $remainingalts = $answerregs[ANSWER_REGEX_ALTERNATIVES]; @@ -466,16 +479,16 @@ function quiz_qtype_multianswer_extract_question($text) { } else { $wrapped->fraction[] = '0'; } - $wrapped->feedback[] = addslashes(str_replace('&\#', '&#', + $wrapped->feedback[] = multianswer_escape( isset($altregs[ANSWER_ALTERNATIVE_REGEX_FEEDBACK]) - ? $altregs[ANSWER_ALTERNATIVE_REGEX_FEEDBACK] : '')); + ? $altregs[ANSWER_ALTERNATIVE_REGEX_FEEDBACK] : ''); if (!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_NUMERICAL]) && ereg(NUMERICAL_ALTERNATIVE_REGEX, $altregs[ANSWER_ALTERNATIVE_REGEX_ANSWER], $numregs) ) { $wrapped->answer[] = - addslashes($numregs[NUMERICAL_CORRECT_ANSWER]); + multianswer_escape($numregs[NUMERICAL_CORRECT_ANSWER]); if ($numregs[NUMERICAL_ABS_ERROR_MARGIN]) { $wrapped->tolerance[] = $numregs[NUMERICAL_ABS_ERROR_MARGIN]; @@ -483,8 +496,8 @@ function quiz_qtype_multianswer_extract_question($text) { $wrapped->tolerance[] = 0; } } else { // Tolerance can stay undefined for non numerical questions - $wrapped->answer[] = addslashes(str_replace('&\#', '&#', - $altregs[ANSWER_ALTERNATIVE_REGEX_ANSWER])); + $wrapped->answer[] = multianswer_escape( + $altregs[ANSWER_ALTERNATIVE_REGEX_ANSWER]); } $tmp = explode($altregs[0], $remainingalts, 2); $remainingalts = $tmp[1]; @@ -495,8 +508,15 @@ function quiz_qtype_multianswer_extract_question($text) { $question->questiontext = implode("{#$positionkey}", explode($answerregs[0], $question->questiontext, 2)); } - $question->questiontext = addslashes(str_replace('&\#', '&#', $question->questiontext)); + $question->questiontext = multianswer_escape($question->questiontext); return $question; } +function multianswer_escape($text) { + $text = str_replace("&", "&", $text); + $text = str_replace('\#', '#', $text); + $text = html_entity_decode($text); + return addslashes($text); +} + ?>