ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
NewsAggregator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use SplQueue;
25
31{
33 protected array $strategies = [];
34 protected \ilTree $tree;
35
36 public function __construct()
37 {
38 global $DIC;
39
40 $this->tree = $DIC->repositoryTree();
41 $this->initializeStrategies();
42 }
43
48 public function aggregate(array $contexts): array
49 {
51 $frontier = new SplQueue();
52 $result = [];
53
54 // Prepare queue
55 foreach ($contexts as $context) {
56 $strategy = $this->getStrategy($context->getObjType());
57 if ($strategy === null || $strategy->shouldSkip($context)) {
58 continue;
59 }
60 $frontier->enqueue($context);
61 }
62
63 while (!$frontier->isEmpty()) {
64 $current = $frontier->dequeue();
65
66 // Ensure each context is only visited once
67 if (array_key_exists($current->getObjId(), $result)) {
68 continue;
69 }
70 $result[$current->getObjId()] = $current;
71
72 // Skip if no processing necessary
73 $strategy = $this->getStrategy($current->getObjType());
74 if ($strategy === null || $strategy->shouldSkip($current)) {
75 continue;
76 }
77
78 $children = $strategy->aggregate($current);
79 foreach ($children as $child) {
80 if ($strategy->isRecursive()) {
81 // Recursive items will be added directly
82 $result[$child->getObjId()] = $child;
83 } else {
84 // Iterative items will be queued for further processing
85 $frontier->enqueue($child);
86 }
87 }
88 }
89
90 return array_values($result);
91 }
92
93 protected function getStrategy(string $object_type): ?NewsAggregationStrategy
94 {
95 return $this->strategies[$object_type] ?? null;
96 }
97
98 protected function initializeStrategies(): void
99 {
100 $subtree_strategy = new SubtreeAggregationStrategy($this->tree);
101
102 $this->strategies['cat'] = new CategoryAggregationStrategy($this->tree);
103 $this->strategies['crs'] = $subtree_strategy;
104 $this->strategies['grp'] = $subtree_strategy;
105 $this->strategies['fold'] = $subtree_strategy;
106 }
107}
Category Aggregation Strategy aggregates related contexts for a category context.
News Aggregator aggregates related contexts for a news context using a layer-wise Batching BFS to agg...
Subtree Aggregation Strategy aggregates related contexts for groups and courses.
News Context DTO represents a context where news items can be associated with.
Definition: NewsContext.php:29
News Aggregation Strategy Interface defines the contract for news aggregation strategies.
global $DIC
Definition: shib_login.php:26