ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilImagemapFileInputGUI.php
Go to the documentation of this file.
1 <?php
2 
20 use ILIAS\UI\Component\Symbol\Glyph\Factory as GlyphFactory;
21 
30 {
31  protected $areas = array();
32  protected $image_path = "";
33  protected $image_path_web = "";
34  protected $line_color = "";
35 
36  protected $pointsUncheckedFieldEnabled = false;
37 
38  protected GlyphFactory $glyph_factory;
39  protected Renderer $renderer;
40 
47  public function __construct($a_title = "", $a_postvar = "")
48  {
49  parent::__construct($a_title, $a_postvar);
50 
51  global $DIC;
52  $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
53  $this->renderer = $DIC->ui()->renderer();
54  }
55 
57  {
58  $this->pointsUncheckedFieldEnabled = (bool) $pointsUncheckedFieldEnabled;
59  }
60 
61  public function getPointsUncheckedFieldEnabled(): bool
62  {
64  }
65 
66  public function setAreas($a_areas): void
67  {
68  $this->areas = $a_areas;
69  }
70 
71  public function getLineColor(): string
72  {
73  return $this->line_color;
74  }
75 
76  public function setLineColor($a_color): void
77  {
78  $this->line_color = $a_color;
79  }
80 
81  public function getImagePath(): string
82  {
83  return $this->image_path;
84  }
85 
86  public function setImagePath($a_path): void
87  {
88  $this->image_path = $a_path;
89  }
90 
91  public function getImagePathWeb(): string
92  {
93  return $this->image_path_web;
94  }
95 
96  public function setImagePathWeb($a_path_web): void
97  {
98  $this->image_path_web = $a_path_web;
99  }
100 
101  public function setAreasByArray($a_areas): void
102  {
103  if (is_array($a_areas['name'])) {
104  $this->areas = array();
105  foreach ($a_areas['name'] as $idx => $name) {
106  if ($this->getPointsUncheckedFieldEnabled() && isset($a_areas['points_unchecked'])) {
107  $pointsUnchecked = $a_areas['points_unchecked'][$idx];
108  } else {
109  $pointsUnchecked = 0.0;
110  }
111 
112  array_push($this->areas, new ASS_AnswerImagemap(
113  $name,
114  $a_areas['points'][$idx],
115  $idx,
116  $a_areas['coords'][$idx],
117  $a_areas['shape'][$idx],
118  -1,
119  $pointsUnchecked
120  ));
121 
122  $imagemap = new ASS_AnswerImagemap($name, $a_areas['points'][$idx], $idx, 0, -1);
123  $imagemap->setCoords($a_areas['coords'][$idx]);
124  $imagemap->setArea($a_areas['shape'][$idx]);
125  $imagemap->setPointsUnchecked($pointsUnchecked);
126  array_push($this->areas, $imagemap);
127  }
128  }
129  }
130 
131  public function getAreas(): array
132  {
133  return $this->areas;
134  }
135 
141  public function setValueByArray(array $a_values): void
142  {
143  if (isset($a_value[$this->getPostVar() . '_name'])) {
144  $this->setValue($a_values[$this->getPostVar() . '_name']);
145  }
146  if (isset($a_value[$this->getPostVar()]['coords'])) {
147  $this->setAreasByArray($a_values[$this->getPostVar()]['coords']);
148  }
149  }
150 
151  public function setValue($a_value): void
152  {
153  parent::setValue($a_value);
154  }
155 
156  public function getInput(): array
157  {
158  return parent::getInput();
159  }
160 
161  private function getPostBody(): array
162  {
163  $val = $this->arrayArray($this->getPostVar());
165  return $val;
166  }
167 
172  public function checkInput(): bool
173  {
174  $lng = $this->lng;
175 
176  // remove trailing '/'
177  $_FILES[$this->getPostVar()]["name"] = rtrim($_FILES[$this->getPostVar()]["name"], '/');
178 
179  $filename = $_FILES[$this->getPostVar()]["name"];
180  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
181  $suffix = $filename_arr["extension"] ?? '';
182  $mimetype = $_FILES[$this->getPostVar()]["type"];
183  $size_bytes = $_FILES[$this->getPostVar()]["size"];
184  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
185  $error = $_FILES[$this->getPostVar()]["error"];
186 
187  // error handling
188  if ($error > 0) {
189  switch ($error) {
190  case UPLOAD_ERR_FORM_SIZE:
191  case UPLOAD_ERR_INI_SIZE:
192  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
193  return false;
194  break;
195 
196  case UPLOAD_ERR_PARTIAL:
197  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
198  return false;
199  break;
200 
201  case UPLOAD_ERR_NO_FILE:
202  if ($this->getRequired()) {
203  if (!strlen($this->getValue())) {
204  $this->setAlert($lng->txt("form_msg_file_no_upload"));
205  return false;
206  }
207  }
208  break;
209 
210  case UPLOAD_ERR_NO_TMP_DIR:
211  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
212  return false;
213  break;
214 
215  case UPLOAD_ERR_CANT_WRITE:
216  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
217  return false;
218  break;
219 
220  case UPLOAD_ERR_EXTENSION:
221  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
222  return false;
223  break;
224  }
225  }
226 
227  // check suffixes
228  if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
229  is_array($this->getSuffixes())) {
230  if (!in_array(strtolower($suffix), $this->getSuffixes())) {
231  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
232  return false;
233  }
234  }
235 
236  // virus handling
237  if ($_FILES[$this->getPostVar()]["tmp_name"] != "") {
238  $vir = ilVirusScanner::virusHandling($temp_name, $filename);
239  if ($vir[0] == false) {
240  $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
241  return false;
242  }
243  }
244 
245  $post_body = $this->getPostBody();
246 
247  $max = 0;
248  if (isset($post_body['coords']) && is_array($post_body['coords']['name'])) {
249  foreach ($post_body['coords']['name'] as $idx => $name) {
250  if ($this->getRequired() && (
251  !isset($post_body['coords']['points'][$idx]) ||
252  $post_body['coords']['points'][$idx] == ''
253  )) {
254  $this->setAlert($lng->txt('form_msg_area_missing_points'));
255  return false;
256  }
257 
258  if ((!is_numeric($post_body['coords']['points'][$idx]))) {
259  $this->setAlert($lng->txt('form_msg_numeric_value_required'));
260  return false;
261  }
262 
263  if ($post_body['coords']['points'][$idx] > 0) {
264  $max = $post_body['coords']['points'][$idx];
265  }
266  }
267  }
268 
269  if ($max == 0 && (!$filename) && !$_FILES['imagemapfile']['tmp_name']) {
270  $this->setAlert($lng->txt("enter_enough_positive_points"));
271  return false;
272  }
273  return true;
274  }
275 
279  public function insert(ilTemplate $a_tpl): void
280  {
281  $lng = $this->lng;
282 
283  $template = new ilTemplate("tpl.prop_imagemap_file.html", true, true, "Modules/TestQuestionPool");
284 
285  $this->outputSuffixes($template, "allowed_image_suffixes");
286 
287  if ($this->getImage() != "") {
288  if (strlen($this->getValue())) {
289  $template->setCurrentBlock("has_value");
290  $template->setVariable("TEXT_IMAGE_NAME", $this->getValue());
291  $template->setVariable("POST_VAR_D", $this->getPostVar());
292  $template->parseCurrentBlock();
293  }
294  $template->setCurrentBlock("image");
295  if (count($this->getAreas())) {
296  $preview = new ilImagemapPreview($this->getImagePath() . $this->getValue());
297  foreach ($this->getAreas() as $index => $area) {
298  $preview->addArea($index, $area->getArea(), $area->getCoords(), $area->getAnswertext(), "", "", true, $this->getLineColor());
299  }
300  $preview->createPreview();
301  $imagepath = $this->getImagePathWeb() . $preview->getPreviewFilename($this->getImagePath(), $this->getValue()) . "?img=" . time();
302  $template->setVariable("SRC_IMAGE", $imagepath);
303  } else {
304  $template->setVariable("SRC_IMAGE", $this->getImage());
305  }
306  $template->setVariable("ALT_IMAGE", $this->getAlt());
307  $template->setVariable("POST_VAR_D", $this->getPostVar());
308  $template->setVariable(
309  "TXT_DELETE_EXISTING",
310  $lng->txt("delete_existing_file")
311  );
312  $template->setVariable("TEXT_ADD_RECT", $lng->txt('add_rect'));
313  $template->setVariable("TEXT_ADD_CIRCLE", $lng->txt('add_circle'));
314  $template->setVariable("TEXT_ADD_POLY", $lng->txt('add_poly'));
315  $template->parseCurrentBlock();
316  }
317 
318  if (is_array($this->getAreas()) && $this->getAreas()) {
319  $counter = 0;
320  foreach ($this->getAreas() as $area) {
321  if (strlen($area->getPoints())) {
322  $template->setCurrentBlock('area_points_value');
323  $template->setVariable('VALUE_POINTS', $area->getPoints());
324  $template->parseCurrentBlock();
325  }
326  if ($this->getPointsUncheckedFieldEnabled()) {
327  if (strlen($area->getPointsUnchecked())) {
328  $template->setCurrentBlock('area_points_unchecked_value');
329  $template->setVariable('VALUE_POINTS_UNCHECKED', $area->getPointsUnchecked());
330  $template->parseCurrentBlock();
331  }
332 
333  $template->setCurrentBlock('area_points_unchecked_field');
334  $template->parseCurrentBlock();
335  }
336  if (strlen($area->getAnswertext())) {
337  $template->setCurrentBlock('area_name_value');
338  $template->setVariable('VALUE_NAME', htmlspecialchars($area->getAnswertext()));
339  $template->parseCurrentBlock();
340  }
341  $template->setCurrentBlock('row');
342  $template->setVariable('POST_VAR_R', $this->getPostVar());
343  $template->setVariable('TEXT_SHAPE', strtoupper($area->getArea()));
344  $template->setVariable('VALUE_SHAPE', $area->getArea());
345  $coords = preg_replace("/(\d+,\d+,)/", "\$1 ", $area->getCoords());
346  $template->setVariable('VALUE_COORDINATES', $area->getCoords());
347  $template->setVariable('TEXT_COORDINATES', $coords);
348  $template->setVariable("REMOVE_BUTTON", $this->renderer->render(
349  $this->glyph_factory->remove()->withAction('#')
350  ));
351  $template->parseCurrentBlock();
352  $counter++;
353  }
354  $template->setCurrentBlock("areas");
355  $template->setVariable("TEXT_NAME", $lng->txt("ass_imap_hint"));
356  if ($this->getPointsUncheckedFieldEnabled()) {
357  $template->setVariable("TEXT_POINTS", $lng->txt("points_checked"));
358 
359  $template->setCurrentBlock('area_points_unchecked_head');
360  $template->setVariable("TEXT_POINTS_UNCHECKED", $lng->txt("points_unchecked"));
361  $template->parseCurrentBlock();
362  } else {
363  $template->setVariable("TEXT_POINTS", $lng->txt("points"));
364  }
365  $template->setVariable("TEXT_SHAPE", $lng->txt("shape"));
366  $template->setVariable("TEXT_COORDINATES", $lng->txt("coordinates"));
367  $template->setVariable("TEXT_COMMANDS", $lng->txt("actions"));
368  $template->parseCurrentBlock();
369  }
370 
371  $template->setVariable("POST_VAR", $this->getPostVar());
372  $template->setVariable("ID", $this->getFieldId());
373  $template->setVariable("TXT_BROWSE", $lng->txt("select_file"));
374  $template->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
375  $template->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
376  $template->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice") . " " .
377  $this->getMaxFileSizeString());
378 
379  $a_tpl->setCurrentBlock("prop_generic");
380  $a_tpl->setVariable("PROP_GENERIC", $template->get());
381  $a_tpl->parseCurrentBlock();
382 
383  $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/answerwizardinput.js");
384  $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagemap.js");
385  }
386 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
An entity that renders components to a string output.
Definition: Renderer.php:30
insert(ilTemplate $a_tpl)
Insert property html.
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
This is how a factory for glyphs looks like.
Definition: Factory.php:26
outputSuffixes(ilTemplate $a_tpl, string $a_block="allowed_suffixes")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
This class represents an image map file property in a property form.
__construct(VocabulariesInterface $vocabularies)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
checkInput()
Check input, strip slashes etc.
setValueByArray(array $a_values)
Set value by array.
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This class represents an image file property in a property form.
__construct($a_title="", $a_postvar="")
Constructor.
setPointsUncheckedFieldEnabled($pointsUncheckedFieldEnabled)