ILIAS  release_8 Revision v8.24
class.ilDidacticTemplateSetting.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 public const TYPE_CREATION = 1;
29
30 private int $id = 0;
31 private bool $enabled = false;
32 private string $title = '';
33 private string $description = '';
34 private string $info = '';
37 private array $assignments = [];
39 private array $effective_from = [];
40 private bool $auto_generated = false;
41 private bool $exclusive = false;
42 private string $icon_ide = '';
43
45
50 private ilTree $tree;
51
56 public function __construct(int $a_id = 0)
57 {
58 global $DIC;
59
60 $this->lng = $DIC->language();
61 $this->user = $DIC->user();
62 $this->db = $DIC->database();
63 $this->setting = $DIC->settings();
64 $this->tree = $DIC->repositoryTree();
65
66 $this->setId($a_id);
67 $this->read();
68 $this->iconHandler = new ilDidacticTemplateIconHandler($this);
69 }
70
72 {
73 return $this->iconHandler;
74 }
75
76 protected function setId(int $a_id): void
77 {
78 $this->id = $a_id;
79 }
80
81 public function getId(): int
82 {
83 return $this->id;
84 }
85
86 public function enable(bool $a_status): void
87 {
88 $this->enabled = $a_status;
89 }
90
91 public function isEnabled(): bool
92 {
93 return $this->enabled;
94 }
95
96 public function setTitle(string $a_title): void
97 {
98 $this->title = $a_title;
99 }
100
101 public function getTitle(): string
102 {
103 return $this->title;
104 }
105
106 public function getPresentationTitle(string $a_lng = ""): string
107 {
108 if ($this->isAutoGenerated()) {
109 return $this->lng->txt($this->getTitle());
110 }
111
112 $tit = $this->getPresentation('title', $a_lng);
113 return $tit ?: $this->getTitle();
114 }
115
116 public function getDescription(): string
117 {
118 return $this->description;
119 }
120
121 public function getPresentationDescription(string $a_lng = ""): string
122 {
123 if ($this->isAutoGenerated()) {
124 return $this->lng->txt($this->getDescription());
125 }
126
127 $desc = $this->getPresentation('description', $a_lng);
128 return $desc ?: $this->getDescription();
129 }
130
131 public function setDescription(string $a_description): void
132 {
133 $this->description = $a_description;
134 }
135
140 public function setInfo(string $a_info): void
141 {
142 $this->info = $a_info;
143 }
144
149 public function getInfo(): string
150 {
151 return $this->info;
152 }
153
154 public function setType(int $a_type): void
155 {
156 $this->type = $a_type;
157 }
158
159 public function getType(): int
160 {
161 return $this->type;
162 }
163
164 public function hasIconSupport(ilObjectDefinition $definition): bool
165 {
166 foreach ($this->getAssignments() as $assignment) {
167 if (!$definition->isContainer($assignment)) {
168 return false;
169 }
170 }
171
172 return true;
173 }
174
179 public function setAssignments(array $a_ass): void
180 {
181 $this->assignments = $a_ass;
182 }
183
187 public function getAssignments(): array
188 {
189 return $this->assignments;
190 }
191
192 public function addAssignment(string $a_obj_type): void
193 {
194 $this->assignments[] = $a_obj_type;
195 }
196
200 public function getEffectiveFrom(): array
201 {
203 }
204
208 public function setEffectiveFrom(array $effective_from): void
209 {
210 $this->effective_from = $effective_from;
211 }
212
213 public function isAutoGenerated(): bool
214 {
216 }
217
218 private function setAutoGenerated(bool $auto_generated): void
219 {
220 $this->auto_generated = $auto_generated;
221 }
222
223 public function isExclusive(): bool
224 {
225 return $this->exclusive;
226 }
227
228 public function setExclusive(bool $exclusive): void
229 {
230 $this->exclusive = $exclusive;
231 }
232
233 public function setIconIdentifier(string $icon_identifier): void
234 {
235 $this->icon_ide = $icon_identifier;
236 }
237
238 public function getIconIdentifier(): string
239 {
240 return $this->icon_ide;
241 }
242
247 public function getTranslations(): array
248 {
249 $trans = $this->getTranslationObject();
250 $lang = $trans->getLanguages();
251
252 foreach ($lang as $k => $v) {
253 if ($v['lang_default']) {
254 $lang[0] = $v;
255 }
256 }
257
258 // fallback if translation object is empty
259 if (!isset($lang[0])) {
260 $lang[0]['title'] = $this->getTitle();
261 $lang[0]['description'] = $this->getDescription();
262 $lang[0]['lang_code'] = $trans->getDefaultLanguage();
263 }
264
265 return $lang;
266 }
267
268 protected function getPresentation(string $a_value, string $a_lng): string
269 {
270 $lang = $this->getTranslationObject()->getLanguages();
271
272 if (!$lang) {
273 return '';
274 }
275 if (!$a_lng) {
276 $a_lng = $this->user->getCurrentLanguage();
277 }
278 if (!isset($lang[$a_lng])) {
279 $a_lng = $this->getTranslationObject()->getDefaultLanguage();
280 }
281 return $lang[$a_lng][$a_value] ?? '';
282 }
283
284 public function delete(): bool
285 {
286 if ($this->isAutoGenerated()) {
287 return false;
288 }
289
290 // Delete settings
291 $query = 'DELETE FROM didactic_tpl_settings ' .
292 'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
293 $this->db->manipulate($query);
294
295 // Delete obj assignments
296 $query = 'DELETE FROM didactic_tpl_sa ' .
297 'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
298 $this->db->manipulate($query);
299
301 $action->delete();
302 }
304 $this->getTranslationObject()->delete();
305 $this->deleteEffectiveNodes();
306 $this->getIconHandler()->delete();
307
308 return true;
309 }
310
311 public function save(): bool
312 {
313 $this->setId($this->db->nextId('didactic_tpl_settings'));
314 $query = 'INSERT INTO didactic_tpl_settings (id,enabled,title,description,info,type,auto_generated,exclusive_tpl,icon_ide) ' .
315 'VALUES( ' .
316 $this->db->quote($this->getId(), 'integer') . ', ' .
317 $this->db->quote($this->isEnabled(), 'integer') . ', ' .
318 $this->db->quote($this->getTitle(), 'text') . ', ' .
319 $this->db->quote($this->getDescription(), 'text') . ', ' .
320 $this->db->quote($this->getInfo(), 'text') . ', ' .
321 $this->db->quote($this->getType(), 'integer') . ', ' .
322 $this->db->quote((int) $this->isAutoGenerated(), 'integer') . ', ' .
323 $this->db->quote((int) $this->isExclusive(), 'integer') . ', ' .
324 $this->db->quote($this->getIconIdentifier(), ilDBConstants::T_TEXT) . ' ' .
325 ')';
326
327 $this->db->manipulate($query);
328 $this->saveAssignments();
329
330 return true;
331 }
332
336 private function saveAssignments(): bool
337 {
338 if ($this->isAutoGenerated()) {
339 return false;
340 }
341
342 foreach ($this->getAssignments() as $ass) {
343 $this->saveAssignment($ass);
344 }
345
346 return true;
347 }
348
353 private function saveAssignment(string $a_obj_type): void
354 {
355 if ($this->isAutoGenerated()) {
356 return;
357 }
358
359 $query = 'INSERT INTO didactic_tpl_sa (id,obj_type) ' .
360 'VALUES( ' .
361 $this->db->quote($this->getId(), 'integer') . ', ' .
362 $this->db->quote($a_obj_type, 'text') .
363 ')';
364 $this->db->manipulate($query);
365 }
366
367 protected function saveEffectiveNodes(): void
368 {
369 if (0 === count($this->getEffectiveFrom())) {
370 return;
371 }
372 $values = [];
373 foreach ($this->getEffectiveFrom() as $node) {
374 $values[] = '( ' .
375 $this->db->quote($this->getId(), 'integer') . ', ' .
376 $this->db->quote($node, 'integer') .
377 ')';
378 }
379
380 $query = 'INSERT INTO didactic_tpl_en (id,node) ' .
381 'VALUES ' . implode(', ', $values);
382
383 $this->db->manipulate($query);
384 }
385
386 protected function deleteEffectiveNodes(): bool
387 {
388 $query = 'DELETE FROM didactic_tpl_en ' .
389 'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
390 $this->db->manipulate($query);
391
392 return true;
393 }
394
395 protected function readEffectiveNodes(): void
396 {
397 $effective_nodes = [];
398
399 $query = 'SELECT * FROM didactic_tpl_en ' .
400 'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
401 $res = $this->db->query($query);
402 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
403 $effective_nodes[] = (int) $row->node;
404 }
405
406 $this->setEffectiveFrom($effective_nodes);
407 }
408
412 private function deleteAssignments(): bool
413 {
414 if ($this->isAutoGenerated()) {
415 return false;
416 }
417
418 $query = 'DELETE FROM didactic_tpl_sa ' .
419 'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
420 $this->db->manipulate($query);
421
422 return true;
423 }
424
425 public function update(): bool
426 {
427 $query = 'UPDATE didactic_tpl_settings ' .
428 'SET ' .
429 'enabled = ' . $this->db->quote($this->isEnabled(), 'integer') . ', ' .
430 'title = ' . $this->db->quote($this->getTitle(), 'text') . ', ' .
431 'description = ' . $this->db->quote($this->getDescription(), 'text') . ', ' .
432 'info = ' . $this->db->quote($this->getInfo(), 'text') . ', ' .
433 'type = ' . $this->db->quote($this->getType(), 'integer') . ', ' .
434 'exclusive_tpl = ' . $this->db->quote((int) $this->isExclusive(), 'integer') . ', ' .
435 'icon_ide = ' . $this->db->quote($this->getIconIdentifier(), ilDBConstants::T_TEXT) . ' ' .
436 'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
437 $this->db->manipulate($query);
438 $this->deleteAssignments();
439 $this->saveAssignments();
440 $this->deleteEffectiveNodes();
441 $this->saveEffectiveNodes();
442
443 return true;
444 }
445
446 protected function read(): bool
447 {
448 if (!$this->getId()) {
449 return false;
450 }
451 $query = 'SELECT * FROM didactic_tpl_settings dtpl ' .
452 'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
453 $res = $this->db->query($query);
454 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
455 $this->setType((int) $row->type);
456 $this->enable((bool) $row->enabled);
457 $this->setTitle((string) $row->title);
458 $this->setDescription((string) $row->description);
459 $this->setInfo((string) $row->info);
460 $this->setAutoGenerated((bool) $row->auto_generated);
461 $this->setExclusive((bool) $row->exclusive_tpl);
462 $this->setIconIdentifier((string) $row->icon_ide);
463 }
464 $query = 'SELECT * FROM didactic_tpl_sa ' .
465 'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
466 $res = $this->db->query($query);
467 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
468 $this->addAssignment($row->obj_type);
469 }
470 $this->readEffectiveNodes();
471
472 return true;
473 }
474
475 public function toXml(ilXmlWriter $writer): ilXmlWriter
476 {
477 $type = '';
478 switch ($this->getType()) {
480 $type = 'creation';
481 break;
482 }
483 $writer->xmlStartTag('didacticTemplate', ['type' => $type]);
484 $writer->xmlElement('title', [], $this->getTitle());
485 $writer->xmlElement('description', [], $this->getDescription());
486 $writer = $this->getIconHandler()->toXml($writer);
487 $writer = $this->getTranslationObject()->toXml($writer);
488
489 // info text with p-tags
490 if ($this->getInfo() !== '') {
491 $writer->xmlStartTag('info');
492
493 $info_lines = (array) explode("\n", $this->getInfo());
494 foreach ($info_lines as $info) {
495 $trimmed_info = trim($info);
496 if ($trimmed_info !== '') {
497 $writer->xmlElement('p', [], $trimmed_info);
498 }
499 }
500
501 $writer->xmlEndTag('info');
502 }
503 if ($this->isExclusive()) {
504 $writer->xmlElement("exclusive");
505 }
506 if (count($this->getEffectiveFrom()) > 0) {
507 $writer->xmlStartTag('effectiveFrom', ['nic_id' => $this->setting->get('inst_id')]);
508
509 foreach ($this->getEffectiveFrom() as $node) {
510 $writer->xmlElement('node', [], $node);
511 }
512 $writer->xmlEndTag('effectiveFrom');
513 }
514 // Assignments
515 $writer->xmlStartTag('assignments');
516 foreach ($this->getAssignments() as $assignment) {
517 $writer->xmlElement('assignment', [], $assignment);
518 }
519 $writer->xmlEndTag('assignments');
520 $writer->xmlStartTag('actions');
522 $action->toXml($writer);
523 }
524 $writer->xmlEndTag('actions');
525 $writer->xmlEndTag('didacticTemplate');
526
527 return $writer;
528 }
529
530 public function __clone()
531 {
532 $this->setId(0);
533
535 $this->enable(false);
536 $this->setAutoGenerated(false);
537 $this->iconHandler = new ilDidacticTemplateIconHandler($this);
538 }
539
541 {
542 return ilMultilingualism::getInstance($this->getId(), "dtpl");
543 }
544
545 public function isEffective(int $a_node_id): bool
546 {
547 if (0 === count($this->getEffectiveFrom()) || in_array($a_node_id, $this->getEffectiveFrom())) {
548 return true;
549 }
550
551 foreach ($this->getEffectiveFrom() as $node) {
552 if ($this->tree->isGrandChild($node, $a_node_id)) {
553 return true;
554 }
555 }
556
557 return false;
558 }
559}
static getActionsByTemplateId(int $a_tpl_id)
Get actions of one template.
static appendCopyInfo(string $a_orig_title)
Icon handler for didactic template custom icons.
saveAssignment(string $a_obj_type)
Add one object assignment.
setInfo(string $a_info)
Set installation info text.
getPresentation(string $a_value, string $a_lng)
getTranslations()
Get all translations from this object.
hasIconSupport(ilObjectDefinition $definition)
getInfo()
Get installation info text.
ilDidacticTemplateIconHandler $iconHandler
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstance(int $a_obj_id, string $a_type)
User class.
parses the objects.xml it handles the xml-description of all ilias objects
isContainer(string $obj_name)
Check if object type is container ('crs','fold','grp' ...)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query
$lang
Definition: xapiexit.php:26