forked from moodleou/moodle-qtype_crossword
-
Notifications
You must be signed in to change notification settings - Fork 0
/
questiontype.php
239 lines (215 loc) · 9.87 KB
/
questiontype.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
<?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 type class for crossword is defined here.
*
* @package qtype_crossword
* @copyright 2022 The Open University
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/questionlib.php');
/**
* Class that represents a crossword question type.
*
* The class loads, saves and deletes questions of the type crossword
* to and from the database and provides methods to help with editing questions
* of this type. It can also provide the implementation for import and export
* in various formats.
*/
class qtype_crossword extends question_type {
// Override functions as necessary from the parent class located at /question/type/questiontype.php.
private const WORD_FIELDS = ['answer', 'clue', 'orientation', 'rowindex', 'columnindex'];
public function get_question_options($question): bool {
global $DB;
parent::get_question_options($question);
$question->options = $DB->get_record('qtype_crossword_options', ['questionid' => $question->id]);
if ($question->options === false) {
// If this has happened, then we have a problem.
// For the user to be able to edit or delete this question, we need options.
debugging("Question ID {$question->id} was missing an options record. Using default.", DEBUG_DEVELOPER);
$question->options = $this->create_default_options($question);
}
$question->options->words = $DB->get_records('qtype_crossword_words',
['questionid' => $question->id], 'id ASC');
return true;
}
/**
* Create a default options object for the provided question.
*
* @param object $question The queston we are working with.
* @return object The options object.
*/
protected function create_default_options($question): object {
// Create a default question options record.
$options = new stdClass();
$options->questionid = $question->id;
// Get the default strings and just set the format.
$options->correctfeedback = get_string('correctfeedbackdefault', 'question');
$options->correctfeedbackformat = FORMAT_HTML;
$options->partiallycorrectfeedback = get_string('partiallycorrectfeedbackdefault', 'question');;
$options->partiallycorrectfeedbackformat = FORMAT_HTML;
$options->incorrectfeedback = get_string('incorrectfeedbackdefault', 'question');
$options->incorrectfeedbackformat = FORMAT_HTML;
$options->shownumcorrect = 1;
$options->numrows = 10;
$options->numcolumns = 10;
return $options;
}
public function save_question($question, $form) {
// For MVP version, default mark will be set automatically.
$marks = 0;
for ($i = 0; $i < count($form->answer); $i++) {
if (trim($form->answer[$i]) === '' || trim($form->clue[$i]) === '') {
continue;
}
$marks++;
}
$form->defaultmark = $marks;
return parent::save_question($question, $form);
}
public function save_question_options($question) {
global $DB;
$context = $question->context;
$result = new stdClass();
// Old words.
$oldwords = $DB->get_records('qtype_crossword_words',
['questionid' => $question->id], 'id ASC');
$numwords = count($question->answer);
// Following hack to check at least 1 words exist.
$answercount = 0;
for ($i = 0; $i < $numwords; $i++) {
if ($question->answer[$i] !== '' && $question->clue[$i] !== '') {
$answercount++;
}
}
if ($answercount < 1) { // Check there are at lest 1 word for crossword.
$result->error = get_string('notenoughwords', 'qtype_crossword', '1');
return $result;
}
// Insert all the new words.
for ($i = 0; $i < $numwords; $i++) {
if (trim($question->answer[$i]) === '' || trim($question->clue[$i]) === '') {
continue;
}
// Update an existing word if possible.
$word = array_shift($oldwords);
if (!$word) {
$word = new stdClass();
$word->questionid = $question->id;
$word->answer = '';
$word->clue = '';
$word->orientation = 0;
$word->rowindex = 0;
$word->columnindex = 0;
$word->id = $DB->insert_record('qtype_crossword_words', $word);
}
$word->answer = trim(mb_strtoupper($question->answer[$i]));
$word->clue = $question->clue[$i];
$word->orientation = $question->orientation[$i];
$word->rowindex = $question->rowindex[$i];
$word->columnindex = $question->columnindex[$i];
$DB->update_record('qtype_crossword_words', $word);
}
// Remove remain words.
if ($oldwords) {
$ids = array_map(function($word){
return $word->id;
}, $oldwords);
list($idssql, $idsparams) = $DB->get_in_or_equal($ids, SQL_PARAMS_QM);
$DB->delete_records_select('qtype_crossword_words', "id $idssql", $idsparams);
}
$options = $DB->get_record('qtype_crossword_options', ['questionid' => $question->id]);
if (!$options) {
$options = new stdClass();
$options->questionid = $question->id;
$options->correctfeedback = '';
$options->partiallycorrectfeedback = '';
$options->incorrectfeedback = '';
$options->numrows = 10;
$options->numcolumns = 10;
$options->id = $DB->insert_record('qtype_crossword_options', $options);
}
$options->numrows = $question->numrows;
$options->numcolumns = $question->numcolumns;
$options = $this->save_combined_feedback_helper($options, $question, $context, true);
$DB->update_record('qtype_crossword_options', $options);
$this->save_hints($question, true);
}
public function delete_question($questionid, $contextid) {
global $DB;
$DB->delete_records('qtype_crossword_options', ['questionid' => $questionid]);
$DB->delete_records('qtype_crossword_words', ['questionid' => $questionid]);
parent::delete_question($questionid, $contextid);
}
protected function make_hint($hint) {
return question_hint_with_parts::load_from_record($hint);
}
protected function initialise_question_instance($question, $questiondata) {
parent::initialise_question_instance($question, $questiondata);
$this->initialise_combined_feedback($question, $questiondata, true);
$question->answer = [];
$question->clue = [];
$question->orientation = [];
$question->rowindex = [];
$question->columnindex = [];
$question->numrows = (int) $questiondata->options->numrows;
$question->numcolumns = (int) $questiondata->options->numcolumns;
foreach ($questiondata->options->words as $wordsub) {
$question->answer[] = $wordsub->answer;
$question->clue[] = $wordsub->clue;
$question->orientation[] = $wordsub->orientation;
$question->rowindex[] = $wordsub->rowindex;
$question->columnindex[] = $wordsub->columnindex;
}
}
public function export_to_xml($question, qformat_xml $format, $extra = null): string {
$expout = parent::export_to_xml($question, $format, $extra);
$expout .= '<numrows>' . $format->xml_escape($question->options->numrows) . "</numrows>\n";
$expout .= '<numcolumns>' . $format->xml_escape($question->options->numcolumns) . "</numcolumns>\n";
$words = $question->options->words;
foreach ($words as $word => $value) {
$expout .= "<word>\n";
foreach (self::WORD_FIELDS as $xmlfield) {
$exportedvalue = $format->xml_escape($value->{$xmlfield});
$expout .= "<$xmlfield>{$exportedvalue}</$xmlfield>\n";
}
$expout .= "</word>\n";
}
$expout .= $format->write_combined_feedback($question->options, $question->id, $question->contextid);
return $expout;
}
public function import_from_xml($data, $question, qformat_xml $format, $extra = null): ?object {
if (!isset($data['#']['word'])) {
return null;
}
$question = $format->import_headers($data);
$question->qtype = 'crossword';
$words = $data['#']['word'];
$wordnum = 0;
$question->numrows = $format->getpath($data, ['#', 'numrows', 0, '#'], '', true);
$question->numcolumns = $format->getpath($data, ['#', 'numcolumns', 0, '#'], '', true);
foreach ($words as $word) {
foreach (self::WORD_FIELDS as $field) {
$question->{$field}[] = $format->getpath($word, ['#', $field, 0, '#'], '', true);
}
$wordnum++;
}
$format->import_combined_feedback($question, $data);
$format->import_hints($question, $data, true, false, $format->get_format($question->questiontextformat));
return $question;
}
}