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