ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilNewsItem.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 define("NEWS_NOTICE", 0);
5 define("NEWS_MESSAGE", 1);
6 define("NEWS_WARNING", 2);
7 
8 include_once("./Services/News/classes/class.ilNewsItemGen.php");
9 
23 {
24 
25  private static $privFeedId = false;
26  private $limitation;
27 
33  public function __construct($a_id = 0)
34  {
35  parent::__construct($a_id);
36  $this->limitation = true;
37  }
38 
39 
45  function setLimitation($a_limitation)
46  {
47  $this->limitation = $a_limitation;
48  }
49 
55  function getLimitation()
56  {
57  return $this->limitation;
58  }
59 
63  function create()
64  {
65  global $ilDB;
66 
68 
69  $news_set = new ilSetting("news");
70  $max_items = $news_set->get("max_items");
71  if ($max_items <= 0)
72  {
73  $max_items = 50;
74  }
75 
76  // limit number of news
77  if ($this->getLimitation())
78  {
79  // Determine how many rows should be deleted
80  $query = "SELECT count(*) cnt ".
81  "FROM il_news_item ".
82  "WHERE ".
83  "context_obj_id = ".$ilDB->quote($this->getContextObjId(), "integer").
84  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType(), "text").
85  " AND context_sub_obj_id = ".$ilDB->quote($this->getContextSubObjId(), "integer").
86  " AND ".$ilDB->equals("context_sub_obj_type", $this->getContextSubObjType(), "text", true)." ";
87 
88  $set = $ilDB->query($query);
89  $rec = $ilDB->fetchAssoc($set);
90 
91  // if we have more records than allowed, delete them
92  if (($rec["cnt"] > $max_items) && $this->getContextObjId() > 0)
93  {
94  $query = "SELECT * ".
95  "FROM il_news_item ".
96  "WHERE ".
97  "context_obj_id = ".$ilDB->quote($this->getContextObjId(), "integer").
98  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType(), "text").
99  " AND context_sub_obj_id = ".$ilDB->quote($this->getContextSubObjId(), "integer").
100  " AND ".$ilDB->equals("context_sub_obj_type", $this->getContextSubObjType(), "text", true).
101  " ORDER BY creation_date ASC";
102 
103  $ilDB->setLimit($rec["cnt"] - $max_items);
104  $del_set = $ilDB->query($query);
105  while ($del_item = $ilDB->fetchAssoc($del_set))
106  {
107  $del_news = new ilNewsItem($del_item["id"]);
108  $del_news->delete();
109  }
110  }
111  }
112  }
113 
117  static function _getNewsItemsOfUser($a_user_id, $a_only_public = false,
118  $a_prevent_aggregation = false, $a_per = 0, &$a_cnt = NULL)
119  {
120  global $ilAccess, $ilUser;
121 
122  $news_item = new ilNewsItem();
123  $news_set = new ilSetting("news");
124 
125  $per = $a_per;
126 
127  include_once("./Services/News/classes/class.ilNewsSubscription.php");
128  include_once("./Services/Block/classes/class.ilBlockSetting.php");
129 
130  // this is currently not used
131  $ref_ids = ilNewsSubscription::_getSubscriptionsOfUser($a_user_id);
132 
133  if (ilObjUser::_lookupPref($a_user_id, "pd_items_news") != "n")
134  {
135  // this should be the case for all users
136  $pd_items = ilObjUser::_lookupDesktopItems($a_user_id);
137  foreach($pd_items as $item)
138  {
139  if (!in_array($item["ref_id"], $ref_ids))
140  {
141  $ref_ids[] = $item["ref_id"];
142  }
143  }
144  }
145 
146  $data = array();
147 
148  foreach($ref_ids as $ref_id)
149  {
150  if (!$a_only_public)
151  {
152  // this loop should not cost too much performance
153  $acc = $ilAccess->checkAccess("read", "", $ref_id);
154 
155  if (!$acc)
156  {
157  continue;
158  }
159  }
160  if (ilNewsItem::getPrivateFeedId() != false) {
161  global $rbacsystem;
162  $acc = $rbacsystem->checkAccessOfUser(ilNewsItem::getPrivateFeedId(),"read", $ref_id);
163 
164  if (!$acc)
165  {
166  continue;
167  }
168  }
169 
170  $obj_id = ilObject::_lookupObjId($ref_id);
171  $obj_type = ilObject::_lookupType($obj_id);
172  $news = $news_item->getNewsForRefId($ref_id, $a_only_public, false,
173  $per, $a_prevent_aggregation);
174 
175  // counter
176  if (!is_null($a_cnt))
177  {
178  $a_cnt[$ref_id] = count($news);
179  }
180 
182  }
183 
184  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
185 
186  return $data;
187  }
188 
192  function getNewsForRefId($a_ref_id, $a_only_public = false, $a_stopnesting = false,
193  $a_time_period = 0, $a_prevent_aggregation = true, $a_forum_group_sequences = false,
194  $a_no_auto_generated = false, $a_ignore_date_filter = false)
195  {
196  $obj_id = ilObject::_lookupObjId($a_ref_id);
197  $obj_type = ilObject::_lookupType($obj_id);
198 
199  // get starting date
200  $starting_date = "";
201  if ($obj_type == "grp" || $obj_type == "crs" || $obj_type == "cat")
202  {
203  include_once("./Services/Block/classes/class.ilBlockSetting.php");
204  $hide_news_per_date = ilBlockSetting::_lookup("news", "hide_news_per_date",
205  0, $obj_id);
206  if ($hide_news_per_date && !$a_ignore_date_filter)
207  {
208  $starting_date = ilBlockSetting::_lookup("news", "hide_news_date",
209  0, $obj_id);
210  }
211  }
212 
213  if ($obj_type == "cat" && !$a_stopnesting)
214  {
215  $news = $this->getAggregatedChildNewsData($a_ref_id, $a_only_public, $a_time_period,
216  $a_prevent_aggregation, $starting_date, $a_no_auto_generated);
217  }
218  else if (($obj_type == "grp" || $obj_type == "crs") &&
219  !$a_stopnesting)
220  {
221  $news = $this->getAggregatedNewsData($a_ref_id, $a_only_public, $a_time_period,
222  $a_prevent_aggregation, $starting_date, $a_no_auto_generated);
223  }
224  else
225  {
226  $news_item = new ilNewsItem();
227  $news_item->setContextObjId($obj_id);
228  $news_item->setContextObjType($obj_type);
229  $news = $news_item->queryNewsForContext($a_only_public, $a_time_period,
230  $starting_date, $a_no_auto_generated);
231  $unset = array();
232  foreach ($news as $k => $v)
233  {
234  if (!$a_only_public || $v["visibility"] == NEWS_PUBLIC ||
235  ($v["priority"] == 0 &&
236  ilBlockSetting::_lookup("news", "public_notifications",
237  0, $obj_id)))
238  {
239  $news[$k]["ref_id"] = $a_ref_id;
240  }
241  else
242  {
243  $unset[] = $k;
244  }
245  }
246  foreach ($unset as $un)
247  {
248  unset($news[$un]);
249  }
250  }
251 
252  if (!$a_prevent_aggregation)
253  {
254  $news = $this->aggregateForums($news);
255  }
256  else if ($a_forum_group_sequences)
257  {
258  $news = $this->aggregateForums($news, true);
259  }
260 
261  return $news;
262  }
263 
267  function getAggregatedNewsData($a_ref_id, $a_only_public = false, $a_time_period = 0,
268  $a_prevent_aggregation = false, $a_starting_date = "", $a_no_auto_generated = false)
269  {
270  global $tree, $ilAccess, $ilObjDataCache;
271 
272  // get news of parent object
273 
274  $data = array();
275 
276  // get subtree
277  $cur_node = $tree->getNodeData($a_ref_id);
278 
279  if ($cur_node["lft"] != "") // should never be empty
280  {
281  $nodes = $tree->getSubTree($cur_node, true);
282  }
283  else
284  {
285  $nodes = array();
286  }
287 
288  // preload object data cache
289  $ref_ids = array();
290  $obj_ids = array();
291  foreach($nodes as $node)
292  {
293  $ref_ids[] = $node["child"];
294  $obj_ids[] = $node["obj_id"];
295  }
296 
297  $ilObjDataCache->preloadReferenceCache($ref_ids);
298  if (!$a_only_public)
299  {
300  $ilAccess->preloadActivationTimes($ref_ids);
301  }
302 
303  // no check, for which of the objects any news are available
304  $news_obj_ids = ilNewsItem::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
305  //$news_obj_ids = $obj_ids;
306 
307  // get news for all subtree nodes
308  $contexts = array();
309  foreach($nodes as $node)
310  {
311  // only go on, if news are available
312  if (!in_array($node["obj_id"], $news_obj_ids))
313  {
314  continue;
315  }
316 
317  if (!$a_only_public)
318  {
319  $acc = $ilAccess->checkAccess("read", "", $node["child"]);
320 
321  if (!$acc)
322  {
323  continue;
324  }
325  }
326 
327  $ref_id[$node["obj_id"]] = $node["child"];
328  $contexts[] = array("obj_id" => $node["obj_id"],
329  "obj_type" => $node["type"]);
330  }
331 
332  // sort and return
333  $news = $this->queryNewsForMultipleContexts($contexts, $a_only_public, $a_time_period,
334  $a_starting_date, $a_no_auto_generated);
335 
336  $to_del = array();
337  foreach ($news as $k => $v)
338  {
339  $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
340  }
341 
343  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
344 
345  if (!$a_prevent_aggregation)
346  {
347  $data = $this->aggregateFiles($data, $a_ref_id);
348  }
349 
350  return $data;
351  }
352 
353  function aggregateForums($news, $a_group_posting_sequence = false)
354  {
355  $to_del = array();
356  $forums = array();
357 
358  // aggregate
359  foreach ($news as $k => $v)
360  {
361  if ($a_group_posting_sequence && $last_aggregation_forum > 0 &&
362  $last_aggregation_forum != $news[$k]["context_obj_id"])
363  {
364  $forums[$last_aggregation_forum] = "";
365  }
366 
367  if ($news[$k]["context_obj_type"] == "frm")
368  {
369  if ($forums[$news[$k]["context_obj_id"]] == "")
370  {
371  // $forums[forum_id] = news_id;
372  $forums[$news[$k]["context_obj_id"]] = $k;
373  $last_aggregation_forum = $news[$k]["context_obj_id"];
374  }
375  else
376  {
377  $to_del[] = $k;
378  }
379 
380  $news[$k]["no_context_title"] = true;
381 
382  // aggregate every forum into it's "k" news
383  $news[$forums[$news[$k]["context_obj_id"]]]["aggregation"][$k]
384  = $news[$k];
385  $news[$k]["agg_ref_id"]
386  = $news[$k]["ref_id"];
387  $news[$k]["content"] = "";
388  $news[$k]["content_long"] = "";
389  }
390  }
391 
392  // delete double entries
393  foreach($to_del as $k)
394  {
395  unset($news[$k]);
396  }
397 //var_dump($news[14]["aggregation"]);
398 
399 
400  return $news;
401  }
402 
403  function aggregateFiles($news, $a_ref_id)
404  {
405  $first_file = "";
406  $to_del = array();
407  foreach ($news as $k => $v)
408  {
409  // aggregate file related news
410  if ($news[$k]["context_obj_type"] == "file")
411  {
412  if ($first_file == "")
413  {
414  $first_file = $k;
415  }
416  else
417  {
418  $to_del[] = $k;
419  }
420  $news[$first_file]["aggregation"][$k] = $news[$k];
421  $news[$first_file]["agg_ref_id"] = $a_ref_id;
422  $news[$first_file]["ref_id"] = $a_ref_id;
423  }
424  }
425 
426  foreach($to_del as $v)
427  {
428  unset($news[$v]);
429  }
430 
431  return $news;
432  }
433 
434 
438  function getAggregatedChildNewsData($a_ref_id, $a_only_public = false,
439  $a_time_period = 0, $a_prevent_aggregation = false, $a_starting_date = "",
440  $a_no_auto_generated = false)
441  {
442  global $tree, $ilAccess;
443 
444  // get news of parent object
445  $data = $this->getNewsForRefId($a_ref_id, $a_only_public, true, $a_time_period,
446  true, false, false, $a_no_auto_generated);
447  foreach ($data as $k => $v)
448  {
449  $data[$k]["ref_id"] = $a_ref_id;
450  }
451 
452  // get childs
453  $nodes = $tree->getChilds($a_ref_id);
454 
455  // no check, for which of the objects any news are available
456  $obj_ids = array();
457  foreach($nodes as $node)
458  {
459  $obj_ids[] = $node["obj_id"];
460  }
461  $news_obj_ids = ilNewsItem::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
462  //$news_obj_ids = $obj_ids;
463 
464  // get news for all subtree nodes
465  $contexts = array();
466  foreach($nodes as $node)
467  {
468  // only go on, if news are available
469  if (!in_array($node["obj_id"], $news_obj_ids))
470  {
471  continue;
472  }
473 
474  if (!$a_only_public && !$ilAccess->checkAccess("read", "", $node["child"]))
475  {
476  continue;
477  }
478  $ref_id[$node["obj_id"]] = $node["child"];
479  $contexts[] = array("obj_id" => $node["obj_id"],
480  "obj_type" => $node["type"]);
481  }
482 
483  $news = $this->queryNewsForMultipleContexts($contexts, $a_only_public, $a_time_period,
484  $a_starting_date, $a_no_auto_generated);
485  foreach ($news as $k => $v)
486  {
487  $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
488  }
490 
491  // sort and return
492  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
493 
494  if (!$a_prevent_aggregation)
495  {
496  $data = $this->aggregateFiles($data, $a_ref_id);
497  }
498 
499  return $data;
500  }
501 
505  function setContext($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
506  {
507  $this->setContextObjId($a_obj_id);
508  $this->setContextObjType($a_obj_type);
509  $this->setContextSubObjId($a_sub_obj_id);
510  $this->setContextSubObjType($a_sub_obj_type);
511  }
512 
521  public function queryNewsForContext($a_for_rss_use = false, $a_time_period = 0,
522  $a_starting_date = "", $a_no_auto_generated = false, $a_oldest_first = false)
523  {
524  global $ilDB, $ilUser, $lng;
525 
526  $and = "";
527  if ($a_time_period > 0)
528  {
529  $limit_ts = date('Y-m-d H:i:s', time() - ($a_time_period * 24 * 60 * 60));
530  $and = " AND creation_date >= ".$ilDB->quote($limit_ts, "timestamp")." ";
531  }
532 
533  if ($a_starting_date != "")
534  {
535  $and.= " AND creation_date > ".$ilDB->quote($a_starting_date, "timestamp")." ";
536  }
537 
538  if ($a_no_auto_generated)
539  {
540  $and.= " AND priority = 1 AND content_type = ".$ilDB->quote("text", "text")." ";
541  }
542 
543  // this is changed with 4.1 (news table for lm pages)
544  if ($this->getContextSubObjId() > 0)
545  {
546  $and.= " AND context_sub_obj_id = ".$ilDB->quote($this->getContextSubObjId(), "integer").
547  " AND context_sub_obj_type = ".$ilDB->quote($this->getContextSubObjType(), "text");
548  }
549 
550  $ordering = ($a_oldest_first)
551  ? " creation_date ASC, id ASC "
552  : " creation_date DESC, id DESC ";
553 
554  if ($a_for_rss_use && ilNewsItem::getPrivateFeedId() == false)
555  {
556  $query = "SELECT * ".
557  "FROM il_news_item ".
558  " WHERE ".
559  "context_obj_id = ".$ilDB->quote($this->getContextObjId(), "integer").
560  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType(), "text").
561  $and.
562  " ORDER BY ".$ordering;
563  }
564  elseif (ilNewsItem::getPrivateFeedId() != false)
565  {
566  $query = "SELECT il_news_item.* ".
567  ", il_news_read.user_id user_read ".
568  "FROM il_news_item LEFT JOIN il_news_read ".
569  "ON il_news_item.id = il_news_read.news_id AND ".
570  " il_news_read.user_id = ".$ilDB->quote(ilNewsItem::getPrivateFeedId(), "integer").
571  " WHERE ".
572  "context_obj_id = ".$ilDB->quote($this->getContextObjId(), "integer").
573  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType(), "text").
574  $and.
575  " ORDER BY ".$ordering;
576  }
577  else
578  {
579  $query = "SELECT il_news_item.* ".
580  ", il_news_read.user_id as user_read ".
581  "FROM il_news_item LEFT JOIN il_news_read ".
582  "ON il_news_item.id = il_news_read.news_id AND ".
583  " il_news_read.user_id = ".$ilDB->quote($ilUser->getId(), "integer").
584  " WHERE ".
585  "context_obj_id = ".$ilDB->quote($this->getContextObjId(), "integer").
586  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType(), "text").
587  $and.
588  " ORDER BY ".$ordering;
589  }
590 //echo $query;
591  $set = $ilDB->query($query);
592  $result = array();
593  while($rec = $ilDB->fetchAssoc($set))
594  {
595  if (!$a_for_rss_use || (ilNewsItem::getPrivateFeedId() != false) || ($rec["visibility"] == NEWS_PUBLIC ||
596  ($rec["priority"] == 0 &&
597  ilBlockSetting::_lookup("news", "public_notifications",
598  0, $rec["context_obj_id"]))))
599  {
600  $result[$rec["id"]] = $rec;
601  }
602  }
603 
604  return $result;
605 
606  }
607 
613  public function queryNewsForMultipleContexts($a_contexts, $a_for_rss_use = false,
614  $a_time_period = 0, $a_starting_date = "", $a_no_auto_generated = false)
615  {
616  global $ilDB, $ilUser, $lng, $ilCtrl;
617 
618  $and = "";
619  if ($a_time_period > 0)
620  {
621  $limit_ts = date('Y-m-d H:i:s', time() - ($a_time_period * 24 * 60 * 60));
622  $and = " AND creation_date >= ".$ilDB->quote($limit_ts, "timestamp")." ";
623  }
624 
625  if ($a_starting_date != "")
626  {
627  $and.= " AND creation_date > ".$ilDB->quote($a_starting_date, "timestamp")." ";
628  }
629 
630  if ($a_no_auto_generated)
631  {
632  $and.= " AND priority = 1 AND content_type = ".$ilDB->quote("text", "text")." ";
633  }
634 
635  $ids = array();
636  $type = array();
637  foreach($a_contexts as $cont)
638  {
639  $ids[] = $cont["obj_id"];
640  $type[$cont["obj_id"]] = $cont["obj_type"];
641  }
642 
643  if ($a_for_rss_use && ilNewsItem::getPrivateFeedId() == false)
644  {
645  $query = "SELECT * ".
646  "FROM il_news_item ".
647  " WHERE ".
648  $ilDB->in("context_obj_id", $ids, false, "integer")." ".
649  $and.
650  " ORDER BY creation_date DESC ";
651  }
652  elseif (ilNewsItem::getPrivateFeedId() != false)
653  {
654  $query = "SELECT il_news_item.* ".
655  ", il_news_read.user_id as user_read ".
656  "FROM il_news_item LEFT JOIN il_news_read ".
657  "ON il_news_item.id = il_news_read.news_id AND ".
658  " il_news_read.user_id = ".$ilDB->quote(ilNewsItem::getPrivateFeedId(), "integer").
659  " WHERE ".
660  $ilDB->in("context_obj_id", $ids, false, "integer")." ".
661  $and.
662  " ORDER BY creation_date DESC ";
663  }
664  else
665  {
666  $query = "SELECT il_news_item.* ".
667  ", il_news_read.user_id as user_read ".
668  "FROM il_news_item LEFT JOIN il_news_read ".
669  "ON il_news_item.id = il_news_read.news_id AND ".
670  " il_news_read.user_id = ".$ilDB->quote($ilUser->getId(), "integer").
671  " WHERE ".
672  $ilDB->in("context_obj_id", $ids, false, "integer")." ".
673  $and.
674  " ORDER BY creation_date DESC ";
675  }
676 
677  $set = $ilDB->query($query);
678  $result = array();
679  while($rec = $ilDB->fetchAssoc($set))
680  {
681  if ($type[$rec["context_obj_id"]] == $rec["context_obj_type"])
682  {
683  if (!$a_for_rss_use || ilNewsItem::getPrivateFeedId() != false || ($rec["visibility"] == NEWS_PUBLIC ||
684  ($rec["priority"] == 0 &&
685  ilBlockSetting::_lookup("news", "public_notifications",
686  0, $rec["context_obj_id"]))))
687  {
688  $result[$rec["id"]] = $rec;
689  }
690  }
691  }
692 
693  return $result;
694 
695  }
696 
697 
701  function _setRead($a_user_id, $a_news_id)
702  {
703  global $ilDB, $ilAppEventHandler;
704 
705  $ilDB->manipulate("DELETE FROM il_news_read WHERE ".
706  "user_id = ".$ilDB->quote($a_user_id, "integer").
707  " AND news_id = ".$ilDB->quote($a_news_id, "integer"));
708  $ilDB->manipulate("INSERT INTO il_news_read (user_id, news_id) VALUES (".
709  $ilDB->quote($a_user_id, "integer").",".
710  $ilDB->quote($a_news_id, "integer").")");
711 
712  $ilAppEventHandler->raise("Services/News", "readNews",
713  array("user_id" => $a_user_id, "news_ids" => array($a_news_id)));
714  }
715 
719  function _setUnread($a_user_id, $a_news_id)
720  {
721  global $ilDB, $ilAppEventHandler;
722 
723  $ilDB->manipulate("DELETE FROM il_news_read (user_id, news_id) VALUES (".
724  " WHERE user_id = ".$ilDB->quote($a_user_id, "integer").
725  " AND news_id = ".$ilDB->quote($a_news_id, "integer"));
726 
727  $ilAppEventHandler->raise("Services/News", "unreadNews",
728  array("user_id" => $a_user_id, "news_ids" => array($a_news_id)));
729  }
730 
739  function mergeNews($n1, $n2)
740  {
741  foreach($n2 as $id => $news)
742  {
743  $n1[$id] = $news;
744  }
745 
746  return $n1;
747  }
748 
754  static function _getDefaultVisibilityForRefId($a_ref_id)
755  {
756  global $tree, $ilSetting;
757 
758  include_once("./Services/Block/classes/class.ilBlockSetting.php");
759 
760  $news_set = new ilSetting("news");
761  $default_visibility = ($news_set->get("default_visibility") != "")
762  ? $news_set->get("default_visibility")
763  : "users";
764 
765  if ($tree->isInTree($a_ref_id))
766  {
767  $path = $tree->getPathFull($a_ref_id);
768 
769  foreach ($path as $key => $row)
770  {
771  if (!in_array($row["type"], array("root", "cat","crs", "fold", "grp", "icrs")))
772  {
773  continue;
774  }
775 
776  $visibility = ilBlockSetting::_lookup("news", "default_visibility",
777  0, $row["obj_id"]);
778 
779  if ($visibility != "")
780  {
781  $default_visibility = $visibility;
782  }
783  }
784  }
785 
786  return $default_visibility;
787  }
788 
789 
794  public function delete()
795  {
796  global $ilDB;
797 
798  // delete il_news_read entries
799  $ilDB->manipulate("DELETE FROM il_news_read ".
800  " WHERE news_id = ".$ilDB->quote($this->getId(), "integer"));
801 
802  // delete multimedia object
803  $mob = $this->getMobId();
804 
805  // delete
806  parent::delete();
807 
808  // delete mob after news, to have a "mob usage" of 0
809  if ($mob > 0 and ilObject::_exists($mob))
810  {
811  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
812  $mob = new ilObjMediaObject($mob);
813  $mob->delete();
814  }
815  }
816 
821  static public function deleteNewsOfContext($a_context_obj_id,
822  $a_context_obj_type, $a_context_sub_obj_id = 0, $a_context_sub_obj_type = "")
823  {
824  global $ilDB;
825 
826  if ($a_context_obj_id == 0 || $a_context_obj_type == "")
827  {
828  return;
829  }
830 
831  if ($a_context_sub_obj_id > 0)
832  {
833  $and = " AND context_sub_obj_id = ".$ilDB->quote($a_context_sub_obj_id, "integer").
834  " AND context_sub_obj_type = ".$ilDB->quote($a_context_sub_obj_type, "text");
835  }
836 
837  // get news records
838  $query = "SELECT * FROM il_news_item".
839  " WHERE context_obj_id = ".$ilDB->quote($a_context_obj_id, "integer").
840  " AND context_obj_type = ".$ilDB->quote($a_context_obj_type, "text").
841  $and;
842 
843  $news_set = $ilDB->query($query);
844 
845  while ($news = $ilDB->fetchAssoc($news_set))
846  {
847  $news_obj = new ilNewsItem($news["id"]);
848  $news_obj->delete();
849  }
850  }
851 
855  static function _lookupTitle($a_news_id)
856  {
857  global $ilDB;
858 
859  $query = "SELECT title FROM il_news_item WHERE id = ".
860  $ilDB->quote($a_news_id, "integer");
861  $set = $ilDB->query($query);
862  $rec = $ilDB->fetchAssoc($set);
863  return $rec["title"];
864  }
865 
869  static function _lookupVisibility($a_news_id)
870  {
871  global $ilDB;
872 
873  $query = "SELECT visibility FROM il_news_item WHERE id = ".
874  $ilDB->quote($a_news_id, "integer");
875  $set = $ilDB->query($query);
876  $rec = $ilDB->fetchAssoc($set);
877 
878  return $rec["visibility"];
879  }
880 
884  static function _lookupMobId($a_news_id)
885  {
886  global $ilDB;
887 
888  $query = "SELECT mob_id FROM il_news_item WHERE id = ".
889  $ilDB->quote($a_news_id, "integer");
890  $set = $ilDB->query($query);
891  $rec = $ilDB->fetchAssoc($set);
892  return $rec["mob_id"];
893  }
894 
898  static function filterObjIdsPerNews($a_obj_ids, $a_time_period = 0, $a_starting_date = "",$a_ending_date = '', $ignore_period = false)
899  {
900  global $ilDB;
901 
902  $and = "";
903  if ($a_time_period > 0)
904  {
905  $limit_ts = date('Y-m-d H:i:s', time() - ($a_time_period * 24 * 60 * 60));
906  $and = " AND creation_date >= ".$ilDB->quote($limit_ts, "timestamp")." ";
907  }
908 
909  if ($a_starting_date != "")
910  {
911  $and.= " AND creation_date >= ".$ilDB->quote($a_starting_date, "timestamp");
912  }
913 
914  $query = "SELECT DISTINCT(context_obj_id) AS obj_id FROM il_news_item".
915  " WHERE ".$ilDB->in("context_obj_id", $a_obj_ids, false, "integer")." ".$and;
916  //" WHERE context_obj_id IN (".implode(ilUtil::quoteArray($a_obj_ids),",").")".$and;
917 
918  $set = $ilDB->query($query);
919  $objs = array();
920  while($rec = $ilDB->fetchAssoc($set))
921  {
922  $objs[] = $rec["obj_id"];
923  }
924 
925  return $objs;
926  }
927 
931  static function determineNewsTitle($a_context_obj_type, $a_title, $a_content_is_lang_var,
932  $a_agg_ref_id = 0, $a_aggregation = "")
933  {
934  global $lng;
935 
936  if ($a_agg_ref_id > 0)
937  {
938  $cnt = count($a_aggregation);
939 
940  // forums
941  if ($a_context_obj_type == "frm")
942  {
943  if ($cnt > 1)
944  {
945  return sprintf($lng->txt("news_x_postings"), $cnt);
946  }
947  else
948  {
949  return $lng->txt("news_1_postings");
950  }
951  }
952  else // files
953  {
954  $up_cnt = $cr_cnt = 0;
955  foreach($a_aggregation as $item)
956  {
957  if ($item["title"] == "file_updated")
958  {
959  $up_cnt++;
960  }
961  else
962  {
963  $cr_cnt++;
964  }
965  }
966  $sep = "";
967  if ($cr_cnt == 1)
968  {
969  $tit = $lng->txt("news_1_file_created");
970  $sep = "<br />";
971  }
972  else if ($cr_cnt > 1)
973  {
974  $tit = sprintf($lng->txt("news_x_files_created"), $cr_cnt);
975  $sep = "<br />";
976  }
977  if ($up_cnt == 1)
978  {
979  $tit .= $sep.$lng->txt("news_1_file_updated");
980  }
981  else if ($up_cnt > 1)
982  {
983  $tit .= $sep.sprintf($lng->txt("news_x_files_updated"), $up_cnt);
984  }
985  return $tit;
986  }
987  }
988  else
989  {
990  if ($a_content_is_lang_var)
991  {
992  return $lng->txt($a_title);
993  }
994  else
995  {
996  return $a_title;
997  }
998  }
999 
1000  return "";
1001  }
1002 
1003 
1007  static function getFirstNewsIdForContext($a_context_obj_id,
1008  $a_context_obj_type, $a_context_sub_obj_id = "", $a_context_sub_obj_type = "")
1009  {
1010  global $ilDB;
1011 
1012  // Determine how many rows should be deleted
1013  $query = "SELECT * ".
1014  "FROM il_news_item ".
1015  "WHERE ".
1016  "context_obj_id = ".$ilDB->quote($a_context_obj_id, "integer").
1017  " AND context_obj_type = ".$ilDB->quote($a_context_obj_type, "text").
1018  " AND context_sub_obj_id = ".$ilDB->quote($a_context_sub_obj_id, "integer").
1019  " AND ".$ilDB->equals("context_sub_obj_type", $a_context_sub_obj_type, "text", true);
1020 
1021  $set = $ilDB->query($query);
1022  $rec = $ilDB->fetchAssoc($set);
1023 
1024  return $rec["id"];
1025  }
1026 
1030  static function _lookupMediaObjectUsages($a_mob_id)
1031  {
1032  global $ilDB;
1033 
1034  $query = "SELECT * ".
1035  "FROM il_news_item ".
1036  "WHERE ".
1037  " mob_id = ".$ilDB->quote($a_mob_id, "integer");
1038 
1039  $usages = array();
1040  $set = $ilDB->query($query);
1041  while ($rec = $ilDB->fetchAssoc($set))
1042  {
1043  $usages[$rec["id"]] = array("type" => "news", "id" => $rec["id"]);
1044  }
1045 
1046  return $usages;
1047  }
1048 
1052  static function _lookupContextObjId($a_news_id)
1053  {
1054  global $ilDB;
1055 
1056  $query = "SELECT * ".
1057  "FROM il_news_item ".
1058  "WHERE ".
1059  " id = ".$ilDB->quote($a_news_id, "integer");
1060  $set = $ilDB->query($query);
1061  $rec = $ilDB->fetchAssoc($set);
1062 
1063  return $rec["context_obj_id"];
1064  }
1065 
1067  {
1068  $news_set = new ilSetting("news");
1069  $per = $news_set->get("pd_period");
1070  if ($per == 0)
1071  {
1072  $per = 30;
1073  }
1074 
1075  return $per;
1076  }
1077 
1078  function _lookupUserPDPeriod($a_user_id)
1079  {
1080  global $ilSetting;
1081 
1082  $news_set = new ilSetting("news");
1083  $allow_shorter_periods = $news_set->get("allow_shorter_periods");
1084  $allow_longer_periods = $news_set->get("allow_longer_periods");
1085  $default_per = ilNewsItem::_lookupDefaultPDPeriod();
1086 
1087  include_once("./Services/Block/classes/class.ilBlockSetting.php");
1088  $per = ilBlockSetting::_lookup("pdnews", "news_pd_period",
1089  $a_user_id, 0);
1090 
1091  // news period information
1092  if ($per <= 0 ||
1093  (!$allow_shorter_periods && ($per < $default_per)) ||
1094  (!$allow_longer_periods && ($per > $default_per))
1095  )
1096  {
1097  $per = $default_per;
1098  }
1099 
1100  return $per;
1101  }
1102 
1103  function _lookupRSSPeriod()
1104  {
1105  $news_set = new ilSetting("news");
1106  $rss_period = $news_set->get("rss_period");
1107  if ($rss_period == 0) // default to two weeks
1108  {
1109  $rss_period = 14;
1110  }
1111  return $rss_period;
1112  }
1113  function setPrivateFeedId ($a_userId)
1114  {
1115  ilNewsItem::$privFeedId = $a_userId;
1116  }
1117 
1118  function getPrivateFeedId () {
1119 
1120  return ilNewsItem::$privFeedId;
1121  }
1122 }
1123 ?>