ILIAS  Release_4_4_x_branch Revision 61816
 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  $cmd = $_GET["cmd"];
151  if($cmd == "post" && is_array($_POST["cmd"]))
152  {
153  $cmd = array_pop(array_keys($_POST["cmd"]));
154  }
155 
156  switch($cmd)
157  {
158  case "showNews":
159  case "showFeedUrl":
160  case "editSettings":
161  case "changeFeedSettings":
162  return IL_SCREEN_CENTER;
163 
164  default:
165  return IL_SCREEN_SIDE;
166  }
167  }
168 
172  function &executeCommand()
173  {
174  global $ilCtrl;
175 
176  $next_class = $ilCtrl->getNextClass();
177  $cmd = $ilCtrl->getCmd("getHTML");
178 
179  switch ($next_class)
180  {
181  default:
182  return $this->$cmd();
183  }
184  }
185 
189  function fillDataSection()
190  {
191  global $ilBench;
192 
193  if ($this->dynamic)
194  {
195  $this->setDataSection($this->getDynamicReload());
196  }
197  else if ($this->getCurrentDetailLevel() > 1 && count($this->getData()) > 0)
198  {
199  $ilBench->start("News", "ilPDNewsBlockGUI_fillDataSection");
201  $ilBench->stop("News", "ilPDNewsBlockGUI_fillDataSection");
202  }
203  else
204  {
205  $this->setEnableNumInfo(false);
206  if (count($this->getData()) == 0)
207  {
208  $this->setEnableDetailRow(false);
209  }
210  $this->setDataSection($this->getOverview());
211  }
212  }
213 
217  function getHTML()
218  {
219  global $ilCtrl, $lng, $ilUser;
220 
221  // set footer info
222  $this->setFooterInfo($lng->txt("news_block_information"), true);
223 
224  $news_set = new ilSetting("news");
225  $enable_internal_rss = $news_set->get("enable_rss_for_internal");
226  $allow_shorter_periods = $news_set->get("allow_shorter_periods");
227  $allow_longer_periods = $news_set->get("allow_longer_periods");
228  $enable_private_feed = $news_set->get("enable_private_feed");
229 
230  // subscribe/unsibscribe link
231  include_once("./Services/News/classes/class.ilNewsSubscription.php");
232 
233  // show feed url
234  if ($enable_internal_rss)
235  {
236  $this->addBlockCommand(
237  $ilCtrl->getLinkTarget($this, "showFeedUrl"),
238  $lng->txt("news_get_feed_url"), "", ilUtil::getImagePath("rss.png"), true);
239  }
240 
241  if ($allow_shorter_periods || $allow_longer_periods || $enable_private_feed)
242  {
243  $this->addBlockCommand(
244  $ilCtrl->getLinkTarget($this, "editSettings"),
245  $lng->txt("settings"));
246  }
247 
248  $per = ilNewsItem::_lookupUserPDPeriod($ilUser->getId());
249 
250  if ($per > 0)
251  {
252  switch ($per)
253  {
254  case 2:
255  case 3:
256  case 5: $per_str = sprintf($lng->txt("news_period_x_days"), $per); break;
257  case 7: $per_str = $lng->txt("news_period_1_week"); break;
258  case 14: $per_str = sprintf($lng->txt("news_period_x_weeks"), 2); break;
259  case 30: $per_str = $lng->txt("news_period_1_month"); break;
260  case 60: $per_str = sprintf($lng->txt("news_period_x_months"), 2); break;
261  case 120: $per_str = sprintf($lng->txt("news_period_x_months"), 4); break;
262  case 180: $per_str = sprintf($lng->txt("news_period_x_months"), 6); break;
263  case 366: $per_str = $lng->txt("news_period_1_year"); break;
264  }
265  if ($per_str != "")
266  {
267  $this->setTitle($this->getTitle().' <span style="font-weight:normal;">- '.$per_str."</span>");
268  }
269  }
270 
271  if ($this->getCurrentDetailLevel() == 0)
272  {
273  return "";
274  }
275 
276  $en = "";
277  if ($ilUser->getPref("il_feed_js") == "n")
278  {
279  $en = $this->getJSEnabler();
280  }
281 
282  return ilBlockGUI::getHTML().$en;
283  }
284 
285 
289  function showFeedUrl()
290  {
291  global $lng, $ilCtrl, $ilUser, $ilSetting;
292 
293  $news_set = new ilSetting("news");
294 
295 
296  include_once("./Services/News/classes/class.ilNewsItem.php");
297 
298  if ($news_set->get("enable_private_feed"))
299  {
300  $tpl = new ilTemplate("tpl.show_priv_feed_url.html", true, true, "Services/News");
301 
302  // $tpl->setVariable("IMG_PRIV_RSS", ilUtil::getImagePath("privrss.png"));
303  $tpl->setVariable("TXT_PRIV_TITLE", $lng->txt("news_get_priv_feed_title"));
304 
305  // #14365
306  if($ilUser->_getFeedPass($_SESSION["AccountId"]))
307  {
308  $tpl->setVariable("TXT_PRIV_INFO", $lng->txt("news_get_priv_feed_info"));
309  $tpl->setVariable("TXT_PRIV_FEED_URL", $lng->txt("news_feed_url"));
310  $tpl->setVariable("VAL_PRIV_FEED_URL",
311  str_replace("://", "://" . $ilUser->getLogin() . ":-password-@", ILIAS_HTTP_PATH)."/privfeed.php?client_id=".rawurlencode(CLIENT_ID)."&user_id=".$ilUser->getId().
312  "&hash=".ilObjUser::_lookupFeedHash($ilUser->getId(), true));
313  $tpl->setVariable("VAL_PRIV_FEED_URL_TXT",
314  str_replace("://", "://" . $ilUser->getLogin() . ":-password-@", ILIAS_HTTP_PATH)."/privfeed.php?client_id=".rawurlencode(CLIENT_ID)."&<br />user_id=".$ilUser->getId().
315  "&hash=".ilObjUser::_lookupFeedHash($ilUser->getId(), true));
316  }
317  else
318  {
319  $tpl->setVariable("TXT_PRIV_INFO", $lng->txt("news_inactive_private_feed_info"));
320  $tpl->setVariable("EDIT_SETTINGS_URL", $ilCtrl->getLinkTarget($this, "editSettings"));
321  $tpl->setVariable("EDIT_SETTINGS_TXT", $lng->txt("news_edit_news_settings"));
322  }
323  }
324  else
325  {
326  $tpl = new ilTemplate("tpl.show_feed_url.html", true, true, "Services/News");
327  }
328  $tpl->setVariable("TXT_TITLE", $lng->txt("news_get_feed_title"));
329  $tpl->setVariable("TXT_INFO", $lng->txt("news_get_feed_info"));
330  $tpl->setVariable("TXT_FEED_URL", $lng->txt("news_feed_url"));
331  $tpl->setVariable("VAL_FEED_URL",
332  ILIAS_HTTP_PATH."/feed.php?client_id=".rawurlencode(CLIENT_ID)."&user_id=".$ilUser->getId().
333  "&hash=".ilObjUser::_lookupFeedHash($ilUser->getId(), true));
334  $tpl->setVariable("VAL_FEED_URL_TXT",
335  ILIAS_HTTP_PATH."/feed.php?client_id=".rawurlencode(CLIENT_ID)."&<br />user_id=".$ilUser->getId().
336  "&hash=".ilObjUser::_lookupFeedHash($ilUser->getId(), true));
337 
338  include_once("./Services/PersonalDesktop/classes/class.ilPDContentBlockGUI.php");
339  $content_block = new ilPDContentBlockGUI();
340  $content_block->setContent($tpl->get());
341  $content_block->setTitle($lng->txt("news_internal_news"));
342  $content_block->setImage(ilUtil::getImagePath("icon_news.png"));
343  $content_block->addHeaderCommand($ilCtrl->getParentReturn($this),
344  $lng->txt("selected_items_back"));
345 
346  return $content_block->getHTML();
347  }
348 
349  function addCloseCommand($a_content_block)
350  {
351  global $lng, $ilCtrl;
352 
353  $a_content_block->addHeaderCommand($ilCtrl->getParentReturn($this),
354  $lng->txt("selected_items_back"));
355  }
356 
357 
361  function showNews()
362  {
363  // workaround for dynamic mode (if cache is disabled, showNews has no data)
364 // if (empty(self::$st_data))
365 // {
366 // $this->setData($this->getNewsData());
367 // }
368 
369  return parent::showNews();
370  }
371 
375  function editSettings(ilPropertyFormGUI $a_private_form = null)
376  {
377  global $ilUser, $lng, $ilCtrl, $ilSetting;
378 
379  $news_set = new ilSetting("news");
380  $enable_internal_rss = $news_set->get("enable_rss_for_internal");
381  $allow_shorter_periods = $news_set->get("allow_shorter_periods");
382  $allow_longer_periods = $news_set->get("allow_longer_periods");
383  $enable_private_feed = $news_set->get("enable_private_feed");
384 
385  if (!$a_private_form && ($allow_shorter_periods || $allow_longer_periods))
386  {
387  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
388  $form = new ilPropertyFormGUI();
389  $form->setFormAction($ilCtrl->getFormaction($this));
390  $form->setTitle($lng->txt("news_settings"));
391  $form->setTitleIcon(ilUtil::getImagePath("icon_news.png"));
392 
393  include_once("./Services/News/classes/class.ilNewsItem.php");
394  $default_per = ilNewsItem::_lookupDefaultPDPeriod();
395  $per = ilNewsItem::_lookupUserPDPeriod($ilUser->getId());
396 
397  $form->setTableWidth("100%");
398 
399  $per_opts = array(
400  2 => "2 ".$lng->txt("days"),
401  3 => "3 ".$lng->txt("days"),
402  5 => "5 ".$lng->txt("days"),
403  7 => "1 ".$lng->txt("week"),
404  14 => "2 ".$lng->txt("weeks"),
405  30 => "1 ".$lng->txt("month"),
406  60 => "2 ".$lng->txt("months"),
407  120 => "4 ".$lng->txt("months"),
408  180 => "6 ".$lng->txt("months"),
409  366 => "1 ".$lng->txt("year"));
410 
411  $unset = array();
412  foreach($per_opts as $k => $opt)
413  {
414  if (!$allow_shorter_periods && ($k < $default_per)) $unset[$k] = $k;
415  if (!$allow_longer_periods && ($k > $default_per)) $unset[$k] = $k;
416  }
417  foreach($unset as $k)
418  {
419  unset($per_opts[$k]);
420  }
421 
422  $per_sel = new ilSelectInputGUI($lng->txt("news_pd_period"),
423  "news_pd_period");
424  //$per_sel->setInfo($lng->txt("news_pd_period_info"));
425  $per_sel->setOptions($per_opts);
426  $per_sel->setValue((int) $per);
427  $form->addItem($per_sel);
428 
429  //$form->addCheckboxProperty($lng->txt("news_public_feed"), "notifications_public_feed",
430  // "1", $public_feed, $lng->txt("news_public_feed_info"));
431  //if ($this->getProperty("public_notifications_option"))
432  //{
433  // $form->addCheckboxProperty($lng->txt("news_notifications_public"), "notifications_public",
434  // "1", $public, $lng->txt("news_notifications_public_info"));
435  //}
436  $form->addCommandButton("saveSettings", $lng->txt("save"));
437  $form->addCommandButton("cancelSettings", $lng->txt("cancel"));
438 
439  $returnForm = $form->getHTML();
440  }
441 
442  if ($enable_private_feed)
443  {
444  if(!$a_private_form)
445  {
446  $a_private_form = $this->initPrivateSettingsForm();
447  }
448  $returnForm .= ($returnForm=="")
449  ? $a_private_form->getHTML()
450  : "<br>".$a_private_form->getHTML();
451  }
452 
453  return $returnForm;
454  }
455 
456  protected function initPrivateSettingsForm()
457  {
458  global $ilCtrl, $lng, $ilUser;
459 
460  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
461  $feed_form = new ilPropertyFormGUI();
462  $feed_form->setFormAction($ilCtrl->getFormaction($this));
463  $feed_form->setTitle($lng->txt("priv_feed_settings"));
464  $feed_form->setTitleIcon(ilUtil::getImagePath("privrss.png"));
465 
466  $feed_form->setTableWidth("100%");
467 
468  $enable_private_feed = new ilCheckboxInputGUI($lng->txt("news_enable_private_feed"), "enable_private_feed");
469  $enable_private_feed->setChecked($ilUser->_getFeedPass($ilUser->getId()));
470  $feed_form->addItem($enable_private_feed);
471 
472  $passwd = new ilPasswordInputGUI($lng->txt("password"), "desired_password");
473  $passwd->setRequired(true);
474  $passwd->setInfo(ilUtil::getPasswordRequirementsInfo());
475  $enable_private_feed->addSubItem($passwd);
476 
477  $feed_form->addCommandButton("changeFeedSettings", $lng->txt("save"));
478  $feed_form->addCommandButton("cancelSettings", $lng->txt("cancel"));
479 
480  return $feed_form;
481  }
482 
486  function cancelSettings()
487  {
488  global $ilCtrl;
489 
490  $ilCtrl->returnToParent($this);
491  }
492 
496  function saveSettings()
497  {
498  global $ilCtrl, $ilUser;
499 
500  $news_set = new ilSetting("news");
501  $enable_internal_rss = $news_set->get("enable_rss_for_internal");
502 
503  ilBlockSetting::_write($this->getBlockType(), "news_pd_period",
504  $_POST["news_pd_period"],
505  $ilUser->getId(), $this->block_id);
506 
507  include_once("./Services/News/classes/class.ilNewsCache.php");
508  $cache = new ilNewsCache();
509  $cache->deleteEntry($ilUser->getId().":0");
510 
511  $ilCtrl->returnToParent($this);
512  }
513 
518  {
519  global $ilCtrl, $lng, $ilUser;
520 
521  $form = $this->initPrivateSettingsForm();
522  if($form->checkInput())
523  {
524  // Deactivate private Feed - just delete the password
525  if (!$form->getInput("enable_private_feed"))
526  {
527  $ilUser->_setFeedPass($ilUser->getId(), "");
528  ilUtil::sendSuccess($lng->txt("priv_feed_disabled"),true);
529  // $ilCtrl->returnToParent($this);
530  $ilCtrl->redirect($this, "showFeedUrl");
531  }
532  else
533  {
534  $passwd = $form->getInput("desired_password");
535  if (md5($passwd) == $ilUser->getPasswd())
536  {
537  $form->getItemByPostVar("desired_password")->setAlert($lng->txt("passwd_equals_ilpasswd"));
538  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
539  }
540  else
541  {
542  $ilUser->_setFeedPass($ilUser->getId(), $passwd);
543  ilUtil::sendSuccess($lng->txt("saved_successfully"),true);
544  // $ilCtrl->returnToParent($this);
545  $ilCtrl->redirect($this, "showFeedUrl");
546  }
547  }
548  }
549 
550  $form->setValuesByPost();
551  return $this->editSettings($form);
552  }
553 
554 }
555 
556 ?>