ILIAS  Release_4_0_x_branch Revision 61816
 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, $ilBench;
121 
122  $ilBench->start("News", "getNewsItemsOfUser");
123 
124  $news_item = new ilNewsItem();
125  $news_set = new ilSetting("news");
126 
127  $per = $a_per;
128 
129  include_once("./Services/News/classes/class.ilNewsSubscription.php");
130  include_once("./Services/Block/classes/class.ilBlockSetting.php");
131 
132  // this is currently not used
133  $ilBench->start("News", "getNewsItemsOfUser_getRefIds");
134  $ref_ids = ilNewsSubscription::_getSubscriptionsOfUser($a_user_id);
135 
136  if (ilObjUser::_lookupPref($a_user_id, "pd_items_news") != "n")
137  {
138  // this should be the case for all users
139  $pd_items = ilObjUser::_lookupDesktopItems($a_user_id);
140  foreach($pd_items as $item)
141  {
142  if (!in_array($item["ref_id"], $ref_ids))
143  {
144  $ref_ids[] = $item["ref_id"];
145  }
146  }
147  }
148  $ilBench->stop("News", "getNewsItemsOfUser_getRefIds");
149 
150  $data = array();
151 
152  foreach($ref_ids as $ref_id)
153  {
154  $ilBench->start("News", "getNewsForRefId");
155  if (!$a_only_public)
156  {
157  // this loop should not cost too much performance
158  $ilBench->start("News", "getAggregatedNewsData_getContexts_checkAccess");
159  $acc = $ilAccess->checkAccess("read", "", $ref_id);
160  $ilBench->stop("News", "getAggregatedNewsData_getContexts_checkAccess");
161 
162  if (!$acc)
163  {
164  continue;
165  }
166  }
167  if (ilNewsItem::getPrivateFeedId() != false) {
168  global $rbacsystem;
169  $acc = $rbacsystem->checkAccessOfUser(ilNewsItem::getPrivateFeedId(),"read", $ref_id);
170 
171  if (!$acc)
172  {
173  continue;
174  }
175  }
176 
177  $ilBench->start("News", "getNewsForRefId_getNews");
178  $obj_id = ilObject::_lookupObjId($ref_id);
179  $obj_type = ilObject::_lookupType($obj_id);
180  $news = $news_item->getNewsForRefId($ref_id, $a_only_public, false,
181  $per, $a_prevent_aggregation);
182  $ilBench->stop("News", "getNewsForRefId_getNews");
183 
184  // counter
185  if (!is_null($a_cnt))
186  {
187  $a_cnt[$ref_id] = count($news);
188  }
189 
190  $ilBench->start("News", "getNewsForRefId_mergeNews");
192  $ilBench->stop("News", "getNewsForRefId_mergeNews");
193 
194  $ilBench->stop("News", "getNewsForRefId");
195  }
196 
197  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
198 
199  $ilBench->stop("News", "getNewsItemsOfUser");
200 
201 //var_dump($data);
202  return $data;
203  }
204 
208  function getNewsForRefId($a_ref_id, $a_only_public = false, $a_stopnesting = false,
209  $a_time_period = 0, $a_prevent_aggregation = true, $a_forum_group_sequences = false)
210  {
211  $obj_id = ilObject::_lookupObjId($a_ref_id);
212  $obj_type = ilObject::_lookupType($obj_id);
213 
214  // get starting date
215  $starting_date = "";
216  if ($obj_type == "grp" || $obj_type == "crs" || $obj_type == "cat")
217  {
218  include_once("./Services/Block/classes/class.ilBlockSetting.php");
219  $hide_news_per_date = ilBlockSetting::_lookup("news", "hide_news_per_date",
220  0, $obj_id);
221  if ($hide_news_per_date)
222  {
223  $starting_date = ilBlockSetting::_lookup("news", "hide_news_date",
224  0, $obj_id);
225  }
226  }
227 
228  if ($obj_type == "cat" && !$a_stopnesting)
229  {
230  $news = $this->getAggregatedChildNewsData($a_ref_id, $a_only_public, $a_time_period,
231  $a_prevent_aggregation, $starting_date);
232  }
233  else if (($obj_type == "grp" || $obj_type == "crs") &&
234  !$a_stopnesting)
235  {
236  $news = $this->getAggregatedNewsData($a_ref_id, $a_only_public, $a_time_period,
237  $a_prevent_aggregation, $starting_date);
238  }
239  else
240  {
241  $news_item = new ilNewsItem();
242  $news_item->setContextObjId($obj_id);
243  $news_item->setContextObjType($obj_type);
244  $news = $news_item->queryNewsForContext($a_only_public, $a_time_period,
245  $starting_date);
246  $unset = array();
247  foreach ($news as $k => $v)
248  {
249  if (!$a_only_public || $v["visibility"] == NEWS_PUBLIC ||
250  ($v["priority"] == 0 &&
251  ilBlockSetting::_lookup("news", "public_notifications",
252  0, $obj_id)))
253  {
254  $news[$k]["ref_id"] = $a_ref_id;
255  }
256  else
257  {
258  $unset[] = $k;
259  }
260  }
261  foreach ($unset as $un)
262  {
263  unset($news[$un]);
264  }
265  }
266 
267  if (!$a_prevent_aggregation)
268  {
269  $news = $this->aggregateForums($news);
270  }
271  else if ($a_forum_group_sequences)
272  {
273  $news = $this->aggregateForums($news, true);
274  }
275 
276  return $news;
277  }
278 
282  function getAggregatedNewsData($a_ref_id, $a_only_public = false, $a_time_period = 0,
283  $a_prevent_aggregation = false, $a_starting_date = "")
284  {
285  global $tree, $ilAccess, $ilBench, $ilObjDataCache;
286 
287  $ilBench->start("News", "getAggregatedNewsData");
288 
289  // get news of parent object
290 
291  $data = array();
292 
293  // get subtree
294  $ilBench->start("News", "getAggregatedNewsData_getSubTree");
295  $cur_node = $tree->getNodeData($a_ref_id);
296 
297  if ($cur_node["lft"] != "") // should never be empty
298  {
299  $nodes = $tree->getSubTree($cur_node, true);
300  }
301  else
302  {
303  $nodes = array();
304  }
305 
306  // preload object data cache
307  $ref_ids = array();
308  $obj_ids = array();
309  foreach($nodes as $node)
310  {
311  $ref_ids[] = $node["child"];
312  $obj_ids[] = $node["obj_id"];
313  }
314  $ilBench->stop("News", "getAggregatedNewsData_getSubTree");
315 
316  $ilBench->start("News", "getAggregatedNewsData_preloadActivationTimes");
317  $ilObjDataCache->preloadReferenceCache($ref_ids);
318  if (!$a_only_public)
319  {
320  $ilAccess->preloadActivationTimes($ref_ids);
321  }
322  $ilBench->stop("News", "getAggregatedNewsData_preloadActivationTimes");
323 
324  // no check, for which of the objects any news are available
325  $news_obj_ids = ilNewsItem::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
326  //$news_obj_ids = $obj_ids;
327 
328  // get news for all subtree nodes
329  $ilBench->start("News", "getAggregatedNewsData_getContexts");
330  $contexts = array();
331  foreach($nodes as $node)
332  {
333  // only go on, if news are available
334  if (!in_array($node["obj_id"], $news_obj_ids))
335  {
336  continue;
337  }
338 
339  if (!$a_only_public)
340  {
341  $ilBench->start("News", "getAggregatedNewsData_getContexts_checkAccess");
342  $acc = $ilAccess->checkAccess("read", "", $node["child"]);
343  $ilBench->stop("News", "getAggregatedNewsData_getContexts_checkAccess");
344 
345  if (!$acc)
346  {
347  continue;
348  }
349  }
350 
351  $ref_id[$node["obj_id"]] = $node["child"];
352  $contexts[] = array("obj_id" => $node["obj_id"],
353  "obj_type" => $node["type"]);
354  }
355  $ilBench->stop("News", "getAggregatedNewsData_getContexts");
356 
357  // sort and return
358  $news = $this->queryNewsForMultipleContexts($contexts, $a_only_public, $a_time_period,
359  $a_starting_date);
360 
361  $ilBench->start("News", "getAggregatedNewsData_mergeAndSort");
362 
363  $to_del = array();
364  foreach ($news as $k => $v)
365  {
366  $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
367  }
368 
370  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
371 
372  if (!$a_prevent_aggregation)
373  {
374  $data = $this->aggregateFiles($data, $a_ref_id);
375  }
376 
377 //var_dump($data);
378 
379  $ilBench->stop("News", "getAggregatedNewsData_mergeAndSort");
380  $ilBench->stop("News", "getAggregatedNewsData");
381 
382  return $data;
383  }
384 
385  function aggregateForums($news, $a_group_posting_sequence = false)
386  {
387  $to_del = array();
388  $forums = array();
389 
390  // aggregate
391  foreach ($news as $k => $v)
392  {
393  if ($a_group_posting_sequence && $last_aggregation_forum > 0 &&
394  $last_aggregation_forum != $news[$k]["context_obj_id"])
395  {
396  $forums[$last_aggregation_forum] = "";
397  }
398 
399  if ($news[$k]["context_obj_type"] == "frm")
400  {
401  if ($forums[$news[$k]["context_obj_id"]] == "")
402  {
403  // $forums[forum_id] = news_id;
404  $forums[$news[$k]["context_obj_id"]] = $k;
405  $last_aggregation_forum = $news[$k]["context_obj_id"];
406  }
407  else
408  {
409  $to_del[] = $k;
410  }
411 
412  $news[$k]["no_context_title"] = true;
413 
414  // aggregate every forum into it's "k" news
415  $news[$forums[$news[$k]["context_obj_id"]]]["aggregation"][$k]
416  = $news[$k];
417  $news[$k]["agg_ref_id"]
418  = $news[$k]["ref_id"];
419  $news[$k]["content"] = "";
420  $news[$k]["content_long"] = "";
421  }
422  }
423 
424  // delete double entries
425  foreach($to_del as $k)
426  {
427  unset($news[$k]);
428  }
429 //var_dump($news[14]["aggregation"]);
430 
431 
432  return $news;
433  }
434 
435  function aggregateFiles($news, $a_ref_id)
436  {
437  $first_file = "";
438  $to_del = array();
439  foreach ($news as $k => $v)
440  {
441  // aggregate file related news
442  if ($news[$k]["context_obj_type"] == "file")
443  {
444  if ($first_file == "")
445  {
446  $first_file = $k;
447  }
448  else
449  {
450  $to_del[] = $k;
451  }
452  $news[$first_file]["aggregation"][$k] = $news[$k];
453  $news[$first_file]["agg_ref_id"] = $a_ref_id;
454  $news[$first_file]["ref_id"] = $a_ref_id;
455  }
456  }
457 
458  foreach($to_del as $v)
459  {
460  unset($news[$v]);
461  }
462 
463  return $news;
464  }
465 
466 
470  function getAggregatedChildNewsData($a_ref_id, $a_only_public = false,
471  $a_time_period = 0, $a_prevent_aggregation = false, $a_starting_date = "")
472  {
473  global $tree, $ilAccess, $ilBench;
474 
475  $ilBench->start("News", "getAggregatedChildNewsData");
476 
477  // get news of parent object
478  $data = $this->getNewsForRefId($a_ref_id, $a_only_public, true, $a_time_period);
479  foreach ($data as $k => $v)
480  {
481  $data[$k]["ref_id"] = $a_ref_id;
482  }
483 
484  // get childs
485  $nodes = $tree->getChilds($a_ref_id);
486 
487  // no check, for which of the objects any news are available
488  $obj_ids = array();
489  foreach($nodes as $node)
490  {
491  $obj_ids[] = $node["obj_id"];
492  }
493  $news_obj_ids = ilNewsItem::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
494  //$news_obj_ids = $obj_ids;
495 
496  // get news for all subtree nodes
497  $contexts = array();
498  foreach($nodes as $node)
499  {
500  // only go on, if news are available
501  if (!in_array($node["obj_id"], $news_obj_ids))
502  {
503  continue;
504  }
505 
506  if (!$a_only_public && !$ilAccess->checkAccess("read", "", $node["child"]))
507  {
508  continue;
509  }
510  $ref_id[$node["obj_id"]] = $node["child"];
511  $contexts[] = array("obj_id" => $node["obj_id"],
512  "obj_type" => $node["type"]);
513  }
514 
515  $news = $this->queryNewsForMultipleContexts($contexts, $a_only_public, $a_time_period,
516  $a_starting_date);
517  foreach ($news as $k => $v)
518  {
519  $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
520  }
522 
523  // sort and return
524  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
525 
526  if (!$a_prevent_aggregation)
527  {
528  $data = $this->aggregateFiles($data, $a_ref_id);
529  }
530 
531  $ilBench->stop("News", "getAggregatedChildNewsData");
532 
533  return $data;
534  }
535 
539  function setContext($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
540  {
541  $this->setContextObjId($a_obj_id);
542  $this->setContextObjType($a_obj_type);
543  $this->setContextSubObjId($a_sub_obj_id);
544  $this->setContextSubObjType($a_sub_obj_type);
545  }
546 
551  public function queryNewsForContext($a_for_rss_use = false, $a_time_period = 0,
552  $a_starting_date = "")
553  {
554  global $ilDB, $ilUser, $lng;
555 
556  $and = "";
557  if ($a_time_period > 0)
558  {
559  $limit_ts = date('Y-m-d H:i:s', time() - ($a_time_period * 24 * 60 * 60));
560  $and = " AND creation_date >= ".$ilDB->quote($limit_ts, "timestamp")." ";
561  }
562 
563  if ($a_starting_date != "")
564  {
565  $and.= " AND creation_date > ".$ilDB->quote($a_starting_date, "timestamp")." ";
566  }
567 
568  if ($a_for_rss_use && ilNewsItem::getPrivateFeedId() == false)
569  {
570  $query = "SELECT * ".
571  "FROM il_news_item ".
572  " WHERE ".
573  "context_obj_id = ".$ilDB->quote($this->getContextObjId(), "integer").
574  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType(), "text").
575  $and.
576  " ORDER BY creation_date DESC ";
577  }
579  {
580  $query = "SELECT il_news_item.* ".
581  ", il_news_read.user_id user_read ".
582  "FROM il_news_item LEFT JOIN il_news_read ".
583  "ON il_news_item.id = il_news_read.news_id AND ".
584  " il_news_read.user_id = ".$ilDB->quote(ilNewsItem::getPrivateFeedId(), "integer").
585  " WHERE ".
586  "context_obj_id = ".$ilDB->quote($this->getContextObjId(), "integer").
587  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType(), "text").
588  $and.
589  " ORDER BY creation_date DESC ";
590  }
591  else
592  {
593  $query = "SELECT il_news_item.* ".
594  ", il_news_read.user_id as user_read ".
595  "FROM il_news_item LEFT JOIN il_news_read ".
596  "ON il_news_item.id = il_news_read.news_id AND ".
597  " il_news_read.user_id = ".$ilDB->quote($ilUser->getId(), "integer").
598  " WHERE ".
599  "context_obj_id = ".$ilDB->quote($this->getContextObjId(), "integer").
600  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType(), "text").
601  $and.
602  " ORDER BY creation_date DESC ";
603  }
604  $set = $ilDB->query($query);
605  $result = array();
606  while($rec = $ilDB->fetchAssoc($set))
607  {
608  if (!$a_for_rss_use || (ilNewsItem::getPrivateFeedId() != false) || ($rec["visibility"] == NEWS_PUBLIC ||
609  ($rec["priority"] == 0 &&
610  ilBlockSetting::_lookup("news", "public_notifications",
611  0, $rec["context_obj_id"]))))
612  {
613  $result[$rec["id"]] = $rec;
614  }
615  }
616 
617  return $result;
618 
619  }
620 
626  public function queryNewsForMultipleContexts($a_contexts, $a_for_rss_use = false,
627  $a_time_period = 0, $a_starting_date = "")
628  {
629  global $ilDB, $ilUser, $ilBench, $lng, $ilCtrl;
630 
631  $ilBench->start("News", "queryNewsForMultipleContexts");
632 
633  $and = "";
634  if ($a_time_period > 0)
635  {
636  $limit_ts = date('Y-m-d H:i:s', time() - ($a_time_period * 24 * 60 * 60));
637  $and = " AND creation_date >= ".$ilDB->quote($limit_ts, "timestamp")." ";
638  }
639 
640  if ($a_starting_date != "")
641  {
642  $and.= " AND creation_date > ".$ilDB->quote($a_starting_date, "timestamp")." ";
643  }
644 
645  $ids = array();
646  $type = array();
647  foreach($a_contexts as $cont)
648  {
649  $ids[] = $cont["obj_id"];
650  $type[$cont["obj_id"]] = $cont["obj_type"];
651  }
652 
653  if ($a_for_rss_use && ilNewsItem::getPrivateFeedId() == false)
654  {
655  $query = "SELECT * ".
656  "FROM il_news_item ".
657  " WHERE ".
658  $ilDB->in("context_obj_id", $ids, false, "integer")." ".
659  $and.
660  " ORDER BY creation_date DESC ";
661  }
663  {
664  $query = "SELECT il_news_item.* ".
665  ", il_news_read.user_id as user_read ".
666  "FROM il_news_item LEFT JOIN il_news_read ".
667  "ON il_news_item.id = il_news_read.news_id AND ".
668  " il_news_read.user_id = ".$ilDB->quote(ilNewsItem::getPrivateFeedId(), "integer").
669  " WHERE ".
670  $ilDB->in("context_obj_id", $ids, false, "integer")." ".
671  $and.
672  " ORDER BY creation_date DESC ";
673  }
674  else
675  {
676  $query = "SELECT il_news_item.* ".
677  ", il_news_read.user_id as user_read ".
678  "FROM il_news_item LEFT JOIN il_news_read ".
679  "ON il_news_item.id = il_news_read.news_id AND ".
680  " il_news_read.user_id = ".$ilDB->quote($ilUser->getId(), "integer").
681  " WHERE ".
682  $ilDB->in("context_obj_id", $ids, false, "integer")." ".
683  $and.
684  " ORDER BY creation_date DESC ";
685  }
686 
687  $set = $ilDB->query($query);
688  $result = array();
689  while($rec = $ilDB->fetchAssoc($set))
690  {
691  if ($type[$rec["context_obj_id"]] == $rec["context_obj_type"])
692  {
693  if (!$a_for_rss_use || ilNewsItem::getPrivateFeedId() != false || ($rec["visibility"] == NEWS_PUBLIC ||
694  ($rec["priority"] == 0 &&
695  ilBlockSetting::_lookup("news", "public_notifications",
696  0, $rec["context_obj_id"]))))
697  {
698  $result[$rec["id"]] = $rec;
699  }
700  }
701  }
702 
703  $ilBench->stop("News", "queryNewsForMultipleContexts");
704 
705  return $result;
706 
707  }
708 
709 
713  function _setRead($a_user_id, $a_news_id)
714  {
715  global $ilDB, $ilAppEventHandler;
716 
717  $ilDB->manipulate("DELETE FROM il_news_read WHERE ".
718  "user_id = ".$ilDB->quote($a_user_id, "integer").
719  " AND news_id = ".$ilDB->quote($a_news_id, "integer"));
720  $ilDB->manipulate("INSERT INTO il_news_read (user_id, news_id) VALUES (".
721  $ilDB->quote($a_user_id, "integer").",".
722  $ilDB->quote($a_news_id, "integer").")");
723 
724  $ilAppEventHandler->raise("Services/News", "readNews",
725  array("user_id" => $a_user_id, "news_ids" => array($a_news_id)));
726  }
727 
731  function _setUnread($a_user_id, $a_news_id)
732  {
733  global $ilDB, $ilAppEventHandler;
734 
735  $ilDB->manipulate("DELETE FROM il_news_read (user_id, news_id) VALUES (".
736  " WHERE user_id = ".$ilDB->quote($a_user_id, "integer").
737  " AND news_id = ".$ilDB->quote($a_news_id, "integer"));
738 
739  $ilAppEventHandler->raise("Services/News", "unreadNews",
740  array("user_id" => $a_user_id, "news_ids" => array($a_news_id)));
741  }
742 
751  function mergeNews($n1, $n2)
752  {
753  foreach($n2 as $id => $news)
754  {
755  $n1[$id] = $news;
756  }
757 
758  return $n1;
759  }
760 
766  static function _getDefaultVisibilityForRefId($a_ref_id)
767  {
768  global $tree, $ilSetting;
769 
770  include_once("./Services/Block/classes/class.ilBlockSetting.php");
771 
772  $news_set = new ilSetting("news");
773  $default_visibility = ($news_set->get("default_visibility") != "")
774  ? $news_set->get("default_visibility")
775  : "users";
776 
777  if ($tree->isInTree($a_ref_id))
778  {
779  $path = $tree->getPathFull($a_ref_id);
780 
781  foreach ($path as $key => $row)
782  {
783  if (!in_array($row["type"], array("root", "cat","crs", "fold", "grp", "icrs")))
784  {
785  continue;
786  }
787 
788  $visibility = ilBlockSetting::_lookup("news", "default_visibility",
789  0, $row["obj_id"]);
790 
791  if ($visibility != "")
792  {
793  $default_visibility = $visibility;
794  }
795  }
796  }
797 
798  return $default_visibility;
799  }
800 
801 
806  public function delete()
807  {
808  global $ilDB;
809 
810  // delete il_news_read entries
811  $ilDB->manipulate("DELETE FROM il_news_read ".
812  " WHERE news_id = ".$ilDB->quote($this->getId(), "integer"));
813 
814  // delete multimedia object
815  $mob = $this->getMobId();
816 
817  // delete
818  parent::delete();
819 
820  // delete mob after news, to have a "mob usage" of 0
821  if ($mob > 0 and ilObject::_exists($mob))
822  {
823  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
824  $mob = new ilObjMediaObject($mob);
825  $mob->delete();
826  }
827  }
828 
833  public function deleteNewsOfContext($a_context_obj_id,
834  $a_context_obj_type)
835  {
836  global $ilDB;
837 
838  if ($a_context_obj_id == 0 || $a_context_obj_type == "")
839  {
840  return;
841  }
842 
843  // get news records
844  $query = "SELECT * FROM il_news_item".
845  " WHERE context_obj_id = ".$ilDB->quote($a_context_obj_id, "integer").
846  " AND context_obj_type = ".$ilDB->quote($a_context_obj_type, "text");
847 
848  $news_set = $ilDB->query($query);
849 
850  while ($news = $ilDB->fetchAssoc($news_set))
851  {
852  $news_obj = new ilNewsItem($news["id"]);
853  $news_obj->delete();
854  }
855  }
856 
860  static function _lookupTitle($a_news_id)
861  {
862  global $ilDB;
863 
864  $query = "SELECT title FROM il_news_item WHERE id = ".
865  $ilDB->quote($a_news_id, "integer");
866  $set = $ilDB->query($query);
867  $rec = $ilDB->fetchAssoc($set);
868  return $rec["title"];
869  }
870 
874  static function _lookupVisibility($a_news_id)
875  {
876  global $ilDB;
877 
878  $query = "SELECT visibility FROM il_news_item WHERE id = ".
879  $ilDB->quote($a_news_id, "integer");
880  $set = $ilDB->query($query);
881  $rec = $ilDB->fetchAssoc($set);
882 
883  return $rec["visibility"];
884  }
885 
889  static function filterObjIdsPerNews($a_obj_ids, $a_time_period = 0, $a_starting_date = "")
890  {
891  global $ilDB;
892 
893  $and = "";
894  if ($a_time_period > 0)
895  {
896  $limit_ts = date('Y-m-d H:i:s', time() - ($a_time_period * 24 * 60 * 60));
897  $and = " AND creation_date >= ".$ilDB->quote($limit_ts, "timestamp")." ";
898  }
899 
900  if ($a_starting_date != "")
901  {
902  $and.= " AND creation_date >= ".$ilDB->quote($a_starting_date, "timestamp");
903  }
904 
905  $query = "SELECT DISTINCT(context_obj_id) AS obj_id FROM il_news_item".
906  " WHERE ".$ilDB->in("context_obj_id", $a_obj_ids, false, "integer")." ".$and;
907  //" WHERE context_obj_id IN (".implode(ilUtil::quoteArray($a_obj_ids),",").")".$and;
908 
909  $set = $ilDB->query($query);
910  $objs = array();
911  while($rec = $ilDB->fetchAssoc($set))
912  {
913  $objs[] = $rec["obj_id"];
914  }
915 
916  return $objs;
917  }
918 
922  static function determineNewsTitle($a_context_obj_type, $a_title, $a_content_is_lang_var,
923  $a_agg_ref_id = 0, $a_aggregation = "")
924  {
925  global $lng;
926 
927  if ($a_agg_ref_id > 0)
928  {
929  $cnt = count($a_aggregation);
930 
931  // forums
932  if ($a_context_obj_type == "frm")
933  {
934  if ($cnt > 1)
935  {
936  return sprintf($lng->txt("news_x_postings"), $cnt);
937  }
938  else
939  {
940  return $lng->txt("news_1_postings");
941  }
942  }
943  else // files
944  {
945  $up_cnt = $cr_cnt = 0;
946  foreach($a_aggregation as $item)
947  {
948  if ($item["title"] == "file_updated")
949  {
950  $up_cnt++;
951  }
952  else
953  {
954  $cr_cnt++;
955  }
956  }
957  $sep = "";
958  if ($cr_cnt == 1)
959  {
960  $tit = $lng->txt("news_1_file_created");
961  $sep = "<br />";
962  }
963  else if ($cr_cnt > 1)
964  {
965  $tit = sprintf($lng->txt("news_x_files_created"), $cr_cnt);
966  $sep = "<br />";
967  }
968  if ($up_cnt == 1)
969  {
970  $tit .= $sep.$lng->txt("news_1_file_updated");
971  }
972  else if ($up_cnt > 1)
973  {
974  $tit .= $sep.sprintf($lng->txt("news_x_files_updated"), $up_cnt);
975  }
976  return $tit;
977  }
978  }
979  else
980  {
981  if ($a_content_is_lang_var)
982  {
983  return $lng->txt($a_title);
984  }
985  else
986  {
987  return $a_title;
988  }
989  }
990 
991  return "";
992  }
993 
994 
998  static function getFirstNewsIdForContext($a_context_obj_id,
999  $a_context_obj_type, $a_context_sub_obj_id = "", $a_context_sub_obj_type = "")
1000  {
1001  global $ilDB;
1002 
1003  // Determine how many rows should be deleted
1004  $query = "SELECT * ".
1005  "FROM il_news_item ".
1006  "WHERE ".
1007  "context_obj_id = ".$ilDB->quote($a_context_obj_id, "integer").
1008  " AND context_obj_type = ".$ilDB->quote($a_context_obj_type, "text").
1009  " AND context_sub_obj_id = ".$ilDB->quote($a_context_sub_obj_id, "integer").
1010  " AND ".$ilDB->equals("context_sub_obj_type", $a_context_sub_obj_type, "text", true);
1011 
1012  $set = $ilDB->query($query);
1013  $rec = $ilDB->fetchAssoc($set);
1014 
1015  return $rec["id"];
1016  }
1017 
1021  static function _lookupMediaObjectUsages($a_mob_id)
1022  {
1023  global $ilDB;
1024 
1025  $query = "SELECT * ".
1026  "FROM il_news_item ".
1027  "WHERE ".
1028  " mob_id = ".$ilDB->quote($a_mob_id, "integer");
1029 
1030  $usages = array();
1031  $set = $ilDB->query($query);
1032  while ($rec = $ilDB->fetchAssoc($set))
1033  {
1034  $usages[$rec["id"]] = array("type" => "news", "id" => $rec["id"]);
1035  }
1036 
1037  return $usages;
1038  }
1039 
1043  static function _lookupContextObjId($a_news_id)
1044  {
1045  global $ilDB;
1046 
1047  $query = "SELECT * ".
1048  "FROM il_news_item ".
1049  "WHERE ".
1050  " id = ".$ilDB->quote($a_news_id, "integer");
1051  $set = $ilDB->query($query);
1052  $rec = $ilDB->fetchAssoc($set);
1053 
1054  return $rec["context_obj_id"];
1055  }
1056 
1058  {
1059  $news_set = new ilSetting("news");
1060  $per = $news_set->get("pd_period");
1061  if ($per == 0)
1062  {
1063  $per = 30;
1064  }
1065 
1066  return $per;
1067  }
1068 
1069  function _lookupUserPDPeriod($a_user_id)
1070  {
1071  global $ilSetting;
1072 
1073  $news_set = new ilSetting("news");
1074  $allow_shorter_periods = $news_set->get("allow_shorter_periods");
1075  $allow_longer_periods = $news_set->get("allow_longer_periods");
1076  $default_per = ilNewsItem::_lookupDefaultPDPeriod();
1077 
1078  include_once("./Services/Block/classes/class.ilBlockSetting.php");
1079  $per = ilBlockSetting::_lookup("pdnews", "news_pd_period",
1080  $a_user_id, 0);
1081 
1082  // news period information
1083  if ($per <= 0 ||
1084  (!$allow_shorter_periods && ($per < $default_per)) ||
1085  (!$allow_longer_periods && ($per > $default_per))
1086  )
1087  {
1088  $per = $default_per;
1089  }
1090 
1091  return $per;
1092  }
1093 
1094  function _lookupRSSPeriod()
1095  {
1096  $news_set = new ilSetting("news");
1097  $rss_period = $news_set->get("rss_period");
1098  if ($rss_period == 0) // default to two weeks
1099  {
1100  $rss_period = 14;
1101  }
1102  return $rss_period;
1103  }
1104  function setPrivateFeedId ($a_userId)
1105  {
1106  ilNewsItem::$privFeedId = $a_userId;
1107  }
1108 
1109  function getPrivateFeedId () {
1110 
1111  return ilNewsItem::$privFeedId;
1112  }
1113 }
1114 ?>