]> git.mjollnir.org Git - moodle.git/commitdiff
merged from STABLE - missing htmlSpecialChars() in INPUT's value
authorskodak <skodak>
Sat, 2 Oct 2004 19:01:42 +0000 (19:01 +0000)
committerskodak <skodak>
Sat, 2 Oct 2004 19:01:42 +0000 (19:01 +0000)
mod/quiz/questiontypes/shortanswer/questiontype.php

index 2c14d15c26750674d77abb515f712d6a25994335..f314d5349401c373d28ae8a8eb12ecfc0c62a2d2 100644 (file)
-<?php  // $Id$
-
-///////////////////
-/// SHORTANSWER ///
-///////////////////
-
-/// QUESTION TYPE CLASS //////////////////
-
-///
-/// This class contains some special features in order to make the
-/// question type embeddable within a multianswer (cloze) question
-///
-
-class quiz_shortanswer_qtype extends quiz_default_questiontype {
-
-    function get_answers($question, $addedcondition='') {
-        // The added condition is one addition that has been added
-        // to the behaviour of this question type in order to make
-        // it embeddable within a multianswer (embedded cloze) question
-
-        global $CFG;
-
-        // There can be multiple answers
-        return get_records_sql("SELECT a.*, sa.usecase
-                                FROM {$CFG->prefix}quiz_shortanswer sa,
-                                     {$CFG->prefix}quiz_answers a
-                                WHERE sa.question = '$question->id'
-                                  AND sa.question = a.question "
-                                      . $addedcondition);
-
-    }
-
-    function name() {
-        return 'shortanswer';
-    }
-
-    function save_question_options($question) {
-        if (!$oldanswers = get_records("quiz_answers", "question", $question->id, "id ASC")) {
-            $oldanswers = array();
-        }
-
-        $answers = array();
-        $maxfraction = -1;
-
-        // Insert all the new answers
-        foreach ($question->answer as $key => $dataanswer) {
-            if ($dataanswer != "") {
-                if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it
-                    $answer = $oldanswer;
-                    $answer->answer   = trim($dataanswer);
-                    $answer->fraction = $question->fraction[$key];
-                    $answer->feedback = $question->feedback[$key];
-                    if (!update_record("quiz_answers", $answer)) {
-                        $result->error = "Could not update quiz answer! (id=$answer->id)";
-                        return $result;
-                    }
-                } else {    // This is a completely new answer
-                    unset($answer);
-                    $answer->answer   = trim($dataanswer);
-                    $answer->question = $question->id;
-                    $answer->fraction = $question->fraction[$key];
-                    $answer->feedback = $question->feedback[$key];
-                    if (!$answer->id = insert_record("quiz_answers", $answer)) {
-                        $result->error = "Could not insert quiz answer!";
-                        return $result;
-                    }
-                }
-                $answers[] = $answer->id;
-                if ($question->fraction[$key] > $maxfraction) {
-                    $maxfraction = $question->fraction[$key];
-                }
-            }
-        }
-
-        if ($options = get_record("quiz_shortanswer", "question", $question->id)) {
-            $options->answers = implode(",",$answers);
-            $options->usecase = $question->usecase;
-            if (!update_record("quiz_shortanswer", $options)) {
-                $result->error = "Could not update quiz shortanswer options! (id=$options->id)";
-                return $result;
-            }
-        } else {
-            unset($options);
-            $options->question = $question->id;
-            $options->answers = implode(",",$answers);
-            $options->usecase = $question->usecase;
-            if (!insert_record("quiz_shortanswer", $options)) {
-                $result->error = "Could not insert quiz shortanswer options!";
-                return $result;
-            }
-        }
-
-        /// Perform sanity checks on fractional grades
-        if ($maxfraction != 1) {
-            $maxfraction = $maxfraction * 100;
-            $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction);
-            return $result;
-        } else {
-            return true;
-        }
-    }
-    
-    function print_question_formulation_and_controls($question,
-            $quiz, $readonly, $answers, $correctanswers, $nameprefix) {
-    /// This implementation is also used by question type NUMERICAL
-
-        /// Print question text and media
-
-        echo format_text($question->questiontext,
-                         $question->questiontextformat,
-                         NULL, $quiz->course);
-        quiz_print_possible_question_image($quiz->id, $question);
-
-        /// Print input controls
-
-        $stranswer = get_string("answer", "quiz");
-        if (isset($question->response[$nameprefix])) {
-            $value = ' value="'.$question->response[$nameprefix].'" ';
-        } else {
-            $value = ' value="" ';
-        }
-        $inputname = ' name="'.$nameprefix.'" ';
-        echo "<p align=\"right\">$stranswer: <input type=\"text\" $readonly $inputname size=\"80\" $value /></p>";
-
-        if ($quiz->feedback && isset($answers[$nameprefix])
-                && $feedback = $answers[$nameprefix]->feedback) {
-           quiz_print_comment("<p align=\"right\">$feedback</p>");
-        }
-        if ($readonly && $quiz->correctanswers) {
-            $delimiter = '';
-            $correct = '';
-            foreach ($correctanswers as $correctanswer) {
-                $correct .= $delimiter.$correctanswer->answer;
-                $delimiter = ', ';
-            }
-            quiz_print_correctanswer($correct);
-        }
-    }
-
-    function grade_response($question, $nameprefix, $addedanswercondition='') {
-
-        if (isset($question->response[$nameprefix])) {
-            $response0 = trim(stripslashes($question->response[$nameprefix]));
-        } else {
-            $response0 = '';
-        }
-        $answers = $this->get_answers($question, $addedanswercondition);
-
-        /// Determine ->answers[]
-        $result->answers = array();
-        if ('' !== $response0) {
-
-            /// These are things to protect in the strings when wildcards are used
-            $search = array('\\', '+', '(', ')', '[', ']', '-');
-            $replace = array('\\\\', '\+', '\(', '\)', '\[', '\]', '\-');
-
-            foreach ($answers as $answer) {
-
-                $answer->answer = trim($answer->answer);  // Just in case
-
-                if (empty($result->answers) || $answer->fraction
-                        > $result->answers[$nameprefix]->fraction) {
-
-                    if (!$answer->usecase) { // Don't compare case
-                        $response0 = strtolower($response0);
-                        $answer0 = strtolower($answer->answer);
-                    } else {
-                        $answer0 = $answer->answer;
-                    }
-
-                    if (strpos(' '.$answer0, '*')) {
-                        $answer0 = str_replace('\*','@@@@@@',$answer0);
-                        $answer0 = str_replace('*','.*',$answer0);
-                        $answer0 = str_replace($search, $replace, $answer0);
-                        $answer0 = str_replace('@@@@@@', '\*',$answer0);
-
-                        if (ereg('^'.$answer0.'$', $response0)) {
-                            $result->answers[$nameprefix] = $answer;
-                        }
-
-                    } else if ($answer0 == $response0) {
-                        $result->answers[$nameprefix] = $answer;
-                    }
-                }
-            }
-        }
-
-
-        $result->grade = isset($result->answers[$nameprefix])
-                ?   $result->answers[$nameprefix]->fraction
-                :   0.0;
-        $result->correctanswers = quiz_extract_correctanswers($answers,
-                                                              $nameprefix);
-        return $result;
-    }
-}
-//// END OF CLASS ////
-
-//////////////////////////////////////////////////////////////////////////
-//// INITIATION - Without this line the question type is not in use... ///
-//////////////////////////////////////////////////////////////////////////
-$QUIZ_QTYPES[SHORTANSWER]= new quiz_shortanswer_qtype();
-
-?>
+<?php  // $Id$\r
+\r
+///////////////////\r
+/// SHORTANSWER ///\r
+///////////////////\r
+\r
+/// QUESTION TYPE CLASS //////////////////\r
+\r
+///\r
+/// This class contains some special features in order to make the\r
+/// question type embeddable within a multianswer (cloze) question\r
+///\r
+\r
+class quiz_shortanswer_qtype extends quiz_default_questiontype {\r
+\r
+    function get_answers($question, $addedcondition='') {\r
+        // The added condition is one addition that has been added\r
+        // to the behaviour of this question type in order to make\r
+        // it embeddable within a multianswer (embedded cloze) question\r
+\r
+        global $CFG;\r
+\r
+        // There can be multiple answers\r
+        return get_records_sql("SELECT a.*, sa.usecase\r
+                                FROM {$CFG->prefix}quiz_shortanswer sa,\r
+                                     {$CFG->prefix}quiz_answers a\r
+                                WHERE sa.question = '$question->id'\r
+                                  AND sa.question = a.question "\r
+                                      . $addedcondition);\r
+\r
+    }\r
+\r
+    function name() {\r
+        return 'shortanswer';\r
+    }\r
+\r
+    function save_question_options($question) {\r
+        if (!$oldanswers = get_records("quiz_answers", "question", $question->id, "id ASC")) {\r
+            $oldanswers = array();\r
+        }\r
+\r
+        $answers = array();\r
+        $maxfraction = -1;\r
+\r
+        // Insert all the new answers\r
+        foreach ($question->answer as $key => $dataanswer) {\r
+            if ($dataanswer != "") {\r
+                if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it\r
+                    $answer = $oldanswer;\r
+                    $answer->answer   = trim($dataanswer);\r
+                    $answer->fraction = $question->fraction[$key];\r
+                    $answer->feedback = $question->feedback[$key];\r
+                    if (!update_record("quiz_answers", $answer)) {\r
+                        $result->error = "Could not update quiz answer! (id=$answer->id)";\r
+                        return $result;\r
+                    }\r
+                } else {    // This is a completely new answer\r
+                    unset($answer);\r
+                    $answer->answer   = trim($dataanswer);\r
+                    $answer->question = $question->id;\r
+                    $answer->fraction = $question->fraction[$key];\r
+                    $answer->feedback = $question->feedback[$key];\r
+                    if (!$answer->id = insert_record("quiz_answers", $answer)) {\r
+                        $result->error = "Could not insert quiz answer!";\r
+                        return $result;\r
+                    }\r
+                }\r
+                $answers[] = $answer->id;\r
+                if ($question->fraction[$key] > $maxfraction) {\r
+                    $maxfraction = $question->fraction[$key];\r
+                }\r
+            }\r
+        }\r
+\r
+        if ($options = get_record("quiz_shortanswer", "question", $question->id)) {\r
+            $options->answers = implode(",",$answers);\r
+            $options->usecase = $question->usecase;\r
+            if (!update_record("quiz_shortanswer", $options)) {\r
+                $result->error = "Could not update quiz shortanswer options! (id=$options->id)";\r
+                return $result;\r
+            }\r
+        } else {\r
+            unset($options);\r
+            $options->question = $question->id;\r
+            $options->answers = implode(",",$answers);\r
+            $options->usecase = $question->usecase;\r
+            if (!insert_record("quiz_shortanswer", $options)) {\r
+                $result->error = "Could not insert quiz shortanswer options!";\r
+                return $result;\r
+            }\r
+        }\r
+\r
+        /// Perform sanity checks on fractional grades\r
+        if ($maxfraction != 1) {\r
+            $maxfraction = $maxfraction * 100;\r
+            $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction);\r
+            return $result;\r
+        } else {\r
+            return true;\r
+        }\r
+    }\r
+\r
+    function print_question_formulation_and_controls($question,\r
+            $quiz, $readonly, $answers, $correctanswers, $nameprefix) {\r
+    /// This implementation is also used by question type NUMERICAL\r
+\r
+        /// Print question text and media\r
+\r
+        echo format_text($question->questiontext,\r
+                         $question->questiontextformat,\r
+                         NULL, $quiz->course);\r
+        quiz_print_possible_question_image($quiz->id, $question);\r
+\r
+        /// Print input controls\r
+\r
+        $stranswer = get_string("answer", "quiz");\r
+        if (isset($question->response[$nameprefix])) {\r
+            $value = ' value="'.htmlSpecialChars($question->response[$nameprefix]).'" ';\r
+        } else {\r
+            $value = ' value="" ';\r
+        }\r
+        $inputname = ' name="'.$nameprefix.'" ';\r
+        echo "<p align=\"right\">$stranswer: <input type=\"text\" $readonly $inputname size=\"80\" $value /></p>";\r
+\r
+        if ($quiz->feedback && isset($answers[$nameprefix])\r
+                && $feedback = $answers[$nameprefix]->feedback) {\r
+           quiz_print_comment("<p align=\"right\">$feedback</p>");\r
+        }\r
+        if ($readonly && $quiz->correctanswers) {\r
+            $delimiter = '';\r
+            $correct = '';\r
+            foreach ($correctanswers as $correctanswer) {\r
+                $correct .= $delimiter.$correctanswer->answer;\r
+                $delimiter = ', ';\r
+            }\r
+            quiz_print_correctanswer($correct);\r
+        }\r
+    }\r
+\r
+    function grade_response($question, $nameprefix, $addedanswercondition='') {\r
+\r
+        if (isset($question->response[$nameprefix])) {\r
+            $response0 = trim(stripslashes($question->response[$nameprefix]));\r
+        } else {\r
+            $response0 = '';\r
+        }\r
+        $answers = $this->get_answers($question, $addedanswercondition);\r
+\r
+        /// Determine ->answers[]\r
+        $result->answers = array();\r
+        if ('' !== $response0) {\r
+\r
+            /// These are things to protect in the strings when wildcards are used\r
+            $search = array('\\', '+', '(', ')', '[', ']', '-');\r
+            $replace = array('\\\\', '\+', '\(', '\)', '\[', '\]', '\-');\r
+\r
+            foreach ($answers as $answer) {\r
+\r
+                $answer->answer = trim($answer->answer);  // Just in case\r
+\r
+                if (empty($result->answers) || $answer->fraction\r
+                        > $result->answers[$nameprefix]->fraction) {\r
+\r
+                    if (!$answer->usecase) { // Don't compare case\r
+                        $response0 = strtolower($response0);\r
+                        $answer0 = strtolower($answer->answer);\r
+                    } else {\r
+                        $answer0 = $answer->answer;\r
+                    }\r
+\r
+                    if (strpos(' '.$answer0, '*')) {\r
+                        $answer0 = str_replace('\*','@@@@@@',$answer0);\r
+                        $answer0 = str_replace('*','.*',$answer0);\r
+                        $answer0 = str_replace($search, $replace, $answer0);\r
+                        $answer0 = str_replace('@@@@@@', '\*',$answer0);\r
+\r
+                        if (ereg('^'.$answer0.'$', $response0)) {\r
+                            $result->answers[$nameprefix] = $answer;\r
+                        }\r
+\r
+                    } else if ($answer0 == $response0) {\r
+                        $result->answers[$nameprefix] = $answer;\r
+                    }\r
+                }\r
+            }\r
+        }\r
+\r
+\r
+        $result->grade = isset($result->answers[$nameprefix])\r
+                ?   $result->answers[$nameprefix]->fraction\r
+                :   0.0;\r
+        $result->correctanswers = quiz_extract_correctanswers($answers,\r
+                                                              $nameprefix);\r
+        return $result;\r
+    }\r
+}\r
+//// END OF CLASS ////\r
+\r
+//////////////////////////////////////////////////////////////////////////\r
+//// INITIATION - Without this line the question type is not in use... ///\r
+//////////////////////////////////////////////////////////////////////////\r
+$QUIZ_QTYPES[SHORTANSWER]= new quiz_shortanswer_qtype();\r
+\r
+?>\r