forked from moodleou/moodle-qtype_ddimageortext
/
edit_ddtoimage_form_base.php
151 lines (128 loc) · 5.82 KB
/
edit_ddtoimage_form_base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base class for editing form for the drag-and-drop images onto images question type.
*
* @package qtype
* @subpackage ddimageortext
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Base class for drag-and-drop onto images editing form definition.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class qtype_ddtoimage_edit_form_base extends question_edit_form {
const MAX_GROUPS = 8;
const START_NUM_ITEMS = 6;
const ADD_NUM_ITEMS = 3;
/**
*
* Options shared by all file pickers in the form.
*/
public static function file_picker_options() {
$filepickeroptions = array();
$filepickeroptions['accepted_types'] = array('web_image');
$filepickeroptions['maxbytes'] = 0;
$filepickeroptions['maxfiles'] = 1;
$filepickeroptions['subdirs'] = 0;
return $filepickeroptions;
}
/**
* definition_inner adds all specific fields to the form.
* @param object $mform (the form being built).
*/
protected function definition_inner($mform) {
$mform->addElement('header', 'previewareaheader',
get_string('previewareaheader', 'qtype_'.$this->qtype()));
$mform->addElement('static', 'previewarea',
get_string('previewarea', 'qtype_'.$this->qtype()),
get_string('previewareamessage', 'qtype_'.$this->qtype()));
$mform->registerNoSubmitButton('refresh');
$mform->addElement('submit', 'refresh', get_string('refresh', 'qtype_'.$this->qtype()));
$mform->closeHeaderBefore('refresh');
list($itemrepeatsatstart, $imagerepeats) = $this->get_drag_item_repeats();
$this->definition_drop_zones($mform, $imagerepeats);
$mform->addElement('advcheckbox', 'shuffleanswers', ' ',
get_string('shuffleimages', 'qtype_'.$this->qtype()));
$mform->setDefault('shuffleanswers', 0);
$mform->closeHeaderBefore('shuffleanswers');
//add the draggable image fields to the form
$this->definition_draggable_items($mform, $itemrepeatsatstart);
$this->add_combined_feedback_fields(true);
$this->add_interactive_settings(true, true);
}
protected function definition_drop_zones($mform, $imagerepeats) {
$mform->addElement('header', 'dropzoneheader',
get_string('dropzoneheader', 'qtype_'.$this->qtype()));
$mform->addElement('filepicker', 'bgimage', get_string('bgimage', 'qtype_'.$this->qtype()),
null, self::file_picker_options());
$countdropzones = 0;
if (isset($this->question->id)) {
foreach ($this->question->options->drops as $drop) {
$countdropzones = max($countdropzones, $drop->no);
}
}
if ($this->question->formoptions->repeatelements) {
$dropzonerepeatsatstart = max(self::START_NUM_ITEMS,
$countdropzones + self::ADD_NUM_ITEMS);
} else {
$dropzonerepeatsatstart = $countdropzones;
}
$this->repeat_elements($this->drop_zone($mform, $imagerepeats), $dropzonerepeatsatstart,
$this->drop_zones_repeated_options(),
'nodropzone', 'adddropzone', self::ADD_NUM_ITEMS,
get_string('addmoredropzones', 'qtype_ddimageortext'));
}
abstract protected function drop_zone($mform, $imagerepeats);
abstract protected function drop_zones_repeated_options();
abstract protected function definition_draggable_items($mform, $itemrepeatsatstart);
abstract protected function draggable_item($mform);
abstract protected function draggable_items_repeated_options();
protected function get_drag_item_repeats() {
$countimages = 0;
if (isset($this->question->id)) {
foreach ($this->question->options->drags as $drag) {
$countimages = max($countimages, $drag->no);
}
}
if ($this->question->formoptions->repeatelements) {
$itemrepeatsatstart = max(self::START_NUM_ITEMS, $countimages + self::ADD_NUM_ITEMS);
} else {
$itemrepeatsatstart = $countimages;
}
$imagerepeats = optional_param('noitems', $itemrepeatsatstart, PARAM_INT);
$addfields = optional_param('additems', '', PARAM_TEXT);
if (!empty($addfields)) {
$imagerepeats += self::ADD_NUM_ITEMS;
}
return array($itemrepeatsatstart, $imagerepeats);
}
abstract public function js_call();
public static function file_uploaded($draftitemid) {
$draftareafiles = file_get_drafarea_files($draftitemid);
do {
$draftareafile = array_shift($draftareafiles->list);
} while ($draftareafile !== null && $draftareafile->filename == '.');
if ($draftareafile === null) {
return false;
}
return true;
}
}