MDL-47494 ddimageortext: Bump the version number for the 1.6 release.
[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();
4139151f 55 $question->draglabel[$dragindex] = $drag->label;
3597982a
JP
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 }
4139151f 96 $labelexists = (trim($question->draglabel[$dragindex]) != '');
3597982a 97 if ($labelexists && !$fileexists) {
4139151f 98 $question->drags[$dragindex]['dragitemtype'] = 'word';
3597982a 99 } else {
4139151f 100 $question->drags[$dragindex]['dragitemtype'] = 'image';
3597982a
JP
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,
4139151f 121 'topnode' => 'fieldset#id_previewareaheader');
bc1bcf6e
JP
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) {
4139151f
CC
131 $mform->addElement('header', 'draggableitemheader',
132 get_string('draggableitems', 'qtype_ddimageortext'));
133 $mform->addElement('advcheckbox', 'shuffleanswers', ' ',
e9a2711b 134 get_string('shuffleimages', 'qtype_'.$this->qtype()));
4139151f 135 $mform->setDefault('shuffleanswers', 0);
bc1bcf6e
JP
136 $this->repeat_elements($this->draggable_item($mform), $itemrepeatsatstart,
137 $this->draggable_items_repeated_options(),
138 'noitems', 'additems', self::ADD_NUM_ITEMS,
4139151f 139 get_string('addmoreimages', 'qtype_ddimageortext'), true);
bc1bcf6e
JP
140 }
141
142 protected function draggable_item($mform) {
143 $draggableimageitem = array();
144
4139151f 145 $grouparray = array();
bc1bcf6e
JP
146 $dragitemtypes = array('image' => get_string('draggableimage', 'qtype_ddimageortext'),
147 'word' => get_string('draggableword', 'qtype_ddimageortext'));
4139151f 148 $grouparray[] = $mform->createElement('select', 'dragitemtype',
bc1bcf6e
JP
149 get_string('draggableitemtype', 'qtype_ddimageortext'),
150 $dragitemtypes,
151 array('class' => 'dragitemtype'));
bc1bcf6e
JP
152 $options = array();
153 for ($i = 1; $i <= self::MAX_GROUPS; $i += 1) {
154 $options[$i] = $i;
155 }
bc1bcf6e
JP
156 $grouparray[] = $mform->createElement('select', 'draggroup',
157 get_string('group', 'qtype_gapselect'),
158 $options,
159 array('class' => 'draggroup'));
160 $grouparray[] = $mform->createElement('advcheckbox', 'infinite', ' ',
161 get_string('infinite', 'qtype_ddimageortext'));
162 $draggableimageitem[] = $mform->createElement('group', 'drags',
4139151f
CC
163 get_string('draggableitemheader', 'qtype_ddimageortext', '{no}'), $grouparray);
164
165 $draggableimageitem[] = $mform->createElement('filepicker', 'dragitem', '', null,
166 self::file_picker_options());
167
168 $draggableimageitem[] = $mform->createElement('text', 'draglabel',
169 get_string('label', 'qtype_ddimageortext'),
170 array('size'=>30, 'class'=>'tweakcss'));
e7a04b04 171 $mform->setType('draglabel', PARAM_RAW); // These are validated manually.
bc1bcf6e
JP
172 return $draggableimageitem;
173 }
174
175 protected function draggable_items_repeated_options() {
176 $repeatedoptions = array();
177 $repeatedoptions['draggroup']['default'] = '1';
178 return $repeatedoptions;
179 }
180
81c87fa7 181 // Drop zones.
bc1bcf6e
JP
182
183 protected function drop_zone($mform, $imagerepeats) {
184 $dropzoneitem = array();
185
186 $grouparray = array();
bc1bcf6e
JP
187 $grouparray[] = $mform->createElement('text', 'xleft',
188 get_string('xleft', 'qtype_ddimageortext'),
189 array('size'=>5, 'class'=>'tweakcss'));
bc1bcf6e
JP
190 $grouparray[] = $mform->createElement('text', 'ytop',
191 get_string('ytop', 'qtype_ddimageortext'),
192 array('size'=>5, 'class'=>'tweakcss'));
bc1bcf6e
JP
193 $options = array();
194
195 $options[0] = '';
196 for ($i = 1; $i <= $imagerepeats; $i += 1) {
197 $options[$i] = $i;
198 }
bc1bcf6e
JP
199 $grouparray[] = $mform->createElement('select', 'choice',
200 get_string('draggableitem', 'qtype_ddimageortext'), $options);
bc1bcf6e
JP
201 $grouparray[] = $mform->createElement('text', 'droplabel',
202 get_string('label', 'qtype_ddimageortext'),
203 array('size'=>10, 'class'=>'tweakcss'));
204 $mform->setType('droplabel', PARAM_NOTAGS);
205 $dropzone = $mform->createElement('group', 'drops',
206 get_string('dropzone', 'qtype_ddimageortext', '{no}'), $grouparray);
207 return array($dropzone);
208 }
209
210 protected function drop_zones_repeated_options() {
211 $repeatedoptions = array();
4139151f
CC
212 // The next two are PARAM_RAW becuase we need to distinguish 0 and ''.
213 // We do the necessary validation in the validation method.
214 $repeatedoptions['drops[xleft]']['type'] = PARAM_RAW;
215 $repeatedoptions['drops[ytop]']['type'] = PARAM_RAW;
874104d5 216 $repeatedoptions['drops[droplabel]']['type'] = PARAM_RAW;
bc1bcf6e
JP
217 $repeatedoptions['choice']['default'] = '0';
218 return $repeatedoptions;
219 }
3597982a 220
81c87fa7 221 public function validation($data, $files) {
3597982a
JP
222 $errors = parent::validation($data, $files);
223 if (!self::file_uploaded($data['bgimage'])) {
224 $errors["bgimage"] = get_string('formerror_nobgimage', 'qtype_'.$this->qtype());
225 }
226
227 $allchoices = array();
228 for ($i=0; $i < $data['nodropzone']; $i++) {
229 $ytoppresent = (trim($data['drops'][$i]['ytop']) !== '');
ae64c686
TH
230 $xleftpresent = (trim($data['drops'][$i]['xleft']) !== '');
231 $ytopisint = (string) clean_param($data['drops'][$i]['ytop'], PARAM_INT) === trim($data['drops'][$i]['ytop']);
232 $xleftisint = (string) clean_param($data['drops'][$i]['xleft'], PARAM_INT) === trim($data['drops'][$i]['xleft']);
3597982a
JP
233 $labelpresent = (trim($data['drops'][$i]['droplabel']) !== '');
234 $choice = $data['drops'][$i]['choice'];
235 $imagechoicepresent = ($choice !== '0');
236
237 if ($imagechoicepresent) {
238 if (!$ytoppresent) {
ae64c686
TH
239 $errors["drops[$i]"] = get_string('formerror_noytop', 'qtype_ddimageortext');
240 } else if (!$ytopisint) {
241 $errors["drops[$i]"] = get_string('formerror_notintytop', 'qtype_ddimageortext');
3597982a
JP
242 }
243 if (!$xleftpresent) {
ae64c686
TH
244 $errors["drops[$i]"] = get_string('formerror_noxleft', 'qtype_ddimageortext');
245 } else if (!$xleftisint) {
246 $errors["drops[$i]"] = get_string('formerror_notintxleft', 'qtype_ddimageortext');
3597982a
JP
247 }
248
4139151f 249 if ($data['drags'][$choice - 1]['dragitemtype'] != 'word' &&
3597982a
JP
250 !self::file_uploaded($data['dragitem'][$choice - 1])) {
251 $errors['dragitem['.($choice - 1).']'] =
252 get_string('formerror_nofile', 'qtype_ddimageortext', $i);
253 }
254
255 if (isset($allchoices[$choice]) && !$data['drags'][$choice-1]['infinite']) {
256 $errors["drops[$i]"] =
ae64c686 257 get_string('formerror_multipledraginstance', 'qtype_ddimageortext', $choice);
3597982a 258 $errors['drops['.($allchoices[$choice]).']'] =
ae64c686 259 get_string('formerror_multipledraginstance', 'qtype_ddimageortext', $choice);
3597982a 260 $errors['drags['.($choice-1).']'] =
ae64c686 261 get_string('formerror_multipledraginstance2', 'qtype_ddimageortext', $choice);
3597982a
JP
262 }
263 $allchoices[$choice] = $i;
264 } else {
265 if ($ytoppresent || $xleftpresent || $labelpresent) {
266 $errors["drops[$i]"] =
ae64c686 267 get_string('formerror_noimageselected', 'qtype_ddimageortext');
3597982a
JP
268 }
269 }
270 }
271 for ($dragindex=0; $dragindex < $data['noitems']; $dragindex++) {
4139151f
CC
272 $label = $data['draglabel'][$dragindex];
273 if ($data['drags'][$dragindex]['dragitemtype'] == 'word') {
3597982a
JP
274 $allowedtags = '<br><sub><sup><b><i><strong><em>';
275 $errormessage = get_string('formerror_disallowedtags', 'qtype_ddimageortext');
276 } else {
277 $allowedtags = '';
278 $errormessage = get_string('formerror_noallowedtags', 'qtype_ddimageortext');
279 }
280 if ($label != strip_tags($label, $allowedtags)) {
281 $errors["drags[{$dragindex}]"] = $errormessage;
282 }
283
284 }
285 return $errors;
286 }
a2bb148a 287}