ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPDNewsBlockGUI.php
Go to the documentation of this file.
1 <?php
2 
20 
28 {
29  public static string $block_type = "pdnews";
30  protected bool $cache_hit = false;
31  protected ilNewsCache $acache;
32  protected bool $dynamic = false;
33  protected bool $acc_results = false;
35 
36  public function __construct()
37  {
38  global $DIC;
39 
41 
42  $lng = $DIC->language();
43  $ilAccess = $DIC->access();
44  $this->obj_definition = $DIC["objDefinition"];
45 
46  // NOT ilNewsForContextBlockGUI::__construct() !
48 
49  $lng->loadLanguageModule("news");
50  $this->setLimit(5);
51 
52  $this->dynamic = false;
53 
54  $this->std_request = $DIC->news()
55  ->internal()
56  ->gui()
57  ->standardRequest();
58 
59  $this->acache = new ilNewsCache();
60  $cres = unserialize((string) $this->acache->getEntry($this->user->getId() . ":0"), ["allowed_classes" => false]);
61  $this->cache_hit = false;
62  if (is_array($cres) && $this->acache->getLastAccessStatus() === "hit") {
63  self::$st_data = ilNewsItem::prepareNewsDataFromCache($cres);
64  $this->cache_hit = true;
65  }
66 
67 
68  if (empty(self::$st_data)) {
69  self::$st_data = $this->getNewsData();
70  }
71  $data = self::$st_data;
72 
73  $this->setTitle($lng->txt("news_internal_news"));
74  $this->setRowTemplate("tpl.block_row_news_for_context.html", "Services/News");
75 
76  $this->setData($data);
77 
78  $this->handleView();
79 
80  // reset access check results
81  $ilAccess->setResults((array) $this->acc_results);
82 
83  $this->setPresentation(self::PRES_SEC_LIST);
84  }
85 
86  public function getNewsData(): array
87  {
88  $ilUser = $this->user;
89 
90  $this->acache = new ilNewsCache();
91 
92  $per = ilNewsItem::_lookupUserPDPeriod($ilUser->getId());
94  $ilUser->getId(),
95  false,
96  false,
97  $per
98  );
99 
100  $this->acache->storeEntry(
101  $ilUser->getId() . ":0",
102  serialize($data)
103  );
104 
105  return $data;
106  }
107 
108  public function getBlockType(): string
109  {
110  return self::$block_type;
111  }
112 
113  public static function getScreenMode(): string
114  {
115  global $DIC;
116 
117  $cmd = $DIC->ctrl()->getCmd();
118 
119  switch ($cmd) {
120  case "showNews":
121  case "showFeedUrl":
122  case "editSettings":
123  case "changeFeedSettings":
124  return IL_SCREEN_CENTER;
125 
126  default:
127  return IL_SCREEN_SIDE;
128  }
129  }
130 
131  public function executeCommand()
132  {
133  $ilCtrl = $this->ctrl;
134 
135  $next_class = $ilCtrl->getNextClass();
136  $cmd = $ilCtrl->getCmd("getHTML");
137 
138  switch ($next_class) {
139  default:
140  return $this->$cmd();
141  }
142  }
143 
144  public function getHTML(): string
145  {
146  $ilCtrl = $this->ctrl;
147  $lng = $this->lng;
148  $ilUser = $this->user;
149 
150  // @todo: find another solution for this
151  //$this->setFooterInfo($lng->txt("news_block_information"), true);
152 
153  $news_set = new ilSetting("news");
154  $enable_internal_rss = $news_set->get("enable_rss_for_internal");
155  $allow_shorter_periods = $news_set->get("allow_shorter_periods");
156  $allow_longer_periods = $news_set->get("allow_longer_periods");
157  $enable_private_feed = $news_set->get("enable_private_feed");
158  // show feed url
159  if ($enable_internal_rss) {
160  // @todo: rss icon html ilRSSButtonGUI::get(ilRSSButtonGUI::ICON_RSS)
161  $this->addBlockCommand(
162  $ilCtrl->getLinkTarget($this, "showFeedUrl"),
163  $lng->txt("news_get_feed_url")
164  );
165  }
166 
167  if ($allow_shorter_periods || $allow_longer_periods || $enable_private_feed) {
168  $this->addBlockCommand(
169  $ilCtrl->getLinkTarget($this, "editSettings"),
170  $lng->txt("settings")
171  );
172  }
173 
174  $en = "";
175  if ($ilUser->getPref("il_feed_js") === "n") {
176  $en = $this->getJSEnabler();
177  }
178 
179  return ilBlockGUI::getHTML() . $en;
180  }
181 
182 
183  public function showFeedUrl(): string
184  {
185  $lng = $this->lng;
186  $ilCtrl = $this->ctrl;
187  $ilUser = $this->user;
188 
189  $news_set = new ilSetting("news");
190 
191  if ($news_set->get("enable_private_feed")) {
192  $tpl = new ilTemplate("tpl.show_priv_feed_url.html", true, true, "Services/News");
193 
194  $tpl->setVariable("TXT_PRIV_TITLE", $lng->txt("news_get_priv_feed_title"));
195 
196  // #14365
197  if (ilObjUser::_getFeedPass($GLOBALS['DIC']['ilUser']->getId())) {
198  $tpl->setVariable("TXT_PRIV_INFO", $lng->txt("news_get_priv_feed_info"));
199  $tpl->setVariable("TXT_PRIV_FEED_URL", $lng->txt("news_feed_url"));
200  $tpl->setVariable(
201  "VAL_PRIV_FEED_URL",
202  str_replace("://", "://" . $ilUser->getLogin() . ":-password-@", ILIAS_HTTP_PATH) . "/privfeed.php?client_id=" . rawurlencode(CLIENT_ID) . "&user_id=" . $ilUser->getId() .
203  "&hash=" . ilObjUser::_lookupFeedHash($ilUser->getId(), true)
204  );
205  $tpl->setVariable(
206  "VAL_PRIV_FEED_URL_TXT",
207  str_replace("://", "://" . $ilUser->getLogin() . ":-password-@", ILIAS_HTTP_PATH) . "/privfeed.php?client_id=" . rawurlencode(CLIENT_ID) . "&<br />user_id=" . $ilUser->getId() .
208  "&hash=" . ilObjUser::_lookupFeedHash($ilUser->getId(), true)
209  );
210  } else {
211  $tpl->setVariable("TXT_PRIV_INFO", $lng->txt("news_inactive_private_feed_info"));
212  $tpl->setVariable("EDIT_SETTINGS_URL", $ilCtrl->getLinkTarget($this, "editSettings"));
213  $tpl->setVariable("EDIT_SETTINGS_TXT", $lng->txt("news_edit_news_settings"));
214  }
215  } else {
216  $tpl = new ilTemplate("tpl.show_feed_url.html", true, true, "Services/News");
217  }
218  $tpl->setVariable("TXT_TITLE", $lng->txt("news_get_feed_title"));
219  $tpl->setVariable("TXT_INFO", $lng->txt("news_get_feed_info"));
220  $tpl->setVariable("TXT_FEED_URL", $lng->txt("news_feed_url"));
221  $tpl->setVariable(
222  "VAL_FEED_URL",
223  ILIAS_HTTP_PATH . "/feed.php?client_id=" . rawurlencode(CLIENT_ID) . "&user_id=" . $ilUser->getId() .
224  "&hash=" . ilObjUser::_lookupFeedHash($ilUser->getId(), true)
225  );
226  $tpl->setVariable(
227  "VAL_FEED_URL_TXT",
228  ILIAS_HTTP_PATH . "/feed.php?client_id=" . rawurlencode(CLIENT_ID) . "&<br />user_id=" . $ilUser->getId() .
229  "&hash=" . ilObjUser::_lookupFeedHash($ilUser->getId(), true)
230  );
231 
232 
233  $panel = $this->ui->factory()->panel()->standard(
234  $lng->txt("news_internal_news"),
235  $this->ui->factory()->legacy($tpl->get())
236  );
237 
238  return $this->ui->renderer()->render($panel);
239  }
240 
241  public function editSettings(ilPropertyFormGUI $a_private_form = null): string
242  {
243  $ilUser = $this->user;
244  $lng = $this->lng;
245  $ilCtrl = $this->ctrl;
246  $returnForm = "";
247 
248  $news_set = new ilSetting("news");
249  $allow_shorter_periods = $news_set->get("allow_shorter_periods");
250  $allow_longer_periods = $news_set->get("allow_longer_periods");
251  $enable_private_feed = $news_set->get("enable_private_feed");
252 
253  if (!$a_private_form && ($allow_shorter_periods || $allow_longer_periods)) {
254  $form = new ilPropertyFormGUI();
255  $form->setFormAction($ilCtrl->getFormAction($this));
256  $form->setTitle($lng->txt("news_settings"));
257 
258  $default_per = ilNewsItem::_lookupDefaultPDPeriod();
259  $per = ilNewsItem::_lookupUserPDPeriod($ilUser->getId());
260 
261  $form->setTableWidth("100%");
262 
263  $per_opts = [
264  2 => "2 " . $lng->txt("days"),
265  3 => "3 " . $lng->txt("days"),
266  5 => "5 " . $lng->txt("days"),
267  7 => "1 " . $lng->txt("week"),
268  14 => "2 " . $lng->txt("weeks"),
269  30 => "1 " . $lng->txt("month"),
270  60 => "2 " . $lng->txt("months"),
271  120 => "4 " . $lng->txt("months"),
272  180 => "6 " . $lng->txt("months"),
273  366 => "1 " . $lng->txt("year")
274  ];
275 
276  $unset = [];
277  foreach ($per_opts as $k => $opt) {
278  if (!$allow_shorter_periods && ($k < $default_per)) {
279  $unset[$k] = $k;
280  }
281  if (!$allow_longer_periods && ($k > $default_per)) {
282  $unset[$k] = $k;
283  }
284  }
285  foreach ($unset as $k) {
286  unset($per_opts[$k]);
287  }
288 
289  $per_sel = new ilSelectInputGUI(
290  $lng->txt("news_pd_period"),
291  "news_pd_period"
292  );
293  $per_sel->setOptions($per_opts);
294  $per_sel->setValue((string) $per);
295  $form->addItem($per_sel);
296 
297  $form->addCommandButton("saveSettings", $lng->txt("save"));
298  $form->addCommandButton("cancelSettings", $lng->txt("cancel"));
299 
300  $returnForm = $form->getHTML();
301  }
302 
303  if ($enable_private_feed) {
304  if (!$a_private_form) {
305  $a_private_form = $this->initPrivateSettingsForm();
306  }
307  $returnForm .= ($returnForm === "")
308  ? $a_private_form->getHTML()
309  : "<br>" . $a_private_form->getHTML();
310  }
311 
312  return $returnForm;
313  }
314 
316  {
317  $ilCtrl = $this->ctrl;
318  $lng = $this->lng;
319  $ilUser = $this->user;
320 
321  $feed_form = new ilPropertyFormGUI();
322  $feed_form->setFormAction($ilCtrl->getFormAction($this));
323  $feed_form->setTitle($lng->txt("priv_feed_settings"));
324 
325  $feed_form->setTableWidth("100%");
326 
327  $enable_private_feed = new ilCheckboxInputGUI($lng->txt("news_enable_private_feed"), "enable_private_feed");
328  $enable_private_feed->setChecked((bool) ilObjUser::_getFeedPass($ilUser->getId()));
329  $feed_form->addItem($enable_private_feed);
330 
331  $passwd = new ilPasswordInputGUI($lng->txt("password"), "desired_password");
332  $passwd->setRequired(true);
334  $enable_private_feed->addSubItem($passwd);
335 
336  $feed_form->addCommandButton("changeFeedSettings", $lng->txt("save"));
337  $feed_form->addCommandButton("cancelSettings", $lng->txt("cancel"));
338 
339  return $feed_form;
340  }
341 
342  public function saveSettings(): string
343  {
344  $ilCtrl = $this->ctrl;
345  $ilUser = $this->user;
346 
347  $news_set = new ilSetting("news");
348 
350  $this->getBlockType(),
351  "news_pd_period",
352  $this->std_request->getDashboardPeriod(),
353  $ilUser->getId(),
354  0
355  );
356 
357  $cache = new ilNewsCache();
358  $cache->deleteEntry($ilUser->getId() . ":0");
359 
360  $ilCtrl->returnToParent($this);
361  return "";
362  }
363 
364  public function changeFeedSettings(): string
365  {
366  $ilCtrl = $this->ctrl;
367  $lng = $this->lng;
368  $ilUser = $this->user;
369 
370  $form = $this->initPrivateSettingsForm();
371  if ($form->checkInput()) {
372  // Deactivate private Feed - just delete the password
373  if (!$form->getInput("enable_private_feed")) {
374  ilObjUser::_setFeedPass($ilUser->getId(), "");
375  $this->main_tpl->setOnScreenMessage('success', $lng->txt("priv_feed_disabled"), true);
376  // $ilCtrl->returnToParent($this);
377  $ilCtrl->redirect($this, "showFeedUrl");
378  } else {
379  $passwd = $form->getInput("desired_password");
380  if (ilUserPasswordManager::getInstance()->verifyPassword($ilUser, $passwd)) {
381  $form->getItemByPostVar("desired_password")->setAlert($lng->txt("passwd_equals_ilpasswd"));
382  $this->main_tpl->setOnScreenMessage('failure', $lng->txt("form_input_not_valid"));
383  } else {
384  ilObjUser::_setFeedPass($ilUser->getId(), $passwd);
385  $this->main_tpl->setOnScreenMessage('success', $lng->txt("saved_successfully"), true);
386  $ilCtrl->redirect($this, "showFeedUrl");
387  }
388  }
389  }
390 
391  $form->setValuesByPost();
392  return $this->editSettings($form);
393  }
394 }
static _setFeedPass(int $a_user_id, string $a_password)
Set news feed password for user.
ilObjUser $user
static _lookupUserPDPeriod(int $a_user_id)
StandardGUIRequest $std_request
This class represents a selection list property in a property form.
get(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
ilLanguage $lng
handleView()
Handles show/hide notification view and removes notifications if hidden.
setLimit(int $a_limit)
static _getNewsItemsOfUser(int $a_user_id, bool $a_only_public=false, bool $a_prevent_aggregation=false, int $a_per=0, array &$a_cnt=[], bool $no_auto_generated=false, array $excluded=[], int $a_limit=0)
Get all news items for a user.
loadLanguageModule(string $a_module)
Load language module.
setOptions(array $a_options)
BlockGUI class for block NewsForContext.
editSettings(ilPropertyFormGUI $a_private_form=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
setRowTemplate(string $a_rowtemplatename, string $a_rowtemplatedir="")
Set Row Template Name.
getNextClass($a_gui_class=null)
__construct(VocabulariesInterface $vocabularies)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
ilTemplate $tpl
$GLOBALS["DIC"]
Definition: wac.php:31
static _getFeedPass(int $a_user_id)
Lookup news feed password for user.
const CLIENT_ID
Definition: constants.php:41
static _write(string $a_type, string $a_setting, string $a_value, int $a_user=0, int $a_block_id=0)
Write setting to database.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addBlockCommand(string $a_href, string $a_text, string $a_onclick="", RoundTrip $modal=null)
const IL_SCREEN_CENTER
setRequired(bool $a_required)
static getPasswordRequirementsInfo()
infotext for ilPasswordInputGUI setInfo()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupFeedHash(int $a_user_id, bool $a_create=false)
Lookup news feed hash for user.
setTitle(string $a_title)
static prepareNewsDataFromCache(array $a_cres)
Prepare news data from cache.
setData(array $a_data)
BlockGUI class for block NewsForContext.
const IL_SCREEN_SIDE
static getInstance()
Singleton method to reduce footprint (included files, created instances)
setPresentation(int $type)
static _lookupDefaultPDPeriod()