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