ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
class.ilPDNewsBlockGUI.php
Go to the documentation of this file.
1<?php
2
21
29{
30 public static string $block_type = "pdnews";
31
32 public function __construct()
33 {
34 $this->prevent_initial_loading = true; // prevents loading in the parent constructor!
36
37 $this->dynamic = false;
38 $this->setLimit(5);
39 $this->setTitle($this->lng->txt("news_internal_news"));
40 $this->setRowTemplate("tpl.block_row_news_for_context.html", "components/ILIAS/News");
41
42 $this->loadNewsData();
43 $this->handleView();
44 $this->setPresentation(self::PRES_SEC_LIST);
45 }
46
47 private function loadNewsData(): void
48 {
49 $news_period = ilNewsItem::_lookupUserPDPeriod($this->user->getId());
50 $collection = $this->domain->collection()->getNewsForUser(
51 $this->user,
52 new NewsCriteria(period: $news_period, only_public: false),
53 true
54 );
55 $this->initData($collection->groupFiles()->groupForums(false));
56 }
57
58 public function getBlockType(): string
59 {
60 return self::$block_type;
61 }
62
63 public static function getScreenMode(): string
64 {
65 global $DIC;
66
67 return match($DIC->ctrl()->getCmd()) {
68 'showNews', 'showFeedUrl', 'editSettings', 'saveSettings', 'changeFeedSettings' => IL_SCREEN_CENTER,
69 default => IL_SCREEN_SIDE
70 };
71 }
72
73 public function executeCommand()
74 {
75 $cmd = $this->ctrl->getCmd();
76 return $this->$cmd();
77 }
78
79 public function getHTML(): string
80 {
81 $ilCtrl = $this->ctrl;
83 $ilUser = $this->user;
84
85 // @todo: find another solution for this
86 //$this->setFooterInfo($lng->txt("news_block_information"), true);
87
88 $news_set = new ilSetting("news");
89 $enable_internal_rss = $news_set->get("enable_rss_for_internal");
90 $allow_shorter_periods = $news_set->get("allow_shorter_periods");
91 $allow_longer_periods = $news_set->get("allow_longer_periods");
92 $enable_private_feed = $news_set->get("enable_private_feed");
93 // show feed url
94 if ($enable_internal_rss) {
95 // @todo: rss icon html ilRSSButtonGUI::get(ilRSSButtonGUI::ICON_RSS)
96 $this->addBlockCommand(
97 $ilCtrl->getLinkTarget($this, "showFeedUrl"),
98 $lng->txt("news_get_feed_url")
99 );
100 }
101
102 if ($allow_shorter_periods || $allow_longer_periods || $enable_private_feed) {
103 $this->addBlockCommand(
104 $ilCtrl->getLinkTarget($this, "editSettings"),
105 $lng->txt("settings")
106 );
107 }
108
109 $en = "";
110 if ($ilUser->getPref("il_feed_js") === "n") {
111 $en = $this->getJSEnabler();
112 }
113
114 return ilBlockGUI::getHTML() . $en;
115 }
116
117
118 public function showFeedUrl(): string
119 {
121 $ilCtrl = $this->ctrl;
122 $ilUser = $this->user;
123
124 $news_set = new ilSetting("news");
125
126 if ($news_set->get("enable_private_feed")) {
127 $tpl = new ilTemplate("tpl.show_priv_feed_url.html", true, true, "components/ILIAS/News");
128
129 $tpl->setVariable("TXT_PRIV_TITLE", $lng->txt("news_get_priv_feed_title"));
130
131 // #14365
132 if (ilObjUser::_getFeedPass($GLOBALS['DIC']['ilUser']->getId())) {
133 $tpl->setVariable("TXT_PRIV_INFO", $lng->txt("news_get_priv_feed_info"));
134 $tpl->setVariable("TXT_PRIV_FEED_URL", $lng->txt("news_feed_url"));
136 "VAL_PRIV_FEED_URL",
137 str_replace("://", "://" . $ilUser->getLogin() . ":-password-@", ILIAS_HTTP_PATH) . "/privfeed.php?client_id=" . rawurlencode(CLIENT_ID) . "&user_id=" . $ilUser->getId() .
138 "&hash=" . ilObjUser::_lookupFeedHash($ilUser->getId(), true)
139 );
141 "VAL_PRIV_FEED_URL_TXT",
142 str_replace("://", "://" . $ilUser->getLogin() . ":-password-@", ILIAS_HTTP_PATH) . "/privfeed.php?client_id=" . rawurlencode(CLIENT_ID) . "&<br />user_id=" . $ilUser->getId() .
143 "&hash=" . ilObjUser::_lookupFeedHash($ilUser->getId(), true)
144 );
145 } else {
146 $tpl->setVariable("TXT_PRIV_INFO", $lng->txt("news_inactive_private_feed_info"));
147 $tpl->setVariable("EDIT_SETTINGS_URL", $ilCtrl->getLinkTarget($this, "editSettings"));
148 $tpl->setVariable("EDIT_SETTINGS_TXT", $lng->txt("news_edit_news_settings"));
149 }
150 } else {
151 $tpl = new ilTemplate("tpl.show_feed_url.html", true, true, "components/ILIAS/News");
152 }
153 $tpl->setVariable("TXT_TITLE", $lng->txt("news_get_feed_title"));
154 $tpl->setVariable("TXT_INFO", $lng->txt("news_get_feed_info"));
155 $tpl->setVariable("TXT_FEED_URL", $lng->txt("news_feed_url"));
157 "VAL_FEED_URL",
158 ILIAS_HTTP_PATH . "/feed.php?client_id=" . rawurlencode(CLIENT_ID) . "&user_id=" . $ilUser->getId() .
159 "&hash=" . ilObjUser::_lookupFeedHash($ilUser->getId(), true)
160 );
162 "VAL_FEED_URL_TXT",
163 ILIAS_HTTP_PATH . "/feed.php?client_id=" . rawurlencode(CLIENT_ID) . "&<br />user_id=" . $ilUser->getId() .
164 "&hash=" . ilObjUser::_lookupFeedHash($ilUser->getId(), true)
165 );
166
167
168 $panel = $this->ui->factory()->panel()->standard(
169 $lng->txt("news_internal_news"),
170 $this->ui->factory()->legacy()->content($tpl->get())
171 );
172
173 return $this->ui->renderer()->render($panel);
174 }
175
176 public function editSettings(?ilPropertyFormGUI $a_private_form = null): string
177 {
178 $ilUser = $this->user;
180 $ilCtrl = $this->ctrl;
181 $returnForm = "";
182
183 $news_set = new ilSetting("news");
184 $allow_shorter_periods = $news_set->get("allow_shorter_periods");
185 $allow_longer_periods = $news_set->get("allow_longer_periods");
186 $enable_private_feed = $news_set->get("enable_private_feed");
187
188 if (!$a_private_form && ($allow_shorter_periods || $allow_longer_periods)) {
189 $form = new ilPropertyFormGUI();
190 $form->setFormAction($ilCtrl->getFormAction($this));
191 $form->setTitle($lng->txt("news_settings"));
192
193 $default_per = ilNewsItem::_lookupDefaultPDPeriod();
194 $per = ilNewsItem::_lookupUserPDPeriod($ilUser->getId());
195
196 $form->setTableWidth("100%");
197
198 $per_opts = [
199 2 => "2 " . $lng->txt("days"),
200 3 => "3 " . $lng->txt("days"),
201 5 => "5 " . $lng->txt("days"),
202 7 => "1 " . $lng->txt("week"),
203 14 => "2 " . $lng->txt("weeks"),
204 30 => "1 " . $lng->txt("month"),
205 60 => "2 " . $lng->txt("months"),
206 120 => "4 " . $lng->txt("months"),
207 180 => "6 " . $lng->txt("months"),
208 366 => "1 " . $lng->txt("year")
209 ];
210
211 $unset = [];
212 foreach ($per_opts as $k => $opt) {
213 if (!$allow_shorter_periods && ($k < $default_per)) {
214 $unset[$k] = $k;
215 }
216 if (!$allow_longer_periods && ($k > $default_per)) {
217 $unset[$k] = $k;
218 }
219 }
220 foreach ($unset as $k) {
221 unset($per_opts[$k]);
222 }
223
224 $per_sel = new ilSelectInputGUI(
225 $lng->txt("news_pd_period"),
226 "news_pd_period"
227 );
228 $per_sel->setOptions($per_opts);
229 $per_sel->setValue((string) $per);
230 $form->addItem($per_sel);
231
232 $form->addCommandButton("saveSettings", $lng->txt("save"));
233 $form->addCommandButton("cancelSettings", $lng->txt("cancel"));
234
235 $returnForm = $form->getHTML();
236 }
237
238 if ($enable_private_feed) {
239 if (!$a_private_form) {
240 $a_private_form = $this->initPrivateSettingsForm();
241 }
242 $returnForm .= ($returnForm === "")
243 ? $a_private_form->getHTML()
244 : "<br>" . $a_private_form->getHTML();
245 }
246
247 return $returnForm;
248 }
249
251 {
252 $ilCtrl = $this->ctrl;
254 $ilUser = $this->user;
255
256 $feed_form = new ilPropertyFormGUI();
257 $feed_form->setFormAction($ilCtrl->getFormAction($this));
258 $feed_form->setTitle($lng->txt("priv_feed_settings"));
259
260 $feed_form->setTableWidth("100%");
261
262 $enable_private_feed = new ilCheckboxInputGUI($lng->txt("news_enable_private_feed"), "enable_private_feed");
263 $enable_private_feed->setChecked((bool) ilObjUser::_getFeedPass($ilUser->getId()));
264 $feed_form->addItem($enable_private_feed);
265
266 $passwd = new ilPasswordInputGUI($lng->txt("password"), "desired_password");
267 $passwd->setRequired(true);
269 $enable_private_feed->addSubItem($passwd);
270
271 $feed_form->addCommandButton("changeFeedSettings", $lng->txt("save"));
272 $feed_form->addCommandButton("cancelSettings", $lng->txt("cancel"));
273
274 return $feed_form;
275 }
276
277 public function saveSettings(): string
278 {
279 $ilCtrl = $this->ctrl;
280 $ilUser = $this->user;
281
282 $news_set = new ilSetting("news");
283
285 $this->getBlockType(),
286 "news_pd_period",
287 $this->std_request->getDashboardPeriod(),
288 $ilUser->getId(),
289 0
290 );
291
292 $this->domain->collection()->invalidateCache($this->user->getId());
293
294 $ilCtrl->returnToParent($this);
295 return "";
296 }
297
298 public function changeFeedSettings(): string
299 {
300 $ilCtrl = $this->ctrl;
302 $ilUser = $this->user;
303
304 $form = $this->initPrivateSettingsForm();
305 if ($form->checkInput()) {
306 // Deactivate private Feed - just delete the password
307 if (!$form->getInput("enable_private_feed")) {
308 $ilUser->setFeedPass("");
309 $this->main_tpl->setOnScreenMessage('success', $lng->txt("priv_feed_disabled"), true);
310 // $ilCtrl->returnToParent($this);
311 $ilCtrl->redirect($this, "showFeedUrl");
312 } else {
313 $passwd = $form->getInput("desired_password");
314 if (LocalUserPasswordManager::getInstance()->verifyPassword($ilUser, $passwd)) {
315 $form->getItemByPostVar("desired_password")->setAlert($lng->txt("passwd_equals_ilpasswd"));
316 $this->main_tpl->setOnScreenMessage('failure', $lng->txt("form_input_not_valid"));
317 } else {
318 $ilUser->setFeedPass($passwd);
319 $this->main_tpl->setOnScreenMessage('success', $lng->txt("saved_successfully"), true);
320 $ilCtrl->redirect($this, "showFeedUrl");
321 }
322 }
323 }
324
325 $form->setValuesByPost();
326 return $this->editSettings($form);
327 }
328}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
News Criteria DTO for querying news items supports caching, JSON serialization, and validation.
const IL_SCREEN_SIDE
const IL_SCREEN_CENTER
ilLanguage $lng
setTitle(string $a_title)
setPresentation(int $type)
ilTemplate $tpl
addBlockCommand(string $a_href, string $a_text, string $a_onclick="", ?RoundTrip $modal=null)
ilObjUser $user
setLimit(int $a_limit)
setRowTemplate(string $a_rowtemplatename, string $a_rowtemplatedir="")
Set Row Template Name.
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 class represents a checkbox property in a property form.
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...
BlockGUI class for block NewsForContext.
handleView()
Handles show/hide notification view and removes notifications if hidden.
initData(NewsCollection $collection)
static _lookupUserPDPeriod(int $a_user_id)
static _lookupDefaultPDPeriod()
static _lookupFeedHash(int $a_user_id, bool $a_create=false)
static _getFeedPass(int $a_user_id)
BlockGUI class for block NewsForContext.
editSettings(?ilPropertyFormGUI $a_private_form=null)
This class represents a password property in a property form.
This class represents a property form user interface.
static getPasswordRequirementsInfo()
infotext for ilPasswordInputGUI setInfo()
This class represents a selection list property in a property form.
ILIAS Setting Class.
special template class to simplify handling of ITX/PEAR
get(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
Returns a block with all replacements done.
const CLIENT_ID
Definition: constants.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54