-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.php
285 lines (247 loc) · 10.2 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?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/>.
/**
* Short answer question definition class.
*
* @package qtype
* @subpackage harakat
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/type/questionbase.php');
/**
* Represents a short answer question.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_harakat_question extends question_graded_by_strategy
implements question_response_answer_comparer {
/** @var array of question_answer. */
public $answers = array();
/** @var string Harakat. */
protected string $unicode =
"~[\x{064B}|\x{064C}|\x{064D}|\x{064E}|\x{064F}|\x{0650}|\x{0651}|\x{0652}|\x{0656}|\x{0657}|\x{0670}]~u";
/** @var string Harakat. */
public string $jsunicode =
"0x064B|0x064C|0x064D|0x064E|0x064F|0x0650|0x0651::0x064F|0x0651::0x064E|0x0651::0x0650|0x0652|0x0656|0x0657|0x0670";
public function __construct() {
parent::__construct(new question_first_matching_answer_grading_strategy($this));
}
public function get_expected_data() {
return array('answer' => PARAM_RAW_TRIMMED);
}
public function summarise_response(array $response) {
if (isset($response['answer'])) {
return $response['answer'];
} else {
return null;
}
}
public function un_summarise_response(string $summary) {
if (!empty($summary)) {
return ['answer' => $summary];
} else {
return [];
}
}
public function is_complete_response(array $response) {
return array_key_exists('answer', $response) &&
($response['answer'] || $response['answer'] === '0');
}
public function get_validation_error(array $response) {
if ($this->is_gradable_response($response)) {
return '';
}
return get_string('pleaseenterananswer', 'qtype_harakat');
}
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 get_answer() {
return reset($this->answers);
}
public function compare_response_with_answer(array $response, question_answer $answer) {
question_response_answer_comparer::compare_response_with_answer($response, $answer);
}
public function get_correct_response() {
$response = parent::get_correct_response();
if ($response) {
$response['answer'] = $this->clean_response($response['answer']);
}
return $response;
}
public function clean_response($answer) {
// Break the string on non-escaped asterisks.
$bits = preg_split('/(?<!\\\\)\*/', $answer);
// Unescape *s in the bits.
$cleanbits = array();
foreach ($bits as $bit) {
$cleanbits[] = str_replace('\*', '*', $bit);
}
// Put it back together with spaces to look nice.
return trim(implode(' ', $cleanbits));
}
/**
* Return the question settings that define this question as structured data.
*
* @param question_attempt $qa the current attempt for which we are exporting the settings.
* @param question_display_options $options the question display options which say which aspects of the question
* should be visible.
* @return mixed structure representing the question settings. In web services, this will be JSON-encoded.
*/
public function get_question_definition_for_external_rendering(question_attempt $qa, question_display_options $options) {
// No need to return anything, external clients do not need additional information for rendering this question type.
return null;
}
public function get_num_parts_right(array $response) {
$result = $this->matching_answer($response);
return [
$result['right'],
$result['total']
];
}
public function grade_response(array $response) {
list($right, $total) = $this->get_num_parts_right($response);
$fraction = $right / $total;
return array($fraction, question_state::graded_state_for_fraction($fraction));
}
public function get_matching_answer(array $response) {
return $this->matching_answer($response)['answer'];
}
public function mapping_harakat(string $str): array {
$map = [];
$plain = '';
$mapindex = -1;
$totalharakat = 0;
foreach (mb_str_split($str) as $s) {
if ($this->is_harakat($s)) {
$map[$mapindex]['harakat'][] = $s;
$totalharakat++;
} else {
$mapindex++;
$map[$mapindex] = ["letter" => $s, "harakat" => []];
$plain .= $s;
}
}
return [
"plain" => $plain,
"original" => $str,
"totalharakat" => $totalharakat,
"map" => $map
];
}
public function matching_answer(array $response) {
$questionanswer = $this->get_answer();
$questionanswertext = $this->mapping_harakat($questionanswer->answer);
$useranswertext = $this->mapping_harakat($response['answer']);
$totalharakat = $questionanswertext['totalharakat'];
$righttotal = 0;
$wrongtotal = 0;
$wrongletters = [];
$htmlanswer = '';
$htmlquestion = '';
foreach ($questionanswertext['map'] as $key => $val) {
$totalharakatinletter = count($questionanswertext['map'][$key]['harakat']);
if ($totalharakatinletter > 0) {
if ($diff = array_diff(
$questionanswertext['map'][$key]['harakat'],
$useranswertext['map'][$key]['harakat'])
) {
$totaldiff = count($diff);
$wrongletters[] = [
"index" => $key,
"questionharakat" => $questionanswertext['map'][$key]['harakat'],
"userharakat" => $useranswertext['map'][$key]['harakat'],
"total" => $totaldiff
];
// If there is more than one harakat in a letter.
if ($totaldiff < $totalharakatinletter) {
$righttotal += $totalharakatinletter - $totaldiff;
}
$wrongtotal += $totaldiff;
// Displaying wrong letters in HTML.
$htmlanswer .= '<span class="harakat_wrong">' . $val['letter'];
foreach ($useranswertext['map'][$key]['harakat'] as $key2 => $val2) {
$htmlanswer .= $val2;
}
$htmlanswer .= '</span>';
} else {
$righttotal += $totalharakatinletter;
// Displaying correct letters in HTML.
$htmlanswer .= '<span class="harakat_correct">' . $useranswertext['map'][$key]['letter'];
foreach ($useranswertext['map'][$key]['harakat'] as $key2 => $val2) {
$htmlanswer .= $val2;
}
$htmlanswer .= '</span>';
$htmlquestion .= '<span class="harakat_yes">' . $useranswertext['map'][$key]['letter'] . '</span>';
}
} else {
// Displaying empty harakat.
$htmlanswer .= '<span class="harakat_no">' . $useranswertext['map'][$key]['letter'] . '</span>';
$htmlquestion .= '<span class="harakat_no">' . $useranswertext['map'][$key]['letter'] . '</span>';
}
}
$questionanswer->fraction = $righttotal / $totalharakat;
return [
'answer' => $questionanswer,
'right' => $righttotal,
'wrong' => $wrongtotal,
'total' => $totalharakat,
'wrongletters' => $wrongletters,
'htmlanswer' => $htmlanswer,
'htmlquestion' => $htmlquestion
];
}
public function html_question($currentanswer) {
$questionanswer = $this->get_answer();
$questionanswertext = $this->mapping_harakat($questionanswer->answer);
if ($currentanswer) {
$currentanswertext = $this->mapping_harakat($currentanswer);
}
$htmlquestion = '';
foreach ($questionanswertext['map'] as $key => $val) {
$totalharakatinletter = count($questionanswertext['map'][$key]['harakat']);
if ($totalharakatinletter > 0) {
$htmlquestion .= '<span class="harakat_yes" original="'.$val['letter'].'">';
} else {
$htmlquestion .= '<span class="harakat_no">';
}
$currentharakat = '';
if ($currentanswer) {
$currentharakat = implode('', $currentanswertext['map'][$key]['harakat']);
}
$htmlquestion .= $val['letter'] . $currentharakat . '</span>';
}
return $htmlquestion;
}
private function is_harakat(string $str): bool {
return preg_match($this->unicode, $str);
}
public function is_harakat_exist(string $str): bool {
foreach (mb_str_split($str) as $s) {
if ($this->is_harakat($s)) {
return true;
}
}
return false;
}
}