ILIAS  Release_4_4_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  }
113  }
114  }
115  }
116 
117  $lng = $old_lng;
118  }
119 
120  // save last run
121  $setting->set(get_class($this), date("Y-m-d"));
122 
123  if($counter)
124  {
125  $status = ilCronJobResult::STATUS_OK;
126  }
127  }
128 
129  $result = new ilCronJobResult();
130  $result->setStatus($status);
131 
132  if($status_details)
133  {
134  $result->setMessage($status_details);
135  }
136 
137  return $result;
138  }
139 
147  protected function sendMail($a_user_id, $a_ref_id, array $news)
148  {
149  global $lng, $ilUser;
150 
151  $obj_id = ilObject::_lookupObjId($a_ref_id);
152  $obj_type = ilObject::_lookupType($obj_id);
153 
154  include_once "./Services/Notification/classes/class.ilSystemNotification.php";
155  $ntf = new ilSystemNotification();
156  $ntf->setLangModules(array("crs", "news"));
157  $ntf->setRefId($a_ref_id);
158  $ntf->setGotoLangId('url');
159  $ntf->setSubjectLangId('crs_subject_course_group_notification');
160 
161  // user specific language
162  $lng = $ntf->getUserLanguage($a_user_id);
163 
164  $obj_title = $lng->txt($obj_type)." \"".ilObject::_lookupTitle($obj_id)."\"";
165  $ntf->setIntroductionDirect(sprintf($lng->txt("crs_intro_course_group_notification_for"), $obj_title));
166 
167  $subject = sprintf($lng->txt("crs_subject_course_group_notification"), $obj_title);
168 
169  // news summary
170  $counter = 1;
171  $txt = "";
172  foreach($news as $item)
173  {
174  $title = ilNewsItem::determineNewsTitle($item["context_obj_type"],
175  $item["title"], $item["content_is_lang_var"], $item["agg_ref_id"],
176  $item["aggregation"]);
177  $content = ilNewsItem::determineNewsContent($item["context_obj_type"],
178  $item["content"], $item["content_text_is_lang_var"]);
179 
180  $obj_id = ilObject::_lookupObjId($item["ref_id"]);
181  $obj_title = ilObject::_lookupTitle($obj_id);
182 
183  // path
184  include_once './Services/Locator/classes/class.ilLocatorGUI.php';
185  $cont_loc = new ilLocatorGUI();
186  $cont_loc->addContextItems($item["ref_id"], true);
187  $cont_loc->setTextOnly(true);
188 
189  // #9954/#10044
190  // see ilInitialisation::requireCommonIncludes()
191  @include_once "HTML/Template/ITX.php"; // new implementation
192  if (class_exists("HTML_Template_ITX"))
193  {
194  include_once "./Services/UICore/classes/class.ilTemplateHTMLITX.php";
195  }
196  else
197  {
198  include_once "HTML/ITX.php"; // old implementation
199  include_once "./Services/UICore/classes/class.ilTemplateITX.php";
200  }
201  require_once "./Services/UICore/classes/class.ilTemplate.php";
202  $loc = "[".$cont_loc->getHTML()."]";
203 
204  if($counter > 1)
205  {
206  $txt .= $ntf->getBlockBorder();
207  }
208  $txt .= '#'.$counter." - ".$loc." ".$obj_title."\n\n";
209  $txt .= $title;
210  if($content)
211  {
212  $txt .= "\n".$content;
213  }
214  $txt .= "\n\n";
215 
216  ++$counter;
217  }
218  $ntf->addAdditionalInfo("news", $txt, true);
219 
220  // #10044
221  $mail = new ilMail($ilUser->getId());
222  $mail->enableSOAP(false); // #10410
223  $mail->sendMail(ilObjUser::_lookupLogin($a_user_id),
224  null,
225  null,
226  $subject,
227  $ntf->composeAndGetMessage($a_user_id, null, "read", true),
228  null,
229  array("system"));
230  }
231 
232  public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
233  {
234  global $lng;
235 
236  switch($a_form_id)
237  {
240  $a_fields["enable_course_group_notifications"] = $a_is_active ?
241  $lng->txt("enabled") :
242  $lng->txt("disabled");
243  break;
244  }
245  }
246 
247  public function activationWasToggled($a_currently_active)
248  {
249  global $ilSetting;
250 
251  // propagate cron-job setting to object setting
252  $ilSetting->set("crsgrp_ntf", (bool)$a_currently_active);
253  }
254 }
255 
256 ?>