ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMembershipCronNotificationsData.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 
6 
24 
31 {
32  protected Service $notes;
36  protected int $last_run_unix;
37  protected string $last_run_date;
38  protected string $cron_id;
39  protected ilLogger $log;
40  protected array $objects = [];
41  protected array $news = [];
42 
46  protected array $news_per_user = [];
47 
51  protected array $user_news_aggr = [];
52  protected array $likes = [];
53  protected array $comments = [];
54  protected array $missing_news_per_user = [];
55  protected array $missing_news = [];
56 
58 
59  public function __construct(int $last_run, string $cron_id)
60  {
61  global $DIC;
62 
63  $this->access = $DIC->access();
64 
65  $this->last_run_unix = $last_run;
66  $this->last_run_date = date('Y-m-d H:i:s', $last_run);
67  $this->cron_id = $cron_id;
68  $this->log = ilLoggerFactory::getLogger("mmbr");
69  $this->notes = $DIC->notes();
70  $this->load();
71  }
72 
76  protected function load(): void
77  {
78  $ilAccess = $this->access;
79 
80  // all group/course notifications: ref id => user ids
82 
83  if (count($this->objects)) {
84  $this->log->debug("nr of objects: " . count($this->objects));
85 
86  // gather news for each user over all objects
87  $this->user_news_aggr = array();
88 
89  foreach ($this->objects as $ref_id => $user_ids) {
90  $this->log->debug("handle ref id " . $ref_id . ", users: " . count($user_ids));
91  $this->log->debug("last run unix: " . $this->last_run_unix);
92  $this->log->debug("last run date: " . $this->last_run_date);
93  // gather news per object
94  $news_item = new ilNewsItem();
95  $objs = $this->getObjectsForRefId($ref_id);
96  if (
97  isset($objs["obj_id"]) &&
98  is_array($objs["obj_id"]) &&
99  $news_item->checkNewsExistsForObjects($objs["obj_id"], $this->last_run_date)
100  ) {
101  $this->log->debug("Got news");
102  foreach ($user_ids as $user_id) {
103  // gather news for user
104  $user_news = $news_item->getNewsForRefId(
105  $ref_id,
106  false,
107  false,
108  $this->last_run_date,
109  false,
110  false,
111  false,
112  false,
113  $user_id
114  );
115  if ($user_news) {
116  $this->user_news_aggr[$user_id][$ref_id] = $user_news;
117 
118  // store all single news
119  foreach ($this->user_news_aggr as $agg_news) {
120  if (isset($agg_news["aggregation"]) && is_array($agg_news["aggregation"]) && $agg_news["aggregation"] !== []) {
121  foreach ($agg_news["aggregation"] as $n) {
122  $this->news[$n["id"]] = $n;
123  $this->news_per_user[$user_id][$ref_id][$n["id"]] = $n["id"];
124  }
125  } elseif (is_array($agg_news)) {
126  if (isset($agg_news["id"])) {
127  $this->news[$agg_news["id"]] = $agg_news;
128  $this->news_per_user[$user_id][$ref_id][$agg_news["id"]] = $agg_news["id"];
129  } else {
130  foreach ($agg_news as $agg_news_items) {
131  foreach ($agg_news_items as $agg_news_item) {
132  if (isset($agg_news_item["id"])) {
133  $this->news[$agg_news_item["id"]] = $agg_news_item;
134  $this->news_per_user[$user_id][$ref_id][$agg_news_item["id"]] = $agg_news_item["id"];
135  }
136  }
137  }
138  }
139  }
140  }
141 
142  $this->ping();
143  }
144  }
145  } else {
146  $this->log->debug("Got no news");
147  }
148 
149  // gather likes per object and store them "per news item"
150  // currently only news can be liked
151  $ref_for_obj_id = array();
152  foreach ($objs["ref_id"] as $i) {
153  $ref_for_obj_id[$i["obj_id"]][$i["ref_id"]] = $i["ref_id"];
154  }
155  $like_data = new ilLikeData(array_keys($objs["obj_id"]));
156  foreach (array_keys($objs["obj_id"]) as $obj_id) {
157  $this->log->debug("Get like data for obj_id: " . $obj_id);
158  foreach ($like_data->getExpressionEntriesForObject($obj_id, $this->last_run_date) as $like) {
159  reset($user_ids);
160  foreach ($user_ids as $user_id) {
161  $has_perm = false;
162  foreach ($ref_for_obj_id[$obj_id] as $perm_ref_id) {
163  if ($ilAccess->checkAccessOfUser($user_id, "read", "", (int) $perm_ref_id)) {
164  $has_perm = true;
165  break;
166  }
167  }
168  if ($has_perm) {
169  $this->likes[$user_id][$like["news_id"]][] = $like;
170 
171  // get news data for news that are not included above
172  $this->checkMissingNews($user_id, $ref_id, $like["news_id"]);
173  $this->ping();
174  }
175  }
176  }
177  }
178 
179  // gather comments
180  foreach (array_keys($objs["obj_id"]) as $obj_id) {
181  $coms = $this->notes
182  ->domain()
183  ->getAllCommentsForObjId(
184  $obj_id,
185  $this->last_run_date
186  );
187  foreach ($coms as $c) {
188  $comment_context = $c->getContext();
189  if ($comment_context->getNewsId() === 0) {
190  continue;
191  }
192  reset($user_ids);
193  foreach ($user_ids as $user_id) {
194  $has_perm = false;
195  foreach ($ref_for_obj_id[$obj_id] as $perm_ref_id) {
196  if ($ilAccess->checkAccessOfUser($user_id, "read", "", (int) $perm_ref_id)) {
197  $has_perm = true;
198  break;
199  }
200  }
201  if ($has_perm) {
202  $this->comments[$user_id][$comment_context->getNewsId()][] = $c;
203 
204  // get news data for news that are not included above
205  $this->checkMissingNews($user_id, $ref_id, $comment_context->getNewsId());
206  $this->ping();
207  }
208  }
209  }
210  }
211  }
212  $this->loadMissingNews();
213  }
214  }
215 
219  protected function checkMissingNews(int $user_id, int $ref_id, int $news_id): void
220  {
221  $this->log->debug("Check missing news: " . $user_id . "-" . $ref_id . "-" . $news_id);
222  if (!is_array($this->news_per_user[$user_id][$ref_id] ?? null) ||
223  !in_array($news_id, $this->news_per_user[$user_id][$ref_id])) {
224  $this->log->debug("Add missing news: " . $news_id);
225  $this->missing_news[$news_id] = $news_id;
226  $this->missing_news_per_user[$user_id][$ref_id][$news_id] = $news_id;
227  }
228  }
229 
233  protected function loadMissingNews(): void
234  {
235  foreach (ilNewsItem::queryNewsByIds($this->missing_news) as $news) {
236  $this->log->debug("Got missing news: " . $news["id"]);
237  $this->news[$news["id"]] = $news;
238  }
239  foreach ($this->missing_news_per_user as $user_id => $r) {
240  foreach ($r as $ref_id => $n) {
241  foreach ($n as $news_id) {
242  $this->log->debug("Load missing news: " . $user_id . "-" . $ref_id . "-" . $news_id);
243  if (isset($this->news[$news_id])) {
244  $this->user_news_aggr[$user_id][$ref_id][$news_id] = $this->news[$news_id];
245  $this->news_per_user[$user_id][$ref_id][$news_id] = $news_id;
246  }
247  }
248  }
249  }
250  }
251 
255  protected function getObjectsForRefId(int $a_ref_id): array
256  {
257  global $DIC;
258 
259  $tree = $DIC->repositoryTree();
260  $nodes = array();
261 
262  if (!$tree->isDeleted($a_ref_id)) {
263  // parse repository branch of group
264 
265  $node = $tree->getNodeData($a_ref_id);
266  foreach ($tree->getSubTree($node) as $child) {
267  if ($child["type"] !== "rolf") {
268  $nodes["obj_id"][$child["obj_id"]] = array(
269  "obj_id" => $child["obj_id"],
270  "type" => $child["type"]
271  );
272  $nodes["ref_id"][$child["child"]] = array(
273  "ref_id" => $child["child"],
274  "obj_id" => $child["obj_id"],
275  "type" => $child["type"]
276  );
277  }
278  }
279  }
280 
281  return $nodes;
282  }
283 
287  protected function ping(): void
288  {
289  global $DIC;
290 
291  $DIC->cron()->manager()->ping($this->cron_id);
292  }
293 
297  public function getAggregatedNews(): array
298  {
299  return $this->user_news_aggr;
300  }
301 
305  public function getLikes(int $news_id, int $user_id): array
306  {
307  if (isset($this->likes[$user_id][$news_id])) {
308  return $this->likes[$user_id][$news_id];
309  }
310  return [];
311  }
312 
316  public function getComments(int $news_id, int $user_id): array
317  {
318  if (isset($this->comments[$user_id][$news_id])) {
319  return $this->comments[$user_id][$news_id];
320  }
321  return [];
322  }
323 }
getComments(int $news_id, int $user_id)
Get comments for a news and user.
$c
Definition: cli.php:38
static getLogger(string $a_component_id)
Get component logger.
Manage data for ilMembershipCronNotifications cron job.
getLikes(int $news_id, int $user_id)
Get likes for a news and user.
getObjectsForRefId(int $a_ref_id)
Get subtree object IDs for ref id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static queryNewsByIds(array $a_news_ids)
Query news data by news ids.
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
checkMissingNews(int $user_id, int $ref_id, int $news_id)
Get missing news*.
A news item can be created by different sources.
array $user_news_aggr
news array (may include aggregated news which contains news as subitems)
static getActiveUsersforAllObjects()
Get active notifications for all objects.
loadMissingNews()
Load missing news (news for new likes and/or comments)
$i
Definition: metadata.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...