ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPDNewsBlockGUI.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/News/classes/class.ilNewsForContextBlockGUI.php");
5 
17 {
18  static $block_type = "pdnews";
19  static $st_data;
20  protected $acc_results = false;
21 
25  function ilPDNewsBlockGUI()
26  {
27  global $ilCtrl, $lng, $ilUser, $ilBench, $ilAccess, $ilCtrl;
28 
29  $ilBench->start("News", "ilPDNewsBlockGUI_Constructor");
30  $news_set = new ilSetting("news");
31 
33 
34  $this->setImage(ilUtil::getImagePath("icon_news_s.png"));
35 
36  $lng->loadLanguageModule("news");
37  include_once("./Services/News/classes/class.ilNewsItem.php");
38 
39  $this->setLimit(5);
40  $this->setAvailableDetailLevels(3);
41 
42  $this->dynamic = false;
43 
44  // store current access check results
45 // $this->acc_results = $ilAccess->getResults();
46 
47  // read access cache
48 // $this->acc_cache_hit = $ilAccess->readCache(
49 // ((int) $news_set->get("acc_cache_mins")) * 60);
50 
51  include_once("./Services/News/classes/class.ilNewsCache.php");
52  $this->acache = new ilNewsCache();
53  $cres = $this->acache->getEntry($ilUser->getId().":0");
54  $this->cache_hit = false;
55  if ($this->acache->getLastAccessStatus() == "hit")
56  {
57  self::$st_data = unserialize($cres);
58  $this->cache_hit = true;
59  }
60 
61  if ($this->getDynamic() && !$this->cache_hit)
62  {
63  $this->dynamic = true;
64  $data = array();
65  }
66  else if ($this->getCurrentDetailLevel() > 0)
67  {
68  // do not ask two times for the data (e.g. if user displays a
69  // single item on the personal desktop and the news block is
70  // displayed at the same time)
71  if (empty(self::$st_data))
72  {
73  self::$st_data = $this->getNewsData();
75  }
76  else
77  {
79  }
80  }
81  else
82  {
83  $data = array();
84  }
85 
86  $this->setTitle($lng->txt("news_internal_news"));
87  $this->setRowTemplate("tpl.block_row_news_for_context.html", "Services/News");
88 
89  $this->setData($data);
90 
91  $this->handleView();
92 
93  // reset access check results
94  $ilAccess->setResults($this->acc_results);
95 
96  $ilBench->stop("News", "ilPDNewsBlockGUI_Constructor");
97  }
98 
102  function getNewsData()
103  {
104  global $ilUser, $ilAccess;
105 
106  include_once("./Services/News/classes/class.ilNewsCache.php");
107  $this->acache = new ilNewsCache();
108 
109  $per = ilNewsItem::_lookupUserPDPeriod($ilUser->getId());
110  $data = ilNewsItem::_getNewsItemsOfUser($ilUser->getId(), false,
111  false, $per);
112 // if (!$this->acc_cache_hit)
113 // {
114 // $ilAccess->storeCache();
115 // }
116 
117  $this->acache->storeEntry($ilUser->getId().":0",
118  serialize($data));
119 
120  return $data;
121  }
122 
128  static function getBlockType()
129  {
130  return self::$block_type;
131  }
132 
138  static function isRepositoryObject()
139  {
140  return false;
141  }
142 
146  static function getScreenMode()
147  {
148  global $ilCtrl;
149 
150  switch($_GET["cmd"])
151  {
152  case "showNews":
153  case "showFeedUrl":
154  case "editSettings":
155  return IL_SCREEN_CENTER;
156  break;
157 
158  default:
159  return IL_SCREEN_SIDE;
160  break;
161  }
162  }
163 
167  function &executeCommand()
168  {
169  global $ilCtrl;
170 
171  $next_class = $ilCtrl->getNextClass();
172  $cmd = $ilCtrl->getCmd("getHTML");
173 
174  switch ($next_class)
175  {
176  default:
177  return $this->$cmd();
178  }
179  }
180 
184  function fillDataSection()
185  {
186  global $ilBench;
187 
188  if ($this->dynamic)
189  {
190  $this->setDataSection($this->getDynamicReload());
191  }
192  else if ($this->getCurrentDetailLevel() > 1 && count($this->getData()) > 0)
193  {
194  $ilBench->start("News", "ilPDNewsBlockGUI_fillDataSection");
196  $ilBench->stop("News", "ilPDNewsBlockGUI_fillDataSection");
197  }
198  else
199  {
200  $this->setEnableNumInfo(false);
201  if (count($this->getData()) == 0)
202  {
203  $this->setEnableDetailRow(false);
204  }
205  $this->setDataSection($this->getOverview());
206  }
207  }
208 
212  function getHTML()
213  {
214  global $ilCtrl, $lng, $ilUser;
215 
216  // set footer info
217  $this->setFooterInfo($lng->txt("news_block_information"), true);
218 
219  $news_set = new ilSetting("news");
220  $enable_internal_rss = $news_set->get("enable_rss_for_internal");
221  $allow_shorter_periods = $news_set->get("allow_shorter_periods");
222  $allow_longer_periods = $news_set->get("allow_longer_periods");
223  $enable_private_feed = $news_set->get("enable_private_feed");
224 
225  // subscribe/unsibscribe link
226  include_once("./Services/News/classes/class.ilNewsSubscription.php");
227 
228  // show feed url
229  if ($enable_internal_rss)
230  {
231  $this->addBlockCommand(
232  $ilCtrl->getLinkTarget($this, "showFeedUrl"),
233  $lng->txt("news_get_feed_url"), "", ilUtil::getImagePath("rss.png"), true);
234  }
235 
236  if ($allow_shorter_periods || $allow_longer_periods || $enable_private_feed)
237  {
238  $this->addBlockCommand(
239  $ilCtrl->getLinkTarget($this, "editSettings"),
240  $lng->txt("settings"));
241  }
242 
243  $per = ilNewsItem::_lookupUserPDPeriod($ilUser->getId());
244 
245  if ($per > 0)
246  {
247  switch ($per)
248  {
249  case 2:
250  case 3:
251  case 5: $per_str = sprintf($lng->txt("news_period_x_days"), $per); break;
252  case 7: $per_str = $lng->txt("news_period_1_week"); break;
253  case 14: $per_str = sprintf($lng->txt("news_period_x_weeks"), 2); break;
254  case 30: $per_str = $lng->txt("news_period_1_month"); break;
255  case 60: $per_str = sprintf($lng->txt("news_period_x_months"), 2); break;
256  case 120: $per_str = sprintf($lng->txt("news_period_x_months"), 4); break;
257  case 180: $per_str = sprintf($lng->txt("news_period_x_months"), 6); break;
258  case 366: $per_str = $lng->txt("news_period_1_year"); break;
259  }
260  if ($per_str != "")
261  {
262  $this->setTitle($this->getTitle().' <span style="font-weight:normal;">- '.$per_str."</span>");
263  }
264  }
265 
266  if ($this->getCurrentDetailLevel() == 0)
267  {
268  return "";
269  }
270 
271  $en = "";
272  if ($ilUser->getPref("il_feed_js") == "n")
273  {
274  $en = $this->getJSEnabler();
275  }
276 
277  return ilBlockGUI::getHTML().$en;
278  }
279 
280 
284  function showFeedUrl()
285  {
286  global $lng, $ilCtrl, $ilUser, $ilSetting;
287 
288  $news_set = new ilSetting("news");
289 
290 
291  include_once("./Services/News/classes/class.ilNewsItem.php");
292 
293  if ($news_set->get("enable_private_feed") && $ilUser->_getFeedPass($_SESSION["AccountId"]))
294  {
295  $tpl = new ilTemplate("tpl.show_priv_feed_url.html", true, true, "Services/News");
296 
297  $tpl->setVariable("IMG_PRIV_RSS", ilUtil::getImagePath("privrss.png"));
298  $tpl->setVariable("TXT_PRIV_TITLE", $lng->txt("news_get_priv_feed_title"));
299  $tpl->setVariable("TXT_PRIV_INFO", $lng->txt("news_get_priv_feed_info"));
300  $tpl->setVariable("TXT_PRIV_FEED_URL", $lng->txt("news_feed_url"));
301  $tpl->setVariable("VAL_PRIV_FEED_URL",
302  str_replace("://", "://" . $ilUser->getLogin() . ":-password-@", ILIAS_HTTP_PATH)."/privfeed.php?client_id=".rawurlencode(CLIENT_ID)."&user_id=".$ilUser->getId().
303  "&hash=".ilObjUser::_lookupFeedHash($ilUser->getId(), true));
304  $tpl->setVariable("VAL_PRIV_FEED_URL_TXT",
305  str_replace("://", "://" . $ilUser->getLogin() . ":-password-@", ILIAS_HTTP_PATH)."/privfeed.php?client_id=".rawurlencode(CLIENT_ID)."&<br />user_id=".$ilUser->getId().
306  "&hash=".ilObjUser::_lookupFeedHash($ilUser->getId(), true));
307  }
308  else
309  {
310  $tpl = new ilTemplate("tpl.show_feed_url.html", true, true, "Services/News");
311  }
312  $tpl->setVariable("TXT_TITLE", $lng->txt("news_get_feed_title"));
313  $tpl->setVariable("TXT_INFO", $lng->txt("news_get_feed_info"));
314  $tpl->setVariable("TXT_FEED_URL", $lng->txt("news_feed_url"));
315  $tpl->setVariable("VAL_FEED_URL",
316  ILIAS_HTTP_PATH."/feed.php?client_id=".rawurlencode(CLIENT_ID)."&user_id=".$ilUser->getId().
317  "&hash=".ilObjUser::_lookupFeedHash($ilUser->getId(), true));
318  $tpl->setVariable("VAL_FEED_URL_TXT",
319  ILIAS_HTTP_PATH."/feed.php?client_id=".rawurlencode(CLIENT_ID)."&<br />user_id=".$ilUser->getId().
320  "&hash=".ilObjUser::_lookupFeedHash($ilUser->getId(), true));
321 
322  include_once("./Services/PersonalDesktop/classes/class.ilPDContentBlockGUI.php");
323  $content_block = new ilPDContentBlockGUI();
324  $content_block->setContent($tpl->get());
325  $content_block->setTitle($lng->txt("news_internal_news"));
326  $content_block->setImage(ilUtil::getImagePath("icon_news.png"));
327  $content_block->addHeaderCommand($ilCtrl->getParentReturn($this),
328  $lng->txt("selected_items_back"));
329 
330  return $content_block->getHTML();
331  }
332 
333  function addCloseCommand($a_content_block)
334  {
335  global $lng, $ilCtrl;
336 
337  $a_content_block->addHeaderCommand($ilCtrl->getParentReturn($this),
338  $lng->txt("selected_items_back"));
339  }
340 
341 
345  function showNews()
346  {
347  // workaround for dynamic mode (if cache is disabled, showNews has no data)
348 // if (empty(self::$st_data))
349 // {
350 // $this->setData($this->getNewsData());
351 // }
352 
353  return parent::showNews();
354  }
355 
359  function editSettings()
360  {
361  global $ilUser, $lng, $ilCtrl, $ilSetting;
362 
363  $news_set = new ilSetting("news");
364  $enable_internal_rss = $news_set->get("enable_rss_for_internal");
365  $allow_shorter_periods = $news_set->get("allow_shorter_periods");
366  $allow_longer_periods = $news_set->get("allow_longer_periods");
367  $enable_private_feed = $news_set->get("enable_private_feed");
368 
369  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
370 
371  if ($allow_shorter_periods || $allow_longer_periods )
372  {
373  $form = new ilPropertyFormGUI();
374  include_once("./Services/News/classes/class.ilNewsItem.php");
375  $default_per = ilNewsItem::_lookupDefaultPDPeriod();
376  $per = ilNewsItem::_lookupUserPDPeriod($ilUser->getId());
377 
378  $form->setTitle($lng->txt("news_settings"));
379  $form->setTitleIcon(ilUtil::getImagePath("icon_news.png"));
380 
381  $form->setTableWidth("100%");
382 
383  $per_opts = array(
384  2 => "2 ".$lng->txt("days"),
385  3 => "3 ".$lng->txt("days"),
386  5 => "5 ".$lng->txt("days"),
387  7 => "1 ".$lng->txt("week"),
388  14 => "2 ".$lng->txt("weeks"),
389  30 => "1 ".$lng->txt("month"),
390  60 => "2 ".$lng->txt("months"),
391  120 => "4 ".$lng->txt("months"),
392  180 => "6 ".$lng->txt("months"),
393  366 => "1 ".$lng->txt("year"));
394 
395  $unset = array();
396  foreach($per_opts as $k => $opt)
397  {
398  if (!$allow_shorter_periods && ($k < $default_per)) $unset[$k] = $k;
399  if (!$allow_longer_periods && ($k > $default_per)) $unset[$k] = $k;
400  }
401  foreach($unset as $k)
402  {
403  unset($per_opts[$k]);
404  }
405 
406  $per_sel = new ilSelectInputGUI($lng->txt("news_pd_period"),
407  "news_pd_period");
408  //$per_sel->setInfo($lng->txt("news_pd_period_info"));
409  $per_sel->setOptions($per_opts);
410  $per_sel->setValue((int) $per);
411  $form->addItem($per_sel);
412 
413  //$form->addCheckboxProperty($lng->txt("news_public_feed"), "notifications_public_feed",
414  // "1", $public_feed, $lng->txt("news_public_feed_info"));
415  //if ($this->getProperty("public_notifications_option"))
416  //{
417  // $form->addCheckboxProperty($lng->txt("news_notifications_public"), "notifications_public",
418  // "1", $public, $lng->txt("news_notifications_public_info"));
419  //}
420  $form->addCommandButton("saveSettings", $lng->txt("save"));
421  $form->addCommandButton("cancelSettings", $lng->txt("cancel"));
422  $form->setFormAction($ilCtrl->getFormaction($this));
423 
424  $returnForm = $form->getHTML();
425  }
426 
427  if ($enable_private_feed)
428  {
429  $feed_form = new ilPropertyFormGUI();
430  $feed_form->setTitle($lng->txt("priv_feed_settings"));
431  $feed_form->setTitleIcon(ilUtil::getImagePath("privrss.png"));
432 
433  $feed_form->setTableWidth("100%");
434 
435  $enable_private_feed = new ilCheckboxInputGUI($lng->txt("news_enable_private_feed"), "enable_private_feed");
436 
437 
438  $retype_pass = new ilTextInputGUI($lng->txt("retype_password"), "retype_password");
439  $desired_pass = new ilTextInputGUI($lng->txt("desired_password"), "desired_password");
440 
441  // user has already valid password
442  if (ilObjUser::_getFeedPass($_SESSION[AccountId]) != false)
443  {
444  $enable_private_feed->setChecked(true);
445  $desired_pass->setValue("******");
446  }
447  $desired_pass->setInputType("password");
448 
449  $retype_pass->setInputType("password");
450 
451  $feed_form->addItem($enable_private_feed);
452  $feed_form->addItem($desired_pass);
453  $feed_form->addItem($retype_pass);
454 
455  $feed_form->addCommandButton("changeFeedSettings", $lng->txt("save"));
456  $feed_form->addCommandButton("cancelSettings", $lng->txt("cancel"));
457  $feed_form->setFormAction($ilCtrl->getFormaction($this));
458 
459  $returnForm .= ($returnForm=="")?$feed_form->getHTML():"<br>".$feed_form->getHTML();
460  }
461 
462  return $returnForm;
463  }
464 
468  function cancelSettings()
469  {
470  global $ilCtrl;
471 
472  $ilCtrl->returnToParent($this);
473  }
474 
478  function saveSettings()
479  {
480  global $ilCtrl, $ilUser;
481 
482  $news_set = new ilSetting("news");
483  $enable_internal_rss = $news_set->get("enable_rss_for_internal");
484 
485  ilBlockSetting::_write($this->getBlockType(), "news_pd_period",
486  $_POST["news_pd_period"],
487  $ilUser->getId(), $this->block_id);
488 
489  include_once("./Services/News/classes/class.ilNewsCache.php");
490  $cache = new ilNewsCache();
491  $cache->deleteEntry($ilUser->getId().":0");
492 
493  $ilCtrl->returnToParent($this);
494  }
495 
500  {
501  global $ilCtrl, $lng, $ilUser;
502 
503  // Deactivate private Feed - just delete the password
504  if (empty($_POST["enable_private_feed"]))
505  {
506  ilUtil::sendSuccess($lng->txt("priv_feed_disabled"),true);
507  $ilUser->_setFeedPass($_SESSION["AccountId"],"");
508  $ilCtrl->returnToParent($this);
509  }
510  else
511  {
512  // check old password
513  if ($_POST["desired_password"] != $_POST["retype_password"])
514  {
515  ilUtil::sendFailure($lng->txt("passwd_not_match"),true);
516  $ilCtrl->redirectByClass("ilPDNewsBlockGUI", "editSettings");
517  }
518  // validate password
519  else if (!ilUtil::isPassword($_POST["desired_password"]))
520  {
521  ilUtil::sendFailure($lng->txt("passwd_invalid"),true);
522  $ilCtrl->redirectByClass("ilPDNewsBlockGUI", "editSettings");
523  }
524  // only works for ILIAS3 passwords
525  else if (md5($_POST["desired_password"]) == $ilUser->getPasswd())
526  {
527  ilUtil::sendFailure($lng->txt("passwd_equals_ilpasswd"),true);
528  $ilCtrl->redirectByClass("ilPDNewsBlockGUI", "editSettings");
529  }
530  else if ($_POST["desired_password"] != "")
531  {
532  ilUtil::sendSuccess($lng->txt("saved_successfully"),true);
533  $ilUser->_setFeedPass($_SESSION["AccountId"],$_POST["desired_password"]);
534  $ilCtrl->returnToParent($this);
535  }
536  }
537  }
538 
539 }
540 
541 ?>