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