MDL-37506 question code: incorrect preg_quote uses.
[moodle.git] / question / type / shortanswer / question.php
CommitLineData
068b4594 1<?php
068b4594
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
068b4594
TH
17/**
18 * Short answer question definition class.
19 *
7764183a
TH
20 * @package qtype
21 * @subpackage shortanswer
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
068b4594
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
068b4594
TH
30/**
31 * Represents a short answer question.
32 *
7764183a
TH
33 * @copyright 2009 The Open University
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
068b4594
TH
35 */
36class qtype_shortanswer_question extends question_graded_by_strategy
37 implements question_response_answer_comparer {
38 /** @var boolean whether answers should be graded case-sensitively. */
39 public $usecase;
40 /** @var array of question_answer. */
41 public $answers = array();
42
43 public function __construct() {
44 parent::__construct(new question_first_matching_answer_grading_strategy($this));
45 }
46
47 public function get_expected_data() {
74c479f2 48 return array('answer' => PARAM_RAW_TRIMMED);
068b4594
TH
49 }
50
51 public function summarise_response(array $response) {
52 if (isset($response['answer'])) {
53 return $response['answer'];
54 } else {
55 return null;
56 }
57 }
58
59 public function is_complete_response(array $response) {
60 return array_key_exists('answer', $response) &&
61 ($response['answer'] || $response['answer'] === '0');
62 }
63
64 public function get_validation_error(array $response) {
65 if ($this->is_gradable_response($response)) {
66 return '';
67 }
68 return get_string('pleaseenterananswer', 'qtype_shortanswer');
69 }
70
71 public function is_same_response(array $prevresponse, array $newresponse) {
72 return question_utils::arrays_same_at_key_missing_is_blank(
73 $prevresponse, $newresponse, 'answer');
74 }
75
76 public function get_answers() {
77 return $this->answers;
78 }
79
80 public function compare_response_with_answer(array $response, question_answer $answer) {
b2a79cc1
TH
81 if (!array_key_exists('answer', $response) || is_null($response['answer'])) {
82 return false;
83 }
84
8cfc4fbd
TH
85 return self::compare_string_with_wildcard(
86 $response['answer'], $answer->answer, !$this->usecase);
068b4594
TH
87 }
88
89 public static function compare_string_with_wildcard($string, $pattern, $ignorecase) {
90 // Break the string on non-escaped asterisks.
91 $bits = preg_split('/(?<!\\\\)\*/', $pattern);
92 // Escape regexp special characters in the bits.
03cefcc9 93 $escapedbits = array();
068b4594 94 foreach ($bits as $bit) {
03cefcc9 95 $escapedbits[] = preg_quote(str_replace('\*', '*', $bit), '|');
068b4594
TH
96 }
97 // Put it back together to make the regexp.
03cefcc9 98 $regexp = '|^' . implode('.*', $escapedbits) . '$|u';
068b4594
TH
99
100 // Make the match insensitive if requested to.
101 if ($ignorecase) {
102 $regexp .= 'i';
103 }
104
5a449045
DR
105 if (function_exists('normalizer_normalize')) {
106 $regexp = normalizer_normalize($regexp, Normalizer::FORM_C);
107 $string = normalizer_normalize($string, Normalizer::FORM_C);
108 }
109
068b4594
TH
110 return preg_match($regexp, trim($string));
111 }
7a719748 112
9eb62b33
TH
113 public function get_correct_response() {
114 $response = parent::get_correct_response();
115 if ($response) {
116 $response['answer'] = $this->clean_response($response['answer']);
117 }
118 return $response;
119 }
120
121 public function clean_response($answer) {
122 // Break the string on non-escaped asterisks.
123 $bits = preg_split('/(?<!\\\\)\*/', $answer);
124
125 // Unescape *s in the bits.
126 $cleanbits = array();
127 foreach ($bits as $bit) {
128 $cleanbits[] = str_replace('\*', '*', $bit);
129 }
130
131 // Put it back together with spaces to look nice.
132 return trim(implode(' ', $cleanbits));
133 }
134
52ad7e0c
TH
135 public function check_file_access($qa, $options, $component, $filearea,
136 $args, $forcedownload) {
7a719748
TH
137 if ($component == 'question' && $filearea == 'answerfeedback') {
138 $currentanswer = $qa->get_last_qt_var('answer');
15dd7727 139 $answer = $this->get_matching_answer(array('answer' => $currentanswer));
7a719748 140 $answerid = reset($args); // itemid is answer id.
b2a79cc1 141 return $options->feedback && $answer && $answerid == $answer->id;
7a719748
TH
142
143 } else if ($component == 'question' && $filearea == 'hint') {
93cadb1e 144 return $this->check_hint_file_access($qa, $options, $args);
7a719748
TH
145
146 } else {
8cfc4fbd
TH
147 return parent::check_file_access($qa, $options, $component, $filearea,
148 $args, $forcedownload);
7a719748
TH
149 }
150 }
068b4594 151}