ILIAS  release_8 Revision v8.24
class.ilObjectActivation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 public const ERR_SUG_START_END = 1;
30
31 public const TIMINGS_ACTIVATION = 0;
32 public const TIMINGS_DEACTIVATED = 1;
33 public const TIMINGS_PRESETTING = 2;
34 public const TIMINGS_FIXED = 3; // session only => obsolete?
35
36 protected static array $preloaded_data = array();
37
39 protected ilLanguage $lng;
40 protected ilDBInterface $db;
41
42 protected int $timing_type = 0;
43 protected ?int $timing_start = null;
44 protected ?int $timing_end = null;
45 protected ?int $suggestion_start = null;
46 protected ?int $suggestion_end = null;
47 protected bool $visible = false;
48 protected int $changeable = 0;
49
50 protected ?int $suggestion_start_rel = null;
51 protected ?int $suggestion_end_rel = null;
52
53 public function __construct()
54 {
55 global $DIC;
56
57 $this->error = $DIC["ilErr"];
58 $this->lng = $DIC->language();
59 $this->db = $DIC->database();
60 }
61
62 public function setTimingType(int $type): void
63 {
64 $this->timing_type = $type;
65 }
66
67 public function getTimingType(): int
68 {
69 return $this->timing_type;
70 }
71
72 public function setTimingStart(?int $start): void
73 {
74 $this->timing_start = $start;
75 }
76
77 public function getTimingStart(): ?int
78 {
80 }
81
82 public function setTimingEnd(?int $end): void
83 {
84 $this->timing_end = $end;
85 }
86
87 public function getTimingEnd(): ?int
88 {
89 return $this->timing_end;
90 }
91
92 public function setSuggestionStart(?int $start): void
93 {
94 if ($start === 0) {
95 $start = null;
96 }
97 $this->suggestion_start = $start;
98 }
99
100 public function setSuggestionStartRelative(?int $start): void
101 {
102 if ($start === 0) {
103 $start = null;
104 }
105 $this->suggestion_start_rel = $start;
106 }
107
108 public function setSuggestionEndRelative(int $end): void
109 {
110 if ($end === 0) {
111 $end = null;
112 }
113 $this->suggestion_end_rel = $end;
114 }
115
116 public function setSuggestionEnd(?int $end): void
117 {
118 if ($end === 0) {
119 $end = null;
120 }
121 $this->suggestion_end = $end;
122 }
123
124 public function toggleVisible(bool $status): void
125 {
126 $this->visible = $status;
127 }
128
129 public function enabledVisible(): bool
130 {
131 return $this->visible;
132 }
133
134 public function toggleChangeable(bool $status): void
135 {
136 $this->changeable = (int) $status;
137 }
138
139 public function enabledChangeable(): bool
140 {
141 return (bool) $this->changeable;
142 }
143
144 public function update(int $ref_id, ?int $parent_id = null): bool
145 {
146 $values = [
147 "timing_type" => ["integer", $this->getTimingType()],
148 "timing_start" => ["integer", $this->getTimingStart() ?? 0],
149 "timing_end" => ["integer", $this->getTimingEnd() ?? 0],
150 "suggestion_start" => ["integer", $this->suggestion_start ?? 0],
151 "suggestion_end" => ["integer", $this->suggestion_end ?? 0],
152 "changeable" => ["integer", (int) $this->enabledChangeable()],
153 "suggestion_start_rel" => ["integer", $this->suggestion_start_rel ?? 0],
154 "suggestion_end_rel" => ["integer", $this->suggestion_end_rel ?? 0],
155 "visible" => ["integer", $this->enabledVisible()]
156 ];
157
158 if (!is_null($parent_id)) {
159 $values["parent_id"] = ["integer", $parent_id];
160 }
161
162 $where = [
163 "obj_id" => ["integer", $ref_id]
164 ];
165
166 $this->db->update("crs_items", $values, $where);
167
168 unset(self::$preloaded_data[$ref_id]);
169
170 return true;
171 }
172
176 public static function preloadData(array $ref_ids): void
177 {
178 global $DIC;
179 $db = $DIC->database();
180
181 $sql =
182 "SELECT parent_id, obj_id, timing_type, timing_start, timing_end, suggestion_start," . PHP_EOL
183 . "suggestion_end, changeable, visible, position, suggestion_start_rel, suggestion_end_rel" . PHP_EOL
184 . "FROM crs_items" . PHP_EOL
185 . "WHERE " . $db->in("obj_id", $ref_ids, false, "integer") . PHP_EOL
186 ;
187 $set = $db->query($sql);
188 while ($row = $db->fetchAssoc($set)) {
189 self::$preloaded_data[$row["obj_id"]] = $row;
190 }
191 }
192
193 public static function getItem(int $ref_id): array
194 {
195 global $DIC;
196 $db = $DIC->database();
197
198 if (isset(self::$preloaded_data[$ref_id])) {
199 return self::$preloaded_data[$ref_id];
200 }
201
202 $sql =
203 "SELECT parent_id, obj_id, timing_type, timing_start, timing_end, suggestion_start," . PHP_EOL
204 . "suggestion_end, changeable, visible, position, suggestion_start_rel, suggestion_end_rel" . PHP_EOL
205 . "FROM crs_items" . PHP_EOL
206 . "WHERE obj_id = " . $db->quote($ref_id, "integer") . PHP_EOL
207 ;
208 $set = $db->query($sql);
209 $row = $db->fetchAssoc($set);
210
211 if (!isset($row["obj_id"])) {
213 } else {
214 self::$preloaded_data[$row["obj_id"]] = $row;
215 }
216 return $row;
217 }
218
222 public static function addAdditionalSubItemInformation(array &$item): void
223 {
224 global $DIC;
225 $ilUser = $DIC->user();
226
227 $item_array = self::getItem((int) $item['ref_id']);
228
229 $item['obj_id'] = ($item['obj_id'] > 0)
230 ? $item['obj_id']
231 : ilObject::_lookupObjId((int) $item['ref_id']);
232 $item['type'] = ($item['type'] != '')
233 ? $item['type']
234 : ilObject::_lookupType((int) $item['obj_id']);
235
236 $item['timing_type'] = $item_array['timing_type'] ?? 0;
237
238 if (($item_array['changeable'] ?? false) &&
239 $item_array['timing_type'] == self::TIMINGS_PRESETTING) {
240 // cognos-blu-patch: begin
241 $user_data = new ilTimingUser((int) $item['ref_id'], $ilUser->getId());
242 if ($user_data->isScheduled()) {
243 $item['start'] = $user_data->getStart()->get(IL_CAL_UNIX);
244 $item['end'] = $user_data->getEnd()->get(IL_CAL_UNIX);
245 $item['activation_info'] = 'crs_timings_planed_info';
246 } else {
247 $item['start'] = $item_array['suggestion_start'] ?? "";
248 $item['end'] = $item_array['suggestion_end'] ?? "";
249 $item['activation_info'] = 'crs_timings_suggested_info';
250 }
251 // cognos-blu-patch: end
252 } elseif (($item_array['timing_type'] ?? 0) == self::TIMINGS_PRESETTING) {
253 $item['start'] = $item_array['suggestion_start'] ?? "";
254 $item['end'] = $item_array['suggestion_end'] ?? "";
255 $item['activation_info'] = 'crs_timings_suggested_info';
256 } elseif (($item_array['timing_type'] ?? 0) == self::TIMINGS_ACTIVATION) {
257 $item['start'] = $item_array['timing_start'] ?? "";
258 $item['end'] = $item_array['timing_end'] ?? "";
259 $item['activation_info'] = 'obj_activation_list_gui';
260 } else {
261 $item['start'] = 'abc';
262 }
263
264 // #7359 - session sorting should always base on appointment date
265 if ($item['type'] == 'sess') {
266 $info = ilSessionAppointment::_lookupAppointment((int) $item['obj_id']);
267 // #11987
268 $item['masked_start'] = $item['start'];
269 $item['masked_end'] = $item['end'] ?? "";
270 $item['start'] = $info['start'] ?? '';
271 $item['end'] = $info['end'] ?? '';
272 }
273 }
274
278 public static function addListGUIActivationProperty(ilObjectListGUI $list_gui, array &$item): void
279 {
281 if (isset($item['timing_type'])) {
282 if (!isset($item['masked_start'])) {
283 $start = $item['start'] ?? 0;
284 $end = $item['end'] ?? 0;
285 } else {
286 $start = $item['masked_start'];
287 $end = $item['masked_end'];
288 }
289 $activation = '';
290 switch ($item['timing_type']) {
293 new ilDateTime($start, IL_CAL_UNIX),
294 new ilDateTime($end, IL_CAL_UNIX)
295 );
296 break;
297
300 new ilDate($start, IL_CAL_UNIX),
301 new ilDate($end, IL_CAL_UNIX)
302 );
303 break;
304 }
305 if ($activation != "") {
306 global $DIC;
307
308 $lng = $DIC->language();
309 $lng->loadLanguageModule('crs');
310
311 $list_gui->addCustomProperty(
312 $lng->txt($item['activation_info']),
313 $activation,
314 false,
315 true
316 );
317 }
318 }
319 }
320
324 protected static function createDefaultEntry(int $ref_id): array
325 {
326 global $DIC;
327
328 $db = $DIC->database();
329 $tree = $DIC->repositoryTree();
330
331 $parent_id = $tree->getParentId($ref_id);
332 if (!$parent_id) {
333 return [];
334 }
335
336 $ilAtomQuery = $db->buildAtomQuery();
337 $ilAtomQuery->addTableLock("crs_items");
338
339 $ilAtomQuery->addQueryCallable(function (ilDBInterface $db) use ($ref_id, $parent_id, &$item): void {
340 $sql =
341 "SELECT parent_id, obj_id, timing_type, timing_start, timing_end, suggestion_start," . PHP_EOL
342 . "suggestion_end, changeable, visible, position, suggestion_start_rel, suggestion_end_rel" . PHP_EOL
343 . "FROM crs_items" . PHP_EOL
344 . "WHERE obj_id = " . $db->quote($ref_id, "integer") . PHP_EOL
345 ;
346 $set = $db->query($sql);
347 if (!$db->numRows($set)) {
348 $now = time();
349
350 $item = array();
351 $item["timing_type"] = self::TIMINGS_DEACTIVATED;
352 $item["timing_start"] = $now;
353 $item["timing_end"] = $now;
354 $item["suggestion_start"] = $now;
355 $item["suggestion_end"] = $now;
356 $item['visible'] = 0;
357 $item['changeable'] = 0;
358
359 $values = [
360 "parent_id" => ["integer", $parent_id],
361 "obj_id" => ["integer", $ref_id],
362 "timing_type" => ["integer", $item["timing_type"]],
363 "timing_start" => ["integer", $item["timing_start"]],
364 "timing_end" => ["integer", $item["timing_end"]],
365 "suggestion_start" => ["integer", $item["suggestion_start"]],
366 "suggestion_end" => ["integer", $item["suggestion_end"]],
367 "changeable" => ["integer", $item["changeable"]],
368 "visible" => ["integer", $item["visible"]],
369 "suggestion_start_rel" => ["integer", $item["suggestion_start_rel"] ?? 0],
370 "suggestion_end_rel" => ["integer", $item["suggestion_end_rel"] ?? 0],
371 "position" => ["integer", 0]
372 ];
373 $db->insert("crs_items", $values);
374 }
375 });
376
377 $ilAtomQuery->run();
378
379 // #9982 - to make getItem()-cache work
380 $item["obj_id"] = $ref_id;
381 $item["parent_id"] = $parent_id;
382
383 return $item;
384 }
385
389 public static function deleteAllEntries(int $ref_id): bool
390 {
391 global $DIC;
392
393 $db = $DIC->database();
394
395 if (!$ref_id) {
396 return false;
397 }
398
399 $sql =
400 "DELETE FROM crs_items " . PHP_EOL
401 . "WHERE obj_id = " . $db->quote($ref_id, 'integer') . PHP_EOL
402 ;
403 $db->manipulate($sql);
404
405 $sql =
406 "DELETE FROM crs_items " . PHP_EOL
407 . "WHERE parent_id = " . $db->quote($ref_id, 'integer') . PHP_EOL
408 ;
409 $db->manipulate($sql);
410
411 return true;
412 }
413
414 public static function cloneDependencies(int $ref_id, int $target_id, int $copy_id): void
415 {
416 global $DIC;
417
418 $ilLog = $DIC["ilLog"];
419
420 $ilLog->write(__METHOD__ . ': Begin course items...' . $ref_id);
421
422 $items = self::getItems($ref_id, false);
423 if (!$items) {
424 $ilLog->write(__METHOD__ . ': No course items found.');
425 return;
426 }
427
428 // new course item object
429 if (!is_object(ilObjectFactory::getInstanceByRefId($target_id, false))) {
430 $ilLog->write(__METHOD__ . ': Cannot create target object.');
431 return;
432 }
433
434 $cp_options = ilCopyWizardOptions::_getInstance($copy_id);
435 $mappings = $cp_options->getMappings();
436
437 foreach ($items as $item) {
438 if (!isset($mappings[$item['parent_id']]) or !$mappings[$item['parent_id']]) {
439 $ilLog->write(__METHOD__ . ': No mapping for parent nr. ' . $item['parent_id']);
440 continue;
441 }
442 if (!isset($mappings[$item['obj_id']]) or !$mappings[$item['obj_id']]) {
443 $ilLog->write(__METHOD__ . ': No mapping for item nr. ' . $item['obj_id']);
444 continue;
445 }
446 $new_item_id = $mappings[$item['obj_id']];
447 $new_parent = $mappings[$item['parent_id']];
448
449 $new_item = new self();
450 $new_item->setTimingType((int) $item['timing_type']);
451 $new_item->setTimingStart((int) $item['timing_start']);
452 $new_item->setTimingEnd((int) $item['timing_end']);
453 $new_item->setSuggestionStart((int) $item['suggestion_start']);
454 $new_item->setSuggestionEnd((int) $item['suggestion_end']);
455 $new_item->toggleChangeable((bool) $item['changeable']);
456 $new_item->toggleVisible((bool) $item['visible']);
457 $new_item->update($new_item_id, $new_parent);
458 $new_item->setSuggestionStartRelative((int) ($item['suggestion_start_rel'] ?? 0));
459 $new_item->setSuggestionEndRelative((int) ($item['suggestion_end_rel'] ?? 0));
460 $new_item->createDefaultEntry($new_item_id);
461 $new_item->update($new_item_id);
462 }
463 }
464
465
466 //
467 // TIMINGS VIEW RELATED (COURSE ONLY)
468 //
469
473 public static function hasTimings(int $ref_id): bool
474 {
475 global $DIC;
476
477 $tree = $DIC->repositoryTree();
478 $db = $DIC->database();
479
480 $subtree = $tree->getSubTree($tree->getNodeData($ref_id));
481 $ref_ids = array();
482 foreach ($subtree as $node) {
483 $ref_ids[] = $node['ref_id'];
484 }
485
486 $sql =
487 "SELECT parent_id" . PHP_EOL
488 . "FROM crs_items" . PHP_EOL
489 . "WHERE timing_type = " . $db->quote(self::TIMINGS_PRESETTING, 'integer') . PHP_EOL
490 . "AND " . $db->in('obj_id', $ref_ids, false, 'integer') . PHP_EOL
491 ;
492 $res = $db->query($sql);
493 return (bool) $res->numRows();
494 }
495
499 public static function hasChangeableTimings(int $ref_id): bool
500 {
501 global $DIC;
502
503 $tree = $DIC->repositoryTree();
504 $db = $DIC->database();
505
506 $subtree = $tree->getSubTree($tree->getNodeData($ref_id));
507 $ref_ids = array();
508 foreach ($subtree as $node) {
509 $ref_ids[] = $node['ref_id'];
510 }
511
512 $sql =
513 "SELECT parent_id" . PHP_EOL
514 . "FROM crs_items" . PHP_EOL
515 . "WHERE timing_type = " . $db->quote(self::TIMINGS_PRESETTING, 'integer') . PHP_EOL
516 . "AND changeable = " . $db->quote(1, 'integer') . PHP_EOL
517 . "AND " . $db->in('obj_id', $ref_ids, false, 'integer') . PHP_EOL
518 ;
519 $res = $db->query($sql);
520 return (bool) $res->numRows();
521 }
522
526 protected static function processListItems(array $ref_ids): array
527 {
528 global $DIC;
529
530 $tree = $DIC->repositoryTree();
531
532 $res = array();
533
534 foreach (array_map('intval', $ref_ids) as $item_ref_id) {
535 if ($tree->isDeleted($item_ref_id)) {
536 continue;
537 }
538 // #7571: when node is removed from system, e.g. inactive trashcan, an empty array is returned
539 $node = $tree->getNodeData($item_ref_id);
540 if (!isset($node["ref_id"]) || (int) $node["ref_id"] !== $item_ref_id) {
541 continue;
542 }
543 $res[$item_ref_id] = $node;
544 }
545
546 if (count($res)) {
547 self::preloadData(array_keys($res));
548 foreach ($res as $idx => $item) {
550 $res[$idx] = $item;
551 }
552 }
553
554 return array_values($res);
555 }
556
560 public static function getItemsByEvent(int $event_id): array
561 {
562 $event_items = new ilEventItems($event_id);
563 return self::processListItems($event_items->getItems());
564 }
565
569 public static function getItemsByItemGroup(int $item_group_ref_id): array
570 {
571 $ig_items = new ilItemGroupItems($item_group_ref_id);
572 $items = $ig_items->getValidItems();
573 return self::processListItems($items);
574 }
575
579 public static function getItemsByObjective(int $objective_id): array
580 {
581 $item_ids = ilCourseObjectiveMaterials::_getAssignedMaterials($objective_id);
582 return self::processListItems($item_ids);
583 }
584
588 public static function getItems(int $parent_id, bool $with_list_data = true): array
589 {
590 global $DIC;
591
592 $tree = $DIC->repositoryTree();
593
594 $items = array();
595
596 $ref_ids = array();
597 foreach ($tree->getChilds($parent_id) as $item) {
598 if ($item['type'] !== 'rolf') {
599 $items[] = $item;
600 $ref_ids[] = (int) $item['ref_id'];
601 }
602 }
603
604 if ($ref_ids) {
605 self::preloadData($ref_ids);
606
607 foreach ($items as $idx => $item) {
608 if (!$with_list_data) {
609 $items[$idx] = array_merge($item, self::getItem((int) $item['ref_id']));
610 } else {
612 $items[$idx] = $item;
613 }
614 }
615 }
616 return $items;
617 }
618
622 public static function getTimingsAdministrationItems(int $parent_id): array
623 {
624 $items = self::getItems($parent_id, false);
625 $active = $availability = $inactive = [];
626 foreach ($items as $item) {
627 if ($item['timing_type'] == self::TIMINGS_DEACTIVATED) {
628 $inactive[] = $item;
629 } elseif ($item['timing_type'] == self::TIMINGS_ACTIVATION) {
630 $availability[] = $item;
631 } else {
632 $active[] = $item;
633 }
634 }
635 $active = ilArrayUtil::sortArray($active, 'suggestion_start');
636 $availability = ilArrayUtil::sortArray($availability, 'timing_start');
637 $inactive = ilArrayUtil::sortArray($inactive, 'title');
638
639 $items = array_merge($active, $availability, $inactive);
640 return $items;
641 }
642
646 public static function getTimingsItems(int $container_ref_id): array
647 {
648 global $DIC;
649
650 $objDefinition = $DIC["objDefinition"];
651
652 $filtered = array();
653
654 $event_items = ilEventItems::_getItemsOfContainer($container_ref_id);
655 foreach (self::getTimingsAdministrationItems($container_ref_id) as $item) {
656 if (!in_array($item['ref_id'], $event_items) &&
657 !$objDefinition->isSideBlock($item['type'])) {
658 $filtered[] = $item;
659 }
660 }
661
662 return $filtered;
663 }
664
665 public function read(int $ref_id, int $parent_id = 0): void
666 {
667 global $DIC;
668 $db = $DIC->database();
669
670 $sql =
671 "SELECT parent_id, obj_id, timing_type, timing_start, timing_end, suggestion_start," . PHP_EOL
672 . "suggestion_end, changeable, visible, position, suggestion_start_rel, suggestion_end_rel" . PHP_EOL
673 . "FROM crs_items" . PHP_EOL
674 . "WHERE obj_id = " . $db->quote($ref_id, 'integer') . PHP_EOL
675 ;
676
677 if ($parent_id) {
678 $sql .= "AND parent_id = " . $db->quote($parent_id, "integer") . PHP_EOL;
679 }
680
681 $res = $db->query($sql);
682 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
683 $this->setSuggestionStart((int) $row->suggestion_start);
684 $this->setSuggestionEnd((int) $row->suggestion_end);
685 $this->setSuggestionStartRelative((int) $row->suggestion_start_rel);
686 $this->setSuggestionEndRelative((int) $row->suggestion_end_rel);
687 $this->toggleVisible((bool) $row->visible);
688 $this->toggleChangeable((bool) $row->changeable);
689 $this->setTimingType((int) $row->timing_type);
690 $this->setTimingStart((int) $row->timing_start);
691 $this->setTimingEnd((int) $row->timing_end);
692 }
693 }
694}
const IL_CAL_UNIX
error(string $a_errmsg)
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
static _getInstance(int $a_copy_id)
static _getAssignedMaterials(int $a_objective_id)
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
@classDescription Date and time handling
Class for single dates.
Error Handling & global info handling uses PEAR error class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getItemsOfContainer(int $a_ref_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
loadLanguageModule(string $a_module)
Load language module.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Class ilObjectActivation.
static cloneDependencies(int $ref_id, int $target_id, int $copy_id)
static processListItems(array $ref_ids)
Validate ref ids and add list data.
static hasChangeableTimings(int $ref_id)
Check if there is any active changeable timing (in subtree)
update(int $ref_id, ?int $parent_id=null)
static getItemsByEvent(int $event_id)
Get session material / event items.
static getTimingsItems(int $container_ref_id)
Get (sub) item data for timings view (no session material, no side blocks)
static getTimingsAdministrationItems(int $parent_id)
Get (sub) item data for timings administration view (active/inactive)
static getItems(int $parent_id, bool $with_list_data=true)
Get sub item data.
static getItem(int $ref_id)
static getItemsByObjective(int $objective_id)
Get objective items.
static getItemsByItemGroup(int $item_group_ref_id)
Get materials of item group.
read(int $ref_id, int $parent_id=0)
static preloadData(array $ref_ids)
Preload data to internal cache.
static createDefaultEntry(int $ref_id)
Create db entry with default values.
static addAdditionalSubItemInformation(array &$item)
Parse item data for list entries.
static hasTimings(int $ref_id)
Check if there is any active timing (in subtree)
static addListGUIActivationProperty(ilObjectListGUI $list_gui, array &$item)
Get timing details for list gui.
static deleteAllEntries(int $ref_id)
Delete all db entries for ref id.
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
addCustomProperty(string $property="", string $value="", bool $alert=false, bool $newline=false)
static _lookupType(int $id, bool $reference=false)
static _lookupObjId(int $ref_id)
static _lookupAppointment(int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
$target_id
Definition: goto.php:52
$ilUser
Definition: imgupload.php:34
Interface ilDBInterface.
numRows(ilDBStatement $statement)
quote($value, string $type)
manipulate(string $query)
Run a (write) Query on the database.
query(string $query)
Run a (read-only) Query on the database.
fetchAssoc(ilDBStatement $statement)
in(string $field, array $values, bool $negate=false, string $type="")
$ref_id
Definition: ltiauth.php:67
$res
Definition: ltiservices.php:69
$type