ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilNewsItem.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 define("NEWS_NOTICE", 0);
25 define("NEWS_MESSAGE", 1);
26 define("NEWS_WARNING", 2);
27 
28 include_once("./Services/News/classes/class.ilNewsItemGen.php");
29 
43 {
44 
45  private static $privFeedId = false;
46  private $limitation;
47 
53  public function __construct($a_id = 0)
54  {
55  parent::__construct($a_id);
56  $this->limitation = true;
57  }
58 
59 
65  function setLimitation($a_limitation)
66  {
67  $this->limitation = $a_limitation;
68  }
69 
75  function getLimitation()
76  {
77  return $this->limitation;
78  }
79 
83  function create()
84  {
85  global $ilDB;
86 
88 
89  $news_set = new ilSetting("news");
90  $max_items = $news_set->get("max_items");
91  if ($max_items <= 0)
92  {
93  $max_items = 50;
94  }
95 
96  // limit number of news
97  if ($this->getLimitation())
98  {
99  // Determine how many rows should be deleted
100  $query = "SELECT count(*) AS cnt ".
101  "FROM il_news_item ".
102  "WHERE ".
103  "context_obj_id = ".$ilDB->quote($this->getContextObjId()).
104  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType()).
105  " AND context_sub_obj_id = ".$ilDB->quote($this->getContextSubObjId()).
106  " AND context_sub_obj_type = ".$ilDB->quote($this->getContextSubObjType());
107 
108  $set = $ilDB->query($query);
109  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
110 
111  // if we have more records than allowed, delete them
112  if (($rec["cnt"] > $max_items) && $this->getContextObjId() > 0)
113  {
114  $query = "SELECT * ".
115  "FROM il_news_item ".
116  "WHERE ".
117  "context_obj_id = ".$ilDB->quote($this->getContextObjId()).
118  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType()).
119  " AND context_sub_obj_id = ".$ilDB->quote($this->getContextSubObjId()).
120  " AND context_sub_obj_type = ".$ilDB->quote($this->getContextSubObjType()).
121  " ORDER BY creation_date ASC".
122  " LIMIT ".($rec["cnt"] - $max_items);
123 
124  $del_set = $ilDB->query($query);
125  while ($del_item = $del_set->fetchRow(DB_FETCHMODE_ASSOC))
126  {
127  $del_news = new ilNewsItem($del_item["id"]);
128  $del_news->delete();
129  }
130  }
131  }
132  }
133 
137  static function _getNewsItemsOfUser($a_user_id, $a_only_public = false,
138  $a_prevent_aggregation = false, $a_per = 0, &$a_cnt = NULL)
139  {
140  global $ilAccess, $ilUser, $ilBench;
141 
142  $ilBench->start("News", "getNewsItemsOfUser");
143 
144  $news_item = new ilNewsItem();
145  $news_set = new ilSetting("news");
146 
147  $per = $a_per;
148 
149  include_once("./Services/News/classes/class.ilNewsSubscription.php");
150  include_once("./Services/Block/classes/class.ilBlockSetting.php");
151 
152  // this is currently not used
153  $ilBench->start("News", "getNewsItemsOfUser_getRefIds");
154  $ref_ids = ilNewsSubscription::_getSubscriptionsOfUser($a_user_id);
155 
156  if (ilObjUser::_lookupPref($a_user_id, "pd_items_news") != "n")
157  {
158  // this should be the case for all users
159  $pd_items = ilObjUser::_lookupDesktopItems($a_user_id);
160  foreach($pd_items as $item)
161  {
162  if (!in_array($item["ref_id"], $ref_ids))
163  {
164  $ref_ids[] = $item["ref_id"];
165  }
166  }
167  }
168  $ilBench->stop("News", "getNewsItemsOfUser_getRefIds");
169 
170  $data = array();
171 
172  foreach($ref_ids as $ref_id)
173  {
174  $ilBench->start("News", "getNewsForRefId");
175  if (!$a_only_public)
176  {
177  // this loop should not cost too much performance
178  $ilBench->start("News", "getAggregatedNewsData_getContexts_checkAccess");
179  $acc = $ilAccess->checkAccess("read", "", $ref_id);
180  $ilBench->stop("News", "getAggregatedNewsData_getContexts_checkAccess");
181 
182  if (!$acc)
183  {
184  continue;
185  }
186  }
187  if (ilNewsItem::getPrivateFeedId() != false) {
188  global $rbacsystem;
189  $acc = $rbacsystem->checkAccessOfUser(ilNewsItem::getPrivateFeedId(),"read", $ref_id);
190 
191  if (!$acc)
192  {
193  continue;
194  }
195  }
196 
197  $ilBench->start("News", "getNewsForRefId_getNews");
198  $obj_id = ilObject::_lookupObjId($ref_id);
199  $obj_type = ilObject::_lookupType($obj_id);
200  $news = $news_item->getNewsForRefId($ref_id, $a_only_public, false,
201  $per, $a_prevent_aggregation);
202  $ilBench->stop("News", "getNewsForRefId_getNews");
203 
204  // counter
205  if (!is_null($a_cnt))
206  {
207  $a_cnt[$ref_id] = count($news);
208  }
209 
210  $ilBench->start("News", "getNewsForRefId_mergeNews");
212  $ilBench->stop("News", "getNewsForRefId_mergeNews");
213 
214  $ilBench->stop("News", "getNewsForRefId");
215  }
216 
217  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
218 
219  $ilBench->stop("News", "getNewsItemsOfUser");
220 
221 //var_dump($data);
222  return $data;
223  }
224 
228  function getNewsForRefId($a_ref_id, $a_only_public = false, $a_stopnesting = false,
229  $a_time_period = 0, $a_prevent_aggregation = true, $a_forum_group_sequences = false)
230  {
231  $obj_id = ilObject::_lookupObjId($a_ref_id);
232  $obj_type = ilObject::_lookupType($obj_id);
233 
234  // get starting date
235  $starting_date = "";
236  if ($obj_type == "grp" || $obj_type == "crs" || $obj_type == "cat")
237  {
238  include_once("./Services/Block/classes/class.ilBlockSetting.php");
239  $hide_news_per_date = ilBlockSetting::_lookup("news", "hide_news_per_date",
240  0, $obj_id);
241  if ($hide_news_per_date)
242  {
243  $starting_date = ilBlockSetting::_lookup("news", "hide_news_date",
244  0, $obj_id);
245  }
246  }
247 
248  if ($obj_type == "cat" && !$a_stopnesting)
249  {
250  $news = $this->getAggregatedChildNewsData($a_ref_id, $a_only_public, $a_time_period,
251  $a_prevent_aggregation, $starting_date);
252  }
253  else if (($obj_type == "grp" || $obj_type == "crs") &&
254  !$a_stopnesting)
255  {
256  $news = $this->getAggregatedNewsData($a_ref_id, $a_only_public, $a_time_period,
257  $a_prevent_aggregation, $starting_date);
258  }
259  else
260  {
261  $news_item = new ilNewsItem();
262  $news_item->setContextObjId($obj_id);
263  $news_item->setContextObjType($obj_type);
264  $news = $news_item->queryNewsForContext($a_only_public, $a_time_period,
265  $starting_date);
266  $unset = array();
267  foreach ($news as $k => $v)
268  {
269  if (!$a_only_public || $v["visibility"] == NEWS_PUBLIC ||
270  ($v["priority"] == 0 &&
271  ilBlockSetting::_lookup("news", "public_notifications",
272  0, $obj_id)))
273  {
274  $news[$k]["ref_id"] = $a_ref_id;
275  }
276  else
277  {
278  $unset[] = $k;
279  }
280  }
281  foreach ($unset as $un)
282  {
283  unset($news[$un]);
284  }
285  }
286 
287  if (!$a_prevent_aggregation)
288  {
289  $news = $this->aggregateForums($news);
290  }
291  else if ($a_forum_group_sequences)
292  {
293  $news = $this->aggregateForums($news, true);
294  }
295 
296  return $news;
297  }
298 
302  function getAggregatedNewsData($a_ref_id, $a_only_public = false, $a_time_period = 0,
303  $a_prevent_aggregation = false, $a_starting_date = "")
304  {
305  global $tree, $ilAccess, $ilBench, $ilObjDataCache;
306 
307  $ilBench->start("News", "getAggregatedNewsData");
308 
309  // get news of parent object
310 
311  $data = array();
312 
313  // get subtree
314  $ilBench->start("News", "getAggregatedNewsData_getSubTree");
315  $cur_node = $tree->getNodeData($a_ref_id);
316 
317  if ($cur_node["lft"] != "") // should never be empty
318  {
319  $nodes = $tree->getSubTree($cur_node, true);
320  }
321  else
322  {
323  $nodes = array();
324  }
325 
326  // preload object data cache
327  $ref_ids = array();
328  $obj_ids = array();
329  foreach($nodes as $node)
330  {
331  $ref_ids[] = $node["child"];
332  $obj_ids[] = $node["obj_id"];
333  }
334  $ilBench->stop("News", "getAggregatedNewsData_getSubTree");
335 
336  $ilBench->start("News", "getAggregatedNewsData_preloadActivationTimes");
337  $ilObjDataCache->preloadReferenceCache($ref_ids);
338  if (!$a_only_public)
339  {
340  $ilAccess->preloadActivationTimes($ref_ids);
341  }
342  $ilBench->stop("News", "getAggregatedNewsData_preloadActivationTimes");
343 
344  // no check, for which of the objects any news are available
345  $news_obj_ids = ilNewsItem::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
346  //$news_obj_ids = $obj_ids;
347 
348  // get news for all subtree nodes
349  $ilBench->start("News", "getAggregatedNewsData_getContexts");
350  $contexts = array();
351  foreach($nodes as $node)
352  {
353  // only go on, if news are available
354  if (!in_array($node["obj_id"], $news_obj_ids))
355  {
356  continue;
357  }
358 
359  if (!$a_only_public)
360  {
361  $ilBench->start("News", "getAggregatedNewsData_getContexts_checkAccess");
362  $acc = $ilAccess->checkAccess("read", "", $node["child"]);
363  $ilBench->stop("News", "getAggregatedNewsData_getContexts_checkAccess");
364 
365  if (!$acc)
366  {
367  continue;
368  }
369  }
370 
371  $ref_id[$node["obj_id"]] = $node["child"];
372  $contexts[] = array("obj_id" => $node["obj_id"],
373  "obj_type" => $node["type"]);
374  }
375  $ilBench->stop("News", "getAggregatedNewsData_getContexts");
376 
377  // sort and return
378  $news = $this->queryNewsForMultipleContexts($contexts, $a_only_public, $a_time_period,
379  $a_starting_date);
380 
381  $ilBench->start("News", "getAggregatedNewsData_mergeAndSort");
382 
383  $to_del = array();
384  foreach ($news as $k => $v)
385  {
386  $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
387  }
388 
390  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
391 
392  if (!$a_prevent_aggregation)
393  {
394  $data = $this->aggregateFiles($data, $a_ref_id);
395  }
396 
397 //var_dump($data);
398 
399  $ilBench->stop("News", "getAggregatedNewsData_mergeAndSort");
400  $ilBench->stop("News", "getAggregatedNewsData");
401 
402  return $data;
403  }
404 
405  function aggregateForums($news, $a_group_posting_sequence = false)
406  {
407  $to_del = array();
408  $forums = array();
409 
410  // aggregate
411  foreach ($news as $k => $v)
412  {
413  if ($a_group_posting_sequence && $last_aggregation_forum > 0 &&
414  $last_aggregation_forum != $news[$k]["context_obj_id"])
415  {
416  $forums[$last_aggregation_forum] = "";
417  }
418 
419  if ($news[$k]["context_obj_type"] == "frm")
420  {
421  if ($forums[$news[$k]["context_obj_id"]] == "")
422  {
423  // $forums[forum_id] = news_id;
424  $forums[$news[$k]["context_obj_id"]] = $k;
425  $last_aggregation_forum = $news[$k]["context_obj_id"];
426  }
427  else
428  {
429  $to_del[] = $k;
430  }
431 
432  $news[$k]["no_context_title"] = true;
433 
434  // aggregate every forum into it's "k" news
435  $news[$forums[$news[$k]["context_obj_id"]]]["aggregation"][$k]
436  = $news[$k];
437  $news[$k]["agg_ref_id"]
438  = $news[$k]["ref_id"];
439  $news[$k]["content"] = "";
440  $news[$k]["content_long"] = "";
441  }
442  }
443 
444  // delete double entries
445  foreach($to_del as $k)
446  {
447  unset($news[$k]);
448  }
449 //var_dump($news[14]["aggregation"]);
450 
451 
452  return $news;
453  }
454 
455  function aggregateFiles($news, $a_ref_id)
456  {
457  $first_file = "";
458  $to_del = array();
459  foreach ($news as $k => $v)
460  {
461  // aggregate file related news
462  if ($news[$k]["context_obj_type"] == "file")
463  {
464  if ($first_file == "")
465  {
466  $first_file = $k;
467  }
468  else
469  {
470  $to_del[] = $k;
471  }
472  $news[$first_file]["aggregation"][$k] = $news[$k];
473  $news[$first_file]["agg_ref_id"] = $a_ref_id;
474  $news[$first_file]["ref_id"] = $a_ref_id;
475  }
476  }
477 
478  foreach($to_del as $v)
479  {
480  unset($news[$v]);
481  }
482 
483  return $news;
484  }
485 
486 
490  function getAggregatedChildNewsData($a_ref_id, $a_only_public = false,
491  $a_time_period = 0, $a_prevent_aggregation = false, $a_starting_date = "")
492  {
493  global $tree, $ilAccess, $ilBench;
494 
495  $ilBench->start("News", "getAggregatedChildNewsData");
496 
497  // get news of parent object
498  $data = $this->getNewsForRefId($a_ref_id, $a_only_public, true, $a_time_period);
499  foreach ($data as $k => $v)
500  {
501  $data[$k]["ref_id"] = $a_ref_id;
502  }
503 
504  // get childs
505  $nodes = $tree->getChilds($a_ref_id);
506 
507  // no check, for which of the objects any news are available
508  $obj_ids = array();
509  foreach($nodes as $node)
510  {
511  $obj_ids[] = $node["obj_id"];
512  }
513  $news_obj_ids = ilNewsItem::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
514  //$news_obj_ids = $obj_ids;
515 
516  // get news for all subtree nodes
517  $contexts = array();
518  foreach($nodes as $node)
519  {
520  // only go on, if news are available
521  if (!in_array($node["obj_id"], $news_obj_ids))
522  {
523  continue;
524  }
525 
526  if (!$a_only_public && !$ilAccess->checkAccess("read", "", $node["child"]))
527  {
528  continue;
529  }
530  $ref_id[$node["obj_id"]] = $node["child"];
531  $contexts[] = array("obj_id" => $node["obj_id"],
532  "obj_type" => $node["type"]);
533  }
534 
535  $news = $this->queryNewsForMultipleContexts($contexts, $a_only_public, $a_time_period,
536  $a_starting_date);
537  foreach ($news as $k => $v)
538  {
539  $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
540  }
542 
543  // sort and return
544  $data = ilUtil::sortArray($data, "creation_date", "desc", false, true);
545 
546  if (!$a_prevent_aggregation)
547  {
548  $data = $this->aggregateFiles($data, $a_ref_id);
549  }
550 
551  $ilBench->stop("News", "getAggregatedChildNewsData");
552 
553  return $data;
554  }
555 
559  function setContext($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "")
560  {
561  $this->setContextObjId($a_obj_id);
562  $this->setContextObjType($a_obj_type);
563  $this->setContextSubObjId($a_sub_obj_id);
564  $this->setContextSubObjType($a_sub_obj_type);
565  }
566 
571  public function queryNewsForContext($a_for_rss_use = false, $a_time_period = 0,
572  $a_starting_date = "")
573  {
574  global $ilDB, $ilUser, $lng;
575 
576  $and = ($a_time_period > 0)
577  ? " AND (TO_DAYS(now()) - TO_DAYS(creation_date)) <= ".((int)$a_time_period)
578  : "";
579 
580  if ($a_starting_date != "")
581  {
582  $and.= " AND creation_date > ".$ilDB->quote($a_starting_date)." ";
583  }
584 
585  if ($a_for_rss_use && ilNewsItem::getPrivateFeedId() == false)
586  {
587  $query = "SELECT * ".
588  "FROM il_news_item ".
589  " WHERE ".
590  "context_obj_id = ".$ilDB->quote($this->getContextObjId()).
591  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType()).
592  $and.
593  " ORDER BY creation_date DESC ";
594  }
595  elseif (ilNewsItem::getPrivateFeedId() != false)
596  {
597  $query = "SELECT il_news_item.* ".
598  ", il_news_read.user_id as user_read ".
599  "FROM il_news_item LEFT JOIN il_news_read ".
600  "ON il_news_item.id = il_news_read.news_id AND ".
601  " il_news_read.user_id = ".ilNewsItem::getPrivateFeedId().
602  " WHERE ".
603  "context_obj_id = ".$ilDB->quote($this->getContextObjId()).
604  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType()).
605  $and.
606  " ORDER BY creation_date DESC ";
607  }
608  else
609  {
610  $query = "SELECT il_news_item.* ".
611  ", il_news_read.user_id as user_read ".
612  "FROM il_news_item LEFT JOIN il_news_read ".
613  "ON il_news_item.id = il_news_read.news_id AND ".
614  " il_news_read.user_id = ".$ilDB->quote($ilUser->getId()).
615  " WHERE ".
616  "context_obj_id = ".$ilDB->quote($this->getContextObjId()).
617  " AND context_obj_type = ".$ilDB->quote($this->getContextObjType()).
618  $and.
619  " ORDER BY creation_date DESC ";
620  }
621  $set = $ilDB->query($query);
622  $result = array();
623  while($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
624  {
625  if (!$a_for_rss_use || (ilNewsItem::getPrivateFeedId() != false) || ($rec["visibility"] == NEWS_PUBLIC ||
626  ($rec["priority"] == 0 &&
627  ilBlockSetting::_lookup("news", "public_notifications",
628  0, $rec["context_obj_id"]))))
629  {
630  $result[$rec["id"]] = $rec;
631  }
632  }
633 
634  return $result;
635 
636  }
637 
643  public function queryNewsForMultipleContexts($a_contexts, $a_for_rss_use = false,
644  $a_time_period = 0, $a_starting_date = "")
645  {
646  global $ilDB, $ilUser, $ilBench, $lng, $ilCtrl;
647 
648  $ilBench->start("News", "queryNewsForMultipleContexts");
649 
650  $and = ($a_time_period > 0)
651  ? " AND (TO_DAYS(now()) - TO_DAYS(creation_date)) <= ".((int)$a_time_period)
652  : "";
653 
654  if ($a_starting_date != "")
655  {
656  $and.= " AND creation_date > ".$ilDB->quote($a_starting_date)." ";
657  }
658 
659  $ids = array();
660  $type = array();
661  foreach($a_contexts as $cont)
662  {
663  $ids[] = $cont["obj_id"];
664  $type[$cont["obj_id"]] = $cont["obj_type"];
665  }
666 
667  if ($a_for_rss_use && ilNewsItem::getPrivateFeedId() == false)
668  {
669  $query = "SELECT * ".
670  "FROM il_news_item ".
671  " WHERE ".
672  "context_obj_id IN (".implode(",",ilUtil::quoteArray($ids)).") ".
673  $and.
674  " ORDER BY creation_date DESC ";
675  }
676  elseif (ilNewsItem::getPrivateFeedId() != false)
677  {
678  $query = "SELECT il_news_item.* ".
679  ", il_news_read.user_id as user_read ".
680  "FROM il_news_item LEFT JOIN il_news_read ".
681  "ON il_news_item.id = il_news_read.news_id AND ".
682  " il_news_read.user_id = ".ilNewsItem::getPrivateFeedId().
683  " WHERE ".
684  "context_obj_id IN (".implode(",",ilUtil::quoteArray($ids)).") ".
685  $and.
686  " ORDER BY creation_date DESC ";
687  }
688  else
689  {
690  $query = "SELECT il_news_item.* ".
691  ", il_news_read.user_id as user_read ".
692  "FROM il_news_item LEFT JOIN il_news_read ".
693  "ON il_news_item.id = il_news_read.news_id AND ".
694  " il_news_read.user_id = ".$ilDB->quote($ilUser->getId()).
695  " WHERE ".
696  "context_obj_id IN (".implode(",",ilUtil::quoteArray($ids)).") ".
697  $and.
698  " ORDER BY creation_date DESC ";
699  }
700 
701  $set = $ilDB->query($query);
702  $result = array();
703  while($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
704  {
705  if ($type[$rec["context_obj_id"]] == $rec["context_obj_type"])
706  {
707  if (!$a_for_rss_use || ilNewsItem::getPrivateFeedId() != false || ($rec["visibility"] == NEWS_PUBLIC ||
708  ($rec["priority"] == 0 &&
709  ilBlockSetting::_lookup("news", "public_notifications",
710  0, $rec["context_obj_id"]))))
711  {
712  $result[$rec["id"]] = $rec;
713  }
714  }
715  }
716 
717  $ilBench->stop("News", "queryNewsForMultipleContexts");
718 
719  return $result;
720 
721  }
722 
723 
727  function _setRead($a_user_id, $a_news_id)
728  {
729  global $ilDB, $ilAppEventHandler;
730 
731  $q = "REPLACE INTO il_news_read (user_id, news_id) VALUES (".
732  $ilDB->quote($a_user_id).",".$ilDB->quote($a_news_id).")";
733  $ilDB->query($q);
734 
735  $ilAppEventHandler->raise("Services/News", "readNews",
736  array("user_id" => $a_user_id, "news_ids" => array($a_news_id)));
737  }
738 
742  function _setUnread($a_user_id, $a_news_id)
743  {
744  global $ilDB, $ilAppEventHandler;
745 
746  $q = "DELETE FROM il_news_read (user_id, news_id) VALUES (".
747  " WHERE user_id = ".$ilDB->quote($a_user_id).
748  " AND news_id = ".$ilDB->quote($a_news_id);
749  $ilDB->query($q);
750 
751  $ilAppEventHandler->raise("Services/News", "unreadNews",
752  array("user_id" => $a_user_id, "news_ids" => array($a_news_id)));
753  }
754 
763  function mergeNews($n1, $n2)
764  {
765  foreach($n2 as $id => $news)
766  {
767  $n1[$id] = $news;
768  }
769 
770  return $n1;
771  }
772 
778  static function _getDefaultVisibilityForRefId($a_ref_id)
779  {
780  global $tree, $ilSetting;
781 
782  include_once("./Services/Block/classes/class.ilBlockSetting.php");
783 
784  $news_set = new ilSetting("news");
785  $default_visibility = ($news_set->get("default_visibility") != "")
786  ? $news_set->get("default_visibility")
787  : "users";
788 
789  if ($tree->isInTree($a_ref_id))
790  {
791  $path = $tree->getPathFull($a_ref_id);
792 
793  foreach ($path as $key => $row)
794  {
795  if (!in_array($row["type"], array("root", "cat","crs", "fold", "grp", "icrs")))
796  {
797  continue;
798  }
799 
800  $visibility = ilBlockSetting::_lookup("news", "default_visibility",
801  0, $row["obj_id"]);
802 
803  if ($visibility != "")
804  {
805  $default_visibility = $visibility;
806  }
807  }
808  }
809 
810  return $default_visibility;
811  }
812 
813 
818  public function delete()
819  {
820  global $ilDB;
821 
822  // delete il_news_read entries
823  $query = "DELETE FROM il_news_read ".
824  " WHERE news_id = ".$ilDB->quote($this->getId());
825  $ilDB->query($query);
826 
827  // delete multimedia object
828  $mob = $this->getMobId();
829 
830  // delete
831  parent::delete();
832 
833  // delete mob after news, to have a "mob usage" of 0
834  if ($mob > 0 and ilObject::_exists($mob))
835  {
836  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
837  $mob = new ilObjMediaObject($mob);
838  $mob->delete();
839  }
840  }
841 
846  public function deleteNewsOfContext($a_context_obj_id,
847  $a_context_obj_type)
848  {
849  global $ilDB;
850 
851  if ($a_context_obj_id == 0 || $a_context_obj_type == "")
852  {
853  return;
854  }
855 
856  // get news records
857  $query = "SELECT * FROM il_news_item".
858  " WHERE context_obj_id = ".$ilDB->quote($a_context_obj_id).
859  " AND context_obj_type = ".$ilDB->quote($a_context_obj_type);
860 
861  $news_set = $ilDB->query($query);
862 
863  while ($news = $news_set->fetchRow(DB_FETCHMODE_ASSOC))
864  {
865  $news_obj = new ilNewsItem($news["id"]);
866  $news_obj->delete();
867  }
868  }
869 
873  static function _lookupTitle($a_news_id)
874  {
875  global $ilDB;
876 
877  $query = "SELECT title FROM il_news_item WHERE id = ".
878  $ilDB->quote($a_news_id);
879  $set = $ilDB->query($query);
880  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
881  return $rec["title"];
882  }
883 
887  static function _lookupVisibility($a_news_id)
888  {
889  global $ilDB;
890 
891  $query = "SELECT visibility FROM il_news_item WHERE id = ".
892  $ilDB->quote($a_news_id);
893  $set = $ilDB->query($query);
894  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
895 
896  return $rec["visibility"];
897  }
898 
902  static function filterObjIdsPerNews($a_obj_ids, $a_time_period = 0, $a_starting_date = "")
903  {
904  global $ilDB;
905 
906  $and = ($a_time_period > 0)
907  ? " AND (TO_DAYS(now()) - TO_DAYS(creation_date)) <= ".((int)$a_time_period)
908  : "";
909 
910  if ($a_starting_date != "")
911  {
912  $and.= " AND creation_date >= ".$ilDB->quote($a_starting_date);
913  }
914 
915  $query = "SELECT DISTINCT(context_obj_id) AS obj_id FROM il_news_item".
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 = $set->fetchRow(DB_FETCHMODE_ASSOC))
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).
1017  " AND context_obj_type = ".$ilDB->quote($a_context_obj_type).
1018  " AND context_sub_obj_id = ".$ilDB->quote($a_context_sub_obj_id).
1019  " AND context_sub_obj_type = ".$ilDB->quote($a_context_sub_obj_type);
1020 
1021  $set = $ilDB->query($query);
1022  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
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);
1038 
1039  $usages = array();
1040  $set = $ilDB->query($query);
1041  while ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
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);
1060  $set = $ilDB->query($query);
1061  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
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 ?>