ILIAS  release_8 Revision v8.24
class.ilAdvancedMDSubstitution.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/*
5 +-----------------------------------------------------------------------------+
6 | ILIAS open source |
7 +-----------------------------------------------------------------------------+
8 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
9 | |
10 | This program is free software; you can redistribute it and/or |
11 | modify it under the terms of the GNU General Public License |
12 | as published by the Free Software Foundation; either version 2 |
13 | of the License, or (at your option) any later version. |
14 | |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU General Public License |
21 | along with this program; if not, write to the Free Software |
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 +-----------------------------------------------------------------------------+
24*/
25
31{
32 private static ?array $instances = null;
33 private static ?array $mappings = null;
34
35 protected ilDBInterface $db;
36
37 protected string $type;
38 protected array $substitutions;
39 protected array $bold = array();
40 protected array $newline = array();
41
42 protected bool $enabled_desc = true;
43 protected bool $enabled_field_names = true;
44 protected bool $active = false;
45 protected array $date_fields = array();
46 protected array $datetime_fields = array();
47 protected array $active_fields = array();
48
49 private function __construct(string $a_type)
50 {
51 global $DIC;
52
53 $this->db = $DIC->database();
54 $this->type = $a_type;
55
56 $this->initECSMappings();
57 $this->read();
58 }
59
62 public static function _getInstanceByObjectType(string $a_type): ilAdvancedMDSubstitution
63 {
64 if (isset(self::$instances[$a_type])) {
65 return self::$instances[$a_type];
66 }
67 return self::$instances[$a_type] = new ilAdvancedMDSubstitution($a_type);
68 }
69
73 public function sortDefinitions(array $a_definitions): array
74 {
75 $sorted = array();
76 foreach ($this->substitutions as $field_id) {
77 if (isset($a_definitions[$field_id])) {
78 $sorted[$field_id] = $a_definitions[$field_id];
79 unset($a_definitions[$field_id]);
80 }
81 }
82 return array_merge($sorted, $a_definitions);
83 }
84
85 public function isActive(): bool
86 {
87 return $this->active;
88 }
89
90 public function isDescriptionEnabled(): bool
91 {
93 }
94
95 public function enableDescription(bool $a_status): void
96 {
97 $this->enabled_desc = $a_status;
98 }
99
100 public function enabledFieldNames(): bool
101 {
103 }
104
105 public function enableFieldNames(bool $a_status): void
106 {
107 $this->enabled_field_names = $a_status;
108 }
109
110 public function getParsedSubstitutions(int $a_ref_id, int $a_obj_id): array
111 {
112 if (!count($this->getSubstitutions())) {
113 return array();
114 }
115
116 $values_records = ilAdvancedMDValues::preloadedRead($this->type, $a_obj_id);
117
118 $counter = 0;
119 $substituted = [];
120 foreach ($this->getSubstitutions() as $field_id) {
121 if (!isset($this->active_fields[$field_id])) {
122 continue;
123 }
124
125 $value = $this->parseValue($field_id, $values_records);
126
127 if ($value === null) {
128 if ($this->hasNewline($field_id) and $counter) {
129 $substituted[$counter - 1]['newline'] = true;
130 }
131 continue;
132 }
133
134 $substituted[$counter]['name'] = $this->active_fields[$field_id];
135 $substituted[$counter]['value'] = $value;
136 $substituted[$counter]['bold'] = $this->isBold($field_id);
137 if ($this->hasNewline($field_id)) {
138 $substituted[$counter]['newline'] = true;
139 } else {
140 $substituted[$counter]['newline'] = false;
141 }
142 $substituted[$counter]['show_field'] = $this->enabledFieldNames();
143 $counter++;
144 }
145 return $substituted;
146 }
147
148 private function parseValue(int $a_field_id, array $a_values_records): ?string
149 {
150 foreach ($a_values_records as $a_values) {
151 if ($a_values->getADTGroup()->hasElement((string) $a_field_id)) {
152 $element = $a_values->getADTGroup()->getElement((string) $a_field_id);
153 if (!$element->isNull()) {
154 return ilADTFactory::getInstance()->getPresentationBridgeForInstance($element)->getList();
155 }
156 }
157 }
158 return null;
159 }
160
161 public function resetSubstitutions(): void
162 {
163 $this->substitutions = array();
164 $this->bold = array();
165 $this->newline = array();
166 }
167
173 public function appendSubstitution(int $a_field_id, bool $a_bold = false, bool $a_newline = false): void
174 {
175 $this->substitutions[] = $a_field_id;
176 if ($a_bold) {
177 $this->bold[] = $a_field_id;
178 }
179 if ($a_newline) {
180 $this->newline[] = $a_field_id;
181 }
182 }
183
184 public function getSubstitutions(): array
185 {
186 return !$this->substitutions ? array() : $this->substitutions;
187 }
188
189 public function isSubstituted(int $a_field_id): bool
190 {
191 return in_array($a_field_id, $this->getSubstitutions());
192 }
193
194 public function isBold(int $a_field_id): bool
195 {
196 return in_array($a_field_id, $this->bold);
197 }
198
199 public function hasNewline(int $a_field_id): bool
200 {
201 return in_array($a_field_id, $this->newline);
202 }
203
208 public function update(): void
209 {
210 global $DIC;
211
212 $ilDB = $DIC['ilDB'];
213
214 $counter = 0;
215 $substitutions = array();
216
217 foreach ($this->substitutions as $field_id) {
218 $substitutions[$counter]['field_id'] = $field_id;
219 $substitutions[$counter]['bold'] = $this->isBold($field_id);
220 $substitutions[$counter]['newline'] = $this->hasNewline($field_id);
221 $counter++;
222 }
223
224 $query = "DELETE FROM adv_md_substitutions WHERE obj_type = " . $ilDB->quote($this->type, 'text');
225 $res = $ilDB->manipulate($query);
226
227 $values = array(
228 'obj_type' => array('text', $this->type),
229 'substitution' => array('clob', serialize($substitutions)),
230 'hide_description' => array('integer', !$this->isDescriptionEnabled()),
231 'hide_field_names' => array('integer', !$this->enabledFieldNames())
232 );
233 $ilDB->insert('adv_md_substitutions', $values);
234 }
235
240 private function read(): void
241 {
242 global $DIC;
243
244 $ilDB = $DIC['ilDB'];
245
246 // Check active status
247 $query = "SELECT active,field_id,amfd.title FROM adv_md_record amr " .
248 "JOIN adv_md_record_objs amro ON amr.record_id = amro.record_id " .
249 "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id " .
250 "WHERE active = 1 " .
251 "AND obj_type = " . $this->db->quote($this->type, 'text') . " ";
252 $res = $this->db->query($query);
253 $this->active = $res->numRows() ? true : false;
254 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
255 $this->active_fields[(int) $row->field_id] = (string) $row->title;
256 }
257
258 $query = "SELECT * FROM adv_md_substitutions " .
259 "WHERE obj_type = " . $this->db->quote($this->type, 'text') . " ";
260 $res = $this->db->query($query);
261 $this->substitutions = array();
262 $this->bold = array();
263 $this->newline = array();
264 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
265 $tmp_substitutions = unserialize((string) $row->substitution);
266 if (is_array($tmp_substitutions)) {
267 foreach ($tmp_substitutions as $substitution) {
268 if ($substitution['field_id']) {
269 $this->substitutions[] = $substitution['field_id'];
270 }
271 if ($substitution['bold']) {
272 $this->bold[] = $substitution['field_id'];
273 }
274 if ($substitution['newline']) {
275 $this->newline[] = $substitution['field_id'];
276 }
277 }
278 }
279 $this->enabled_desc = !$row->hide_description;
280 $this->enabled_field_names = !$row->hide_field_names;
281 }
282 }
283
284 private function initECSMappings(): void
285 {
286 }
287}
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)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query