ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAdvancedMDSubstitution.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
25{
26 private static ?array $instances = null;
27 private static ?array $mappings = null;
28
29 protected ilDBInterface $db;
30
31 protected string $type;
32 protected array $substitutions;
33 protected array $bold = array();
34 protected array $newline = array();
35
36 protected bool $enabled_desc = true;
37 protected bool $enabled_field_names = true;
38 protected bool $active = false;
39 protected array $date_fields = array();
40 protected array $datetime_fields = array();
41 protected array $active_fields = array();
42
43 private function __construct(string $a_type)
44 {
45 global $DIC;
46
47 $this->db = $DIC->database();
48 $this->type = $a_type;
49
50 $this->initECSMappings();
51 $this->read();
52 }
53
56 public static function _getInstanceByObjectType(string $a_type): ilAdvancedMDSubstitution
57 {
58 if (isset(self::$instances[$a_type])) {
59 return self::$instances[$a_type];
60 }
61 return self::$instances[$a_type] = new ilAdvancedMDSubstitution($a_type);
62 }
63
67 public function sortDefinitions(array $a_definitions): array
68 {
69 $sorted = array();
70 foreach ($this->substitutions as $field_id) {
71 if (isset($a_definitions[$field_id])) {
72 $sorted[$field_id] = $a_definitions[$field_id];
73 unset($a_definitions[$field_id]);
74 }
75 }
76 return array_merge($sorted, $a_definitions);
77 }
78
79 public function isActive(): bool
80 {
81 return $this->active;
82 }
83
84 public function isDescriptionEnabled(): bool
85 {
87 }
88
89 public function enableDescription(bool $a_status): void
90 {
91 $this->enabled_desc = $a_status;
92 }
93
94 public function enabledFieldNames(): bool
95 {
97 }
98
99 public function enableFieldNames(bool $a_status): void
100 {
101 $this->enabled_field_names = $a_status;
102 }
103
104 public function getParsedSubstitutions(int $a_ref_id, int $a_obj_id): array
105 {
106 if (!count($this->getSubstitutions())) {
107 return array();
108 }
109
110 $values_records = ilAdvancedMDValues::preloadedRead($this->type, $a_obj_id);
111
112 $counter = 0;
113 $substituted = [];
114 foreach ($this->getSubstitutions() as $field_id) {
115 if (!isset($this->active_fields[$field_id])) {
116 continue;
117 }
118
119 $value = $this->parseValue($field_id, $values_records);
120
121 if ($value === null) {
122 if ($this->hasNewline($field_id) and $counter) {
123 $substituted[$counter - 1]['newline'] = true;
124 }
125 continue;
126 }
127
128 $substituted[$counter]['name'] = $this->active_fields[$field_id];
129 $substituted[$counter]['value'] = $value;
130 $substituted[$counter]['bold'] = $this->isBold($field_id);
131 if ($this->hasNewline($field_id)) {
132 $substituted[$counter]['newline'] = true;
133 } else {
134 $substituted[$counter]['newline'] = false;
135 }
136 $substituted[$counter]['show_field'] = $this->enabledFieldNames();
137 $counter++;
138 }
139 return $substituted;
140 }
141
142 private function parseValue(int $a_field_id, array $a_values_records): ?string
143 {
144 foreach ($a_values_records as $a_values) {
145 if ($a_values->getADTGroup()->hasElement((string) $a_field_id)) {
146 $element = $a_values->getADTGroup()->getElement((string) $a_field_id);
147 if (!$element->isNull()) {
148 return ilADTFactory::getInstance()->getPresentationBridgeForInstance($element)->getList();
149 }
150 }
151 }
152 return null;
153 }
154
155 public function resetSubstitutions(): void
156 {
157 $this->substitutions = array();
158 $this->bold = array();
159 $this->newline = array();
160 }
161
167 public function appendSubstitution(int $a_field_id, bool $a_bold = false, bool $a_newline = false): void
168 {
169 $this->substitutions[] = $a_field_id;
170 if ($a_bold) {
171 $this->bold[] = $a_field_id;
172 }
173 if ($a_newline) {
174 $this->newline[] = $a_field_id;
175 }
176 }
177
178 public function getSubstitutions(): array
179 {
180 return !$this->substitutions ? array() : $this->substitutions;
181 }
182
183 public function isSubstituted(int $a_field_id): bool
184 {
185 return in_array($a_field_id, $this->getSubstitutions());
186 }
187
188 public function isBold(int $a_field_id): bool
189 {
190 return in_array($a_field_id, $this->bold);
191 }
192
193 public function hasNewline(int $a_field_id): bool
194 {
195 return in_array($a_field_id, $this->newline);
196 }
197
202 public function update(): void
203 {
204 global $DIC;
205
206 $ilDB = $DIC['ilDB'];
207
208 $counter = 0;
209 $substitutions = array();
210
211 foreach ($this->substitutions as $field_id) {
212 $substitutions[$counter]['field_id'] = $field_id;
213 $substitutions[$counter]['bold'] = $this->isBold($field_id);
214 $substitutions[$counter]['newline'] = $this->hasNewline($field_id);
215 $counter++;
216 }
217
218 $query = "DELETE FROM adv_md_substitutions WHERE obj_type = " . $ilDB->quote($this->type, 'text');
219 $res = $ilDB->manipulate($query);
220
221 $values = array(
222 'obj_type' => array('text', $this->type),
223 'substitution' => array('clob', serialize($substitutions)),
224 'hide_description' => array('integer', !$this->isDescriptionEnabled()),
225 'hide_field_names' => array('integer', !$this->enabledFieldNames())
226 );
227 $ilDB->insert('adv_md_substitutions', $values);
228 }
229
234 private function read(): void
235 {
236 global $DIC;
237
238 $ilDB = $DIC['ilDB'];
239
240 // Check active status
241 $query = "SELECT active,field_id,amfd.title FROM adv_md_record amr " .
242 "JOIN adv_md_record_objs amro ON amr.record_id = amro.record_id " .
243 "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id " .
244 "WHERE active = 1 " .
245 "AND obj_type = " . $this->db->quote($this->type, 'text') . " ";
246 $res = $this->db->query($query);
247 $this->active = $res->numRows() ? true : false;
248 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
249 $this->active_fields[(int) $row->field_id] = (string) $row->title;
250 }
251
252 $query = "SELECT * FROM adv_md_substitutions " .
253 "WHERE obj_type = " . $this->db->quote($this->type, 'text') . " ";
254 $res = $this->db->query($query);
255 $this->substitutions = array();
256 $this->bold = array();
257 $this->newline = array();
258 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
259 $tmp_substitutions = unserialize((string) $row->substitution);
260 if (is_array($tmp_substitutions)) {
261 foreach ($tmp_substitutions as $substitution) {
262 if ($substitution['field_id']) {
263 $this->substitutions[] = $substitution['field_id'];
264 }
265 if ($substitution['bold']) {
266 $this->bold[] = $substitution['field_id'];
267 }
268 if ($substitution['newline']) {
269 $this->newline[] = $substitution['field_id'];
270 }
271 }
272 }
273 $this->enabled_desc = !$row->hide_description;
274 $this->enabled_field_names = !$row->hide_field_names;
275 }
276 }
277
278 private function initECSMappings(): void
279 {
280 }
281}
return true
getParsedSubstitutions(int $a_ref_id, int $a_obj_id)
read()
Read db entries @access private.
static _getInstanceByObjectType(string $a_type)
parseValue(int $a_field_id, array $a_values_records)
appendSubstitution(int $a_field_id, bool $a_bold=false, bool $a_newline=false)
append field to substitutions @access public
sortDefinitions(array $a_definitions)
Sort definitions.
static preloadedRead(string $a_type, int $a_obj_id)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26
$counter