forked from moodleou/moodle-qtype_crossword
-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.php
137 lines (121 loc) · 4.34 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
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Question definition class for crossword.
*
* @package qtype_crossword
* @copyright The Open University
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// For a complete list of base question classes please examine the file
// /question/type/questionbase.php.
//
// Make sure to implement all the abstract methods of the base class.
/**
* Class that represents a crossword question.
*/
class qtype_crossword_question extends question_graded_automatically {
/**
* Answer field name.
*
* @param int $key Key number.
* @return string The answer key name.
*/
protected function field($key): string {
return 'sub' . $key;
}
public function get_expected_data(): array {
$response = [];
for ($i = 0; $i < count($this->answer); $i++) {
$response[$this->field($i)] = PARAM_RAW_TRIMMED;
}
return $response;
}
public function get_correct_response(): ?array {
$response = [];
for ($i = 0; $i < count($this->answer); $i++) {
$response[$this->field($i)] = $this->answer[$i];
}
return $response;
}
public function summarise_response(array $response): ?string {
$selectedchoices = [];
foreach ($response as $answer) {
$selectedchoices[] = str_replace('_', ' ', $answer);
}
if (empty($selectedchoices)) {
return null;
}
return implode('; ', $selectedchoices);
}
public function is_complete_response(array $response): bool {
$filteredresponse = $this->filter_answers($response);
return count($this->answer) === count($filteredresponse);
}
public function is_gradable_response(array $response): bool {
$filteredresponse = $this->filter_answers($response);
return count($filteredresponse) > 0;
}
public function get_validation_error(array $response): string {
if ($this->is_complete_response($response)) {
return '';
}
return get_string('pleaseananswerallparts', 'qtype_crossword');
}
public function is_same_response(array $prevresponse, array $newresponse): bool {
foreach ($this->answer as $key => $notused) {
$fieldname = $this->field($key);
if (!question_utils::arrays_same_at_key(
$prevresponse, $newresponse, $fieldname)) {
return false;
}
}
return true;
}
public function grade_response(array $response): array {
list($right, $total) = $this->get_num_parts_right($response);
$fraction = $right / $total;
return [$fraction, question_state::graded_state_for_fraction($fraction)];
}
public function get_num_parts_right(array $response): array {
$answers = $this->answer;
$numright = count($answers) - count(array_diff($answers, $response));
return [$numright, count($answers)];
}
public function clear_wrong_from_response(array $response): array {
$key = 0;
foreach ($response as $answer) {
if ($this->answer[$key] !== $answer) {
$response[$this->field($key)] = '';
}
$key++;
}
return $response;
}
/**
* Retrieve the valid answers list.
*
* @param array $answers The answers list.
* @return array The filtered list.
*/
private function filter_answers(array $answers): array {
$response = array_filter($answers, function(string $answer) {
return mb_strlen(trim(str_replace('_', '', $answer))) > 0;
});
return $response;
}
}