MDL-47494 ddimageortext: Fix import/export and unit tests.
[moodle.git] / question / type / ddimageortext / edit_ddimageortext_form.php
CommitLineData
a2bb148a 1<?php
a2bb148a
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
d2c112fd
JP
17require_once($CFG->dirroot.'/question/type/ddimageortext/edit_ddtoimage_form_base.php');
18
a2bb148a 19/**
e79dad73 20 * Defines the editing form for the drag-and-drop images onto images question type.
a2bb148a 21 *
5b4602d2 22 * @package qtype
31077627 23 * @subpackage ddimageortext
5b4602d2
TH
24 * @copyright 2009 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a2bb148a
TH
26 */
27
42536280
TH
28defined('MOODLE_INTERNAL') || die();
29
a2bb148a 30/**
e79dad73 31 * Drag-and-drop images onto images editing form definition.
a2bb148a 32 *
5b4602d2
TH
33 * @copyright 2009 The Open University
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a2bb148a 35 */
d2c112fd 36class qtype_ddimageortext_edit_form extends qtype_ddtoimage_edit_form_base {
e79dad73 37
adebc4e5 38 public function qtype() {
31077627 39 return 'ddimageortext';
a2bb148a
TH
40 }
41
e79dad73 42 public function data_preprocessing($question) {
e79dad73
JP
43 $question = parent::data_preprocessing($question);
44 $question = $this->data_preprocessing_combined_feedback($question, true);
45 $question = $this->data_preprocessing_hints($question, true, true);
46
3c2b258d 47 $dragids = array(); // drag no -> dragid
e79dad73
JP
48 if (!empty($question->options)) {
49 $question->shuffleanswers = $question->options->shuffleanswers;
50 $question->drags = array();
51 foreach ($question->options->drags as $drag) {
52 $dragindex = $drag->no -1;
53 $question->drags[$dragindex] = array();
54 $question->drags[$dragindex]['draglabel'] = $drag->label;
55 $question->drags[$dragindex]['infinite'] = $drag->infinite;
56 $question->drags[$dragindex]['draggroup'] = $drag->draggroup;
3c2b258d 57 $dragids[$dragindex] = $drag->id;
e79dad73
JP
58 }
59 $question->drops = array();
60 foreach ($question->options->drops as $drop) {
61 $question->drops[$drop->no -1] = array();
62 $question->drops[$drop->no -1]['choice'] = $drop->choice;
63 $question->drops[$drop->no -1]['droplabel'] = $drop->label;
64 $question->drops[$drop->no -1]['xleft'] = $drop->xleft;
65 $question->drops[$drop->no -1]['ytop'] = $drop->ytop;
66 }
3c2b258d
JP
67 }
68 //initialise file picker for bgimage
69 $draftitemid = file_get_submitted_draft_itemid('bgimage');
cc5a1217 70
31077627 71 file_prepare_draft_area($draftitemid, $this->context->id, 'qtype_ddimageortext',
3c2b258d
JP
72 'bgimage', !empty($question->id) ? (int) $question->id : null,
73 self::file_picker_options());
74 $question->bgimage = $draftitemid;
75
76 //initialise file picker for dragimages
3597982a 77 list(, $imagerepeats) = $this->get_drag_item_repeats();
ce6d6b17 78 $draftitemids = optional_param('dragitem', array(), PARAM_INT);
3c2b258d
JP
79 for ($imageindex = 0; $imageindex < $imagerepeats; $imageindex++) {
80 $draftitemid = isset($draftitemids[$imageindex]) ? $draftitemids[$imageindex] :0;
81 //numbers not allowed in filearea name
82 $itemid = isset($dragids[$imageindex]) ? $dragids[$imageindex] : null;
31077627 83 file_prepare_draft_area($draftitemid, $this->context->id, 'qtype_ddimageortext',
3c2b258d
JP
84 'dragimage', $itemid, self::file_picker_options());
85 $question->dragitem[$imageindex] = $draftitemid;
e79dad73 86 }
42167fc4
JP
87 if (!empty($question->options)) {
88 foreach ($question->options->drags as $drag) {
89 $dragindex = $drag->no -1;
c4d8ad0e 90 if (!isset($question->dragitem[$dragindex])) {
42167fc4
JP
91 $fileexists = false;
92 } else {
c4d8ad0e 93 $fileexists = self::file_uploaded($question->dragitem[$dragindex]);
42167fc4 94 }
c1bf1673 95 $labelexists = (trim($question->drags[$dragindex]['draglabel']) != '');
42167fc4 96 if ($labelexists && !$fileexists) {
c4d8ad0e 97 $question->dragitemtype[$dragindex] = 'word';
42167fc4 98 } else {
c4d8ad0e 99 $question->dragitemtype[$dragindex] = 'image';
42167fc4
JP
100 }
101 }
102 }
3597982a
JP
103 $this->js_call();
104
105 return $question;
106 }
107
108
bc1bcf6e
JP
109 public function js_call() {
110 global $PAGE;
a61b274b 111 $maxsizes =new stdClass();
f20ac87f 112 $maxsizes->bgimage = new stdClass();
31077627
JP
113 $maxsizes->bgimage->width = QTYPE_DDIMAGEORTEXT_BGIMAGE_MAXWIDTH;
114 $maxsizes->bgimage->height = QTYPE_DDIMAGEORTEXT_BGIMAGE_MAXHEIGHT;
f20ac87f 115 $maxsizes->dragimage = new stdClass();
31077627
JP
116 $maxsizes->dragimage->width = QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXWIDTH;
117 $maxsizes->dragimage->height = QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXHEIGHT;
a61b274b 118
26295e67
JP
119 $params = array('maxsizes' => $maxsizes,
120 'topnode' => 'fieldset#previewareaheader');
121
31077627
JP
122 $PAGE->requires->yui_module('moodle-qtype_ddimageortext-form',
123 'M.qtype_ddimageortext.init_form',
26295e67 124 array($params));
e79dad73
JP
125 }
126
bc1bcf6e
JP
127 //drag items
128
129 protected function definition_draggable_items($mform, $itemrepeatsatstart) {
130
131 $this->repeat_elements($this->draggable_item($mform), $itemrepeatsatstart,
132 $this->draggable_items_repeated_options(),
133 'noitems', 'additems', self::ADD_NUM_ITEMS,
134 get_string('addmoreimages', 'qtype_ddimageortext'));
135 }
136
137 protected function draggable_item($mform) {
138 $draggableimageitem = array();
139
140 $draggableimageitem[] = $mform->createElement('header', 'draggableitemheader',
141 get_string('draggableitemheader', 'qtype_ddimageortext', '{no}'));
142 $dragitemtypes = array('image' => get_string('draggableimage', 'qtype_ddimageortext'),
143 'word' => get_string('draggableword', 'qtype_ddimageortext'));
144 $draggableimageitem[] = $mform->createElement('select', 'dragitemtype',
145 get_string('draggableitemtype', 'qtype_ddimageortext'),
146 $dragitemtypes,
147 array('class' => 'dragitemtype'));
148 $draggableimageitem[] = $mform->createElement('filepicker', 'dragitem', '', null,
149 self::file_picker_options());
150
151 $grouparray = array();
152 $grouparray[] = $mform->createElement('text', 'draglabel',
153 get_string('label', 'qtype_ddimageortext'),
154 array('size'=>30, 'class'=>'tweakcss'));
155 $mform->setType('draglabel', PARAM_NOTAGS);
156 $options = array();
157 for ($i = 1; $i <= self::MAX_GROUPS; $i += 1) {
158 $options[$i] = $i;
159 }
160 $grouparray[] = $mform->createElement('static', '', '', ' ' .
161 get_string('group', 'qtype_gapselect').' ');
162 $grouparray[] = $mform->createElement('select', 'draggroup',
163 get_string('group', 'qtype_gapselect'),
164 $options,
165 array('class' => 'draggroup'));
166 $grouparray[] = $mform->createElement('advcheckbox', 'infinite', ' ',
167 get_string('infinite', 'qtype_ddimageortext'));
168 $draggableimageitem[] = $mform->createElement('group', 'drags',
169 get_string('label', 'qtype_ddimageortext'), $grouparray);
170 return $draggableimageitem;
171 }
172
173 protected function draggable_items_repeated_options() {
174 $repeatedoptions = array();
175 $repeatedoptions['draggroup']['default'] = '1';
176 return $repeatedoptions;
177 }
178
179 //drop zones
180
181 protected function drop_zone($mform, $imagerepeats) {
182 $dropzoneitem = array();
649e58e9 183
bc1bcf6e
JP
184 $grouparray = array();
185 $grouparray[] = $mform->createElement('static', 'xleftlabel', '',
186 ' '.get_string('xleft', 'qtype_ddimageortext').' ');
187 $grouparray[] = $mform->createElement('text', 'xleft',
188 get_string('xleft', 'qtype_ddimageortext'),
189 array('size'=>5, 'class'=>'tweakcss'));
190 $mform->setType('xleft', PARAM_NOTAGS);
191 $grouparray[] = $mform->createElement('static', 'ytoplabel', '',
192 ' '.get_string('ytop', 'qtype_ddimageortext').' ');
193 $grouparray[] = $mform->createElement('text', 'ytop',
194 get_string('ytop', 'qtype_ddimageortext'),
195 array('size'=>5, 'class'=>'tweakcss'));
196 $mform->setType('ytop', PARAM_NOTAGS);
197 $options = array();
198
199 $options[0] = '';
200 for ($i = 1; $i <= $imagerepeats; $i += 1) {
201 $options[$i] = $i;
649e58e9 202 }
bc1bcf6e
JP
203 $grouparray[] = $mform->createElement('static', '', '', ' ' .
204 get_string('draggableitem', 'qtype_ddimageortext').' ');
205 $grouparray[] = $mform->createElement('select', 'choice',
206 get_string('draggableitem', 'qtype_ddimageortext'), $options);
207 $grouparray[] = $mform->createElement('static', '', '', ' ' .
208 get_string('label', 'qtype_ddimageortext').' ');
209 $grouparray[] = $mform->createElement('text', 'droplabel',
210 get_string('label', 'qtype_ddimageortext'),
211 array('size'=>10, 'class'=>'tweakcss'));
212 $mform->setType('droplabel', PARAM_NOTAGS);
213 $dropzone = $mform->createElement('group', 'drops',
214 get_string('dropzone', 'qtype_ddimageortext', '{no}'), $grouparray);
215 return array($dropzone);
216 }
217
218 protected function drop_zones_repeated_options() {
219 $repeatedoptions = array();
220 $repeatedoptions['choice']['default'] = '0';
221 return $repeatedoptions;
0fe992a9
JP
222 }
223
3597982a 224 public function validation($data, $files) {
e79dad73 225 $errors = parent::validation($data, $files);
87c64021 226 if (!self::file_uploaded($data['bgimage'])) {
3597982a 227 $errors["bgimage"] = get_string('formerror_nobgimage', 'qtype_'.$this->qtype());
649e58e9
JP
228 }
229
230 $allchoices = array();
231 for ($i=0; $i < $data['nodropzone']; $i++) {
232 $ytoppresent = (trim($data['drops'][$i]['ytop']) !== '');
233 $xleftpresent = (trim($data['drops'][$i]['ytop']) !== '');
234 $labelpresent = (trim($data['drops'][$i]['droplabel']) !== '');
235 $choice = $data['drops'][$i]['choice'];
236 $imagechoicepresent = ($choice !== '0');
237
238 if ($imagechoicepresent) {
239 if (!$ytoppresent) {
240 $errors["drops[$i]"] =
31077627 241 get_string('formerror_noytop', 'qtype_ddimageortext');
649e58e9
JP
242 }
243 if (!$xleftpresent) {
244 $errors["drops[$i]"] =
31077627 245 get_string('formerror_noxleft', 'qtype_ddimageortext');
649e58e9 246 }
a3be6d9d 247
c4d8ad0e
JP
248 if ($data['dragitemtype'][$choice - 1] != 'word' &&
249 !self::file_uploaded($data['dragitem'][$choice - 1])) {
250 $errors['dragitem['.($choice - 1).']'] =
31077627 251 get_string('formerror_nofile', 'qtype_ddimageortext', $i);
c4d8ad0e
JP
252 }
253
649e58e9
JP
254 if (isset($allchoices[$choice]) && !$data['drags'][$choice-1]['infinite']) {
255 $errors["drops[$i]"] =
31077627 256 get_string('formerror_multipledraginstance', 'qtype_ddimageortext', $choice);
649e58e9 257 $errors['drops['.($allchoices[$choice]).']'] =
31077627 258 get_string('formerror_multipledraginstance', 'qtype_ddimageortext', $choice);
649e58e9 259 $errors['drags['.($choice-1).']'] =
31077627 260 get_string('formerror_multipledraginstance2', 'qtype_ddimageortext', $choice);
649e58e9
JP
261 }
262 $allchoices[$choice] = $i;
263 } else {
264 if ($ytoppresent || $xleftpresent || $labelpresent) {
649e58e9 265 $errors["drops[$i]"] =
31077627 266 get_string('formerror_noimageselected', 'qtype_ddimageortext');
649e58e9
JP
267 }
268 }
649e58e9 269 }
c4d8ad0e
JP
270 for ($dragindex=0; $dragindex < $data['noitems']; $dragindex++) {
271 $label = $data['drags'][$dragindex]['draglabel'];
272 if ($data['dragitemtype'][$dragindex] == 'word') {
273 $allowedtags = '<br><sub><sup><b><i><strong><em>';
31077627 274 $errormessage = get_string('formerror_disallowedtags', 'qtype_ddimageortext');
c4d8ad0e
JP
275 } else {
276 $allowedtags = '';
31077627 277 $errormessage = get_string('formerror_noallowedtags', 'qtype_ddimageortext');
c4d8ad0e 278 }
38955535 279 if ($label != strip_tags($label, $allowedtags)) {
c4d8ad0e
JP
280 $errors["drags[{$dragindex}]"] = $errormessage;
281 }
282
283 }
e79dad73 284 return $errors;
a2bb148a 285 }
e79dad73 286
a2bb148a 287}