ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCronForumNotification.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
14  public function sendMails($res)
15  {
16  global $ilAccess, $ilDB, $lng;
17 
18  static $cache = array();
19  static $attachments_cache = array();
20 
21  include_once 'Modules/Forum/classes/class.ilObjForum.php';
22  include_once 'Services/Mail/classes/class.ilMail.php';
23  include_once 'Services/User/classes/class.ilObjUser.php';
24  include_once 'Services/Language/classes/class.ilLanguage.php';
25 
26  $forumObj = new ilObjForum((int)$_GET['ref_id']);
27  $frm = $forumObj->Forum;
28 
29  $numRows = 0;
30  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
31  $mail_obj->enableSOAP(false);
32  while($row = $ilDB->fetchAssoc($res))
33  {
34  // don not send a notification to the post author
35  if($row['pos_usr_id'] != $row['user_id'])
36  {
37  // GET AUTHOR OF NEW POST
38  if($row['pos_usr_id'])
39  {
40  $row['pos_usr_name'] = ilObjUser::_lookupLogin($row['pos_usr_id']);
41  }
42  else if(strlen($row['pos_usr_alias']))
43  {
44  $row['pos_usr_name'] = $row['pos_usr_alias'].' ('.$lng->txt('frm_pseudonym').')';
45  }
46 
47  if($row['pos_usr_name'] == '')
48  {
49  $row['pos_usr_name'] = $lng->txt('forums_anonymous');
50  }
51 
52  // get all references of obj_id
53  if(!isset($cache[$row['obj_id']]))
54  $cache[$row['obj_id']] = ilObject::_getAllReferences($row['obj_id']);
55 
56  // check for attachments
57  $has_attachments = false;
58  if(!isset($attachments_cache[$row['obj_id']][$row['pos_pk']]))
59  {
60  $fileDataForum = new ilFileDataForum($row['obj_id'], $row['pos_pk']);
61  $filesOfPost = $fileDataForum->getFilesOfPost();
62  foreach($filesOfPost as $attachment)
63  {
64  $attachments_cache[$row['obj_id']][$row['pos_pk']][] = $attachment['name'];
65  $has_attachments = true;
66  }
67  }
68  else
69  {
70  $has_attachments = true;
71  }
72 
73  // do rbac check before sending notification
74  $send_mail = false;
75  foreach((array)$cache[$row['obj_id']] as $ref_id)
76  {
77  if($ilAccess->checkAccessOfUser($row['user_id'], 'read', '', $ref_id))
78  {
79  $row['ref_id'] = $ref_id;
80  $send_mail = true;
81  break;
82  }
83  }
84  $attached_files = array();
85  if($has_attachments == true)
86  {
87  $attached_files = $attachments_cache[$row['obj_id']][$row['pos_pk']];
88  }
89 
90  if($send_mail)
91  {
92  $frm->setLanguage(ilForum::_getLanguageInstanceByUsrId($row['user_id']));
93  $mail_obj->sendMail(
94  ilObjUser::_lookupLogin($row['user_id']), '', '',
95  $frm->formatNotificationSubject($row),
96  $frm->formatNotification($row, 1, $attached_files, $row['user_id']),
97  array(), array(
98  'normal'
99  ));
100  $numRows++;
101  }
102  }
103  }
104 
105  return $numRows;
106  }
107 
108  public function sendNotifications()
109  {
110  global $ilDB, $ilLog, $ilSetting, $lng;
111 
112  $lng->loadLanguageModule('forum');
113 
114  if(!($lastDate = $ilSetting->get('cron_forum_notification_last_date')))
115  {
116  $lastDate = null;
117  }
118 
119  $numRows = 0;
120  $datecondition_frm = '';
121  $types = array();
122  $values = array();
123 
124  if($lastDate != null &&
125  checkDate(date('m', strtotime($lastDate)), date('d', strtotime($lastDate)), date('Y', strtotime($lastDate))))
126  {
127  $datecondition_frm = ' frm_posts.pos_date >= %s AND ';
128  $types[] = 'timestamp';
129  $values[] = $lastDate;
130  }
131 
132  /*** FORUMS ***/
133  $res = $ilDB->queryf('
134  SELECT frm_threads.thr_subject thr_subject,
135  frm_data.top_name top_name,
136  frm_data.top_frm_fk obj_id,
137  frm_notification.user_id user_id,
138  frm_posts.*
139  FROM frm_notification, frm_posts, frm_threads, frm_data
140  WHERE '.$datecondition_frm.' frm_posts.pos_thr_fk = frm_threads.thr_pk
141  AND frm_threads.thr_top_fk = frm_data.top_pk
142  AND frm_data.top_frm_fk = frm_notification.frm_id
143  ORDER BY frm_posts.pos_date ASC',
144  $types,
145  $values
146  );
147 
148  $numRows += $this->sendMails($res);
149 
150  /*** THREADS ***/
151  $res = $ilDB->queryf('
152  SELECT frm_threads.thr_subject thr_subject,
153  frm_data.top_name top_name,
154  frm_data.top_frm_fk obj_id,
155  frm_notification.user_id user_id,
156  frm_posts.*
157  FROM frm_notification, frm_posts, frm_threads, frm_data
158  WHERE '.$datecondition_frm.' frm_posts.pos_thr_fk = frm_threads.thr_pk
159  AND frm_threads.thr_pk = frm_notification.thread_id
160  AND frm_data.top_pk = frm_threads.thr_top_fk
161  ORDER BY frm_posts.pos_date ASC',
162  $types,
163  $values
164  );
165 
166  $numRows += $this->sendMails($res);
167 
168  $ilSetting->set('cron_forum_notification_last_date', date('Y-m-d H:i:s'));
169 
170  $ilLog->write(__METHOD__.': Send '.$numRows.' messages.');
171 
172  return true;
173  }
174 }
175 ?>