moodle/moodle mirrored from git://git.moodle.org/moodle.git

Added code for escaping control characters with backslash.

• Loading branch information...
thetrinity committed Feb 27, 2004
1 parent c253550 commit 8ec4b43554986154c42707663fe899b56289335b
Showing with 56 additions and 10 deletions.
1. +22 −1 mod/quiz/format/gift/examples.txt
2. +34 −9 mod/quiz/format/gift/format.php
 @@ -50,7 +50,7 @@ Grant is buried in Grant's tomb.{F} The sun rises in the east.{T} -// ===Numerical=== +// ===Matching=== Matching Question. { =subquestion1 -> subanswer1 @@ -134,6 +134,27 @@ What two people are entombed in Grant's tomb? { ~%50%Grant's wife ~%-50%Grant's father } +// ===Special Characters=== +Which answer equals 5? { + ~ \= 2 + 2 + = \= 2 + 3 + ~ \= 2 + 4 } + +::GIFT Control Characters:: +Which of the following is NOT a control character for the GIFT import format? { + ~ \~ # \~ is a control character. + ~ \= # \= is a control character. + ~ \# # \# is a control character. + ~ \{ # \{ is a control character. + ~ \} # \} is a control character. + = \\ # Correct! \\ (backslash) is not a control character. BUT, + it is used to escape the control characters. So, to specify + a literal backslash, you must escape it with a backslash + (as shown in this example). +} + + + //-----------------------------------------// // EXAMPLES FROM gift/format.php //-----------------------------------------//
 @@ -47,14 +47,35 @@ function answerweightparser(&\$answer) { function commentparser(&\$answer) { if (strpos(\$answer,"#") > 0){ \$hashpos = strpos(\$answer,"#"); - \$comment = addslashes(substr(\$answer, \$hashpos+1)); + \$comment = substr(\$answer, \$hashpos+1); + \$comment = addslashes(trim(\$this->escapedchar_post(\$comment))); \$answer = substr(\$answer, 0, \$hashpos); } else { \$comment = " "; } return \$comment; } + function escapedchar_pre(\$string) { + //Replaces escaped control characters with a placeholder BEFORE processing + + \$escapedcharacters = array("\\#", "\\=", "\\{", "\\}", "\\~" ); + \$placeholders = array("&&035;", "&&061;", "&&123;", "&&125;", "&&126;"); + + \$string = str_replace("\\\\", "&&092;", \$string); + \$string = str_replace(\$escapedcharacters, \$placeholders, \$string); + \$string = str_replace("&&092;", "\\", \$string); + return \$string; + } + + function escapedchar_post(\$string) { + //Replaces placeholders with corresponding character AFTER processing is done + \$placeholders = array("&&035;", "&&061;", "&&123;", "&&125;", "&&126;"); + \$characters = array("#", "=", "{", "}", "~" ); + \$string = str_replace(\$placeholders, \$characters, \$string); + return \$string; + } + function readquestion(\$lines) { // Given an array of lines known to define a question in this format, this function @@ -80,6 +101,9 @@ function readquestion(\$lines) { return false; } + // Substitute escaped control characters with placeholders + \$text = \$this->escapedchar_pre(\$text); + // QUESTION NAME parser if (substr(\$text, 0, 2) == "::") { \$text = substr(\$text, 2); @@ -89,14 +113,14 @@ function readquestion(\$lines) { \$question->name = false; // name will be assigned after processing question text below } else { - \$question->name = addslashes(trim(substr(\$text, 0, \$namefinish))); + \$questionname = substr(\$text, 0, \$namefinish); + \$question->name = addslashes(trim(\$this->escapedchar_post(\$questionname))); \$text = trim(substr(\$text, \$namefinish+2)); // Remove name from text } } else { \$question->name = false; } - // FIND ANSWER section \$answerstart = strpos(\$text, "{"); if (\$answerstart === false) { @@ -120,11 +144,12 @@ function readquestion(\$lines) { // Format QUESTION TEXT without answer, inserting "_____" as necessary if (substr(\$text, -1) == "}") { // no blank line if answers follow question, outside of closing punctuation - \$question->questiontext = addslashes(trim(substr_replace(\$text, "", \$answerstart, \$answerlength+1))); + \$questiontext = substr_replace(\$text, "", \$answerstart, \$answerlength+1); } else { // inserts blank line for missing word format - \$question->questiontext = addslashes(trim(substr_replace(\$text, "_____", \$answerstart, \$answerlength+1))); + \$questiontext = substr_replace(\$text, "_____", \$answerstart, \$answerlength+1); } + \$question->questiontext = addslashes(trim(\$this->escapedchar_post(\$questiontext))); // set question name if not already set if (\$question->name === false) { @@ -216,7 +241,7 @@ function readquestion(\$lines) { } \$question->fraction[\$key] = \$answer_weight; \$question->feedback[\$key] = \$this->commentparser(\$answer); // commentparser also removes comment from \$answer - \$question->answer[\$key] = addslashes(\$answer); + \$question->answer[\$key] = addslashes(\$this->escapedchar_post(\$answer)); } // end foreach answer \$question->defaultgrade = 1; @@ -256,8 +281,8 @@ function readquestion(\$lines) { } \$marker = strpos(\$answer,"->"); - \$question->subquestions[\$key] = addslashes(trim(substr(\$answer, 0, \$marker))); - \$question->subanswers[\$key] = addslashes(trim(substr(\$answer, \$marker+2))); + \$question->subquestions[\$key] = addslashes(trim(\$this->escapedchar_post(substr(\$answer, 0, \$marker)))); + \$question->subanswers[\$key] = addslashes(trim(\$this->escapedchar_post(substr(\$answer, \$marker+2)))); } // end foreach answer @@ -312,7 +337,7 @@ function readquestion(\$lines) { } \$question->fraction[\$key] = \$answer_weight; \$question->feedback[\$key] = \$this->commentparser(\$answer); //commentparser also removes comment from \$answer - \$question->answer[\$key] = addslashes(\$answer); + \$question->answer[\$key] = addslashes(\$this->escapedchar_post(\$answer)); } // end foreach \$question->usecase = 0; // Ignore case

0 comments on commit `8ec4b43`

Please sign in to comment.