ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilAdvancedMDClaimingPlugin.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
6 
12 abstract class ilAdvancedMDClaimingPlugin extends ilPlugin
13 {
14  abstract public function checkPermission(
15  int $a_user_id,
16  int $a_context_type,
17  int $a_context_id,
18  int $a_action_id,
19  int $a_action_sub_id
20  ): bool;
21 
22  public static function hasDBRecord(int $a_record_id): bool
23  {
24  global $DIC;
25  $ilDB = $DIC->database();
26 
27  $set = $ilDB->query("SELECT record_id FROM adv_md_record" .
28  " WHERE record_id = " . $ilDB->quote($a_record_id, "integer"));
29  return (bool) $ilDB->numRows($set);
30  }
31 
39  public static function createDBRecord(
40  string $a_title,
41  string $a_description,
42  bool $a_active,
43  array $a_obj_types
44  ): int {
45  global $DIC;
46 
47  $ilDB = $DIC->database();
48 
49  $record_id = $ilDB->nextId("adv_md_record");
50 
51  $fields = array(
52  "record_id" => array("integer", $record_id),
53  "import_id" => array("text", 'il_' . IL_INST_ID . '_adv_md_record_' . $record_id),
54  "title" => array("text", trim($a_title)),
55  "description" => array("text", trim($a_description)),
56  "active" => array("integer", (int) $a_active)
57  );
58  $ilDB->insert("adv_md_record", $fields);
59  self::saveRecordObjTypes($record_id, $a_obj_types);
60  return $record_id;
61  }
62 
67  protected static function isValidObjType(string $a_obj_type, bool $a_is_substitution = false): bool
68  {
69  // ecs not supported yet
70  $valid = ["crs", "cat", "book", "wiki", "glo", "orgu", "prg", 'grp', 'iass'];
71 
72  if (!$a_is_substitution) {
73  $valid[] = "orgu";
74  $valid[] = "prg";
75  }
76 
77  return in_array($a_obj_type, $valid);
78  }
79 
85  protected static function saveRecordObjTypes(int $a_record_id, array $a_obj_types): void
86  {
87  global $DIC;
88  $ilDB = $DIC->database();
89 
90  foreach ($a_obj_types as $type) {
91  if (!is_array($type)) {
92  $type = strtolower(trim($type));
93  $subtype = "-";
94  } else {
95  $subtype = strtolower(trim($type[1]));
96  $type = strtolower(trim($type[0]));
97  }
98 
99  if (self::isValidObjType($type)) {
100  $fields = array(
101  "record_id" => array("integer", $a_record_id),
102  "obj_type" => array("text", $type),
103  "sub_type" => array("text", $subtype)
104  );
105  $ilDB->insert("adv_md_record_objs", $fields);
106  }
107  }
108  }
109 
119  public static function updateDBRecord(
120  int $a_record_id,
121  string $a_title,
122  string $a_description,
123  bool $a_active,
124  array $a_obj_types
125  ): bool {
126  global $DIC;
127 
128  $ilDB = $DIC->database();
129 
130  if (self::hasDBRecord($a_record_id)) {
131  $fields = array(
132  "title" => array("text", trim($a_title)),
133  "description" => array("text", trim($a_description)),
134  "active" => array("integer", (int) $a_active)
135  );
136  $ilDB->update(
137  "adv_md_record",
138  $fields,
139  array("record_id" => array("integer", $a_record_id))
140  );
141 
142  $ilDB->manipulate("DELETE FROM adv_md_record_objs" .
143  " WHERE record_id = " . $ilDB->quote($a_record_id, "integer"));
144 
145  self::saveRecordObjTypes($a_record_id, $a_obj_types);
146 
147  return true;
148  }
149  return false;
150  }
151 
152  public static function deleteDBRecord(int $a_record_id): bool
153  {
154  global $DIC;
155 
156  $ilDB = $DIC->database();
157 
158  if (self::hasDBRecord($a_record_id)) {
159  $ilDB->manipulate("DELETE FROM adv_md_record" .
160  " WHERE record_id = " . $ilDB->quote($a_record_id, "integer"));
161  return true;
162  }
163 
164  return false;
165  }
166 
167  public static function hasDBField(int $a_field_id): bool
168  {
169  global $DIC;
170  $ilDB = $DIC->database();
171 
172  $set = $ilDB->query("SELECT field_id FROM adv_mdf_definition" .
173  " WHERE field_id = " . $ilDB->quote($a_field_id, "integer"));
174  return (bool) $ilDB->numRows($set);
175  }
176 
180  protected static function getDBFieldLastPosition(int $a_record_id): int
181  {
182  global $DIC;
183 
184  $ilDB = $DIC->database();
185 
186  $sql = "SELECT max(position) pos" .
187  " FROM adv_mdf_definition" .
188  " WHERE record_id = " . $ilDB->quote($a_record_id, "integer");
189  $set = $ilDB->query($sql);
190  if ($ilDB->numRows($set)) {
191  $pos = $ilDB->fetchAssoc($set);
192  return (int) $pos["pos"];
193  }
194  return 0;
195  }
196 
197  public static function createDBField(
198  int $a_record_id,
199  int $a_type,
200  string $a_title,
201  ?string $a_description = null,
202  bool $a_searchable = false,
203  array $a_definition = null
204  ): ?int {
205  global $DIC;
206 
207  $ilDB = $DIC->database();
208 
209  if (!self::hasDBRecord($a_record_id)) {
210  return null;
211  }
212 
213  $field_id = $ilDB->nextId("adv_mdf_definition");
214 
215  // validating type
217  return null;
218  }
219 
220  $options_in_different_table = $a_definition &&
223 
224  $pos = self::getDBFieldLastPosition($a_record_id) + 1;
225 
226  $fields = array(
227  "record_id" => array("integer", $a_record_id),
228  "field_id" => array("integer", $field_id),
229  "import_id" => array("text", "il_" . IL_INST_ID . "_adv_md_field_" . $field_id),
230  "field_type" => array("integer", $a_type),
231  "position" => array("integer", $pos),
232  "title" => array("text", trim($a_title)),
233  "description" => array("text", trim((string) $a_description)),
234  "searchable" => array("integer", (int) $a_searchable)
235  );
236  if ($a_definition && !$options_in_different_table) {
237  $fields["field_values"] = array("text", serialize($a_definition));
238  }
239  $ilDB->insert("adv_mdf_definition", $fields);
240 
241  if ($options_in_different_table) {
242  $ilDB->manipulate(
243  'DELETE FROM adv_mdf_enum WHERE field_id = ' .
244  $ilDB->quote($field_id, ilDBConstants::T_INTEGER)
245  );
246 
247  $default_language = '';
248  $res = $ilDB->query(
249  'SELECT lang_default FROM adv_md_record WHERE record_id = ' .
250  $ilDB->quote($a_record_id, 'integer')
251  );
252  if ($row = $res->fetchAssoc()) {
253  $default_language = (string) $row['lang_default'];
254  }
255 
256  $idx = 0;
257  foreach ($a_definition as $option) {
258  if (!is_string($option)) {
259  continue;
260  }
261  $ilDB->insert(
262  'adv_mdf_enum',
263  [
264  'field_id' => [ilDBConstants::T_INTEGER, $field_id],
265  'lang_code' => [ilDBConstants::T_TEXT, $default_language],
266  'idx' => [ilDBConstants::T_INTEGER, $idx],
267  'value' => [ilDBConstants::T_TEXT, $option],
268  'position' => [ilDBConstants::T_INTEGER, $idx]
269  ]
270  );
271  $idx++;
272  }
273  }
274 
275  return $field_id;
276  }
277 
278  public static function updateDBField(
279  int $a_field_id,
280  string $a_title,
281  ?string $a_description = null,
282  bool $a_searchable = false,
283  ?array $a_definition = null
284  ): bool {
285  global $DIC;
286 
287  $ilDB = $DIC->database();
288 
289  if (self::hasDBField($a_field_id)) {
290  $fields = array(
291  "field_id" => array("integer", $a_field_id),
292  "title" => array("text", trim($a_title)),
293  "description" => array("text", trim($a_description)),
294  "searchable" => array("integer", (int) $a_searchable)
295  );
296  if ($a_definition) {
297  $fields["field_values"] = array("text", serialize($a_definition));
298  }
299  $ilDB->update(
300  "adv_mdf_definition",
301  $fields,
302  array("field_id" => array("integer", $a_field_id))
303  );
304  return true;
305  }
306 
307  return false;
308  }
309 
310  public static function deleteDBField(int $a_field_id): bool
311  {
312  global $DIC;
313 
314  $ilDB = $DIC['ilDB'];
315 
316  if (self::hasDBField($a_field_id)) {
317  $ilDB->manipulate("DELETE FROM adv_mdf_definition" .
318  " WHERE field_id = " . $ilDB->quote($a_field_id, "integer"));
319  return true;
320  }
321 
322  return false;
323  }
324 
325  protected static function getDBSubstitution(string $a_obj_type, bool $a_include_field_data = false): array
326  {
327  global $DIC;
328 
329  $ilDB = $DIC->database();
330 
331  $set = $ilDB->query("SELECT * FROM adv_md_substitutions" .
332  " WHERE obj_type = " . $ilDB->quote($a_obj_type, "text"));
333  if ($ilDB->numRows($set)) {
334  $res = $ilDB->fetchAssoc($set);
335  $res["hide_description"] = array("integer", (bool) $res["hide_description"]);
336  $res["hide_field_names"] = array("integer", (bool) $res["hide_field_names"]);
337 
338  if ($a_include_field_data) {
339  $res["substitution"] = array("text", (array) unserialize($res["substitution"]));
340  } else {
341  unset($res["substitution"]);
342  }
343  unset($res["obj_type"]);
344 
345  return $res;
346  }
347  return [];
348  }
349 
350  public static function setDBSubstitution(
351  string $a_obj_type,
352  bool $a_show_description,
353  bool $a_show_field_names
354  ): bool {
355  global $DIC;
356 
357  $ilDB = $DIC->database();
358 
359  if (self::isValidObjType($a_obj_type, true)) {
360  $fields = self::getDBSubstitution($a_obj_type);
361 
362  $create = false;
363  if (!$fields) {
364  $create = true;
365  $fields = array("obj_type" => array("text", $a_obj_type));
366  }
367 
368  $fields["hide_description"] = array("integer", !$a_show_description);
369  $fields["hide_field_names"] = array("integer", !$a_show_field_names);
370 
371  if ($create) {
372  $ilDB->insert("adv_md_substitutions", $fields);
373  } else {
374  $ilDB->update(
375  "adv_md_substitutions",
376  $fields,
377  array("obj_type" => array("text", $a_obj_type))
378  );
379  }
380 
381  return true;
382  }
383  return false;
384  }
385 
386  public static function hasDBFieldSubstitution(string $a_obj_type, int $a_field_id): bool
387  {
388  if (self::isValidObjType($a_obj_type, true)) {
389  $fields = self::getDBSubstitution($a_obj_type, true);
390  $fields = $fields["substitution"][1];
391  foreach ($fields as $field) {
392  if ($field["field_id"] == $a_field_id) {
393  return true;
394  }
395  }
396  }
397  return false;
398  }
399 
400  public static function setDBFieldSubstitution(
401  string $a_obj_type,
402  int $a_field_id,
403  bool $a_bold = false,
404  bool $a_newline = false
405  ): bool {
406  global $DIC;
407 
408  $ilDB = $DIC->database();
409 
410  if (self::isValidObjType($a_obj_type, true)) {
411  $fields = self::getDBSubstitution($a_obj_type, true);
412  if (!$fields) {
413  self::setDBSubstitution($a_obj_type, true, true);
414  $fields = array();
415  } else {
416  $fields = $fields["substitution"][1];
417  }
418 
419  $found = false;
420  foreach ($fields as $idx => $field) {
421  if ($field["field_id"] == $a_field_id) {
422  $fields[$idx]["bold"] = $a_bold;
423  $fields[$idx]["newline"] = $a_newline;
424  $found = true;
425  break;
426  }
427  }
428  if (!$found) {
429  $fields[] = array(
430  "field_id" => $a_field_id
431  ,
432  "bold" => $a_bold
433  ,
434  "newline" => $a_newline
435  );
436  }
437 
438  $fields = array("substitution" => array("text", serialize($fields)));
439  $ilDB->update(
440  "adv_md_substitutions",
441  $fields,
442  array("obj_type" => array("text", $a_obj_type))
443  );
444  }
445  return false;
446  }
447 
448  public static function removeDBFieldSubstitution(string $a_obj_type, int $a_field_id): bool
449  {
450  global $DIC;
451 
452  $ilDB = $DIC->database();
453 
454  if (self::isValidObjType($a_obj_type, true)) {
455  $fields = self::getDBSubstitution($a_obj_type, true);
456  if (!$fields) {
457  return true;
458  } else {
459  $fields = $fields["substitution"][1];
460  }
461 
462  $found = false;
463  foreach ($fields as $idx => $field) {
464  if ($field["field_id"] == $a_field_id) {
465  unset($fields[$idx]);
466  $found = true;
467  break;
468  }
469  }
470  if ($found) {
471  $fields = array("substitution" => array("text", serialize($fields)));
472  $ilDB->update(
473  "adv_md_substitutions",
474  $fields,
475  array("obj_type" => array("text", $a_obj_type))
476  );
477  }
478  return true;
479  }
480  return false;
481  }
482 }
static updateDBField(int $a_field_id, string $a_title, ?string $a_description=null, bool $a_searchable=false, ?array $a_definition=null)
$res
Definition: ltiservices.php:69
static isValidObjType(string $a_obj_type, bool $a_is_substitution=false)
Validate object type.
const IL_INST_ID
Definition: constants.php:40
static saveRecordObjTypes(int $a_record_id, array $a_obj_types)
Save object type assignments for record.
checkPermission(int $a_user_id, int $a_context_type, int $a_context_id, int $a_action_id, int $a_action_sub_id)
const TYPE_SELECT
TODO: put this in when minimum php version is set to 8.2.
$valid
static createDBField(int $a_record_id, int $a_type, string $a_title, ?string $a_description=null, bool $a_searchable=false, array $a_definition=null)
static setDBSubstitution(string $a_obj_type, bool $a_show_description, bool $a_show_field_names)
Abstract parent class for all advanced md claiming plugin classes.
static createDBRecord(string $a_title, string $a_description, bool $a_active, array $a_obj_types)
static getDBSubstitution(string $a_obj_type, bool $a_include_field_data=false)
static hasDBFieldSubstitution(string $a_obj_type, int $a_field_id)
static updateDBRecord(int $a_record_id, string $a_title, string $a_description, bool $a_active, array $a_obj_types)
Update record db entry.
global $DIC
Definition: shib_login.php:25
static removeDBFieldSubstitution(string $a_obj_type, int $a_field_id)
static setDBFieldSubstitution(string $a_obj_type, int $a_field_id, bool $a_bold=false, bool $a_newline=false)