ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDclFieldFactory.php
Go to the documentation of this file.
1 <?php
2 
20 {
21  public static string $field_base_path_patter = "./Modules/DataCollection/classes/Fields/%s/";
22  public static string $default_prefix = "ilDcl";
23  public static string $record_field_class_patter = "%sRecordFieldModel";
24  public static string $field_class_patter = "%sFieldModel";
25  public static string $record_representation_class_pattern = "%sRecordRepresentation";
26  public static string $field_representation_class_pattern = "%sFieldRepresentation";
27  protected static array $record_field_cache = array();
28 
36  public static function getRecordFieldInstance(object $field, object $record): ilDclBaseRecordFieldModel
37  {
38  if (!empty(self::$record_field_cache[$field->getId()][$record->getId()])) {
39  return self::$record_field_cache[$field->getId()][$record->getId()];
40  }
41 
42  $path = self::getClassPathByInstance($field, self::$record_field_class_patter);
43  if (file_exists($path)) {
44  $class = self::getClassByInstance($field, self::$record_field_class_patter);
45  $instance = new $class($record, $field);
46  if ($instance instanceof ilDclBaseRecordFieldModel) {
47  if (!$instance->getFieldRepresentation()) {
48  $instance->setFieldRepresentation(self::getFieldRepresentationInstance($field));
49  }
50 
51  if (!$instance->getRecordRepresentation()) {
52  $instance->setRecordRepresentation(self::getRecordRepresentationInstance($instance));
53  }
54  self::$record_field_cache[$field->getId()][$record->getId()] = $instance;
55 
56  return $instance;
57  }
58  }
59 
60  throw new RuntimeException("file not found " . $path);
61  }
62 
63  protected static array $field_class_cache = array();
64 
68  public static function getFieldClass(string $datatype, string $class_pattern): string
69  {
70  if (!empty(self::$field_class_cache[$datatype . $class_pattern])) {
71  return self::$field_class_cache[$datatype . $class_pattern];
72  }
73 
74  $fieldtype = $datatype;
75 
76  $class = sprintf($class_pattern, $fieldtype);
77  self::$field_class_cache[$datatype . $class_pattern] = $class;
78 
79  return $class;
80  }
81 
82  public static function getFieldClassFile(string $datatype, string $class_pattern): string
83  {
84  return "class." . self::getFieldClass($datatype, $class_pattern) . ".php";
85  }
86 
87  protected static array $field_representation_cache = array();
88 
90  {
91  // when the datatype overview is generated no field-models are available, so an empty instance is used => no caching there
92  if ($field->getId() != null && !empty(self::$field_representation_cache[$field->getId()])) {
93  return self::$field_representation_cache[$field->getId()];
94  }
95 
96  $class_path = self::getClassPathByInstance($field, self::$field_representation_class_pattern);
97 
98  $instance = null;
99  if (file_exists($class_path)) {
100  $class = self::getClassByInstance($field, self::$field_representation_class_pattern);
101  $instance = new $class($field);
102  } else {
103  throw new ilDclException("Path for FieldRepresentation with file " . $class_path . " does not exists!");
104  }
105 
106  if ($instance == null) {
107  throw new ilDclException("Could not create FieldRepresentation of " . $class . " with file " . $class_path);
108  }
109 
110  if ($field->getId() != null) {
111  self::$field_representation_cache[$field->getId()] = $instance;
112  }
113 
114  return $instance;
115  }
116 
117  protected static array $record_representation_cache = array();
118 
123  public static function getRecordRepresentationInstance(
124  ilDclBaseRecordFieldModel $record_field
126  // there are some field types which have no recordFieldModel object (e.g rating) => no caching
127  if ($record_field->getId() != null && !empty(self::$record_representation_cache[$record_field->getId()])) {
128  return self::$record_representation_cache[$record_field->getId()];
129  }
130 
131  $class_path = self::getClassPathByInstance(
132  $record_field->getField(),
133  self::$record_representation_class_pattern
134  );
135  $instance = null;
136 
137  if (file_exists($class_path)) {
138  $class = self::getClassByInstance($record_field->getField(), self::$record_representation_class_pattern);
139  } else {
140  $class = self::getFieldClass(self::$default_prefix . "Base", self::$record_representation_class_pattern);
141  }
142 
143  $instance = new $class($record_field);
144 
145  if ($instance == null) {
146  throw new ilDclException("Could not create RecordRepresentation of " . $class_path . " " . $record_field->getField()->getDatatype()->getTitle());
147  }
148 
149  if ($record_field->getId() != null) {
150  self::$record_representation_cache[$record_field->getId()] = $instance;
151  }
152 
153  return $instance;
154  }
155 
160  public static function getFieldModelInstance(int $field_id, ?int $datatype = null): ilDclBaseFieldModel
161  {
162  $base = new ilDclBaseFieldModel($field_id);
163  if ($datatype != null) {
164  $base->setDatatypeId($datatype);
165  }
166 
167  $ilDclBaseFieldModel = self::getFieldModelInstanceByClass($base, $field_id);
168 
169  return $ilDclBaseFieldModel;
170  }
171 
172  protected static array $field_model_cache = array();
173 
179  public static function getFieldModelInstanceByClass(
180  ilDclBaseFieldModel $field,
181  ?int $field_id = null
183  if ($field->getId() != null && !empty(self::$field_model_cache[$field->getId()])) {
184  return self::$field_model_cache[$field->getId()];
185  }
186 
187  $path_type = self::getClassPathByInstance($field, self::$field_class_patter);
188 
189  if (file_exists($path_type)) {
190  $class = self::getClassByInstance($field, self::$field_class_patter);
191  } else {
192  $class = self::getFieldClass(self::$default_prefix . "Base", self::$field_class_patter);
193  }
194 
195  if ($field_id) {
196  $instance = new $class($field_id);
197  } else {
198  $instance = new $class();
199  }
200 
201  if ($instance == null) {
202  throw new ilDclException("Could not create FieldModel of " . $class);
203  }
204  $instance->setDatatypeId($field->getDatatypeId());
205 
206  if ($field->getId() != null) {
207  self::$field_model_cache[$field->getId()] = $instance;
208  }
209 
210  return $instance;
211  }
212 
213  protected static array $field_type_cache = array();
214 
215  public static function getFieldTypeByInstance(ilDclBaseFieldModel $field): string
216  {
217  global $DIC;
218  $datatype = $field->getDatatype();
219 
220  if (!empty(self::$field_type_cache[$datatype->getId()])) {
221  return self::$field_type_cache[$datatype->getId()];
222  }
223 
224  if (ilDclFieldTypePlugin::isPluginDatatype($datatype->getTitle())) {
225  $plugin_id = ilDclFieldTypePlugin::getPluginId($datatype->getTitle());
226  if ($DIC["component.repository"]->hasActivatedPlugin($plugin_id)) {
227  $fieldtype = 'il' . $DIC["component.repository"]->getPluginById($plugin_id)->getName();
228  } else {
229  $fieldtype = '';
230  }
231  } else {
232  $fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
233  }
234  self::$field_type_cache[$datatype->getId()] = $fieldtype;
235  return $fieldtype;
236  }
237 
238  public static function getClassByInstance(ilDclBaseFieldModel $field, string $class_pattern): string
239  {
240  $fieldtype = self::getFieldTypeByInstance($field);
241 
242  return self::getFieldClass($fieldtype, $class_pattern);
243  }
244 
245  protected static array $class_path_cache = array();
246 
250  public static function getClassPathByInstance(ilDclBaseFieldModel $field, string $class_pattern): string
251  {
252  global $DIC;
253  $datatype = $field->getDatatype();
254 
255  if ($field->getId() != null && !empty(self::$class_path_cache[$field->getId()][$class_pattern])) {
256  return self::$class_path_cache[$field->getId()][$class_pattern];
257  }
258 
259  if (ilDclFieldTypePlugin::isPluginDatatype($datatype->getTitle())) {
260  $plugin_id = ilDclFieldTypePlugin::getPluginId($datatype->getTitle());
261  if ($DIC["component.repository"]->hasActivatedPlugin($plugin_id)) {
262  $class_path = $DIC["component.repository"]->getPluginById($plugin_id)->getPath() . '/classes/';
263  } else {
264  return '';
265  }
266  } else {
267  $class_path = sprintf(
268  self::$field_base_path_patter,
269  ucfirst(self::parseDatatypeTitle($datatype->getTitle()))
270  );
271  }
272 
273  $return = $class_path . self::getFieldClassFile(self::getFieldTypeByInstance($field), $class_pattern);
274 
275  if ($field->getId() != null) {
276  self::$class_path_cache[$field->getId()][$class_pattern] = $return;
277  }
278 
279  return $return;
280  }
281 
286  public static function parseDatatypeTitle(string $title): string
287  {
288  $parts = explode("_", $title);
289  $func = function ($value) {
290  return ucfirst($value);
291  };
292 
293  $parts = array_map($func, $parts);
294  $title = implode("", $parts);
295 
296  return $title;
297  }
298 
299  public static function getRecordModelInstance(?int $record_id): ilDclBaseRecordModel
300  {
301  return new ilDclBaseRecordModel($record_id);
302  }
303 
304  public static function getPluginNameFromFieldModel(ilDclBaseFieldModel $object): string
305  {
306  $class_name = get_class($object);
307  $class_name = substr($class_name, 2, -(strlen(self::$field_class_patter) - 2));
308 
309  return $class_name;
310  }
311 }
static array $record_representation_cache
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getFieldClass(string $datatype, string $class_pattern)
Concatenates Classname from datatype and pattern.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
static getClassPathByInstance(ilDclBaseFieldModel $field, string $class_pattern)
static getFieldTypeByInstance(ilDclBaseFieldModel $field)
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
static getRecordModelInstance(?int $record_id)
static isPluginDatatype(string $datatype)
static getClassByInstance(ilDclBaseFieldModel $field, string $class_pattern)
static getPluginId(string $datatype)
static string $field_class_patter
static string $field_base_path_patter
static string $record_representation_class_pattern
$path
Definition: ltiservices.php:32
static parseDatatypeTitle(string $title)
Parse string to FieldClass format Replaces _ with camelcase-notation.
global $DIC
Definition: feed.php:28
static getFieldModelInstanceByClass(ilDclBaseFieldModel $field, ?int $field_id=null)
Gets the correct instance of a fieldModel class Checks if a field is a plugin a replaces the fieldMod...
static getFieldModelInstance(int $field_id, ?int $datatype=null)
Get FieldModel from field-id and datatype.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getRecordFieldInstance(object $field, object $record)
Creates a RecordField instance and loads the field and record representation.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getRecordRepresentationInstance(ilDclBaseRecordFieldModel $record_field)
Get RecordRepresentation from RecordFieldModel.
static array $field_representation_cache
static string $record_field_class_patter
static getFieldClassFile(string $datatype, string $class_pattern)
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...
static getPluginNameFromFieldModel(ilDclBaseFieldModel $object)
getDatatypeId()
Get datatype_id.
static string $field_representation_class_pattern
$base
Definition: index.php:4