ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjForumNotificationDataProvider.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
11 protected $ref_id = 0;
13 protected $obj_id = 0;
15 protected $post_user_name = null;
17 protected $update_user_name = null;
19 public $pos_author_id = 0;
21 protected $forum_id = 0;
23 protected $closest_container = null;
25 protected $forum_title = '';
27 protected $thread_title = '';
29 protected $attachments = [];
31 public $objPost;
32 private $db;
33 private $access;
34 private $user;
36 private $tree;
37
41 protected $is_anonymized = false;
42
45
52 {
53 global $DIC;
54 $this->db = $DIC->database();
55 $this->access = $DIC->access();
56 $this->user = $DIC->user();
57 $this->tree = $DIC->repositoryTree();
58
59 $this->notificationCache = $notificationCache;
60
61 $this->objPost = $objPost;
62 $this->ref_id = $ref_id;
63 $this->obj_id = ilObject::_lookupObjId($ref_id);
64 $this->read();
65 }
66
70 public function getRefId()
71 {
72 return $this->ref_id;
73 }
74
78 public function getObjId()
79 {
80 return $this->obj_id;
81 }
82
86 public function getThreadId()
87 {
88 return $this->objPost->getThreadId();
89 }
90
94 public function getPostId()
95 {
96 return $this->objPost->getId();
97 }
98
102 public function getForumId()
103 {
104 return $this->forum_id;
105 }
106
107 public function closestContainer() : ?ilObject
108 {
110 }
111
112 public function providesClosestContainer() : bool
113 {
114 return $this->closest_container !== null;
115 }
116
120 public function getForumTitle()
121 {
122 return $this->forum_title;
123 }
124
128 public function getThreadTitle()
129 {
130 return $this->thread_title;
131 }
132
136 public function getPostTitle()
137 {
138 return $this->objPost->getSubject();
139 }
140
144 public function getPostMessage()
145 {
146 return $this->objPost->getMessage();
147 }
148
152 public function getPosDisplayUserId()
153 {
154 return $this->objPost->getDisplayUserId();
155 }
156
160 public function getPostDate()
161 {
162 return $this->objPost->getCreateDate();
163 }
164
168 public function getPostUpdate()
169 {
170 return $this->objPost->getChangeDate();
171 }
175 public function getPostCensored()
176 {
177 return $this->objPost->isCensored();
178 }
179
183 public function getPostCensoredDate()
184 {
185 return $this->objPost->getCensoredDate();
186 }
187
188 public function getCensorshipComment()
189 {
190 return $this->objPost->getCensorshipComment();
191 }
192
196 public function getAttachments()
197 {
198 return $this->attachments;
199 }
200
204 public function getPosUserAlias()
205 {
206 return $this->objPost->getUserAlias();
207 }
208
212 public function isAnonymized()
213 {
215 }
219 public function getImportName()
220 {
221 return $this->objPost->getImportName();
222 }
223
227 public function getPostUpdateUserId()
228 {
229 return $this->objPost->getUpdateUserId();
230 }
231
235 public function getPostUserName(\ilLanguage $user_lang)
236 {
237 if ($this->post_user_name === null) {
238 $authorinfo = new ilForumAuthorInformation(
239 $this->getPosAuthorId(),
240 $this->getPosDisplayUserId(),
241 $this->getPosUserAlias(),
242 $this->getImportName(),
243 array(),
244 $user_lang
245 );
246 $this->post_user_name = $this->getPublicUserInformation($authorinfo);
247 }
248
249 return (string) $this->post_user_name;
250 }
251
255 public function getPostUpdateUserName(\ilLanguage $user_lang)
256 {
257 if ($this->update_user_name === null) {
258 $authorinfo = new ilForumAuthorInformation(
259 $this->getPosAuthorId(),
260 $this->getPostUpdateUserId(),
261 $this->getPosUserAlias(),
262 $this->getImportName(),
263 array(),
264 $user_lang
265 );
266 $this->update_user_name = $this->getPublicUserInformation($authorinfo);
267 }
268
269 // Possible Fix for #25432
270 if ($this->objPost->getUserAlias() && $this->objPost->getDisplayUserId() == 0
271 && $this->objPost->getPosAuthorId() == $this->objPost->getUpdateUserId()) {
272 return (string) $this->objPost->getUserAlias();
273 }
274
275 return (string) $this->update_user_name;
276 }
277
283 {
284 if ($authorinfo->hasSuffix()) {
285 $public_name = $authorinfo->getAuthorName();
286 } else {
287 $public_name = $authorinfo->getAuthorShortName();
288
289 if ($authorinfo->getAuthorName() && !$this->isAnonymized()) {
290 $public_name = $authorinfo->getAuthorName();
291 }
292 }
293
294 return $public_name;
295 }
296
300 protected function read()
301 {
302 $this->readForumData();
303 $this->readThreadTitle();
304 $this->readAttachments();
305 }
306
310 private function readThreadTitle()
311 {
312 $cacheKey = $this->notificationCache->createKeyByValues(array(
313 'thread_title',
314 $this->getObjId()
315 ));
316
317 if (false === $this->notificationCache->exists($cacheKey)) {
318 $result = $this->db->queryf(
319 '
320 SELECT thr_subject FROM frm_threads
321 WHERE thr_pk = %s',
322 array('integer'),
323 array($this->objPost->getThreadId())
324 );
325
326 $row = $this->db->fetchAssoc($result);
327 $this->notificationCache->store($cacheKey, $row);
328 }
329
330 $row = $this->notificationCache->fetch($cacheKey);
331 $this->thread_title = $row['thr_subject'];
332 }
333
337 private function readForumData()
338 {
339 $cacheKey = $this->notificationCache->createKeyByValues(array(
340 'forum_data',
341 $this->getObjId()
342 ));
343
344 if (false === $this->notificationCache->exists($cacheKey)) {
345 $result = $this->db->queryf(
346 '
347 SELECT top_pk, top_name, frm_settings.anonymized FROM frm_data
348 INNER JOIN frm_settings ON top_frm_fk = frm_settings.obj_id
349 WHERE top_frm_fk = %s',
350 array('integer'),
351 array($this->getObjId()
352 )
353 );
354
355 $row = $this->db->fetchAssoc($result);
356
358 if ($container instanceof ilObjCourse || $container instanceof ilObjGroup) {
359 $row['closest_container'] = $container;
360 }
361
362 $this->notificationCache->store($cacheKey, $row);
363 }
364
365 $row = $row ?? $this->notificationCache->fetch($cacheKey);
366 $this->forum_id = $row['top_pk'];
367 $this->forum_title = $row['top_name'];
368 $this->closest_container = $row['closest_container'] ?? null;
369
370
371 $this->is_anonymized = (bool) $row['anonymized'];
372 }
373
374 public function determineClosestContainer(int $frm_ref_id) : ?ilObject
375 {
376 $cacheKey = $this->notificationCache->createKeyByValues([
377 'forum_container',
378 $frm_ref_id
379 ]);
380
381 if (false === $this->notificationCache->exists($cacheKey)) {
382 $ref_id = $this->tree->checkForParentType($frm_ref_id, 'crs');
383 if (!($ref_id > 0)) {
384 $ref_id = $this->tree->checkForParentType($frm_ref_id, 'grp');
385 }
386
387 if ($ref_id > 0) {
389 $this->notificationCache->store($cacheKey, $container);
390 return $container;
391 }
392 }
393
394 return null;
395 }
396
400 private function readAttachments()
401 {
403 $fileDataForum = new ilFileDataForum($this->getObjId(), $this->objPost->getId());
404 $filesOfPost = $fileDataForum->getFilesOfPost();
405
406 $fileDataMail = new ilFileDataMail(ANONYMOUS_USER_ID);
407
408 foreach ($filesOfPost as $attachment) {
409 $this->attachments[$attachment['path']] = $attachment['name'];
410 $fileDataMail->copyAttachmentFile($attachment['path'], $attachment['name']);
411 }
412 }
413 }
414
419 {
420 $cacheKey = $this->notificationCache->createKeyByValues(array(
421 'forum',
422 $this->getForumId(),
423 $this->user->getId()
424 ));
425
426 if (false === $this->notificationCache->exists($cacheKey)) {
427 $res = $this->db->queryf(
428 '
429 SELECT frm_notification.user_id FROM frm_notification, frm_data
430 WHERE frm_data.top_pk = %s
431 AND frm_notification.frm_id = frm_data.top_frm_fk
432 AND frm_notification.user_id != %s
433 GROUP BY frm_notification.user_id',
434 array('integer', 'integer'),
435 array($this->getForumId(), $this->user->getId())
436 );
437
438 $rcps = $this->createRecipientArray($res);
439 $this->notificationCache->store($cacheKey, $rcps);
440 }
441
442 $rcps = $this->notificationCache->fetch($cacheKey);
443
444 return array_unique($rcps);
445 }
446
451 {
452 if (!$this->getThreadId()) {
453 return [];
454 }
455
456 $cacheKey = $this->notificationCache->createKeyByValues(array(
457 'thread',
458 $this->getThreadId(),
459 $this->user->getId()
460 ));
461
462 if (false === $this->notificationCache->exists($cacheKey)) {
463 $res = $this->db->queryF(
464 '
465 SELECT frm_notification.user_id
466 FROM frm_notification
467 INNER JOIN frm_threads ON frm_threads.thr_pk = frm_notification.thread_id
468 WHERE frm_notification.thread_id = %s
469 AND frm_notification.user_id != %s',
470 array('integer', 'integer'),
471 array($this->getThreadId(), $this->user->getId())
472 );
473
474 $usrIds = $this->createRecipientArray($res);
475 $this->notificationCache->store($cacheKey, $usrIds);
476 }
477
478 $usrIds = $this->notificationCache->fetch($cacheKey);
479
480 return $usrIds;
481 }
482
487 {
488 $cacheKey = $this->notificationCache->createKeyByValues(array(
489 'post_answered',
490 $this->objPost->getParentId()
491 ));
492
493 if (false === $this->notificationCache->exists($cacheKey)) {
494 $parent_objPost = new ilForumPost($this->objPost->getParentId());
495
496 $this->notificationCache->store($cacheKey, $parent_objPost);
497 }
498
499 $parent_objPost = $this->notificationCache->fetch($cacheKey);
500 $rcps = array();
501 $rcps[] = $parent_objPost->getPosAuthorId();
502
503 return $rcps;
504 }
505
510 {
511 $cacheKey = $this->notificationCache->createKeyByValues(array(
512 'post_activation',
513 $this->getRefId()
514 ));
515
516 if (false === $this->notificationCache->exists($cacheKey)) {
517 // get moderators to notify about needed activation
518 $rcps = ilForum::_getModerators($this->getRefId());
519 $this->notificationCache->store($cacheKey, $rcps);
520 }
521
522 $rcps = $this->notificationCache->fetch($cacheKey);
523
524 return (array) $rcps;
525 }
526
531 {
532 $this->pos_author_id = $pos_author_id;
533 }
534
538 public function getPosAuthorId()
539 {
541 }
542
547 private function getRefIdsByObjId(int $objId) : array
548 {
549 $cacheKey = $this->notificationCache->createKeyByValues([
550 'refs_by_obj_id',
551 $objId
552 ]);
553
554 if (!$this->notificationCache->exists($cacheKey)) {
555 $this->notificationCache->store($cacheKey, (array) ilObject::_getAllReferences($objId));
556 }
557
558 return $this->notificationCache->fetch($cacheKey);
559 }
560
566 private function createRecipientArray(\ilPDOStatement $statement) : array
567 {
568 $refIds = $this->getRefIdsByObjId((int) $this->getObjId());
569
570 $usrIds = [];
571 while ($row = $this->db->fetchAssoc($statement)) {
572 foreach ($refIds as $refId) {
573 if ($this->access->checkAccessOfUser($row['user_id'], 'read', '', $refId)) {
574 $usrIds[] = (int) $row['user_id'];
575 }
576 }
577 }
578
579 return $usrIds;
580 }
581
585 public function getDeletedBy()
586 {
587 global $DIC;
588
589 if ($this->objPost->getUserAlias() && $this->objPost->getDisplayUserId() == 0
590 && $this->objPost->getPosAuthorId() == $DIC->user()->getId()) {
591 return (string) $this->objPost->getUserAlias();
592 } else {
593 return $DIC->user()->getLogin();
594 }
595 }
596}
$result
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
This class handles all operations on files for the forum object.
Class ilFileDataMail.
getAuthorName($without_short_name=false)
Class ilForumNotificationCache.
static _getModerators($a_ref_id)
get all users assigned to local role il_frm_moderator_<frm_ref_id> (static)
language handling
Class ilObjCourse.
getPublicUserInformation(ilForumAuthorInformation $authorinfo)
__construct(ilForumPost $objPost, $ref_id, \ilForumNotificationCache $notificationCache)
Class ilObjGroup.
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
Class ilObject Basic functions for all objects.
static _lookupObjId($a_id)
static _getAllReferences($a_id)
get all reference ids of object
Class ilPDOStatement is a Wrapper Class for PDOStatement.
const ANONYMOUS_USER_ID
Definition: constants.php:25
global $DIC
Definition: goto.php:24
Interface ilForumNotificationMailData.
foreach($_POST as $key=> $value) $res
$container
Definition: wac.php:13
$objId
Definition: xapitoken.php:39
$refId
Definition: xapitoken.php:40