MDL-47494 ddimageortext: Move lang strings to AMOS.
[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
bc1bcf6e
JP
38 public function qtype() {
39 return 'ddimageortext';
40 }
41
3597982a
JP
42 public function data_preprocessing($question) {
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
47 $dragids = array(); // drag no -> dragid
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;
57 $dragids[$dragindex] = $drag->id;
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 }
67 }
68 //initialise file picker for bgimage
69 $draftitemid = file_get_submitted_draft_itemid('bgimage');
70
71 file_prepare_draft_area($draftitemid, $this->context->id, 'qtype_ddimageortext',
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
77 list(, $imagerepeats) = $this->get_drag_item_repeats();
78 $draftitemids = optional_param_array('dragitem', array(), PARAM_INT);
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;
83 file_prepare_draft_area($draftitemid, $this->context->id, 'qtype_ddimageortext',
84 'dragimage', $itemid, self::file_picker_options());
85 $question->dragitem[$imageindex] = $draftitemid;
86 }
87 if (!empty($question->options)) {
88 foreach ($question->options->drags as $drag) {
89 $dragindex = $drag->no -1;
90 if (!isset($question->dragitem[$dragindex])) {
91 $fileexists = false;
92 } else {
93 $fileexists = self::file_uploaded($question->dragitem[$dragindex]);
94 }
c1bf1673 95 $labelexists = (trim($question->drags[$dragindex]['draglabel']) != '');
3597982a
JP
96 if ($labelexists && !$fileexists) {
97 $question->dragitemtype[$dragindex] = 'word';
98 } else {
99 $question->dragitemtype[$dragindex] = 'image';
100 }
101 }
102 }
103 $this->js_call();
104
105 return $question;
106 }
107
108
bc1bcf6e
JP
109 public function js_call() {
110 global $PAGE;
111 $maxsizes =new stdClass();
112 $maxsizes->bgimage = new stdClass();
113 $maxsizes->bgimage->width = QTYPE_DDIMAGEORTEXT_BGIMAGE_MAXWIDTH;
114 $maxsizes->bgimage->height = QTYPE_DDIMAGEORTEXT_BGIMAGE_MAXHEIGHT;
115 $maxsizes->dragimage = new stdClass();
116 $maxsizes->dragimage->width = QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXWIDTH;
117 $maxsizes->dragimage->height = QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXHEIGHT;
118
119 $params = array('maxsizes' => $maxsizes,
120 'topnode' => 'fieldset#previewareaheader');
121
122 $PAGE->requires->yui_module('moodle-qtype_ddimageortext-form',
123 'M.qtype_ddimageortext.init_form',
124 array($params));
125 }
126
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();
183
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;
202 }
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();
ae64c686
TH
220 $repeatedoptions['xleft']['type'] = PARAM_INT;
221 $repeatedoptions['ytop']['type'] = PARAM_INT;
bc1bcf6e
JP
222 $repeatedoptions['choice']['default'] = '0';
223 return $repeatedoptions;
224 }
3597982a
JP
225
226 public function validation($data, $files) {
227 $errors = parent::validation($data, $files);
228 if (!self::file_uploaded($data['bgimage'])) {
229 $errors["bgimage"] = get_string('formerror_nobgimage', 'qtype_'.$this->qtype());
230 }
231
232 $allchoices = array();
233 for ($i=0; $i < $data['nodropzone']; $i++) {
234 $ytoppresent = (trim($data['drops'][$i]['ytop']) !== '');
ae64c686
TH
235 $xleftpresent = (trim($data['drops'][$i]['xleft']) !== '');
236 $ytopisint = (string) clean_param($data['drops'][$i]['ytop'], PARAM_INT) === trim($data['drops'][$i]['ytop']);
237 $xleftisint = (string) clean_param($data['drops'][$i]['xleft'], PARAM_INT) === trim($data['drops'][$i]['xleft']);
3597982a
JP
238 $labelpresent = (trim($data['drops'][$i]['droplabel']) !== '');
239 $choice = $data['drops'][$i]['choice'];
240 $imagechoicepresent = ($choice !== '0');
241
242 if ($imagechoicepresent) {
243 if (!$ytoppresent) {
ae64c686
TH
244 $errors["drops[$i]"] = get_string('formerror_noytop', 'qtype_ddimageortext');
245 } else if (!$ytopisint) {
246 $errors["drops[$i]"] = get_string('formerror_notintytop', 'qtype_ddimageortext');
3597982a
JP
247 }
248 if (!$xleftpresent) {
ae64c686
TH
249 $errors["drops[$i]"] = get_string('formerror_noxleft', 'qtype_ddimageortext');
250 } else if (!$xleftisint) {
251 $errors["drops[$i]"] = get_string('formerror_notintxleft', 'qtype_ddimageortext');
3597982a
JP
252 }
253
254 if ($data['dragitemtype'][$choice - 1] != 'word' &&
255 !self::file_uploaded($data['dragitem'][$choice - 1])) {
256 $errors['dragitem['.($choice - 1).']'] =
257 get_string('formerror_nofile', 'qtype_ddimageortext', $i);
258 }
259
260 if (isset($allchoices[$choice]) && !$data['drags'][$choice-1]['infinite']) {
261 $errors["drops[$i]"] =
ae64c686 262 get_string('formerror_multipledraginstance', 'qtype_ddimageortext', $choice);
3597982a 263 $errors['drops['.($allchoices[$choice]).']'] =
ae64c686 264 get_string('formerror_multipledraginstance', 'qtype_ddimageortext', $choice);
3597982a 265 $errors['drags['.($choice-1).']'] =
ae64c686 266 get_string('formerror_multipledraginstance2', 'qtype_ddimageortext', $choice);
3597982a
JP
267 }
268 $allchoices[$choice] = $i;
269 } else {
270 if ($ytoppresent || $xleftpresent || $labelpresent) {
271 $errors["drops[$i]"] =
ae64c686 272 get_string('formerror_noimageselected', 'qtype_ddimageortext');
3597982a
JP
273 }
274 }
275 }
276 for ($dragindex=0; $dragindex < $data['noitems']; $dragindex++) {
277 $label = $data['drags'][$dragindex]['draglabel'];
278 if ($data['dragitemtype'][$dragindex] == 'word') {
279 $allowedtags = '<br><sub><sup><b><i><strong><em>';
280 $errormessage = get_string('formerror_disallowedtags', 'qtype_ddimageortext');
281 } else {
282 $allowedtags = '';
283 $errormessage = get_string('formerror_noallowedtags', 'qtype_ddimageortext');
284 }
285 if ($label != strip_tags($label, $allowedtags)) {
286 $errors["drags[{$dragindex}]"] = $errormessage;
287 }
288
289 }
290 return $errors;
291 }
292
a2bb148a 293}