ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilDidacticTemplateSetting.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
12  const TYPE_CREATION = 1;
13 
14 
15  private $id = 0;
16  private $enabled = false;
17  private $title = '';
18  private $description = '';
19  private $info = '';
20  private $type = self::TYPE_CREATION;
21  private $assignments = array();
22  private $effective_from = array();
23  private $auto_generated = false;
24  private $exclusive = false;
25 
26  private $icon_ide = '';
27 
28  private $iconHandler = null;
29 
30 
35  public function __construct($a_id = 0)
36  {
37  $this->setId($a_id);
38  $this->read();
39  $this->iconHandler = new ilDidacticTemplateIconHandler($this);
40  }
41 
46  {
47  return $this->iconHandler;
48  }
49 
54  protected function setId($a_id)
55  {
56  $this->id = $a_id;
57  }
58 
63  public function getId()
64  {
65  return $this->id;
66  }
67 
72  public function enable($a_status)
73  {
74  $this->enabled = $a_status;
75  }
76 
81  public function isEnabled()
82  {
83  return $this->enabled;
84  }
85 
90  public function setTitle($a_title)
91  {
92  $this->title = $a_title;
93  }
94 
99  public function getTitle()
100  {
101  return $this->title;
102  }
103 
108  public function getPresentationTitle($a_lng = "")
109  {
110  if ($this->isAutoGenerated()) {
111  global $DIC;
112 
113  $lng = $DIC['lng'];
114  return $lng->txt($this->getTitle());
115  }
116 
117  $tit = $this->getPresentation('title', $a_lng);
118  return $tit ? $tit : $this->getTitle();
119  }
120 
125  public function getDescription()
126  {
127  return $this->description;
128  }
129 
134  public function getPresentationDescription($a_lng = "")
135  {
136  if ($this->isAutoGenerated()) {
137  global $DIC;
138 
139  $lng = $DIC['lng'];
140  return $lng->txt($this->getDescription());
141  }
142 
143  $desc = $this->getPresentation('description', $a_lng);
144  return $desc ? $desc : $this->getDescription();
145  }
146 
151  public function setDescription($a_description)
152  {
153  $this->description = $a_description;
154  }
155 
160  public function setInfo($a_info)
161  {
162  $this->info = $a_info;
163  }
164 
169  public function getInfo()
170  {
171  return $this->info;
172  }
173 
178  public function setType($a_type)
179  {
180  $this->type = $a_type;
181  }
182 
187  public function getType()
188  {
189  return $this->type;
190  }
191 
196  public function hasIconSupport(ilObjectDefinition $definition) : bool
197  {
198  foreach ($this->getAssignments() as $assignment) {
199  if (!$definition->isContainer($assignment)) {
200  return false;
201  }
202  }
203  return true;
204  }
205 
210  public function setAssignments(array $a_ass)
211  {
212  $this->assignments = (array) $a_ass;
213  }
214 
219  public function getAssignments()
220  {
221  return (array) $this->assignments;
222  }
223 
228  public function addAssignment($a_obj_type)
229  {
230  $this->assignments[] = $a_obj_type;
231  }
232 
236  public function getEffectiveFrom()
237  {
238  return $this->effective_from;
239  }
240 
245  {
246  $this->effective_from = $effective_from;
247  }
248 
252  public function isAutoGenerated()
253  {
254  return $this->auto_generated;
255  }
256 
263  {
264  $this->auto_generated = $auto_generated;
265  }
266 
270  public function isExclusive()
271  {
272  return $this->exclusive;
273  }
274 
278  public function setExclusive($exclusive)
279  {
280  $this->exclusive = $exclusive;
281  }
282 
283  public function setIconIdentifier(string $icon_identifier)
284  {
285  $this->icon_ide = $icon_identifier;
286  }
287 
288  public function getIconIdentifier() : string
289  {
290  return $this->icon_ide;
291  }
292 
299  public function getTranslations()
300  {
301  $trans = $this->getTranslationObject();
302  $lang = $trans->getLanguages();
303 
304  foreach ($lang as $k => $v) {
305  if ($v['lang_default']) {
306  $lang[0] = $lang[$k];
307  }
308  }
309 
310  // fallback if translation object is empty
311  if (!isset($lang[0])) {
312  $lang[0]['title'] = $this->getTitle();
313  $lang[0]['description'] = $this->getDescription();
314  $lang[0]['lang_code'] = $trans->getDefaultLanguage();
315  }
316 
317  return $lang;
318  }
319 
320  protected function getPresentation($a_value, $a_lng)
321  {
322  $lang = $this->getTranslationObject()->getLanguages();
323 
324  if (!$lang) {
325  return "";
326  }
327 
328  if (!$a_lng) {
329  global $DIC;
330 
331  $ilUser = $DIC['ilUser'];
332  $a_lng = $ilUser->getCurrentLanguage();
333  }
334 
335  if (isset($lang[$a_lng][$a_value])) {
336  return $lang[$a_lng][$a_value] ;
337  } else {
338  return $lang[$a_lng][$this->getTranslationObject()->getDefaultLanguage()];
339  }
340  }
341 
345  public function delete()
346  {
347  global $DIC;
348 
349  $ilDB = $DIC['ilDB'];
350 
351  if ($this->isAutoGenerated()) {
352  return false;
353  }
354 
355  // Delete settings
356  $query = 'DELETE FROM didactic_tpl_settings ' .
357  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
358  $ilDB->manipulate($query);
359 
360  // Delete obj assignments
361  $query = 'DELETE FROM didactic_tpl_sa ' .
362  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
363  $ilDB->manipulate($query);
364 
365  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateActionFactory.php';
366  foreach (ilDidacticTemplateActionFactory::getActionsByTemplateId($this->getId()) as $action) {
367  $action->delete();
368  }
369 
370  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateObjSettings.php';
372 
373  $this->getTranslationObject()->delete();
374  $this->deleteEffectiveNodes();
375  $this->getIconHandler()->delete();
376  return true;
377  }
378 
382  public function save()
383  {
384  global $DIC;
385 
386  $ilDB = $DIC['ilDB'];
387 
388  $this->setId($ilDB->nextId('didactic_tpl_settings'));
389 
390  $query = 'INSERT INTO didactic_tpl_settings (id,enabled,title,description,info,type,auto_generated,exclusive_tpl,icon_ide) ' .
391  'VALUES( ' .
392  $ilDB->quote($this->getId(), 'integer') . ', ' .
393  $ilDB->quote($this->isEnabled(), 'integer') . ', ' .
394  $ilDB->quote($this->getTitle(), 'text') . ', ' .
395  $ilDB->quote($this->getDescription(), 'text') . ', ' .
396  $ilDB->quote($this->getInfo(), 'text') . ', ' .
397  $ilDB->quote($this->getType(), 'integer') . ', ' .
398  $ilDB->quote((int) $this->isAutoGenerated(), 'integer') . ', ' .
399  $ilDB->quote((int) $this->isExclusive(), 'integer') . ', ' .
400  $ilDB->quote((string) $this->getIconIdentifier(), ilDBConstants::T_TEXT) . ' ' .
401  ')';
402 
403  $ilDB->manipulate($query);
404 
405  $this->saveAssignments();
406 
407  return true;
408  }
409 
414  private function saveAssignments()
415  {
416  if ($this->isAutoGenerated()) {
417  return false;
418  }
419 
420  foreach ($this->getAssignments() as $ass) {
421  $this->saveAssignment($ass);
422  }
423  return true;
424  }
425 
431  private function saveAssignment($a_obj_type)
432  {
433  global $DIC;
434 
435  $ilDB = $DIC['ilDB'];
436 
437  if ($this->isAutoGenerated()) {
438  return;
439  }
440 
441  $query = 'INSERT INTO didactic_tpl_sa (id,obj_type) ' .
442  'VALUES( ' .
443  $ilDB->quote($this->getId(), 'integer') . ', ' .
444  $ilDB->quote($a_obj_type, 'text') .
445  ')';
446  $ilDB->manipulate($query);
447  }
448 
452  protected function saveEffectiveNodes()
453  {
454  global $DIC;
455 
456  $ilDB = $DIC['ilDB'];
457 
458  if (!count($this->getEffectiveFrom())) {
459  return;
460  }
461 
462  foreach ($this->getEffectiveFrom() as $node) {
463  $values[] = '( ' .
464  $ilDB->quote($this->getId(), 'integer') . ', ' .
465  $ilDB->quote($node, 'integer') .
466  ')';
467  }
468 
469  $query = 'INSERT INTO didactic_tpl_en (id,node) ' .
470  'VALUES ' . implode(', ', $values);
471 
472  $ilDB->manipulate($query);
473  }
474 
475  protected function deleteEffectiveNodes()
476  {
477  global $DIC;
478 
479  $ilDB = $DIC['ilDB'];
480 
481  $query = 'DELETE FROM didactic_tpl_en ' .
482  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
483  $ilDB->manipulate($query);
484  return true;
485  }
486 
487  protected function readEffectiveNodes()
488  {
489  global $DIC;
490 
491  $ilDB = $DIC['ilDB'];
492  $effective_nodes = array();
493 
494  $query = 'SELECT * FROM didactic_tpl_en ' .
495  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
496  $res = $ilDB->query($query);
497  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
498  $effective_nodes[] = $row->node;
499  }
500 
501  $this->setEffectiveFrom($effective_nodes);
502  }
503 
509  private function deleteAssignments()
510  {
511  global $DIC;
512 
513  $ilDB = $DIC['ilDB'];
514 
515  if ($this->isAutoGenerated()) {
516  return false;
517  }
518 
519  $query = 'DELETE FROM didactic_tpl_sa ' .
520  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
521  $ilDB->manipulate($query);
522  return true;
523  }
524 
530  public function update()
531  {
532  global $DIC;
533 
534  $ilDB = $DIC['ilDB'];
535 
536  $query = 'UPDATE didactic_tpl_settings ' .
537  'SET ' .
538  'enabled = ' . $ilDB->quote($this->isEnabled(), 'integer') . ', ' .
539  'title = ' . $ilDB->quote($this->getTitle(), 'text') . ', ' .
540  'description = ' . $ilDB->quote($this->getDescription(), 'text') . ', ' .
541  'info = ' . $ilDB->quote($this->getInfo(), 'text') . ', ' .
542  'type = ' . $ilDB->quote($this->getType(), 'integer') . ', ' .
543  'exclusive_tpl = ' . $ilDB->quote((int) $this->isExclusive(), 'integer') . ', ' .
544  'icon_ide = ' . $ilDB->quote((string) $this->getIconIdentifier(), ilDBConstants::T_TEXT) . ' ' .
545  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
546  $ilDB->manipulate($query);
547  $this->deleteAssignments();
548  $this->saveAssignments();
549 
550  $this->deleteEffectiveNodes();
551  $this->saveEffectiveNodes();
552 
553  return true;
554  }
555 
560  protected function read()
561  {
562  global $DIC;
563 
564  $ilDB = $DIC['ilDB'];
565 
566  if (!$this->getId()) {
567  return false;
568  }
569 
573  $query = 'SELECT * FROM didactic_tpl_settings dtpl ' .
574  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
575  $res = $ilDB->query($query);
576  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
577  $this->setType($row->type);
578  $this->enable($row->enabled);
579  $this->setTitle($row->title);
580  $this->setDescription($row->description);
581  $this->setInfo($row->info);
582  $this->setAutoGenerated((bool) $row->auto_generated);
583  $this->setExclusive((bool) $row->exclusive_tpl);
584  $this->setIconIdentifier((string) $row->icon_ide);
585  }
586 
590  $query = 'SELECT * FROM didactic_tpl_sa ' .
591  'WHERE id = ' . $ilDB->quote($this->getId(), 'integer');
592  $res = $ilDB->query($query);
593  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
594  $this->addAssignment($row->obj_type);
595  }
596 
597  $this->readEffectiveNodes();
598 
599  return true;
600  }
601 
607  public function toXml(ilXmlWriter $writer)
608  {
609  global $DIC;
610 
611  $ilSetting = $DIC['ilSetting'];
612  switch ($this->getType()) {
613  case self::TYPE_CREATION:
614  $type = 'creation';
615  break;
616  }
617 
618  $writer->xmlStartTag('didacticTemplate', array('type' => $type));
619  $writer->xmlElement('title', array(), $this->getTitle());
620  $writer->xmlElement('description', array(), $this->getDescription());
621  $writer = $this->getIconHandler()->toXml($writer);
622  $writer = $this->getTranslationObject()->toXml($writer);
623 
624  // info text with p-tags
625  if (strlen($this->getInfo())) {
626  $writer->xmlStartTag('info');
627 
628  $info_lines = (array) explode("\n", $this->getInfo());
629  foreach ($info_lines as $info) {
630  $trimmed_info = trim($info);
631  if (strlen($trimmed_info)) {
632  $writer->xmlElement('p', array(), $trimmed_info);
633  }
634  }
635 
636  $writer->xmlEndTag('info');
637  }
638 
639  if ($this->isExclusive()) {
640  $writer->xmlElement("exclusive");
641  }
642 
643 
644  if (count($this->getEffectiveFrom()) > 0) {
645  $writer->xmlStartTag('effectiveFrom', array('nic_id' => $ilSetting->get('inst_id')));
646 
647  foreach ($this->getEffectiveFrom() as $node) {
648  $writer->xmlElement('node', array(), $node);
649  }
650  $writer->xmlEndTag('effectiveFrom');
651  }
652 
653  // Assignments
654  $writer->xmlStartTag('assignments');
655  foreach ($this->getAssignments() as $assignment) {
656  $writer->xmlElement('assignment', array(), $assignment);
657  }
658  $writer->xmlEndTag('assignments');
659 
660 
661  $writer->xmlStartTag('actions');
662  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateActionFactory.php';
663  foreach (ilDidacticTemplateActionFactory::getActionsByTemplateId($this->getId()) as $action) {
664  $action->toXml($writer);
665  }
666  $writer->xmlEndTag('actions');
667  $writer->xmlEndTag('didacticTemplate');
668 
669  return $writer;
670  }
671 
675  public function __clone()
676  {
677  $this->setId(0);
678  include_once './Services/DidacticTemplate/classes/class.ilDidacticTemplateCopier.php';
680  $this->enable(false);
681  $this->setAutoGenerated(false);
682  $this->iconHandler = new ilDidacticTemplateIconHandler($this);
683  }
684 
688  public function getTranslationObject()
689  {
690  include_once("./Services/Multilingualism/classes/class.ilMultilingualism.php");
691  return ilMultilingualism::getInstance($this->getId(), "dtpl");
692  }
693 
698  public function isEffective($a_node_id)
699  {
700  global $DIC;
701 
702  $tree = $DIC['tree'];
703 
704  if (!count($this->getEffectiveFrom()) || in_array($a_node_id, $this->getEffectiveFrom())) {
705  return true;
706  }
707 
708  foreach ($this->getEffectiveFrom() as $node) {
709  if ($tree->isGrandChild($node, $a_node_id)) {
710  return true;
711  }
712  }
713 
714  return false;
715  }
716 }
static getInstance($a_obj_id, $a_type)
Get instance.
setInfo($a_info)
Set installation info text.
isEnabled()
Check if template is enabled.
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
XML writer class.
hasIconSupport(ilObjectDefinition $definition)
xmlEndTag($tag)
Writes an endtag.
addAssignment($a_obj_type)
Add one assignment obj type.
parses the objects.xml it handles the xml-description of all ilias objects
setAutoGenerated($auto_generated)
DO NOT CHANGE THIS VALUE.
foreach($_POST as $key=> $value) $res
$lng
update()
Update settings ilDB $ilDB.
setAssignments(array $a_ass)
Set assignments.
static deleteByTemplateId($a_tpl_id)
Delete by template id ilDB $ilDB.
global $DIC
Definition: goto.php:24
$query
Icon handler for didactic template custom icons.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
$lang
Definition: xapiexit.php:8
enable($a_status)
Set enabled status.
saveAssignment($a_obj_type)
Add one object assignment ilDB $ilDB.
global $ilSetting
Definition: privfeed.php:17
info()
Definition: info.php:2
global $ilDB
deleteAssignments()
Delete assignments ilDB $ilDB.
getInfo()
Get installation info text.
isContainer($a_obj_name)
Check if object type is container (&#39;crs&#39;,&#39;fold&#39;,&#39;grp&#39; ...)
$ilUser
Definition: imgupload.php:18
getTranslations()
get all translations from this object
setDescription($a_description)
Set description.
static getActionsByTemplateId($a_tpl_id)
Get actions of one template.