forked from marcusgreen/moodle-qtype_wordselect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
questiontype.php
296 lines (272 loc) · 12.2 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
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
286
287
288
289
290
291
292
293
294
295
296
<?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/>.
/**
* Question type class for the wordselect question type.
*
* @package qtype_wordselect
* @copyright 2017 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
require_once($CFG->dirroot . '/question/type/wordselect/question.php');
/**
* The wordselect question type.
*
* @copyright 2017 Marcus Green
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_wordselect extends question_type {
/* data used by export_to_xml (among other things possibly */
public function extra_question_fields() {
return array('question_wordselect', 'introduction', 'delimitchars');
}
/**
* Utility method used by {@link qtype_renderer::head_code()}
* It looks for any of the files script.js or script.php that
* exist in the plugin folder and ensures they get included.
* It also includes the jquery files required for this plugin
*/
public function find_standard_scripts() {
global $PAGE;
parent::find_standard_scripts();
$PAGE->requires->jquery();
}
public function move_files($questionid, $oldcontextid, $newcontextid) {
parent::move_files($questionid, $oldcontextid, $newcontextid);
$fs = get_file_storage();
$fs->move_area_files_to_new_context($oldcontextid,
$newcontextid, 'qtype_wordselect', 'introduction', $questionid);
$this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
}
protected function delete_files($questionid, $contextid) {
parent::delete_files($questionid, $contextid);
$this->delete_files_in_hints($questionid, $contextid);
}
public function save_question_options($formdata) {
global $DB;
/* Save the extra data to your database tables from the
$formdata object, which has all the post data from editformdata.html
* */
$answerwords = $this->get_answerwords($formdata->delimitchars, $formdata->questiontext);
$context = $formdata->context;
// Fetch old answer ids so that we can reuse them.
$this->update_question_answers($formdata, $answerwords);
$options = $DB->get_record('question_wordselect', array('questionid' => $formdata->id));
$this->update_question_wordselect($formdata, $options, $context);
$this->save_hints($formdata, true);
return true;
}
/* it really does need to be static */
public static function get_answerwords($delimitchars, $questiontext) {
/* l for left delimiter r for right delimiter
* defaults to []
* e.g. l=[ and r=] where question is
* The [cat] sat on the [mat]
*/
$delim = self::get_delimit_array($delimitchars);
$fieldregex = '/.*?\\' . $delim["l"] . '(.*?)\\' . $delim["r"] . '/';
$matches = array();
preg_match_all($fieldregex, $questiontext, $matches);
return $matches[1];
}
/* chop the delimit string into a two element array
* this might be better done on initialisation
*/
public static function get_delimit_array($delimitchars) {
$delimitarray = array();
$delimitarray["l"] = substr($delimitchars, 0, 1);
$delimitarray["r"] = substr($delimitchars, 1, 1);
return $delimitarray;
}
/**
* Set up all the answer fields with respective fraction (mark values)
* This is used to update the question_answers table. Answerwords has
* been pulled from within the delimitchars e.g. the cat within [cat]
* Wronganswers (distractors) has been pulled from a comma delimited edit
* form field
* wro
* @param array $answerwords
* @param type $question
* @return type array
*/
public function get_answer_fields(array $answerwords, $question) {
/* this code runs both on saving from a form and from importing and needs
* improving as it mixes pulling information from the question object which
* comes from the import and from $question->wronganswers field which
* comes from the question_editing form.
*/
$answerfields = array();
/* this next block runs when importing from xml */
if (property_exists($question, 'answer')) {
foreach ($question->answer as $key => $value) {
if ($question->fraction[$key] == 0) {
$answerfields[$key]['value'] = $question->answer[$key];
$answerfields[$key]['fraction'] = 0;
} else {
$answerfields[$key]['value'] = $question->answer[$key];
$answerfields[$key]['fraction'] = 1;
}
}
}
/* the rest of this function runs when saving from edit form */
if (!property_exists($question, 'answer')) {
foreach ($answerwords as $key => $value) {
$answerfields[$key]['value'] = $value;
$answerfields[$key]['fraction'] = 1;
}
}
return $answerfields;
}
public function update_question_answers($question, array $answerwords) {
global $DB;
$oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC');
// Insert all the new answers.
foreach ($answerwords as $word) {
// Save the true answer - update an existing answer if possible.
if ($answer = array_shift($oldanswers)) {
$answer->question = $question->id;
$answer->answer = $word;
$answer->feedback = '';
$answer->fraction = 1;
$DB->update_record('question_answers', $answer);
} else {
// Insert a blank record.
$answer = new stdClass();
$answer->question = $question->id;
$answer->answer = $word;
$answer->feedback = '';
$answer->fraction = 1;
$answer->id = $DB->insert_record('question_answers', $answer);
}
}
// Delete old answer records.
foreach ($oldanswers as $oa) {
$DB->delete_records('question_answers', array('id' => $oa->id));
}
}
/* runs from question editing form */
public function update_question_wordselect($formdata, $options, $context) {
/* question is actually formdata */
global $DB;
$options = $DB->get_record('question_wordselect', array('questionid' => $formdata->id));
if (!$options) {
$options = new stdClass();
$options->questionid = $formdata->id;
$options->introduction = '';
$options->delimitchars = '';
$options->correctfeedback = '';
$options->partiallycorrectfeedback = '';
$options->incorrectfeedback = '';
$options->incorrectfeedback = '';
$options->id = $DB->insert_record('question_wordselect', $options);
}
/* when coming in from form */
if (is_array($formdata->introduction)) {
$options->introduction = $this->import_or_save_files($formdata->introduction,
$context, 'qtype_wordselect', 'introduction', $formdata->id);
} else {
/* when being imported e.g. from an xml import */
$options->introduction = $formdata->introduction;
}
$options->delimitchars = $formdata->delimitchars;
$options->correctfeedback = "";
$options = $this->save_combined_feedback_helper($options, $formdata, $context, true);
$DB->update_record('question_wordselect', $options);
}
/* populates fields such as combined feedback */
public function get_question_options($question) {
global $DB;
$question->options = $DB->get_record('question_wordselect',
array('questionid' => $question->id), '*', MUST_EXIST);
parent::get_question_options($question);
}
protected function initialise_question_instance(question_definition $question, $questiondata) {
parent::initialise_question_instance($question, $questiondata);
$this->initialise_question_answers($question, $questiondata);
parent::initialise_combined_feedback($question, $questiondata);
}
protected function initialise_question_answers(question_definition $question, $questiondata, $forceplaintextanswers = true) {
if (empty($questiondata->options->answers)) {
return;
}
$placecounter = 0;
foreach ($questiondata->options->answers as $a) {
$question->places[$placecounter++] = "";
if (strstr($a->fraction, '1') == false) {
/* if this is a wronganswer/distractor strip any
* backslahses, this allows escaped backslashes to
* be used i.e. \, and not displayed in the draggable
* area
*/
$a->answer = stripslashes($a->answer);
}
/* answer in this context means correct answers, i.e. where
* fraction contains a 1 */
if (strpos($a->fraction, '1') !== false) {
$question->answers[$a->id] = new question_answer($a->id, $a->answer, $a->fraction,
$a->feedback, $a->feedbackformat);
if (!$forceplaintextanswers) {
$question->answers[$a->id]->answerformat = $a->answerformat;
}
}
}
}
public function import_from_xml($data, $question, qformat_xml $format, $extra = null) {
if (!isset($data['@']['type']) || $data['@']['type'] != 'wordselect') {
return false;
}
$question = parent::import_from_xml($data, $question, $format, null);
$format->import_combined_feedback($question, $data, true);
$format->import_hints($question, $data, true, false, $format->get_format($question->questiontextformat));
return $question;
}
public function export_to_xml($question, qformat_xml $format, $extra = null) {
global $CFG;
$pluginmanager = core_plugin_manager::instance();
$wordselectinfo = $pluginmanager->get_plugin_info('qtype_wordselect');
$output = parent::export_to_xml($question, $format);
$output .= ' <delimitchars>' . $question->options->delimitchars .
"</delimitchars>\n";
$output .= ' <!-- Wordselect release:'
. $wordselectinfo->release . ' version:' . $wordselectinfo->versiondisk . ' Moodle version:'
. $CFG->version . ' release:' . $CFG->release
. " -->\n";
$output .= $format->write_combined_feedback($question->options, $question->id, $question->contextid);
return $output;
}
public function get_random_guess_score($questiondata) {
// TODO.
return 0;
}
public function get_possible_responses($questiondata) {
// TODO.
return array();
}
/**
* @param type $question The current question
* @param type $form The question editing form data
* @return type object
* Sets the default mark as 1* the number of words
* Does not allow setting any other value per word at the moment
*/
public function save_question($question, $form) {
$correctplaces = qtype_wordselect_question::get_correct_places($form->questiontext['text'], $form->delimitchars);
$form->defaultmark = count($correctplaces);
return parent::save_question($question, $form);
}
}