ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilForumTopicTableGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 {
30  private ilForum $mapper;
31  private bool $is_moderator = false;
32  private int $ref_id = 0;
35  private int $position = 1;
36  private readonly bool $is_post_draft_allowed;
38  private readonly ilObjUser $user;
39  private readonly ilSetting $settings;
40 
41  public function __construct(
42  ilObjForumGUI $a_parent_obj,
43  string $a_parent_cmd,
44  int $ref_id,
45  ForumDto $topicData,
46  bool $is_moderator = false
47  ) {
48  global $DIC;
49 
50  $this->lng = $DIC->language();
51  $this->mainTemplate = $DIC->ui()->mainTemplate();
52  $this->user = $DIC->user();
53  $this->settings = $DIC->settings();
54 
55  $this->parent_cmd = $a_parent_cmd;
56  $this->setIsModerator($is_moderator);
57  $this->setRefId($ref_id);
58  $this->setTopicData($topicData);
59 
60  $id = 'frm_tt_' . substr(md5($this->parent_cmd), 0, 3) . '_' . $this->getRefId();
61  $this->setId($id);
62 
63  $this->setDefaultOrderDirection('DESC');
64  $this->setDefaultOrderField('lp_date');
65  $this->setExternalSorting(true);
66  $this->setExternalSegmentation(true);
67 
68  parent::__construct($a_parent_obj, $a_parent_cmd);
69  $this->mainTemplate->addCss('./assets/css/forum_table.css');
70  $this->is_post_draft_allowed = ilForumPostDraft::isSavePostDraftAllowed();
71  }
72 
73  public function init(): void
74  {
75  if ($this->parent_cmd === 'mergeThreads') {
76  $this->initMergeThreadsTable();
77  } else {
78  $this->initTopicsOverviewTable();
79  }
80  }
81 
82  public function initTopicsOverviewTable(): void
83  {
84  if ($this->parent_cmd === "showThreads") {
85  $this->setSelectAllCheckbox('thread_ids');
86  $this->addColumn('', 'check', '1px', true);
87  } else {
88  $this->addColumn('', 'check', '10px', true);
89  }
90 
91  $this->addColumn($this->lng->txt('forums_thread'), 'thr_subject');
92  $this->addColumn($this->lng->txt('forums_created_by'), '');
93  $this->addColumn($this->lng->txt('forums_articles'), 'num_posts');
94  $this->addColumn($this->lng->txt('visits'), 'num_visit');
95 
96  if ($this->is_post_draft_allowed) {
97  $this->addColumn($this->lng->txt('drafts', ''));
98  }
99 
100  $this->addColumn($this->lng->txt('forums_last_post'), 'post_date');
101  if ('showThreads' === $this->parent_cmd && $this->parent_obj->objProperties->isIsThreadRatingEnabled()) {
102  $this->addColumn($this->lng->txt('frm_rating'), 'rating');
103  }
104 
105  $this->setFormAction($this->ctrl->getFormAction($this->getParentObject(), 'showThreads'));
106  $this->setRowTemplate('tpl.forums_threads_table.html', 'components/ILIAS/Forum');
107 
108  $this->addMultiCommand('', $this->lng->txt('please_choose'));
109  if ($this->settings->get('forum_notification') > 0 && !$this->user->isAnonymous()) {
110  $this->addMultiCommand('enable_notifications', $this->lng->txt('forums_enable_notification'));
111  $this->addMultiCommand('disable_notifications', $this->lng->txt('forums_disable_notification'));
112  }
113  if ($this->getIsModerator()) {
114  $this->addMultiCommand('makesticky', $this->lng->txt('make_topics_sticky'));
115  $this->addMultiCommand('unmakesticky', $this->lng->txt('make_topics_non_sticky'));
116  $this->addMultiCommand('editThread', $this->lng->txt('frm_edit_title'));
117  $this->addMultiCommand('close', $this->lng->txt('close_topics'));
118  $this->addMultiCommand('reopen', $this->lng->txt('reopen_topics'));
119  $this->addMultiCommand('move', $this->lng->txt('move_thread_to_forum'));
120  }
121  $this->addMultiCommand('html', $this->lng->txt('export_html'));
122  if ($this->getIsModerator()) {
123  $this->addMultiCommand('confirmDeleteThreads', $this->lng->txt('delete'));
124  $this->addMultiCommand('mergeThreads', $this->lng->txt('merge_posts_into_thread'));
125  }
126  $this->setShowRowsSelector(true);
127  $this->setRowSelectorLabel($this->lng->txt('number_of_threads'));
128  }
129 
130  public function initMergeThreadsTable(): void
131  {
132  $this->addColumn('', 'check', '1px', true);
133  $this->addColumn($this->lng->txt('forums_thread'), 'th_title');
134  $this->addColumn($this->lng->txt('forums_created_by'), 'author');
135  $this->addColumn($this->lng->txt('forums_articles'), 'num_posts');
136  $this->addColumn($this->lng->txt('visits'), 'num_visit');
137  if ($this->is_post_draft_allowed) {
138  $this->addColumn($this->lng->txt('drafts', ''));
139  }
140  $this->addColumn($this->lng->txt('forums_last_post'), 'lp_date');
141 
142  $this->disable('sort');
143  $this->setFormAction($this->ctrl->getFormAction($this->getParentObject(), 'confirmMergeThreads'));
144  $this->setRowTemplate('tpl.forums_threads_table.html', 'components/ILIAS/Forum');
145 
146  $this->mainTemplate->setOnScreenMessage('info', $this->lng->txt('please_choose_target'));
147 
148  $this->setTitle(sprintf($this->lng->txt('frm_selected_merge_src'), $this->getSelectedThread()->getSubject()));
149 
150  $this->addCommandButton('confirmMergeThreads', $this->lng->txt('merge'));
151  $this->addCommandButton('showThreads', $this->lng->txt('cancel'));
152  $this->setShowRowsSelector(true);
153  $this->setRowSelectorLabel($this->lng->txt('number_of_threads'));
154  }
155 
156  protected function fillRow(array $a_set): void
157  {
159  $thread = $a_set['thread'];
160 
161  $this->ctrl->setParameter($this->getParentObject(), 'thr_pk', $thread->getId());
162  global $DIC;
163  $thread_ids = [];
164  if ($DIC->http()->wrapper()->post()->has('thread_ids')) {
165  $thread_ids = $DIC->http()->wrapper()->post()->retrieve(
166  'thread_ids',
167  $DIC->refinery()->kindlyTo()->listOf($DIC->refinery()->kindlyTo()->int())
168  );
169  }
170 
171  if ('mergeThreads' === $this->parent_cmd) {
172  $checked = $this->max_count === 1 || (isset($thread_ids) && in_array($thread->getId(), $thread_ids, true));
173  $this->tpl->setVariable(
174  'VAL_CHECK',
176  $checked,
177  'thread_ids[]',
178  (string) $thread->getId()
179  )
180  );
181  } else {
182  $this->tpl->setVariable(
183  'VAL_CHECK',
185  (isset($thread_ids) && in_array($thread->getId(), $thread_ids, true)),
186  'thread_ids[]',
187  (string) $thread->getId()
188  )
189  );
190 
191  if ($this->parent_obj->objProperties->isIsThreadRatingEnabled()) {
192  $rating = new ilRatingGUI();
193  $rating->setObject(
194  $this->parent_obj->getObject()->getId(),
195  $this->parent_obj->getObject()->getType(),
196  $thread->getId(),
197  'thread'
198  );
199  $rating->setUserId($this->user->getId());
200  $this->tpl->setVariable('VAL_RATING', $rating->getHTML());
201  }
202  }
203 
204  $subject = '';
205  if ($thread->isSticky()) {
206  $subject .= '<span class="light">[' . $this->lng->txt('sticky') . ']</span> ';
207  }
208  if ($thread->isClosed()) {
209  $subject .= '<span class="light">[' . $this->lng->txt('topic_close') . ']</span> ';
210  }
211 
212  if (!$this->user->isAnonymous() &&
213  (int) $this->settings->get('forum_notification', '0') !== 0 &&
214  $thread->isUserNotificationEnabled()
215  ) {
216  $subject .= '<span class="light">[' . $this->lng->txt('forums_notification_enabled') . ']</span> ';
217  }
218 
219  $num_posts = $thread->getNumPosts();
220  $num_unread = $thread->getNumUnreadPosts();
221 
222  $this->ctrl->setParameter($this->getParentObject(), 'page', 0);
223  $subject = $thread->getSubject();
224 
225  $this->ctrl->setParameter($this->getParentObject(), 'page', null);
226  $this->tpl->setVariable('VAL_SUBJECT', $subject);
227 
228  $this->ctrl->setParameter(
229  $this->getParentObject(),
230  'backurl',
231  urlencode($this->ctrl->getLinkTargetByClass("ilrepositorygui", ""))
232  );
233  $this->ctrl->setParameter($this->getParentObject(), 'user', $thread->getDisplayUserId());
234 
235  $authorinfo = new ilForumAuthorInformation(
236  $thread->getThrAuthorId(),
237  $thread->getDisplayUserId(),
238  (string) $thread->getUserAlias(),
239  (string) $thread->getImportName(),
240  [
241  'class' => 'il_ItemProperty',
242  'href' => $this->ctrl->getLinkTarget($this->getParentObject(), 'showUser')
243  ]
244  );
245  $this->tpl->setVariable('VAL_AUTHOR', $authorinfo->getLinkedAuthorName());
246 
247  $topicStats = $num_posts;
248  if (!$this->user->isAnonymous()) {
249  if ($num_unread > 0) {
250  $topicStats .= '<br /><span class="ilAlert ilWhiteSpaceNowrap">' . $this->lng->txt('unread') . ': ' . $num_unread . '</span>';
251  }
252  }
253 
254  $this->tpl->setVariable('VAL_ARTICLE_STATS', $topicStats);
255  $this->tpl->setVariable('VAL_NUM_VISIT', $thread->getVisits());
256  if ($this->is_post_draft_allowed) {
257  $draft_statistics = ilForumPostDraft::getDraftsStatisticsByRefId($this->getRefId());
258  $this->tpl->setVariable(
259  'VAL_DRAFTS',
260  (int) isset($draft_statistics[$thread->getId()]) !== 0 ? $draft_statistics[$thread->getId()] : 0
261  );
262  }
263 
264  if ($num_posts > 0 && $thread->getLastPostForThreadOverview() instanceof ilForumPost) {
265  $objLastPost = $thread->getLastPostForThreadOverview();
266 
267  $this->ctrl->setParameter($this->getParentObject(), 'user', $objLastPost->getDisplayUserId());
268  $authorinfo = new ilForumAuthorInformation(
269  $objLastPost->getPosAuthorId(),
270  $objLastPost->getDisplayUserId(),
271  (string) $objLastPost->getUserAlias(),
272  (string) $objLastPost->getImportName(),
273  [
274  'href' => $this->ctrl->getLinkTarget($this->getParentObject(), 'showUser')
275  ]
276  );
277 
278  $this->tpl->setVariable(
279  'VAL_LP_DATE',
280  '<div class="ilWhiteSpaceNowrap">' . ilDatePresentation::formatDate(new ilDateTime(
281  $objLastPost->getCreateDate(),
283  )) . '</div>' .
284  '<div class="ilWhiteSpaceNowrap">' . $this->lng->txt('from') . ' ' . $authorinfo->getLinkedAuthorName() . '</div>'
285  );
286  }
287 
289  if ($thread->isSticky()) {
290  $css_row = $css_row === 'tblrow1' ? 'tblstickyrow1' : 'tblstickyrow2';
291  }
292  $this->tpl->setVariable('CSS_ROW', $css_row);
293 
294  $this->ctrl->setParameter($this->getParentObject(), 'thr_pk', '');
295  $this->ctrl->setParameter($this->getParentObject(), 'user', '');
296  $this->ctrl->setParameter($this->getParentObject(), 'backurl', '');
297  }
298 
299  public function fetchData(): ilForumTopicTableGUI
300  {
301  $this->determineOffsetAndOrder();
302 
303  $excluded_ids = [];
304  if ($this->parent_cmd === 'mergeThreads' &&
305  $this->getSelectedThread() instanceof ilForumTopic) {
306  $excluded_ids[] = $this->getSelectedThread()->getId();
307  }
308 
309  $params = [
310  'is_moderator' => $this->getIsModerator(),
311  'excluded_ids' => $excluded_ids,
312  'order_column' => $this->getOrderField(),
313  'order_direction' => $this->getOrderDirection()
314  ];
315 
316  $data = $this->getMapper()->getAllThreads(
317  $this->topicData->getTopPk(),
318  $params,
319  $this->getLimit(),
320  $this->getOffset()
321  );
322  if ($data['items'] === [] && $this->getOffset() > 0) {
323  $this->resetOffset();
324  $data = $this->getMapper()->getAllThreads(
325  $this->topicData->getTopPk(),
326  $params,
327  $this->getLimit(),
328  $this->getOffset()
329  );
330  }
331 
332  $this->setMaxCount($data['cnt']);
333  $this->setData(array_map(static fn(ilForumTopic $thread): array => ['thread' => $thread], $data['items']));
334 
335  $thread_ids = [];
336  $user_ids = [];
337  foreach ($data['items'] as $thread) {
339  $thread_ids[] = $thread->getId();
340  if ($thread->getDisplayUserId() > 0) {
341  $user_ids[$thread->getDisplayUserId()] = $thread->getDisplayUserId();
342  }
343  }
344 
345  $user_ids = array_merge(
347  $user_ids
348  );
349 
350  ilForumAuthorInformationCache::preloadUserObjects(array_unique($user_ids));
351 
352  return $this;
353  }
354 
355  public function setMapper(ilForum $mapper): self
356  {
357  $this->mapper = $mapper;
358  return $this;
359  }
360 
361  public function getMapper(): ilForum
362  {
363  return $this->mapper;
364  }
365 
366  public function setRefId(int $ref_id): self
367  {
368  $this->ref_id = $ref_id;
369  return $this;
370  }
371 
372  public function getRefId(): int
373  {
374  return $this->ref_id;
375  }
376 
377  public function setIsModerator(bool $is_moderator): self
378  {
379  $this->is_moderator = $is_moderator;
380  return $this;
381  }
382 
383  public function getIsModerator(): bool
384  {
385  return $this->is_moderator;
386  }
387 
388  public function setTopicData(ForumDto $topicData): self
389  {
390  $this->topicData = $topicData;
391  return $this;
392  }
393 
394  public function getTopicData(): ForumDto
395  {
396  return $this->topicData;
397  }
398 
399  public function setSelectedThread(ilForumTopic $thread_obj): self
400  {
401  $this->merge_thread_obj = $thread_obj;
402  return $this;
403  }
404 
405  public function getSelectedThread(): ?ilForumTopic
406  {
408  }
409 }
setData(array $a_data)
const IL_CAL_DATETIME
setFormAction(string $a_form_action, bool $a_multipart=false)
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
addCommandButton(string $a_cmd, string $a_text, string $a_onclick='', string $a_id="", string $a_class="")
static getUserIdsOfLastPostsByRefIdAndThreadIds(int $ref_id, array $thread_ids)
setSelectAllCheckbox(string $a_select_all_checkbox, bool $a_select_all_on_top=false)
setIsModerator(bool $is_moderator)
__construct(ilObjForumGUI $a_parent_obj, string $a_parent_cmd, int $ref_id, ForumDto $topicData, bool $is_moderator=false)
setId(string $a_val)
setSelectedThread(ilForumTopic $thread_obj)
Class ilForumTopicTableGUI.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
resetOffset(bool $a_in_determination=false)
setExternalSorting(bool $a_val)
setShowRowsSelector(bool $a_value)
Toggle rows-per-page selector.
fillRow(array $a_set)
Standard Version of Fill Row.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setDefaultOrderField(string $a_defaultorderfield)
static getDraftsStatisticsByRefId(int $ref_id)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
global $DIC
Definition: shib_login.php:26
setDefaultOrderDirection(string $a_defaultorderdirection)
static formCheckbox(bool $checked, string $varname, string $value, bool $disabled=false)
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
readonly ilGlobalTemplateInterface $mainTemplate
setRowSelectorLabel(string $row_selector_label)
__construct(Container $dic, ilPlugin $plugin)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
disable(string $a_module_name)
setTopicData(ForumDto $topicData)
static formRadioButton(bool $checked, string $varname, string $value, ?string $onclick=null, bool $disabled=false)
addMultiCommand(string $a_cmd, string $a_text)
determineOffsetAndOrder(bool $a_omit_offset=false)
setMaxCount(int $a_max_count)
set max.
mainTemplate()
setExternalSegmentation(bool $a_val)