ILIAS  release_8 Revision v8.24
class.ilAdvancedMDClaimingPlugin.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
6
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 ]
269 );
270 $idx++;
271 }
272 }
273
274 return $field_id;
275 }
276
277 public static function updateDBField(
278 int $a_field_id,
279 string $a_title,
280 ?string $a_description = null,
281 bool $a_searchable = false,
282 ?array $a_definition = null
283 ): bool {
284 global $DIC;
285
286 $ilDB = $DIC->database();
287
288 if (self::hasDBField($a_field_id)) {
289 $fields = array(
290 "field_id" => array("integer", $a_field_id),
291 "title" => array("text", trim($a_title)),
292 "description" => array("text", trim($a_description)),
293 "searchable" => array("integer", (int) $a_searchable)
294 );
295 if ($a_definition) {
296 $fields["field_values"] = array("text", serialize($a_definition));
297 }
298 $ilDB->update(
299 "adv_mdf_definition",
300 $fields,
301 array("field_id" => array("integer", $a_field_id))
302 );
303 return true;
304 }
305
306 return false;
307 }
308
309 public static function deleteDBField(int $a_field_id): bool
310 {
311 global $DIC;
312
313 $ilDB = $DIC['ilDB'];
314
315 if (self::hasDBField($a_field_id)) {
316 $ilDB->manipulate("DELETE FROM adv_mdf_definition" .
317 " WHERE field_id = " . $ilDB->quote($a_field_id, "integer"));
318 return true;
319 }
320
321 return false;
322 }
323
324 protected static function getDBSubstitution(string $a_obj_type, bool $a_include_field_data = false): array
325 {
326 global $DIC;
327
328 $ilDB = $DIC->database();
329
330 $set = $ilDB->query("SELECT * FROM adv_md_substitutions" .
331 " WHERE obj_type = " . $ilDB->quote($a_obj_type, "text"));
332 if ($ilDB->numRows($set)) {
333 $res = $ilDB->fetchAssoc($set);
334 $res["hide_description"] = array("integer", (bool) $res["hide_description"]);
335 $res["hide_field_names"] = array("integer", (bool) $res["hide_field_names"]);
336
337 if ($a_include_field_data) {
338 $res["substitution"] = array("text", (array) unserialize($res["substitution"]));
339 } else {
340 unset($res["substitution"]);
341 }
342 unset($res["obj_type"]);
343
344 return $res;
345 }
346 return [];
347 }
348
349 public static function setDBSubstitution(
350 string $a_obj_type,
351 bool $a_show_description,
352 bool $a_show_field_names
353 ): bool {
354 global $DIC;
355
356 $ilDB = $DIC->database();
357
358 if (self::isValidObjType($a_obj_type, true)) {
359 $fields = self::getDBSubstitution($a_obj_type);
360
361 $create = false;
362 if (!$fields) {
363 $create = true;
364 $fields = array("obj_type" => array("text", $a_obj_type));
365 }
366
367 $fields["hide_description"] = array("integer", !$a_show_description);
368 $fields["hide_field_names"] = array("integer", !$a_show_field_names);
369
370 if ($create) {
371 $ilDB->insert("adv_md_substitutions", $fields);
372 } else {
373 $ilDB->update(
374 "adv_md_substitutions",
375 $fields,
376 array("obj_type" => array("text", $a_obj_type))
377 );
378 }
379
380 return true;
381 }
382 return false;
383 }
384
385 public static function hasDBFieldSubstitution(string $a_obj_type, int $a_field_id): bool
386 {
387 if (self::isValidObjType($a_obj_type, true)) {
388 $fields = self::getDBSubstitution($a_obj_type, true);
389 $fields = $fields["substitution"][1];
390 foreach ($fields as $field) {
391 if ($field["field_id"] == $a_field_id) {
392 return true;
393 }
394 }
395 }
396 return false;
397 }
398
399 public static function setDBFieldSubstitution(
400 string $a_obj_type,
401 int $a_field_id,
402 bool $a_bold = false,
403 bool $a_newline = false
404 ): bool {
405 global $DIC;
406
407 $ilDB = $DIC->database();
408
409 if (self::isValidObjType($a_obj_type, true)) {
410 $fields = self::getDBSubstitution($a_obj_type, true);
411 if (!$fields) {
412 self::setDBSubstitution($a_obj_type, true, true);
413 $fields = array();
414 } else {
415 $fields = $fields["substitution"][1];
416 }
417
418 $found = false;
419 foreach ($fields as $idx => $field) {
420 if ($field["field_id"] == $a_field_id) {
421 $fields[$idx]["bold"] = $a_bold;
422 $fields[$idx]["newline"] = $a_newline;
423 $found = true;
424 break;
425 }
426 }
427 if (!$found) {
428 $fields[] = array(
429 "field_id" => $a_field_id
430 ,
431 "bold" => $a_bold
432 ,
433 "newline" => $a_newline
434 );
435 }
436
437 $fields = array("substitution" => array("text", serialize($fields)));
438 $ilDB->update(
439 "adv_md_substitutions",
440 $fields,
441 array("obj_type" => array("text", $a_obj_type))
442 );
443 }
444 return false;
445 }
446
447 public static function removeDBFieldSubstitution(string $a_obj_type, int $a_field_id): bool
448 {
449 global $DIC;
450
451 $ilDB = $DIC->database();
452
453 if (self::isValidObjType($a_obj_type, true)) {
454 $fields = self::getDBSubstitution($a_obj_type, true);
455 if (!$fields) {
456 return true;
457 } else {
458 $fields = $fields["substitution"][1];
459 }
460
461 $found = false;
462 foreach ($fields as $idx => $field) {
463 if ($field["field_id"] == $a_field_id) {
464 unset($fields[$idx]);
465 $found = true;
466 break;
467 }
468 }
469 if ($found) {
470 $fields = array("substitution" => array("text", serialize($fields)));
471 $ilDB->update(
472 "adv_md_substitutions",
473 $fields,
474 array("obj_type" => array("text", $a_obj_type))
475 );
476 }
477 return true;
478 }
479 return false;
480 }
481}
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 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 createDBField(int $a_record_id, int $a_type, string $a_title, ?string $a_description=null, bool $a_searchable=false, array $a_definition=null)
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 IL_INST_ID
Definition: constants.php:40
$valid
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$type