ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMembershipCronNotifications.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once "Services/Cron/classes/class.ilCronJob.php";
6 
13 {
14  public function getId()
15  {
16  return "mem_notification";
17  }
18 
19  public function getTitle()
20  {
21  global $lng;
22 
23  return $lng->txt("enable_course_group_notifications");
24  }
25 
26  public function getDescription()
27  {
28  global $lng;
29 
30  return $lng->txt("enable_course_group_notifications_desc");
31  }
32 
33  public function getDefaultScheduleType()
34  {
36  }
37 
38  public function getDefaultScheduleValue()
39  {
40  return;
41  }
42 
43  public function hasAutoActivation()
44  {
45  return false;
46  }
47 
48  public function hasFlexibleSchedule()
49  {
50  return false;
51  }
52 
53  public function run()
54  {
55  global $lng, $ilDB;
56 
58  $status_details = null;
59 
60  $setting = new ilSetting("cron");
61  $last_run = $setting->get(get_class($this));
62 
63  // #10284 - we already did send today, do nothing
64  if($last_run == date("Y-m-d"))
65  {
66  // #14005
67  $status_details = "Did already run today.";
68  }
69  else
70  {
71  // gather objects and participants with notification setting
72  $objects = array();
73  $set = $ilDB->query("SELECT usr_id,keyword FROM usr_pref".
74  " WHERE ".$ilDB->like("keyword", "text", "grpcrs_ntf_%").
75  " AND value = ".$ilDB->quote("1", "text"));
76  while($row = $ilDB->fetchAssoc($set))
77  {
78  $ref_id = substr($row["keyword"], 11);
79  $type = ilObject::_lookupType($ref_id, true);
80  if($type)
81  {
82  $objects[$type][$ref_id][] = $row["usr_id"];
83  }
84  }
85 
86  $counter = 0;
87  if(sizeof($objects))
88  {
89  $old_lng = $lng;
90 
91  include_once "Services/News/classes/class.ilNewsItem.php";
92  foreach($objects as $type => $ref_ids)
93  {
94  // type is not needed for now
95  foreach($ref_ids as $ref_id => $user_ids)
96  {
97  // gather news per object
98  $news_item = new ilNewsItem();
99  if($news_item->checkNewsExistsForGroupCourse($ref_id))
100  {
101  foreach($user_ids as $user_id)
102  {
103  // gather news for user
104  $user_news = $news_item->getNewsForRefId($ref_id,
105  false, false, 1, false, false, false, false,
106  $user_id);
107  if($user_news)
108  {
109  $this->sendMail($user_id, $ref_id, $user_news);
110  $counter++;
111 
112  // #17928
113  ilCronManager::ping($this->getId());
114  }
115  }
116  }
117  }
118  }
119 
120  $lng = $old_lng;
121  }
122 
123  // save last run
124  $setting->set(get_class($this), date("Y-m-d"));
125 
126  if($counter)
127  {
128  $status = ilCronJobResult::STATUS_OK;
129  }
130  }
131 
132  $result = new ilCronJobResult();
133  $result->setStatus($status);
134 
135  if($status_details)
136  {
137  $result->setMessage($status_details);
138  }
139 
140  return $result;
141  }
142 
150  protected function sendMail($a_user_id, $a_ref_id, array $news)
151  {
152  global $lng, $ilUser;
153 
154  $obj_id = ilObject::_lookupObjId($a_ref_id);
155  $obj_type = ilObject::_lookupType($obj_id);
156 
157  include_once "./Services/Notification/classes/class.ilSystemNotification.php";
158  $ntf = new ilSystemNotification();
159  $ntf->setLangModules(array("crs", "news"));
160  $ntf->setRefId($a_ref_id);
161  $ntf->setGotoLangId('url');
162  $ntf->setSubjectLangId('crs_subject_course_group_notification');
163 
164  // user specific language
165  $lng = $ntf->getUserLanguage($a_user_id);
166 
167  $obj_title = $lng->txt($obj_type)." \"".ilObject::_lookupTitle($obj_id)."\"";
168  $ntf->setIntroductionDirect(sprintf($lng->txt("crs_intro_course_group_notification_for"), $obj_title));
169 
170  $subject = sprintf($lng->txt("crs_subject_course_group_notification"), $obj_title);
171 
172  // news summary
173  $counter = 1;
174  $txt = "";
175  foreach($news as $item)
176  {
177  $title = ilNewsItem::determineNewsTitle($item["context_obj_type"],
178  $item["title"], $item["content_is_lang_var"], $item["agg_ref_id"],
179  $item["aggregation"]);
180  $content = ilNewsItem::determineNewsContent($item["context_obj_type"],
181  $item["content"], $item["content_text_is_lang_var"]);
182 
183  $obj_id = ilObject::_lookupObjId($item["ref_id"]);
184  $obj_title = ilObject::_lookupTitle($obj_id);
185 
186  // path
187  include_once './Services/Locator/classes/class.ilLocatorGUI.php';
188  $cont_loc = new ilLocatorGUI();
189  $cont_loc->addContextItems($item["ref_id"], true);
190  $cont_loc->setTextOnly(true);
191 
192  // #9954/#10044
193  // see ilInitialisation::requireCommonIncludes()
194  @include_once "HTML/Template/ITX.php"; // new implementation
195  if (class_exists("HTML_Template_ITX"))
196  {
197  include_once "./Services/UICore/classes/class.ilTemplateHTMLITX.php";
198  }
199  else
200  {
201  include_once "HTML/ITX.php"; // old implementation
202  include_once "./Services/UICore/classes/class.ilTemplateITX.php";
203  }
204  require_once "./Services/UICore/classes/class.ilTemplate.php";
205  $loc = "[".$cont_loc->getHTML()."]";
206 
207  if($counter > 1)
208  {
209  $txt .= $ntf->getBlockBorder();
210  }
211  $txt .= '#'.$counter." - ".$loc." ".$obj_title."\n\n";
212  $txt .= $title;
213  if($content)
214  {
215  $txt .= "\n".$content;
216  }
217  $txt .= "\n\n";
218 
219  ++$counter;
220  }
221  $ntf->addAdditionalInfo("news", $txt, true);
222 
223  // #10044
224  $mail = new ilMail($ilUser->getId());
225  $mail->enableSOAP(false); // #10410
226  $mail->sendMail(ilObjUser::_lookupLogin($a_user_id),
227  null,
228  null,
229  $subject,
230  $ntf->composeAndGetMessage($a_user_id, null, "read", true),
231  null,
232  array("system"));
233  }
234 
235  public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
236  {
237  global $lng;
238 
239  switch($a_form_id)
240  {
243  $a_fields["enable_course_group_notifications"] = $a_is_active ?
244  $lng->txt("enabled") :
245  $lng->txt("disabled");
246  break;
247  }
248  }
249 
250  public function activationWasToggled($a_currently_active)
251  {
252  global $ilSetting;
253 
254  // propagate cron-job setting to object setting
255  $ilSetting->set("crsgrp_ntf", (bool)$a_currently_active);
256  }
257 }
258 
259 ?>