ILIAS  release_8 Revision v8.24
class.ilDclSelectionRecordFieldModel.php
Go to the documentation of this file.
1<?php
2
20{
21 // those should be overwritten by subclasses
22 public const PROP_SELECTION_TYPE = '';
23 public const PROP_SELECTION_OPTIONS = '';
24
28 public function getValue()
29 {
30 if ($this->getField()->isMulti() && !is_array($this->value)) {
31 return [$this->value];
32 }
33 if (!$this->getField()->isMulti() && is_array($this->value)) {
34 return (count($this->value) == 1) ? array_shift($this->value) : '';
35 }
36
37 return $this->value;
38 }
39
43 public function parseExportValue($value): string
44 {
46
47 return implode("; ", $values);
48 }
49
50 public function getValueFromExcel(ilExcel $excel, int $row, int $col)
51 {
52 $string = parent::getValueFromExcel($excel, $row, $col);
53 $old = $string;
54 if ($this->getField()->isMulti()) {
55 $string = $this->getMultipleValuesFromString($string);
56 $has_value = count($string);
57 } else {
58 $string = $this->getValueFromString($string);
59 $has_value = $string;
60 }
61
62 if (!$has_value && $old) {
63 $warning = "(" . $row . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $this->lng->txt("dcl_no_such_reference") . " "
64 . $old;
65
66 return ['warning' => $warning];
67 }
68
69 return $string;
70 }
71
80 protected function getMultipleValuesFromString(string $stringValues): array
81 {
82 $delimiter = strpos($stringValues, '; ') ? '; ' : ', ';
83 $slicedStrings = explode($delimiter, $stringValues);
84 $slicedReferences = [];
85 $resolved = 0;
86 for ($i = 0; $i < count($slicedStrings); $i++) {
87 //try to find a reference since the last resolved value separated by a comma.
88 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "hello, world".
89 $searchString = implode(array_slice($slicedStrings, $resolved, $i - $resolved + 1));
90 if ($ref = $this->getValueFromString($searchString)) {
91 $slicedReferences[] = $ref;
92 $resolved = $i;
93 continue;
94 }
95
96 //try to find a reference with the current index.
97 // $i = 1; $resolved = 0; $string = "hello, world, gaga" -> try to match "world".
98 $searchString = $slicedStrings[$i];
99 if ($ref = $this->getValueFromString($searchString)) {
100 $slicedReferences[] = $ref;
101 $resolved = $i;
102 }
103 }
104
105 return $slicedReferences;
106 }
107
108 protected function getValueFromString(string $string): ?int
109 {
110 $options = $this->getField()->getProperty(static::PROP_SELECTION_OPTIONS);
111 foreach ($options as $opt) {
113 if ($opt->getValue() == $string) {
114 return $opt->getOptId();
115 }
116 }
117
118 return null;
119 }
120}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getValues(int $field_id, $opt_ids)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getValueFromExcel(ilExcel $excel, int $row, int $col)
getMultipleValuesFromString(string $stringValues)
Copied from reference field and slightly adjusted.
$i
Definition: metadata.php:41