ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCronCourseGroupNotification.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/Mail/classes/class.ilMailNotification.php';
5 include_once 'Services/Mail/classes/class.ilMimeMail.php';
6 
14 {
15  public function sendNotifications()
16  {
17  global $ilDB, $lng;
18 
19  $setting = new ilSetting("cron");
20  $last_run = $setting->get(get_class($this));
21 
22  // #10284 - we already did send today, do nothing
23  if($last_run == date("Y-m-d"))
24  {
25  return;
26  }
27 
28  // gather objects and participants with notification setting
29  $objects = array();
30  $set = $ilDB->query("SELECT usr_id,keyword FROM usr_pref".
31  " WHERE ".$ilDB->like("keyword", "text", "grpcrs_ntf_%").
32  " AND value = ".$ilDB->quote("1", "text"));
33  while($row = $ilDB->fetchAssoc($set))
34  {
35  $ref_id = substr($row["keyword"], 11);
37  if($type)
38  {
39  $objects[$type][$ref_id][] = $row["usr_id"];
40  }
41  }
42 
43  if(sizeof($objects))
44  {
45  $old_lng = $lng;
46 
47  include_once "Services/News/classes/class.ilNewsItem.php";
48  foreach($objects as $type => $ref_ids)
49  {
50  // type is not needed for now
51  foreach($ref_ids as $ref_id => $user_ids)
52  {
53  // gather news per object
54  $news_item = new ilNewsItem();
55  if($news_item->checkNewsExistsForGroupCourse($ref_id))
56  {
57  foreach($user_ids as $user_id)
58  {
59  // gather news for user
60  $user_news = $news_item->getNewsForRefId($ref_id,
61  false, false, 1, false, false, false, false,
62  $user_id);
63  if($user_news)
64  {
65  $this->sendMail($user_id, $ref_id, $user_news);
66  }
67  }
68  }
69  }
70  }
71 
72  $lng = $old_lng;
73  }
74 
75  // save last run
76  $setting->set(get_class($this), date("Y-m-d"));
77 
78  return true;
79  }
80 
88  public function sendMail($a_user_id, $a_ref_id, array $news)
89  {
90  global $lng;
91 
92  $obj_id = ilObject::_lookupObjId($a_ref_id);
94 
95  $this->initLanguage($a_user_id);
96  $this->getLanguage()->loadLanguageModule("crs");
97  $this->getLanguage()->loadLanguageModule("news");
98 
99  // needed for ilNewsItem
100  $lng = $this->getLanguage();
101 
102  $this->initMail();
103 
104  $obj_title = $this->getLanguageText($obj_type)." \"".ilObject::_lookupTitle($obj_id)."\"";
105 
106  $this->setRecipients($a_user_id);
107  $this->setSubject(sprintf($this->getLanguageText("crs_subject_course_group_notification"), $obj_title));
108 
109  $this->setBody(ilMail::getSalutation($a_user_id, $this->getLanguage()));
110  $this->appendBody("\n\n");
111 
112  $this->appendBody(sprintf($this->getLanguageText("crs_intro_course_group_notification_for"), $obj_title));
113  $this->appendBody("\n\n");
114 
115  // ilDatePresentation::setUseRelativeDates(false);
116 
117  // news summary
118  $counter = 1;
119  foreach($news as $item)
120  {
121  $title = ilNewsItem::determineNewsTitle($item["context_obj_type"],
122  $item["title"], $item["content_is_lang_var"], $item["agg_ref_id"],
123  $item["aggregation"]);
124  $content = ilNewsItem::determineNewsContent($item["context_obj_type"],
125  $item["content"], $item["content_text_is_lang_var"]);
126 
127  /* process sub-item info
128  if($item["aggregation"])
129  {
130  $sub = array();
131  foreach($item["aggregation"] as $subitem)
132  {
133  $sub_id = ilObject::_lookupObjId($subitem["ref_id"]);
134  $sub_title = ilObject::_lookupTitle($sub_id);
135 
136  // to include posting title
137  if($subitem["context_obj_type"] == "frm")
138  {
139  $sub_title = ilNewsItem::determineNewsTitle($subitem["context_obj_type"],
140  $subitem["title"], $subitem["content_is_lang_var"]);
141  }
142 
143  $sub[] = $sub_title;
144 
145  $sub_content = ilNewsItem::determineNewsContent($subitem["context_obj_type"],
146  $subitem["content"], $subitem["content_text_is_lang_var"]);
147  if($sub_content)
148  {
149  $sub[] = strip_tags($sub_content);
150  }
151  }
152  $content .= "\n".implode("\n\n", $sub);
153  }
154  */
155 
156  $obj_id = ilObject::_lookupObjId($item["ref_id"]);
157  $obj_title = ilObject::_lookupTitle($obj_id);
158 
159  // path
160  $cont_loc = new ilLocatorGUI();
161  $cont_loc->addContextItems($item["ref_id"], true);
162  $cont_loc->setTextOnly(true);
163  $loc = "[".$cont_loc->getHTML()."]";
164 
165  $this->appendBody("----------------------------------------------------------------------------------------------");
166  $this->appendBody("\n\n");
167  $this->appendBody('#'.$counter." - ".$loc." ".$obj_title."\n\n");
168  $this->appendBody($title);
169  if($content)
170  {
171  $this->appendBody("\n");
172  $this->appendBody($content);
173  }
174  $this->appendBody("\n\n");
175 
176  ++$counter;
177  }
178  $this->appendBody("----------------------------------------------------------------------------------------------");
179  $this->appendBody("\n\n");
180 
181  // link to object
182  $this->appendBody($this->getLanguageText("crs_course_group_notification_link"));
183  $this->appendBody("\n");
184  $object_link = ilUtil::_getHttpPath();
185  $object_link .= "/goto.php?target=".$obj_type."_".$a_ref_id.
186  "&client_id=".CLIENT_ID;
187  $this->appendBody($object_link);
188 
189  $this->appendBody("\n\n");
192 
193  $mmail = new ilMimeMail();
194  $mmail->autoCheck(false);
195  $mmail->From('noreply');
196  $mmail->To(ilObjUser::_lookupEmail($a_user_id));
197 
198  $mmail->Subject($this->getSubject());
199  $mmail->Body($this->getBody());
200  $mmail->Send();
201  }
202 }
203 ?>