From 3a0d7ad2b9998d0c7fe07296120571f4d6a9a84d Mon Sep 17 00:00:00 2001 From: Marvin Beym Date: Fri, 4 Jul 2025 09:04:52 +0200 Subject: [PATCH 1/2] Move forum statistics to own gui class --- .../Forum/classes/class.ilObjForumGUI.php | 50 +------ .../Forum/src/class.ForumStatisticsGUI.php | 123 ++++++++++++++++++ 2 files changed, 130 insertions(+), 43 deletions(-) create mode 100644 components/ILIAS/Forum/src/class.ForumStatisticsGUI.php diff --git a/components/ILIAS/Forum/classes/class.ilObjForumGUI.php b/components/ILIAS/Forum/classes/class.ilObjForumGUI.php index f673d190b9f2..7122266528c2 100755 --- a/components/ILIAS/Forum/classes/class.ilObjForumGUI.php +++ b/components/ILIAS/Forum/classes/class.ilObjForumGUI.php @@ -17,7 +17,6 @@ *********************************************************************/ declare(strict_types=1); - use ILIAS\Forum\Thread\ForumThreadTableSessionStorage; use ILIAS\UI\Factory; use ILIAS\UI\Renderer; @@ -543,7 +542,9 @@ public function executeCommand(): void $news_set_gui->setPublicNotification(true); $this->ctrl->forwardCommand($news_set_gui); break; - + case strtolower(ForumStatisticsGUI::class): + $this->ctrl->forwardCommand(new ForumStatisticsGUI($this->getRefId())); + break; default: if (in_array($cmd, ['close', 'reopen', 'make_topics_non_sticky', 'makesticky', 'editThread', 'move'])) { $cmd = 'performThreadsAction'; @@ -1669,12 +1670,12 @@ protected function getTabs(): void } if ($hasStatisticsAccess) { - $force_active = $this->ctrl->getCmd() === 'showStatistics'; + $force_active = $this->ctrl->getCmd() === ForumStatisticsGUI::CMD_SHOW; $this->tabs_gui->addTarget( self::UI_TAB_ID_STATS, - $this->ctrl->getLinkTarget($this, 'showStatistics'), - 'showStatistics', - static::class, + $this->ctrl->getLinkTargetByClass(ForumStatisticsGUI::class, ForumStatisticsGUI::CMD_SHOW), + ForumStatisticsGUI::CMD_SHOW, + ForumStatisticsGUI::class, '', $force_active ); @@ -1700,43 +1701,6 @@ protected function getTabs(): void } } - public function showStatisticsObject(): void - { - if (!$this->settings->get('enable_fora_statistics', '0')) { - $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); - } - - if (!$this->access->checkAccess('read', '', $this->object->getRefId())) { - $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); - } - - if (!$this->objProperties->isStatisticEnabled()) { - if ($this->access->checkAccess('write', '', $this->object->getRefId())) { - $this->tpl->setOnScreenMessage('info', $this->lng->txt('frm_statistics_disabled_for_participants')); - } else { - $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); - } - } - - $this->object->Forum->setForumId($this->object->getId()); - - $tbl = new \ILIAS\Forum\Statistics\ForumStatisticsTable( - $this->object, - $this->objProperties, - ilLearningProgressAccess::checkAccess($this->object->getRefId()), - $this->access->checkRbacOrPositionPermissionAccess( - 'read_learning_progress', - 'read_learning_progress', - $this->object->getRefId() - ), - $this->user, - $this->ui_factory, - $this->request, - $this->lng - ); - $this->tpl->setContent($this->uiRenderer->render($tbl->getComponent())); - } - public static function _goto($a_target, $a_thread = 0, $a_posting = 0): void { global $DIC; diff --git a/components/ILIAS/Forum/src/class.ForumStatisticsGUI.php b/components/ILIAS/Forum/src/class.ForumStatisticsGUI.php new file mode 100644 index 000000000000..4d9e3a60d59d --- /dev/null +++ b/components/ILIAS/Forum/src/class.ForumStatisticsGUI.php @@ -0,0 +1,123 @@ +object = ilObjectFactory::getInstanceByRefId($ref_id); + $this->ctrl = $ctrl ?? $DIC->ctrl(); + $this->settings = $settings ?? $DIC->settings(); + $this->access = $access ?? $DIC->access(); + $this->lng = $lng ?? $DIC->language(); + $this->obj_data_cache = $obj_data_cache ?? $DIC['ilObjDataCache']; + $this->obj_properties = ilForumProperties::getInstance($this->obj_data_cache->lookupObjId($ref_id)); + $this->error = $error ?? $DIC['ilErr']; + $this->main_tpl = $main_tpl ?? $DIC->ui()->mainTemplate(); + $this->user = $user ?? $DIC->user(); + $this->ui_factory = $ui_factory ?? $DIC->ui()->factory(); + $this->ui_renderer = $ui_renderer ?? $DIC->ui()->renderer(); + $this->request = $request ?? $DIC->http()->request(); + + } + + public function executeCommand(): void + { + $cmd = $this->ctrl->getCmd(); + if (!method_exists($this, $cmd) || !(new ReflectionMethod($this, $cmd))->isPublic()) { + $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); + } + $this->$cmd(); + } + + public function showStatistics(): void + { + if (!$this->settings->get('enable_fora_statistics', '0')) { + $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); + } + + if (!$this->access->checkAccess('read', '', $this->object->getRefId())) { + $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); + } + + if (!$this->obj_properties->isStatisticEnabled()) { + if ($this->access->checkAccess('write', '', $this->object->getRefId())) { + $this->main_tpl->setOnScreenMessage('info', $this->lng->txt('frm_statistics_disabled_for_participants')); + } else { + $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); + } + } + + $this->object->Forum->setForumId($this->object->getId()); + + $tbl = new ForumStatisticsTable( + $this->object, + $this->obj_properties, + ilLearningProgressAccess::checkAccess($this->object->getRefId()), + $this->access->checkRbacOrPositionPermissionAccess( + 'read_learning_progress', + 'read_learning_progress', + $this->object->getRefId() + ), + $this->user, + $this->ui_factory, + $this->request, + $this->lng + ); + $this->main_tpl->setContent($this->ui_renderer->render($tbl->getComponent())); + } +} From 0f154f5a836d282a6dc8f30894dbd87ae95a6ca8 Mon Sep 17 00:00:00 2001 From: Marvin Beym Date: Fri, 4 Jul 2025 11:20:56 +0200 Subject: [PATCH 2/2] Move file delivery of forum to own gui class --- .../Forum/classes/class.ilObjForumGUI.php | 104 ++++++++------ .../ILIAS/Forum/src/ForumDraftAccess.php | 31 +++++ .../ILIAS/Forum/src/ThreadBelongsToForum.php | 31 +++++ .../Forum/src/class.ForumFileDeliveryGUI.php | 127 ++++++++++++++++++ 4 files changed, 254 insertions(+), 39 deletions(-) create mode 100644 components/ILIAS/Forum/src/ForumDraftAccess.php create mode 100644 components/ILIAS/Forum/src/ThreadBelongsToForum.php create mode 100644 components/ILIAS/Forum/src/class.ForumFileDeliveryGUI.php diff --git a/components/ILIAS/Forum/classes/class.ilObjForumGUI.php b/components/ILIAS/Forum/classes/class.ilObjForumGUI.php index 7122266528c2..62f9ac2005ec 100755 --- a/components/ILIAS/Forum/classes/class.ilObjForumGUI.php +++ b/components/ILIAS/Forum/classes/class.ilObjForumGUI.php @@ -17,7 +17,9 @@ *********************************************************************/ declare(strict_types=1); +use ILIAS\Forum\ForumDraftAccess; use ILIAS\Forum\Thread\ForumThreadTableSessionStorage; +use ILIAS\Forum\ThreadBelongsToForum; use ILIAS\UI\Factory; use ILIAS\UI\Renderer; use ILIAS\UI\Component\Dropdown\Standard; @@ -545,6 +547,45 @@ public function executeCommand(): void case strtolower(ForumStatisticsGUI::class): $this->ctrl->forwardCommand(new ForumStatisticsGUI($this->getRefId())); break; + case strtolower(ForumFileDeliveryGUI::class): + $forum_draft_access_adapter = new readonly class ( + function(int|ilForumPostDraft $draft): void { + $this->checkDraftAccess($draft); + } + ) implements ForumDraftAccess { + public function __construct(private Closure $closure) + { + } + public function checkDraftAccess(int|ilForumPostDraft $draft): void + { + ($this->closure)($draft); + } + }; + + $thread_belongs_to_forum_adapter = new readonly class ( + function(int $objId, ilForumTopic $thread): void { + $this->ensureThreadBelongsToForum($objId, $thread); + } + ) implements ThreadBelongsToForum { + public function __construct(private Closure $closure) + { + } + + public function ensureThreadBelongsToForum(int $objId, ilForumTopic $thread): void + { + ($this->closure)($objId, $thread); + } + }; + + $forum_file_delivery_gui = new ForumFileDeliveryGUI( + $this->getRefId(), + $this->retrieveDraftId(), + $this->objCurrentPost, + $forum_draft_access_adapter, + $thread_belongs_to_forum_adapter, + ); + $this->ctrl->forwardCommand($forum_file_delivery_gui); + break; default: if (in_array($cmd, ['close', 'reopen', 'make_topics_non_sticky', 'makesticky', 'editThread', 'move'])) { $cmd = 'performThreadsAction'; @@ -1141,7 +1182,10 @@ private function renderDraftContent( $download_zip_button = $this->uiFactory->button() ->standard( $this->lng->txt('download'), - $this->ctrl->getLinkTarget($this, 'deliverDraftZipFile') + $this->ctrl->getLinkTargetByClass( + ForumFileDeliveryGUI::class, + ForumFileDeliveryGUI::CMD_DELIVER_DRAFT_ZIP_FILE + ) ); $this->ctrl->setParameter($this, 'draft_id', ''); $tpl->setVariable('DOWNLOAD_ZIP', $this->uiRenderer->render($download_zip_button)); @@ -1334,7 +1378,10 @@ protected function renderPostContent( $download_zip_button = $this->uiFactory->button() ->standard( $this->lng->txt('download'), - $this->ctrl->getLinkTarget($this, 'deliverZipFile') + $this->ctrl->getLinkTargetByClass( + ForumFileDeliveryGUI::class, + ForumFileDeliveryGUI::CMD_DELIVER_ZIP_FILE + ) ); $tpl->setVariable('DOWNLOAD_ZIP', $this->uiRenderer->render($download_zip_button)); } @@ -3092,15 +3139,22 @@ public function viewThreadObject(): void $oForumObjects = $this->getForumObjects(); $forumObj = $oForumObjects['forumObj']; $frm = $oForumObjects['frm']; - $file_obj = $oForumObjects['file_obj']; - $selected_draft_id = (int) ($this->httpRequest->getQueryParams()['draft_id'] ?? 0); - if (isset($this->httpRequest->getQueryParams()['file'])) { - $file_obj_for_delivery = $file_obj; - if ($selected_draft_id > 0 && ilForumPostDraft::isSavePostDraftAllowed()) { - $file_obj_for_delivery = new ilFileDataForumDrafts($forumObj->getId(), $selected_draft_id); - } - $file_obj_for_delivery->deliverFile(ilUtil::stripSlashes($this->httpRequest->getQueryParams()['file'])); + $file = $this->http->wrapper()->query()->retrieve( + 'file', + $this->refinery->byTrying([ + $this->refinery->kindlyTo()->string(), + $this->refinery->always(null) + ]) + ); + if ($file) { + $this->ctrl->setParameterByClass(ForumFileDeliveryGUI::class, 'pos_pk', $this->objCurrentPost->getId()); + $this->ctrl->setParameterByClass(ForumFileDeliveryGUI::class, 'draft_id', $this->retrieveDraftId()); + $this->ctrl->setParameterByClass(ForumFileDeliveryGUI::class, 'file', $file); + $this->ctrl->redirectByClass( + ForumFileDeliveryGUI::class, + ForumFileDeliveryGUI::CMD_DELIVER_FILE + ); } if ($this->objCurrentTopic->getId() === 0) { @@ -4749,34 +4803,6 @@ protected function setSideBlocks(): void $this->tpl->setRightContent($content); } - protected function deliverDraftZipFileObject(): void - { - if (!$this->access->checkAccess('read', '', $this->object->getRefId())) { - $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); - } - - $draft = ilForumPostDraft::newInstanceByDraftId($this->retrieveDraftId()); - $this->checkDraftAccess($draft); - $fileData = new ilFileDataForumDrafts(0, $draft->getDraftId()); - if (!$fileData->deliverZipFile()) { - $this->ctrl->redirect($this); - } - } - - protected function deliverZipFileObject(): void - { - if (!$this->access->checkAccess('read', '', $this->object->getRefId())) { - $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); - } - - $this->ensureThreadBelongsToForum($this->object->getId(), $this->objCurrentPost->getThread()); - - $fileData = new ilFileDataForum($this->object->getId(), $this->objCurrentPost->getId()); - if (!$fileData->deliverZipFile()) { - $this->ctrl->redirect($this); - } - } - protected function editThreadDraftObject(?ilPropertyFormGUI $form = null): void { if (!ilForumPostDraft::isSavePostDraftAllowed() || @@ -5646,7 +5672,7 @@ private function hasDraftAccess(int|ilForumPostDraft $draft): bool { $draft = is_int($draft) ? ilForumPostDraft::newInstanceByDraftId($draft) : $draft; return ( - $draft->getDraftId() > 1 && + $draft->getDraftId() > 0 && !$this->user->isAnonymous() && $this->access->checkAccess('add_reply', '', $this->object->getRefId()) && $this->user->getId() === $draft->getPostAuthorId() diff --git a/components/ILIAS/Forum/src/ForumDraftAccess.php b/components/ILIAS/Forum/src/ForumDraftAccess.php new file mode 100644 index 000000000000..c1664b5b462b --- /dev/null +++ b/components/ILIAS/Forum/src/ForumDraftAccess.php @@ -0,0 +1,31 @@ +draft_id = $draft_id; + $this->forum_draft_access = $forum_draft_access; + $this->forum_thread_belongs_to_forum = $forum_thread_belongs_to_forum; + $this->obj_current_post = $obj_current_post; + $this->object = ilObjectFactory::getInstanceByRefId($ref_id); + $this->ctrl = $ctrl ?? $DIC->ctrl(); + $this->access = $access ?? $DIC->access(); + $this->lng = $lng ?? $DIC->language(); + $this->error = $error ?? $DIC['ilErr']; + $this->http_wrapper = $http_wrapper ?? $DIC->http()->wrapper(); + $this->refinery = $refinery ?? $DIC->refinery(); + } + + public function executeCommand(): void + { + $cmd = $this->ctrl->getCmd(); + if (!method_exists($this, $cmd) || !(new ReflectionMethod($this, $cmd))->isPublic()) { + $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); + } + $this->$cmd(); + } + + public function deliverFile(): void + { + $file_obj_for_delivery = new ilFileDataForum($this->object->getId(), $this->obj_current_post->getId()); + if ($this->draft_id > 0 && ilForumPostDraft::isSavePostDraftAllowed()) { + $file_obj_for_delivery = new ilFileDataForumDrafts($this->object->getId(), $this->draft_id); + } + $file = $this->http_wrapper->query()->retrieve( + 'file', + $this->refinery->kindlyTo()->string() + ); + $file_obj_for_delivery->deliverFile($file); + } + + public function deliverZipFile(): void + { + if (!$this->access->checkAccess('read', '', $this->object->getRefId())) { + $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); + } + + $this->forum_thread_belongs_to_forum->ensureThreadBelongsToForum( + $this->object->getId(), + $this->obj_current_post->getThread() + ); + + $fileData = new ilFileDataForum($this->object->getId(), $this->obj_current_post->getId()); + if (!$fileData->deliverZipFile()) { + $this->ctrl->redirect($this); + } + } + + public function deliverDraftZipFile(): void + { + if (!$this->access->checkAccess('read', '', $this->object->getRefId())) { + $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE); + } + + $draft = ilForumPostDraft::newInstanceByDraftId($this->draft_id); + $this->forum_draft_access->checkDraftAccess($draft); + $fileData = new ilFileDataForumDrafts(0, $draft->getDraftId()); + if (!$fileData->deliverZipFile()) { + $this->ctrl->redirectByClass(ilObjForumGUI::class); + } + } +}