-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathquestion.php
263 lines (219 loc) · 9.79 KB
/
question.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Pattern-match question definition class.
*
* @package qtype_pmatch
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use qtype_pmatch\local\spell\qtype_pmatch_spell_checker;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/question/type/pmatch/pmatchlib.php');
/**
* Represents a pattern-match question.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_pmatch_question extends question_graded_by_strategy
implements question_response_answer_comparer {
/** @var boolean whether answers should be graded case-sensitively. */
public $usecase;
/** @var string add more words to the dictionary. */
public $extenddictionary;
/** @var string not really used here, the value used is stored in the pmatch_options,
* but this gets set because of extra_question_fields() so we need to declare it. */
public $sentencedividers;
/** @var string not really used here, the value used is stored in the pmatch_options,
* but this gets set because of extra_question_fields() so we need to declare it. */
public $converttospace;
/** @var boolean whether to allow students to use subscript. */
public $allowsubscript;
/** @var boolean whether to allow students to use super script. */
public $allowsuperscript;
/** @var boolean whether to warn student if their response is longer than 20 words. */
public $forcelength;
/** @var boolean whether to spell check students response. */
public $applydictionarycheck;
/** @var string to be used for 'Preview question' and 'Answer sheet' in print. */
public $modelanswer;
/** @var string to be used for display a pre-fill answer */
public $responsetemplate;
/** @var pmatch_options options for pmatch expression matching. */
public $pmatchoptions;
/** @var array of question_answer. */
public $answers = [];
public function __construct() {
parent::__construct(new question_first_matching_answer_grading_strategy($this));
}
public function get_expected_data() {
return ['answer' => PARAM_RAW_TRIMMED];
}
public function summarise_response(array $response) {
if (isset($response['answer'])) {
return $response['answer'];
} else {
return null;
}
}
public function is_gradable_response(array $response) {
if (!array_key_exists('answer', $response) || ((!$response['answer']) && $response['answer'] !== '0')) {
return false;
} else {
return true;
}
}
public function is_complete_response(array $response) {
if ($this->is_gradable_response($response)) {
return (count($this->validate($response)) === 0);
} else {
return false;
}
}
protected function validate(array $response) {
$responsevalidationerrors = [];
if (!array_key_exists('answer', $response) || ((!$response['answer']) && $response['answer'] !== '0')) {
return [get_string('pleaseenterananswer', 'qtype_pmatch')];
}
$parsestring = new pmatch_parsed_string($response['answer'], $this->pmatchoptions);
if (!$parsestring->is_parseable()) {
$a = $parsestring->unparseable();
$responsevalidationerrors[] = get_string('unparseable', 'qtype_pmatch', $a);
}
if ($this->applydictionarycheck != qtype_pmatch_spell_checker::DO_NOT_CHECK_OPTION &&
!$parsestring->is_spelled_correctly() && (!$this->allowsubscript && !$this->allowsuperscript)) {
$misspelledwords = $parsestring->get_spelling_errors();
$a = join(' ', $misspelledwords);
$responsevalidationerrors[] = get_string('spellingmistakes', 'qtype_pmatch', $a);
}
if ($this->forcelength) {
if ($parsestring->get_word_count() > 20) {
$responsevalidationerrors[] = get_string('toomanywords', 'qtype_pmatch');
}
}
return $responsevalidationerrors;
}
public function get_validation_error(array $response) {
$errors = $this->validate($response);
if (count($errors) === 1) {
return array_pop($errors);
} else {
$errorslist = html_writer::alist($errors);
return get_string('errors', 'qtype_pmatch', $errorslist);
}
}
public function is_same_response(array $prevresponse, array $newresponse) {
return question_utils::arrays_same_at_key_missing_is_blank(
$prevresponse, $newresponse, 'answer');
}
public function get_answers() {
return $this->answers;
}
public function compare_response_with_answer(array $response, question_answer $answer) {
if ($answer->answer == '*') {
return true;
}
// Normally this test won't be called if answer is not set.
// However, it can be called like that from combined, so this test is necessary.
if (!isset($response['answer'])) {
return false;
}
return self::compare_string_with_pmatch_expression($response['answer'],
$answer->answer,
$this->pmatchoptions);
}
public static function compare_string_with_pmatch_expression($string, $expression, $options) {
$string = new pmatch_parsed_string($string, $options);
$expression = new pmatch_expression($expression, $options);
return $expression->matches($string);
}
public function get_correct_response() {
if ($this->modelanswer === '' || $this->modelanswer === null) {
// We don't have a correct answer.
return null;
}
return ['answer' => $this->modelanswer];
}
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($component == 'question' && $filearea == 'answerfeedback') {
$currentanswer = $qa->get_last_qt_var('answer');
$answer = $qa->get_question()->get_matching_answer(['answer' => $currentanswer]);
$answerid = reset($args); // Itemid is answer id.
return $options->feedback && $answerid == $answer->id;
} else if ($component == 'question' && $filearea == 'hint') {
return $this->check_hint_file_access($qa, $options, $args);
} else {
return parent::check_file_access($qa, $options, $component,
$filearea, $args, $forcedownload);
}
}
public function start_attempt(question_attempt_step $step, $variant) {
$this->pmatchoptions->lang = $this->applydictionarycheck;
$step->set_qt_var('_responselang', $this->pmatchoptions->lang);
}
public function apply_attempt_state(question_attempt_step $step) {
$this->pmatchoptions->lang = $step->get_qt_var('_responselang');
}
public function get_context() {
return context::instance_by_id($this->contextid);
}
protected function has_question_capability($type) {
global $USER;
$context = $this->get_context();
return has_capability("moodle/question:{$type}all", $context) ||
($USER->id == $this->createdby && has_capability("moodle/question:{$type}mine", $context));
}
public function user_can_view() {
return $this->has_question_capability('view');
}
/**
* Check that current user can see the missing dictionary warning message.
*
* @return bool True ìf user has the require capability, otherwise False
*/
public function user_can_see_missing_dict_warning() {
return $this->has_question_capability('edit');
}
/**
* Checks whether the spell-check language for this question is available on the server.
*
* @return bool returns false if the question is set to use spell-checking, and the required
* language dictionaries are not available.
*/
public function is_spell_check_laguage_available() {
$spellchecklanguagesdata = get_config('qtype_pmatch', 'spellcheck_languages');
if (!$spellchecklanguagesdata) {
return false;
}
$availablelangs = explode(',', $spellchecklanguagesdata);
return !in_array($this->applydictionarycheck, $availablelangs) &&
$this->applydictionarycheck !== qtype_pmatch_spell_checker::DO_NOT_CHECK_OPTION;
}
/**
* Modify the current answer base on question display option and response template.
*
* @param string|null $currentanswer the current answer of user in the question attempt.
* @param question_display_options $options controls what should and should not be displayed.
* @return string|null
*/
public function modify_current_answer(?string $currentanswer, question_display_options $options): ?string {
if (!$currentanswer && !$options->readonly) {
$currentanswer = $this->responsetemplate;
}
return $currentanswer;
}
}