ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMembershipCronNotificationsData.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
12 {
13  protected $last_run;
14 
15  protected $cron_id;
16 
17  protected $log;
18 
19  protected $objects;
20 
24  protected $news = array();
25 
29  protected $news_per_user;
30 
35  protected $user_news_aggr = array();
36 
40  protected $likes = array();
41 
45  protected $comments = array();
46 
47  protected $missing_news_per_user = array();
48 
49  protected $missing_news = array();
50 
56  public function __construct($last_run, $cron_id)
57  {
58  global $DIC;
59 
60  $this->access = $DIC->access();
61 
62  $this->last_run = $last_run;
63  $this->cron_id = $cron_id;
64  $this->log = ilLoggerFactory::getLogger("mmbr");
65  $this->load();
66  }
67 
71  protected function load()
72  {
73  $ilAccess = $this->access;
74 
75  include_once "Services/Membership/classes/class.ilMembershipNotifications.php";
76 
77  // all group/course notifications: ref id => user ids
79 
80  if (sizeof($this->objects)) {
81  $this->log->debug("nr of objects: " . count($this->objects));
82 
83  // gather news for each user over all objects
84  $this->user_news_aggr = array();
85 
86  include_once "Services/News/classes/class.ilNewsItem.php";
87  foreach ($this->objects as $ref_id => $user_ids) {
88  $this->log->debug("handle ref id " . $ref_id . ", users: " . count($user_ids));
89 
90  // gather news per object
91  $news_item = new ilNewsItem();
92  $objs = $this->getObjectsForRefId($ref_id);
93  if ($news_item->checkNewsExistsForObjects($objs["obj_id"], $this->last_run)) {
94  $this->log->debug("Got news");
95  foreach ($user_ids as $user_id) {
96  // gather news for user
97  $user_news = $news_item->getNewsForRefId(
98  $ref_id,
99  false,
100  false,
101  $this->last_run,
102  false,
103  false,
104  false,
105  false,
106  $user_id
107  );
108  if ($user_news) {
109  $this->user_news_aggr[$user_id][$ref_id] = $user_news;
110 
111  // store all single news
112  foreach ($this->user_news_aggr as $agg_news) {
113  if (isset($agg_news["aggregation"]) && is_array($agg_news["aggregation"]) && $agg_news["aggregation"] !== []) {
114  foreach ($agg_news["aggregation"] as $n) {
115  $this->news[$n["id"]] = $n;
116  $this->news_per_user[$user_id][$ref_id][$n["id"]] = $n["id"];
117  }
118  } else {
119  if (is_array($agg_news)) {
120  if (isset($agg_news["id"])) {
121  $this->news[$agg_news["id"]] = $agg_news;
122  $this->news_per_user[$user_id][$ref_id][$agg_news["id"]] = $agg_news["id"];
123  } else {
124  foreach ($agg_news as $agg_news_items) {
125  foreach ($agg_news_items as $agg_news_item) {
126  if (isset($agg_news_item["id"])) {
127  $this->news[$agg_news_item["id"]] = $agg_news_item;
128  $this->news_per_user[$user_id][$ref_id][$agg_news_item["id"]] = $agg_news_item["id"];
129  }
130  }
131  }
132  }
133  }
134  }
135  }
136 
137  $this->ping();
138  }
139  }
140  } else {
141  $this->log->debug("Got no news");
142  }
143 
144  // gather likes per object and store them "per news item"
145  // currently only news can be liked
146  $ref_for_obj_id = array();
147  foreach ($objs["ref_id"] as $i) {
148  $ref_for_obj_id[$i["obj_id"]][$i["ref_id"]] = $i["ref_id"];
149  }
150  include_once("./Services/Like/classes/class.ilLikeData.php");
151  $like_data = new ilLikeData(array_keys($objs["obj_id"]));
152  foreach (array_keys($objs["obj_id"]) as $obj_id) {
153  $this->log->debug("Get like data for obj_id: " . $obj_id);
154  foreach ($like_data->getExpressionEntriesForObject($obj_id, $this->last_run) as $like) {
155  reset($user_ids);
156  foreach ($user_ids as $user_id) {
157  $has_perm = false;
158  foreach ($ref_for_obj_id[$obj_id] as $perm_ref_id) {
159  if ($ilAccess->checkAccessOfUser($user_id, "read", "", $perm_ref_id)) {
160  $has_perm = true;
161  break;
162  }
163  }
164  if ($has_perm) {
165  $this->likes[$user_id][$like["news_id"]][] = $like;
166 
167  // get news data for news that are not included above
168  $this->checkMissingNews($user_id, $ref_id, $like["news_id"]);
169  $this->ping();
170  }
171  }
172  }
173  }
174 
175  // gather comments
176  foreach (array_keys($objs["obj_id"]) as $obj_id) {
178  $obj_id,
180  false,
181  false,
182  $this->last_run
183  );
184  foreach ($coms as $c) {
185  if ($c->getNewsId() == 0) {
186  continue;
187  }
188  reset($user_ids);
189  foreach ($user_ids as $user_id) {
190  $has_perm = false;
191  foreach ($ref_for_obj_id[$obj_id] as $perm_ref_id) {
192  if ($ilAccess->checkAccessOfUser($user_id, "read", "", $perm_ref_id)) {
193  $has_perm = true;
194  break;
195  }
196  }
197  if ($has_perm) {
198  $this->comments[$user_id][$c->getNewsId()][] = $c;
199 
200  // get news data for news that are not included above
201  $this->checkMissingNews($user_id, $ref_id, $c->getNewsId());
202  $this->ping();
203  }
204  }
205  }
206  }
207  }
208  $this->loadMissingNews();
209  }
210  }
211 
219  protected function checkMissingNews($user_id, $ref_id, $news_id)
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]) ||
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()
234  {
235  include_once("./Services/News/classes/class.ilNewsItem.php");
236  foreach (ilNewsItem::queryNewsByIds($this->missing_news) as $news) {
237  $this->log->debug("Got missing news: " . $news["id"]);
238  $this->news[$news["id"]] = $news;
239  }
240  foreach ($this->missing_news_per_user as $user_id => $r) {
241  foreach ($r as $ref_id => $n) {
242  foreach ($n as $news_id) {
243  $this->log->debug("Load missing news: " . $user_id . "-" . $ref_id . "-" . $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 
252 
259  protected function getObjectsForRefId($a_ref_id)
260  {
261  global $DIC;
262 
263  $tree = $DIC->repositoryTree();
264  $nodes = array();
265 
266  if (!$tree->isDeleted($a_ref_id)) {
267  // parse repository branch of group
268 
269  $node = $tree->getNodeData($a_ref_id);
270  foreach ($tree->getSubTree($node) as $child) {
271  if ($child["type"] != "rolf") {
272  $nodes["obj_id"][$child["obj_id"]] = array(
273  "obj_id" => $child["obj_id"],
274  "type" => $child["type"]);
275  $nodes["ref_id"][$child["child"]] = array(
276  "ref_id" => $child["child"],
277  "obj_id" => $child["obj_id"],
278  "type" => $child["type"]);
279  }
280  }
281  }
282 
283  return $nodes;
284  }
285 
286 
290  protected function ping()
291  {
292  ilCronManager::ping($this->cron_id);
293  }
294 
295 
300  public function getAggregatedNews()
301  {
302  return $this->user_news_aggr;
303  }
304 
312  public function getLikes($news_id, $user_id)
313  {
314  if (is_array($this->likes[$user_id][$news_id])) {
315  return $this->likes[$user_id][$news_id];
316  }
317  return [];
318  }
319 
327  public function getComments($news_id, $user_id)
328  {
329  if (is_array($this->comments[$user_id][$news_id])) {
330  return $this->comments[$user_id][$news_id];
331  }
332  return [];
333  }
334 }
getComments($news_id, $user_id)
Get comments for a news and user.
global $DIC
Definition: saml.php:7
getObjectsForRefId($a_ref_id)
Get subtree object IDs for ref id.
static _getAllNotesOfSingleRepObject( $a_rep_obj_id, $a_type=IL_NOTE_PRIVATE, $a_incl_sub=false, $a_sort_ascending=false, $a_since="")
get all notes related to a single repository object
Manage data for ilMembershipCronNotifications cron job.
Data class for like feature.
static queryNewsByIds(array $a_news_ids)
Query news data by news ids.
checkMissingNews($user_id, $ref_id, $news_id)
Get missing news.
$r
Definition: example_031.php:79
const IL_NOTE_PUBLIC
Definition: class.ilNote.php:6
getLikes($news_id, $user_id)
Get likes for a news and user.
$n
Definition: RandomTest.php:85
static ping($a_job_id)
Keep cron job alive.
$i
Definition: disco.tpl.php:19
static getLogger($a_component_id)
Get component logger.
static getActiveUsersforAllObjects()
Get active notifications for all objects.
loadMissingNews()
Load missing news (news for new likes and/or comments)