forked from moodleou/moodle-qtype_ddimageortext
/
questiontypebase.php
227 lines (199 loc) · 8.71 KB
/
questiontypebase.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
<?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 drag-and-drop onto image question type.
*
* @package qtype_ddimageortext
* @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->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
require_once($CFG->dirroot . '/question/format/xml/format.php');
require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
/**
* The drag-and-drop onto image question type class.
*
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ddtoimage_base extends question_type {
protected function choice_group_key() {
return 'draggroup';
}
public function get_question_options($question) {
global $DB;
$dbprefix = 'qtype_'.$this->name();
$question->options = $DB->get_record($dbprefix,
array('questionid' => $question->id), '*', MUST_EXIST);
$question->options->drags = $DB->get_records($dbprefix.'_drags',
array('questionid' => $question->id), 'no ASC', '*');
$question->options->drops = $DB->get_records($dbprefix.'_drops',
array('questionid' => $question->id), 'no ASC', '*');
parent::get_question_options($question);
}
protected function initialise_question_instance(question_definition $question, $questiondata) {
parent::initialise_question_instance($question, $questiondata);
$question->shufflechoices = $questiondata->options->shuffleanswers;
$this->initialise_combined_feedback($question, $questiondata, true);
$question->choices = array();
$choiceindexmap= array();
// Store the choices in arrays by group.
foreach ($questiondata->options->drags as $dragdata) {
$choice = $this->make_choice($dragdata);
if (array_key_exists($choice->choice_group(), $question->choices)) {
$question->choices[$choice->choice_group()][$dragdata->no] = $choice;
} else {
$question->choices[$choice->choice_group()][1] = $choice;
}
end($question->choices[$choice->choice_group()]);
$choiceindexmap[$dragdata->no] = array($choice->choice_group(),
key($question->choices[$choice->choice_group()]));
}
$question->places = array();
$question->rightchoices = array();
$i = 1;
foreach ($questiondata->options->drops as $dropdata) {
list($group, $choiceindex) = $choiceindexmap[$dropdata->choice];
$dropdata->group = $group;
$question->places[$dropdata->no] = $this->make_place($dropdata);
$question->rightchoices[$dropdata->no] = $choiceindex;
}
}
public static function constrain_image_size_in_draft_area($draftitemid, $maxwidth, $maxheight) {
global $USER;
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id');
if ($draftfiles) {
foreach ($draftfiles as $file) {
if ($file->is_directory()) {
continue;
}
$imageinfo = $file->get_imageinfo();
$width = $imageinfo['width'];
$height = $imageinfo['height'];
$mimetype = $imageinfo['mimetype'];
switch ($mimetype) {
case 'image/jpeg' :
$quality = 80;
break;
case 'image/png' :
$quality = 8;
break;
default :
$quality = null;
}
$newwidth = min($maxwidth, $width);
$newheight = min($maxheight, $height);
if ($newwidth != $width || $newheight != $height) {
$newimagefilename = $file->get_filename();
$newimagefilename =
preg_replace('!\.!', "_{$newwidth}x{$newheight}.", $newimagefilename, 1);
$newrecord = new stdClass();
$newrecord->contextid = $usercontext->id;
$newrecord->component = 'user';
$newrecord->filearea = 'draft';
$newrecord->itemid = $draftitemid;
$newrecord->filepath = '/';
$newrecord->filename = $newimagefilename;
$fs->convert_image($newrecord, $file, $newwidth, $newheight, true, $quality);
$file->delete();
}
}
}
}
/**
* Create a draft files area, import files into it and return the draft item id.
* @param qformat_xml $format
* @param array $xml an array of <file> nodes from the the parsed XML.
* @return integer draftitemid
*/
public function import_files_to_draft_file_area($format, $xml) {
global $USER;
$fs = get_file_storage();
$files = $format->import_files($xml);
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$draftitemid = file_get_unused_draft_itemid();
foreach ($files as $file) {
$record = new stdClass();
$record->contextid = $usercontext->id;
$record->component = 'user';
$record->filearea = 'draft';
$record->itemid = $draftitemid;
$record->filename = $file->name;
$record->filepath = '/';
$fs->create_file_from_string($record, $this->decode_file($file));
}
return $draftitemid;
}
/**
* Convert files into text output in the given format.
* This method is copied from qformat_default as a quick fix, as the method there is
* protected.
* @param array
* @param string encoding method
* @return string $string
*/
public function write_files($files, $indent) {
if (empty($files)) {
return '';
}
$string = '';
foreach ($files as $file) {
if ($file->is_directory()) {
continue;
}
$string .= str_repeat(' ', $indent);
$string .= '<file name="' . $file->get_filename() . '" encoding="base64">';
$string .= base64_encode($file->get_content());
$string .= "</file>\n";
}
return $string;
}
public function get_possible_responses($questiondata) {
$question = $this->make_question($questiondata);
$parts = array();
foreach ($question->places as $placeno => $place) {
$group = $place->group;
$choices = array();
foreach ($question->choices[$group] as $i => $choice) {
$summarisechoice = $choice->summarise();
$correct = $question->rightchoices[$placeno] == $i;
if ($correct) {
$choices[$choice->no] = new question_possible_response(
$summarisechoice,
$correct?1:0);
}
}
$choices[null] = question_possible_response::no_response();
$parts[$placeno] = $choices;
}
return $parts;
}
public function get_random_guess_score($questiondata) {
$question = $this->make_question($questiondata);
return $question->get_random_guess_score();
}
public function delete_question($questionid, $contextid) {
global $DB;
$DB->delete_records('qtype_'.$this->name(), array('questionid' => $questionid));
$DB->delete_records('qtype_'.$this->name().'_drags', array('questionid' => $questionid));
$DB->delete_records('qtype_'.$this->name().'_drops', array('questionid' => $questionid));
return parent::delete_question($questionid, $contextid);
}
}