ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilDidacticTemplateIconFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 
35 {
37 
41  private array $icon_types = [];
43  private array $assignments = [];
44 
45  public function __construct()
46  {
47  global $DIC;
48 
49  $this->definition = $DIC['objDefinition'];
50  $this->initTemplates();
51  }
52 
53  public static function getInstance(): ilDidacticTemplateIconFactory
54  {
55  if (!isset(self::$instance)) {
56  self::$instance = new self();
57  }
58 
59  return self::$instance;
60  }
61 
62  public function getIconPathForReference(int $ref_id): string
63  {
64  $obj_id = ilObject::_lookupObjId($ref_id);
65  $type = ilObject::_lookupType($obj_id);
66 
67  if (!$type || !$this->supportsCustomIcon($type)) {
68  return '';
69  }
70  $assigned_template = $this->findAssignedTemplate($ref_id);
71  if (!$assigned_template) {
72  return '';
73  }
74 
75  $path = $this->getIconPathForTemplate($assigned_template);
76  return $path;
77  }
78 
79  protected function getIconPathForTemplate(int $template_id): ?string
80  {
81  foreach ($this->settings->getTemplates() as $template) {
82  if ($template->getId() === $template_id) {
83  return $template->getIconHandler()->getAbsolutePath();
84  }
85  }
86 
87  return null;
88  }
89 
90  protected function findAssignedTemplate(int $ref_id): int
91  {
92  foreach ($this->assignments as $tpl_id => $assignments) {
93  if (in_array($ref_id, $assignments, true)) {
94  return $tpl_id;
95  }
96  }
97 
98  return 0;
99  }
100 
107  public function getIconPathForObject(int $obj_id): string
108  {
109  // no support for referenced objects
110  if (!$this->definition->isContainer(ilObject::_lookupType($obj_id))) {
111  return '';
112  }
113  $refs = ilObject::_getAllReferences($obj_id);
114  $ref_id = end($refs);
115 
116  return $this->getIconPathForReference((int) $ref_id);
117  }
118 
119  protected function supportsCustomIcon(string $type): bool
120  {
121  return in_array($type, $this->icon_types, true);
122  }
123 
124  private function initTemplates(): void
125  {
127  $this->icon_types = [];
128 
129  $templates = [];
130  foreach ($this->settings->getTemplates() as $tpl) {
131  if ($tpl->getIconIdentifier() !== '') {
132  $templates[] = $tpl->getId();
133  foreach ($tpl->getAssignments() as $assignment) {
134  if (!in_array($assignment, $this->icon_types, true)) {
135  $this->icon_types[] = $assignment;
136  }
137  }
138  }
139  }
140  $this->assignments = ilDidacticTemplateObjSettings::getAssignmentsForTemplates($templates);
141  }
142 }
Icon factory for didactic template custom icons.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getAllReferences(int $id)
get all reference ids for object ID
getIconPathForObject(int $obj_id)
Get icon path for object Not applicable to non container objects, use getIconPathForReference instead...
$path
Definition: ltiservices.php:29
static _lookupObjId(int $ref_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
static getAssignmentsForTemplates(array $template_ids)
global $DIC
Definition: shib_login.php:22
static _lookupType(int $id, bool $reference=false)
static ilDidacticTemplateIconFactory $instance