MDL-47494 ddimageortext: DD markers / onto image: form, drag items before drop zones...
[moodle.git] / question / type / ddimageortext / edit_ddtoimage_form_base.php
CommitLineData
d2c112fd
JP
1<?php
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
17/**
18 * Base class for editing form for the drag-and-drop images onto images question type.
19 *
20 * @package qtype
21 * @subpackage ddimageortext
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Base class for drag-and-drop onto images editing form definition.
30 *
31 * @copyright 2011 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
bc1bcf6e 34abstract class qtype_ddtoimage_edit_form_base extends question_edit_form {
d2c112fd
JP
35 const MAX_GROUPS = 8;
36 const START_NUM_ITEMS = 6;
37 const ADD_NUM_ITEMS = 3;
38
d2c112fd
JP
39 /**
40 *
41 * Options shared by all file pickers in the form.
42 */
43 public static function file_picker_options() {
44 $filepickeroptions = array();
45 $filepickeroptions['accepted_types'] = array('web_image');
46 $filepickeroptions['maxbytes'] = 0;
47 $filepickeroptions['maxfiles'] = 1;
48 $filepickeroptions['subdirs'] = 0;
49 return $filepickeroptions;
50 }
51
d2c112fd
JP
52 /**
53 * definition_inner adds all specific fields to the form.
e9a2711b 54 * @param MoodleQuickForm $mform (the form being built).
d2c112fd
JP
55 */
56 protected function definition_inner($mform) {
57
58 $mform->addElement('header', 'previewareaheader',
3597982a 59 get_string('previewareaheader', 'qtype_'.$this->qtype()));
e9a2711b
TH
60 $mform->setExpanded('previewareaheader');
61 $mform->addElement('static', 'previewarea', '',
3597982a 62 get_string('previewareamessage', 'qtype_'.$this->qtype()));
d2c112fd
JP
63
64 $mform->registerNoSubmitButton('refresh');
3597982a 65 $mform->addElement('submit', 'refresh', get_string('refresh', 'qtype_'.$this->qtype()));
4139151f
CC
66 $mform->addElement('filepicker', 'bgimage', get_string('bgimage', 'qtype_'.$this->qtype()),
67 null, self::file_picker_options());
68 $mform->closeHeaderBefore('dropzoneheader');
d2c112fd 69
145f46ed 70 // Add the draggable image fields & drop zones to the form.
bc1bcf6e 71 list($itemrepeatsatstart, $imagerepeats) = $this->get_drag_item_repeats();
bc1bcf6e 72 $this->definition_draggable_items($mform, $itemrepeatsatstart);
145f46ed 73 $this->definition_drop_zones($mform, $imagerepeats);
d2c112fd
JP
74
75 $this->add_combined_feedback_fields(true);
76 $this->add_interactive_settings(true, true);
77 }
78
79 protected function definition_drop_zones($mform, $imagerepeats) {
80 $mform->addElement('header', 'dropzoneheader',
3597982a 81 get_string('dropzoneheader', 'qtype_'.$this->qtype()));
d2c112fd 82
d2c112fd
JP
83 $countdropzones = 0;
84 if (isset($this->question->id)) {
85 foreach ($this->question->options->drops as $drop) {
86 $countdropzones = max($countdropzones, $drop->no);
87 }
88 }
4139151f
CC
89
90 if (!$countdropzones) {
91 $countdropzones = self::START_NUM_ITEMS;
d2c112fd 92 }
4139151f 93 $dropzonerepeatsatstart = $countdropzones;
d2c112fd
JP
94
95 $this->repeat_elements($this->drop_zone($mform, $imagerepeats), $dropzonerepeatsatstart,
96 $this->drop_zones_repeated_options(),
97 'nodropzone', 'adddropzone', self::ADD_NUM_ITEMS,
0cf0f860 98 get_string('addmoredropzones', 'qtype_ddimageortext'), true);
d2c112fd 99 }
bc1bcf6e 100 abstract protected function drop_zone($mform, $imagerepeats);
d2c112fd 101
bc1bcf6e 102 abstract protected function drop_zones_repeated_options();
d2c112fd 103
e9a2711b 104 abstract protected function definition_draggable_items($mform, $itemrepeatsatstart);
d2c112fd 105
bc1bcf6e 106 abstract protected function draggable_item($mform);
d2c112fd 107
bc1bcf6e 108 abstract protected function draggable_items_repeated_options();
d2c112fd 109
bc1bcf6e
JP
110 protected function get_drag_item_repeats() {
111 $countimages = 0;
112 if (isset($this->question->id)) {
113 foreach ($this->question->options->drags as $drag) {
114 $countimages = max($countimages, $drag->no);
115 }
d2c112fd 116 }
e9a2711b
TH
117
118 if (!$countimages) {
119 $countimages = self::START_NUM_ITEMS;
bc1bcf6e 120 }
e9a2711b
TH
121 $itemrepeatsatstart = $countimages;
122
bc1bcf6e 123 $imagerepeats = optional_param('noitems', $itemrepeatsatstart, PARAM_INT);
874104d5
TH
124 $addfields = optional_param('additems', false, PARAM_BOOL);
125 if ($addfields) {
bc1bcf6e
JP
126 $imagerepeats += self::ADD_NUM_ITEMS;
127 }
128 return array($itemrepeatsatstart, $imagerepeats);
d2c112fd
JP
129 }
130
bc1bcf6e 131 abstract public function js_call();
d2c112fd
JP
132
133 public static function file_uploaded($draftitemid) {
134 $draftareafiles = file_get_drafarea_files($draftitemid);
135 do {
136 $draftareafile = array_shift($draftareafiles->list);
137 } while ($draftareafile !== null && $draftareafile->filename == '.');
138 if ($draftareafile === null) {
139 return false;
140 }
141 return true;
142 }
143
d2c112fd
JP
144
145}