ILIAS  release_8 Revision v8.24
class.ilImagemapFileInputGUI.php
Go to the documentation of this file.
1<?php
2
27{
28 protected $areas = array();
29 protected $image_path = "";
30 protected $image_path_web = "";
31 protected $line_color = "";
32
34
41 public function __construct($a_title = "", $a_postvar = "")
42 {
43 parent::__construct($a_title, $a_postvar);
44 }
45
47 {
48 $this->pointsUncheckedFieldEnabled = (bool) $pointsUncheckedFieldEnabled;
49 }
50
51 public function getPointsUncheckedFieldEnabled(): bool
52 {
54 }
55
56 public function setAreas($a_areas): void
57 {
58 $this->areas = $a_areas;
59 }
60
61 public function getLineColor(): string
62 {
63 return $this->line_color;
64 }
65
66 public function setLineColor($a_color): void
67 {
68 $this->line_color = $a_color;
69 }
70
71 public function getImagePath(): string
72 {
73 return $this->image_path;
74 }
75
76 public function setImagePath($a_path): void
77 {
78 $this->image_path = $a_path;
79 }
80
81 public function getImagePathWeb(): string
82 {
84 }
85
86 public function setImagePathWeb($a_path_web): void
87 {
88 $this->image_path_web = $a_path_web;
89 }
90
91 public function setAreasByArray($a_areas): void
92 {
93 if (is_array($a_areas['name'])) {
94 $this->areas = array();
95 foreach ($a_areas['name'] as $idx => $name) {
96 if ($this->getPointsUncheckedFieldEnabled() && isset($a_areas['points_unchecked'])) {
97 $pointsUnchecked = $a_areas['points_unchecked'][$idx];
98 } else {
99 $pointsUnchecked = 0.0;
100 }
101
102 array_push($this->areas, new ASS_AnswerImagemap(
103 $name,
104 $a_areas['points'][$idx],
105 $idx,
106 $a_areas['coords'][$idx],
107 $a_areas['shape'][$idx],
108 -1,
109 $pointsUnchecked
110 ));
111
112 $imagemap = new ASS_AnswerImagemap($name, $a_areas['points'][$idx], $idx, 0, -1);
113 $imagemap->setCoords($a_areas['coords'][$idx]);
114 $imagemap->setArea($a_areas['shape'][$idx]);
115 $imagemap->setPointsUnchecked($pointsUnchecked);
116 array_push($this->areas, $imagemap);
117 }
118 }
119 }
120
121 public function getAreas(): array
122 {
123 return $this->areas;
124 }
125
131 public function setValueByArray(array $a_values): void
132 {
133 if (isset($a_value[$this->getPostVar() . '_name'])) {
134 $this->setValue($a_values[$this->getPostVar() . '_name']);
135 }
136 if (isset($a_value[$this->getPostVar()]['coords'])) {
137 $this->setAreasByArray($a_values[$this->getPostVar()]['coords']);
138 }
139 }
140
141 public function setValue($a_value): void
142 {
143 parent::setValue($a_value);
144 }
145
146 public function getInput(): array
147 {
148 return parent::getInput();
149 }
150
151 private function getPostBody(): array
152 {
153 $val = $this->arrayArray($this->getPostVar());
155 return $val;
156 }
157
162 public function checkInput(): bool
163 {
165
166 // remove trailing '/'
167 $_FILES[$this->getPostVar()]["name"] = rtrim($_FILES[$this->getPostVar()]["name"], '/');
168
169 $filename = $_FILES[$this->getPostVar()]["name"];
170 $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
171 $suffix = $filename_arr["extension"] ?? '';
172 $mimetype = $_FILES[$this->getPostVar()]["type"];
173 $size_bytes = $_FILES[$this->getPostVar()]["size"];
174 $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
175 $error = $_FILES[$this->getPostVar()]["error"];
176
177 // error handling
178 if ($error > 0) {
179 switch ($error) {
180 case UPLOAD_ERR_FORM_SIZE:
181 case UPLOAD_ERR_INI_SIZE:
182 $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
183 return false;
184 break;
185
186 case UPLOAD_ERR_PARTIAL:
187 $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
188 return false;
189 break;
190
191 case UPLOAD_ERR_NO_FILE:
192 if ($this->getRequired()) {
193 if (!strlen($this->getValue())) {
194 $this->setAlert($lng->txt("form_msg_file_no_upload"));
195 return false;
196 }
197 }
198 break;
199
200 case UPLOAD_ERR_NO_TMP_DIR:
201 $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
202 return false;
203 break;
204
205 case UPLOAD_ERR_CANT_WRITE:
206 $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
207 return false;
208 break;
209
210 case UPLOAD_ERR_EXTENSION:
211 $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
212 return false;
213 break;
214 }
215 }
216
217 // check suffixes
218 if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
219 is_array($this->getSuffixes())) {
220 if (!in_array(strtolower($suffix), $this->getSuffixes())) {
221 $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
222 return false;
223 }
224 }
225
226 // virus handling
227 if ($_FILES[$this->getPostVar()]["tmp_name"] != "") {
228 $vir = ilVirusScanner::virusHandling($temp_name, $filename);
229 if ($vir[0] == false) {
230 $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
231 return false;
232 }
233 }
234
235 $post_body = $this->getPostBody();
236
237 $max = 0;
238 if (isset($post_body['coords']) && is_array($post_body['coords']['name'])) {
239 foreach ($post_body['coords']['name'] as $idx => $name) {
240 if ($this->getRequired() && (
241 !isset($post_body['coords']['points'][$idx]) ||
242 $post_body['coords']['points'][$idx] == ''
243 )) {
244 $this->setAlert($lng->txt('form_msg_area_missing_points'));
245 return false;
246 }
247
248 if ((!is_numeric($post_body['coords']['points'][$idx]))) {
249 $this->setAlert($lng->txt('form_msg_numeric_value_required'));
250 return false;
251 }
252
253 if ($post_body['coords']['points'][$idx] > 0) {
254 $max = $post_body['coords']['points'][$idx];
255 }
256 }
257 }
258
259 if ($max == 0 && (!$filename) && !$_FILES['imagemapfile']['tmp_name']) {
260 $this->setAlert($lng->txt("enter_enough_positive_points"));
261 return false;
262 }
263 return true;
264 }
265
269 public function insert(ilTemplate $a_tpl): void
270 {
272
273 $template = new ilTemplate("tpl.prop_imagemap_file.html", true, true, "Modules/TestQuestionPool");
274
275 $this->outputSuffixes($template, "allowed_image_suffixes");
276
277 if ($this->getImage() != "") {
278 if (strlen($this->getValue())) {
279 $template->setCurrentBlock("has_value");
280 $template->setVariable("TEXT_IMAGE_NAME", $this->getValue());
281 $template->setVariable("POST_VAR_D", $this->getPostVar());
282 $template->parseCurrentBlock();
283 }
284 $template->setCurrentBlock("image");
285 if (count($this->getAreas())) {
286 $preview = new ilImagemapPreview($this->getImagePath() . $this->getValue());
287 foreach ($this->getAreas() as $index => $area) {
288 $preview->addArea($index, $area->getArea(), $area->getCoords(), $area->getAnswertext(), "", "", true, $this->getLineColor());
289 }
290 $preview->createPreview();
291 $imagepath = $this->getImagePathWeb() . $preview->getPreviewFilename($this->getImagePath(), $this->getValue()) . "?img=" . time();
292 $template->setVariable("SRC_IMAGE", $imagepath);
293 } else {
294 $template->setVariable("SRC_IMAGE", $this->getImage());
295 }
296 $template->setVariable("ALT_IMAGE", $this->getAlt());
297 $template->setVariable("POST_VAR_D", $this->getPostVar());
298 $template->setVariable(
299 "TXT_DELETE_EXISTING",
300 $lng->txt("delete_existing_file")
301 );
302 $template->setVariable("TEXT_ADD_RECT", $lng->txt('add_rect'));
303 $template->setVariable("TEXT_ADD_CIRCLE", $lng->txt('add_circle'));
304 $template->setVariable("TEXT_ADD_POLY", $lng->txt('add_poly'));
305 $template->parseCurrentBlock();
306 }
307
308 if (is_array($this->getAreas()) && $this->getAreas()) {
309 $counter = 0;
310 foreach ($this->getAreas() as $area) {
311 if (strlen($area->getPoints())) {
312 $template->setCurrentBlock('area_points_value');
313 $template->setVariable('VALUE_POINTS', $area->getPoints());
314 $template->parseCurrentBlock();
315 }
316 if ($this->getPointsUncheckedFieldEnabled()) {
317 if (strlen($area->getPointsUnchecked())) {
318 $template->setCurrentBlock('area_points_unchecked_value');
319 $template->setVariable('VALUE_POINTS_UNCHECKED', $area->getPointsUnchecked());
320 $template->parseCurrentBlock();
321 }
322
323 $template->setCurrentBlock('area_points_unchecked_field');
324 $template->parseCurrentBlock();
325 }
326 if (strlen($area->getAnswertext())) {
327 $template->setCurrentBlock('area_name_value');
328 $template->setVariable('VALUE_NAME', htmlspecialchars($area->getAnswertext()));
329 $template->parseCurrentBlock();
330 }
331 $template->setCurrentBlock('row');
332 $template->setVariable('POST_VAR_R', $this->getPostVar());
333 $template->setVariable('TEXT_SHAPE', strtoupper($area->getArea()));
334 $template->setVariable('VALUE_SHAPE', $area->getArea());
335 $coords = preg_replace("/(\d+,\d+,)/", "\$1 ", $area->getCoords());
336 $template->setVariable('VALUE_COORDINATES', $area->getCoords());
337 $template->setVariable('TEXT_COORDINATES', $coords);
338 $template->setVariable('COUNTER', $counter);
339 $template->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
340 $template->parseCurrentBlock();
341 $counter++;
342 }
343 $template->setCurrentBlock("areas");
344 $template->setVariable("TEXT_NAME", $lng->txt("ass_imap_hint"));
345 if ($this->getPointsUncheckedFieldEnabled()) {
346 $template->setVariable("TEXT_POINTS", $lng->txt("points_checked"));
347
348 $template->setCurrentBlock('area_points_unchecked_head');
349 $template->setVariable("TEXT_POINTS_UNCHECKED", $lng->txt("points_unchecked"));
350 $template->parseCurrentBlock();
351 } else {
352 $template->setVariable("TEXT_POINTS", $lng->txt("points"));
353 }
354 $template->setVariable("TEXT_SHAPE", $lng->txt("shape"));
355 $template->setVariable("TEXT_COORDINATES", $lng->txt("coordinates"));
356 $template->setVariable("TEXT_COMMANDS", $lng->txt("actions"));
357 $template->parseCurrentBlock();
358 }
359
360 $template->setVariable("POST_VAR", $this->getPostVar());
361 $template->setVariable("ID", $this->getFieldId());
362 $template->setVariable("TXT_BROWSE", $lng->txt("select_file"));
363 $template->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice") . " " .
364 $this->getMaxFileSizeString());
365
366 $a_tpl->setCurrentBlock("prop_generic");
367 $a_tpl->setVariable("PROP_GENERIC", $template->get());
368 $a_tpl->parseCurrentBlock();
369
370 $this->tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
371 $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagemap.js");
372 }
373}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
outputSuffixes(ilTemplate $a_tpl, string $a_block="allowed_suffixes")
static get(string $a_glyph, string $a_text="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setPointsUncheckedFieldEnabled($pointsUncheckedFieldEnabled)
setValueByArray(array $a_values)
Set value by array.
insert(ilTemplate $a_tpl)
Insert property html.
__construct($a_title="", $a_postvar="")
Constructor.
checkInput()
Check input, strip slashes etc.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
$preview
Definition: imgupload.php:81
if($format !==null) $name
Definition: metadata.php:247
$index
Definition: metadata.php:145
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc