ILIAS  release_7 Revision v7.30-3-g800a261c036
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 (
94 isset($objs["obj_id"]) &&
95 is_array($objs["obj_id"]) &&
96 $news_item->checkNewsExistsForObjects($objs["obj_id"], $this->last_run)
97 ) {
98 $this->log->debug("Got news");
99 foreach ($user_ids as $user_id) {
100 // gather news for user
101 $user_news = $news_item->getNewsForRefId(
102 $ref_id,
103 false,
104 false,
105 $this->last_run,
106 false,
107 false,
108 false,
109 false,
110 $user_id
111 );
112 if ($user_news) {
113 $this->user_news_aggr[$user_id][$ref_id] = $user_news;
114
115 // store all single news
116 foreach ($this->user_news_aggr as $agg_news) {
117 if (isset($agg_news["aggregation"]) && is_array($agg_news["aggregation"]) && $agg_news["aggregation"] !== []) {
118 foreach ($agg_news["aggregation"] as $n) {
119 $this->news[$n["id"]] = $n;
120 $this->news_per_user[$user_id][$ref_id][$n["id"]] = $n["id"];
121 }
122 } else {
123 if (is_array($agg_news)) {
124 if (isset($agg_news["id"])) {
125 $this->news[$agg_news["id"]] = $agg_news;
126 $this->news_per_user[$user_id][$ref_id][$agg_news["id"]] = $agg_news["id"];
127 } else {
128 foreach ($agg_news as $agg_news_items) {
129 foreach ($agg_news_items as $agg_news_item) {
130 if (isset($agg_news_item["id"])) {
131 $this->news[$agg_news_item["id"]] = $agg_news_item;
132 $this->news_per_user[$user_id][$ref_id][$agg_news_item["id"]] = $agg_news_item["id"];
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140
141 $this->ping();
142 }
143 }
144 } else {
145 $this->log->debug("Got no news");
146 }
147
148 // gather likes per object and store them "per news item"
149 // currently only news can be liked
150 $ref_for_obj_id = array();
151 foreach ($objs["ref_id"] as $i) {
152 $ref_for_obj_id[$i["obj_id"]][$i["ref_id"]] = $i["ref_id"];
153 }
154 include_once("./Services/Like/classes/class.ilLikeData.php");
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) 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", "", $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) {
182 $obj_id,
184 false,
185 false,
186 $this->last_run
187 );
188 foreach ($coms as $c) {
189 if ($c->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", "", $perm_ref_id)) {
197 $has_perm = true;
198 break;
199 }
200 }
201 if ($has_perm) {
202 $this->comments[$user_id][$c->getNewsId()][] = $c;
203
204 // get news data for news that are not included above
205 $this->checkMissingNews($user_id, $ref_id, $c->getNewsId());
206 $this->ping();
207 }
208 }
209 }
210 }
211 }
212 $this->loadMissingNews();
213 }
214 }
215
223 protected function checkMissingNews($user_id, $ref_id, $news_id)
224 {
225 $this->log->debug("Check missing news: " . $user_id . "-" . $ref_id . "-" . $news_id);
226 if (!is_array($this->news_per_user[$user_id][$ref_id]) ||
227 !in_array($news_id, $this->news_per_user[$user_id][$ref_id])) {
228 $this->log->debug("Add missing news: " . $news_id);
229 $this->missing_news[$news_id] = $news_id;
230 $this->missing_news_per_user[$user_id][$ref_id][$news_id] = $news_id;
231 }
232 }
233
237 protected function loadMissingNews()
238 {
239 include_once("./Services/News/classes/class.ilNewsItem.php");
240 foreach (ilNewsItem::queryNewsByIds($this->missing_news) as $news) {
241 $this->log->debug("Got missing news: " . $news["id"]);
242 $this->news[$news["id"]] = $news;
243 }
244 foreach ($this->missing_news_per_user as $user_id => $r) {
245 foreach ($r as $ref_id => $n) {
246 foreach ($n as $news_id) {
247 $this->log->debug("Load missing news: " . $user_id . "-" . $ref_id . "-" . $news_id);
248 $this->user_news_aggr[$user_id][$ref_id][$news_id] = $this->news[$news_id];
249 $this->news_per_user[$user_id][$ref_id][$news_id] = $news_id;
250 }
251 }
252 }
253 }
254
255
256
263 protected function getObjectsForRefId($a_ref_id)
264 {
265 global $DIC;
266
267 $tree = $DIC->repositoryTree();
268 $nodes = array();
269
270 if (!$tree->isDeleted($a_ref_id)) {
271 // parse repository branch of group
272
273 $node = $tree->getNodeData($a_ref_id);
274 foreach ($tree->getSubTree($node) as $child) {
275 if ($child["type"] != "rolf") {
276 $nodes["obj_id"][$child["obj_id"]] = array(
277 "obj_id" => $child["obj_id"],
278 "type" => $child["type"]);
279 $nodes["ref_id"][$child["child"]] = array(
280 "ref_id" => $child["child"],
281 "obj_id" => $child["obj_id"],
282 "type" => $child["type"]);
283 }
284 }
285 }
286
287 return $nodes;
288 }
289
290
294 protected function ping()
295 {
296 ilCronManager::ping($this->cron_id);
297 }
298
299
304 public function getAggregatedNews()
305 {
307 }
308
316 public function getLikes($news_id, $user_id)
317 {
318 if (is_array($this->likes[$user_id][$news_id])) {
319 return $this->likes[$user_id][$news_id];
320 }
321 return [];
322 }
323
331 public function getComments($news_id, $user_id)
332 {
333 if (is_array($this->comments[$user_id][$news_id])) {
334 return $this->comments[$user_id][$news_id];
335 }
336 return [];
337 }
338}
$n
Definition: RandomTest.php:85
An exception for terminatinating execution or to throw for unit testing.
const IL_NOTE_PUBLIC
Definition: class.ilNote.php:6
static ping($a_job_id)
Keep cron job alive.
Data class for like feature.
static getLogger($a_component_id)
Get component logger.
Manage data for ilMembershipCronNotifications cron job.
loadMissingNews()
Load missing news (news for new likes and/or comments)
getComments($news_id, $user_id)
Get comments for a news and user.
getLikes($news_id, $user_id)
Get likes for a news and user.
getObjectsForRefId($a_ref_id)
Get subtree object IDs for ref id.
checkMissingNews($user_id, $ref_id, $news_id)
Get missing news.
static getActiveUsersforAllObjects()
Get active notifications for all objects.
static queryNewsByIds(array $a_news_ids)
Query news data by news ids.
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
$c
Definition: cli.php:37
global $DIC
Definition: goto.php:24
$i
Definition: metadata.php:24