ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $forum_title = '';
25  protected $thread_title = '';
27  protected $attachments = [];
29  public $objPost;
30  private $db;
31  private $access;
32  private $user;
33 
37  protected $is_anonymized = false;
38 
41 
48  {
49  global $DIC;
50  $this->db = $DIC->database();
51  $this->access = $DIC->access();
52  $this->user = $DIC->user();
53 
54  $this->notificationCache = $notificationCache;
55 
56  $this->objPost = $objPost;
57  $this->ref_id = $ref_id;
58  $this->obj_id = ilObject::_lookupObjId($ref_id);
59  $this->read();
60  }
61 
65  public function getRefId()
66  {
67  return $this->ref_id;
68  }
69 
73  public function getObjId()
74  {
75  return $this->obj_id;
76  }
77 
81  public function getThreadId()
82  {
83  return $this->objPost->getThreadId();
84  }
85 
89  public function getPostId()
90  {
91  return $this->objPost->getId();
92  }
93 
97  public function getForumId()
98  {
99  return $this->forum_id;
100  }
101 
105  public function getForumTitle()
106  {
107  return $this->forum_title;
108  }
109 
113  public function getThreadTitle()
114  {
115  return $this->thread_title;
116  }
117 
121  public function getPostTitle()
122  {
123  return $this->objPost->getSubject();
124  }
125 
129  public function getPostMessage()
130  {
131  return $this->objPost->getMessage();
132  }
133 
137  public function getPosDisplayUserId()
138  {
139  return $this->objPost->getDisplayUserId();
140  }
141 
145  public function getPostDate()
146  {
147  return $this->objPost->getCreateDate();
148  }
149 
153  public function getPostUpdate()
154  {
155  return $this->objPost->getChangeDate();
156  }
160  public function getPostCensored()
161  {
162  return $this->objPost->isCensored();
163  }
164 
168  public function getPostCensoredDate()
169  {
170  return $this->objPost->getCensoredDate();
171  }
172 
173  public function getCensorshipComment()
174  {
175  return $this->objPost->getCensorshipComment();
176  }
177 
181  public function getAttachments()
182  {
183  return $this->attachments;
184  }
185 
189  public function getPosUserAlias()
190  {
191  return $this->objPost->getUserAlias();
192  }
193 
197  public function isAnonymized()
198  {
199  return $this->is_anonymized;
200  }
204  public function getImportName()
205  {
206  return $this->objPost->getImportName();
207  }
208 
212  public function getPostUpdateUserId()
213  {
214  return $this->objPost->getUpdateUserId();
215  }
216 
220  public function getPostUserName(\ilLanguage $user_lang)
221  {
222  if ($this->post_user_name === null) {
223  $authorinfo = new ilForumAuthorInformation(
224  $this->getPosAuthorId(),
225  $this->getPosDisplayUserId(),
226  $this->getPosUserAlias(),
227  $this->getImportName(),
228  array(),
229  $user_lang
230  );
231  $this->post_user_name = $this->getPublicUserInformation($authorinfo);
232  }
233 
234  return (string) $this->post_user_name;
235  }
236 
240  public function getPostUpdateUserName(\ilLanguage $user_lang)
241  {
242  if ($this->update_user_name === null) {
243  $authorinfo = new ilForumAuthorInformation(
244  $this->getPosAuthorId(),
245  $this->getPostUpdateUserId(),
246  $this->getPosUserAlias(),
247  $this->getImportName(),
248  array(),
249  $user_lang
250  );
251  $this->update_user_name = $this->getPublicUserInformation($authorinfo);
252  }
253 
254  // Possible Fix for #25432
255  if ($this->objPost->getUserAlias() && $this->objPost->getDisplayUserId() == 0
256  && $this->objPost->getPosAuthorId() == $this->objPost->getUpdateUserId()) {
257  return (string) $this->objPost->getUserAlias();
258  }
259 
260  return (string) $this->update_user_name;
261  }
262 
268  {
269  if ($authorinfo->hasSuffix()) {
270  $public_name = $authorinfo->getAuthorName();
271  } else {
272  $public_name = $authorinfo->getAuthorShortName();
273 
274  if ($authorinfo->getAuthorName() && !$this->isAnonymized()) {
275  $public_name = $authorinfo->getAuthorName();
276  }
277  }
278 
279  return $public_name;
280  }
281 
285  protected function read()
286  {
287  $this->readForumData();
288  $this->readThreadTitle();
289  $this->readAttachments();
290  }
291 
295  private function readThreadTitle()
296  {
297  $cacheKey = $this->notificationCache->createKeyByValues(array(
298  'thread_title',
299  $this->getObjId()
300  ));
301 
302  if (false === $this->notificationCache->exists($cacheKey)) {
303  $result = $this->db->queryf(
304  '
305  SELECT thr_subject FROM frm_threads
306  WHERE thr_pk = %s',
307  array('integer'),
308  array($this->objPost->getThreadId())
309  );
310 
311  $row = $this->db->fetchAssoc($result);
312  $this->notificationCache->store($cacheKey, $row);
313  }
314 
315  $row = $this->notificationCache->fetch($cacheKey);
316  $this->thread_title = $row['thr_subject'];
317  }
318 
322  private function readForumData()
323  {
324  $cacheKey = $this->notificationCache->createKeyByValues(array(
325  'forum_data',
326  $this->getObjId()
327  ));
328 
329  if (false === $this->notificationCache->exists($cacheKey)) {
330  $result = $this->db->queryf(
331  '
332  SELECT top_pk, top_name, frm_settings.anonymized FROM frm_data
333  INNER JOIN frm_settings ON top_frm_fk = frm_settings.obj_id
334  WHERE top_frm_fk = %s',
335  array('integer'),
336  array($this->getObjId()
337  )
338  );
339 
340  $row = $this->db->fetchAssoc($result);
341 
342  $this->notificationCache->store($cacheKey, $row);
343  }
344 
345  $row = $this->notificationCache->fetch($cacheKey);
346  $this->forum_id = $row['top_pk'];
347  $this->forum_title = $row['top_name'];
348  $this->is_anonymized = (bool) $row['anonymized'];
349  }
350 
354  private function readAttachments()
355  {
357  $fileDataForum = new ilFileDataForum($this->getObjId(), $this->objPost->getId());
358  $filesOfPost = $fileDataForum->getFilesOfPost();
359 
360  $fileDataMail = new ilFileDataMail(ANONYMOUS_USER_ID);
361 
362  foreach ($filesOfPost as $attachment) {
363  $this->attachments[$attachment['path']] = $attachment['name'];
364  $fileDataMail->copyAttachmentFile($attachment['path'], $attachment['name']);
365  }
366  }
367  }
368 
373  {
374  $cacheKey = $this->notificationCache->createKeyByValues(array(
375  'forum',
376  $this->getForumId(),
377  $this->user->getId()
378  ));
379 
380  if (false === $this->notificationCache->exists($cacheKey)) {
381  $res = $this->db->queryf(
382  '
383  SELECT frm_notification.user_id FROM frm_notification, frm_data
384  WHERE frm_data.top_pk = %s
385  AND frm_notification.frm_id = frm_data.top_frm_fk
386  AND frm_notification.user_id != %s
387  GROUP BY frm_notification.user_id',
388  array('integer', 'integer'),
389  array($this->getForumId(), $this->user->getId())
390  );
391 
392  $rcps = $this->createRecipientArray($res);
393  $this->notificationCache->store($cacheKey, $rcps);
394  }
395 
396  $rcps = $this->notificationCache->fetch($cacheKey);
397 
398  return array_unique($rcps);
399  }
400 
405  {
406  if (!$this->getThreadId()) {
407  return [];
408  }
409 
410  $cacheKey = $this->notificationCache->createKeyByValues(array(
411  'thread',
412  $this->getThreadId(),
413  $this->user->getId()
414  ));
415 
416  if (false === $this->notificationCache->exists($cacheKey)) {
417  $res = $this->db->queryF(
418  '
419  SELECT frm_notification.user_id
420  FROM frm_notification
421  INNER JOIN frm_threads ON frm_threads.thr_pk = frm_notification.thread_id
422  WHERE frm_notification.thread_id = %s
423  AND frm_notification.user_id != %s',
424  array('integer', 'integer'),
425  array($this->getThreadId(), $this->user->getId())
426  );
427 
428  $usrIds = $this->createRecipientArray($res);
429  $this->notificationCache->store($cacheKey, $usrIds);
430  }
431 
432  $usrIds = $this->notificationCache->fetch($cacheKey);
433 
434  return $usrIds;
435  }
436 
440  public function getPostAnsweredRecipients()
441  {
442  $cacheKey = $this->notificationCache->createKeyByValues(array(
443  'post_answered',
444  $this->objPost->getParentId()
445  ));
446 
447  if (false === $this->notificationCache->exists($cacheKey)) {
448  $parent_objPost = new ilForumPost($this->objPost->getParentId());
449 
450  $this->notificationCache->store($cacheKey, $parent_objPost);
451  }
452 
453  $parent_objPost = $this->notificationCache->fetch($cacheKey);
454  $rcps = array();
455  $rcps[] = $parent_objPost->getPosAuthorId();
456 
457  return $rcps;
458  }
459 
463  public function getPostActivationRecipients()
464  {
465  $cacheKey = $this->notificationCache->createKeyByValues(array(
466  'post_activation',
467  $this->getRefId()
468  ));
469 
470  if (false === $this->notificationCache->exists($cacheKey)) {
471  // get moderators to notify about needed activation
472  $rcps = ilForum::_getModerators($this->getRefId());
473  $this->notificationCache->store($cacheKey, $rcps);
474  }
475 
476  $rcps = $this->notificationCache->fetch($cacheKey);
477 
478  return (array) $rcps;
479  }
480 
485  {
486  $this->pos_author_id = $pos_author_id;
487  }
488 
492  public function getPosAuthorId()
493  {
494  return $this->pos_author_id;
495  }
496 
501  private function getRefIdsByObjId(int $objId) : array
502  {
503  $cacheKey = $this->notificationCache->createKeyByValues([
504  'refs_by_obj_id',
505  $objId
506  ]);
507 
508  if (!$this->notificationCache->exists($cacheKey)) {
509  $this->notificationCache->store($cacheKey, (array) ilObject::_getAllReferences($objId));
510  }
511 
512  return $this->notificationCache->fetch($cacheKey);
513  }
514 
520  private function createRecipientArray(\ilPDOStatement $statement) : array
521  {
522  $refIds = $this->getRefIdsByObjId((int) $this->getObjId());
523 
524  $usrIds = [];
525  while ($row = $this->db->fetchAssoc($statement)) {
526  foreach ($refIds as $refId) {
527  if ($this->access->checkAccessOfUser($row['user_id'], 'read', '', $refId)) {
528  $usrIds[] = (int) $row['user_id'];
529  }
530  }
531  }
532 
533  return $usrIds;
534  }
535 
539  public function getDeletedBy()
540  {
541  global $DIC;
542 
543  if ($this->shouldUsePseudonym()) {
544  return (string) $this->objPost->getUserAlias();
545  }
546  return $DIC->user()->getLogin();
547  }
548 
552  private function shouldUsePseudonym()
553  {
554  global $DIC;
555 
556  if ($this->objPost->getUserAlias() && $this->objPost->getDisplayUserId() == 0
557  && $this->objPost->getPosAuthorId() == $DIC->user()->getId()) {
558  return true;
559  }
560  return false;
561  }
562 }
__construct(ilForumPost $objPost, $ref_id, \ilForumNotificationCache $notificationCache)
Class ilObjForumNotificationDataProvider.
$result
global $DIC
Definition: saml.php:7
Class ilFileDataMail.
Class ilPDOStatement is a Wrapper Class for PDOStatement.
getAuthorName($without_short_name=false)
user()
Definition: user.php:4
static _getAllReferences($a_id)
get all reference ids of object
foreach($_POST as $key=> $value) $res
static _lookupObjId($a_id)
Class ilForumNotificationCache.
getPublicUserInformation(ilForumAuthorInformation $authorinfo)
Interface ilForumNotificationMailData.
$row
This class handles all operations on files for the forum object.
language handling
static _getModerators($a_ref_id)
get all users assigned to local role il_frm_moderator_<frm_ref_id> (static)