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