ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilForumModeratorsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 {
31  private readonly ilCtrlInterface $ctrl;
32  private readonly ilGlobalTemplateInterface $tpl;
33  private readonly ilLanguage $lng;
34  private readonly ilTabsGUI $tabs;
35  private readonly ilErrorHandling $error;
36  private readonly ilObjUser $user;
37  private readonly ilToolbarGUI $toolbar;
39  private readonly ilAccessHandler $access;
40  private readonly \ILIAS\HTTP\Wrapper\WrapperFactory $http_wrapper;
41  private readonly \ILIAS\Refinery\Factory $refinery;
42  private readonly \ILIAS\HTTP\Services $http;
43  private readonly \ILIAS\UI\Factory $ui_factory;
44  protected \ILIAS\UI\Renderer $ui_renderer;
45 
46  public function __construct(private readonly ilObjForum $forum)
47  {
49  global $DIC;
50 
51  $this->ctrl = $DIC->ctrl();
52  $this->tpl = $DIC->ui()->mainTemplate();
53  $this->lng = $DIC->language();
54  $this->access = $DIC->access();
55  $this->tabs = $DIC->tabs();
56  $this->error = $DIC['ilErr'];
57  $this->user = $DIC->user();
58  $this->toolbar = $DIC->toolbar();
59 
60  $this->tabs->activateTab('frm_moderators');
61  $this->lng->loadLanguageModule('search');
62  $this->http_wrapper = $DIC->http()->wrapper();
63  $this->http = $DIC->http();
64  $this->refinery = $DIC->refinery();
65  $this->ui_renderer = $DIC->ui()->renderer();
66  $this->ui_factory = $DIC->ui()->factory();
67 
68  if (!$this->access->checkAccess('write', '', $this->forum->getRefId())) {
69  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
70  }
71 
72  $this->oForumModerators = new ilForumModerators($this->forum->getRefId());
73  }
74 
75  public function executeCommand(): void
76  {
77  $next_class = $this->ctrl->getNextClass($this) ?? '';
78  $cmd = $this->ctrl->getCmd() ?? '';
79 
80  switch (strtolower($next_class)) {
81  case strtolower(ilRepositorySearchGUI::class):
82  $rep_search = new ilRepositorySearchGUI();
83  $rep_search->setCallback($this, 'addModerator');
84  $this->ctrl->setReturn($this, 'showModerators');
85  $this->ctrl->forwardCommand($rep_search);
86  break;
87 
88  default:
89  if (!$cmd) {
90  $cmd = 'showModerators';
91  }
92  $this->$cmd();
93  break;
94  }
95  }
96 
100  public function addModerator(array $users = []): void
101  {
102  if ($users === []) {
103  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('frm_moderators_select_one'));
104  return;
105  }
106 
107  $frm_properties = ilForumProperties::getInstance(ilObject::_lookupObjId($this->forum->getRefId()));
108  $notificaton_type = $frm_properties->getNotificationType();
109  $is_membersip_enabled_parent = $this->forum->isParentMembershipEnabledContainer();
110  $tmp_frm_noti = new ilForumNotification($this->forum->getRefId());
111 
112  foreach ($users as $usr_id) {
113  $this->oForumModerators->addModeratorRole($usr_id);
114  if ($is_membersip_enabled_parent && $notificaton_type !== NotificationType::DEFAULT) {
115  $tmp_frm_noti->setUserId($usr_id);
116  $tmp_frm_noti->setUserIdNoti($this->user->getId());
117  $tmp_frm_noti->setUserToggle($frm_properties->getUserToggleNoti());
118  $tmp_frm_noti->setAdminForce($frm_properties->getAdminForceNoti());
119  $tmp_frm_noti->insertAdminForce();
120  }
121  }
122 
123  $this->tpl->setOnScreenMessage('success', $this->lng->txt('frm_moderator_role_added_successfully'), true);
124  $this->ctrl->redirect($this, 'showModerators');
125  }
126 
127  public function detachModeratorRole(): void
128  {
129  $usr_ids = $this->http_wrapper->post()->retrieve(
130  'frm_moderators_table_usr_ids',
131  $this->refinery->byTrying([
132  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
133  $this->refinery->always([])
134  ])
135  );
136 
137  if ($usr_ids === []) {
138  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('frm_moderators_select_at_least_one'));
139  $this->ctrl->redirect($this, 'showModerators');
140  }
141 
142  $entries = $this->oForumModerators->getCurrentModerators();
143  if (count($usr_ids) === count($entries)) {
144  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('frm_at_least_one_moderator'), true);
145  $this->ctrl->redirect($this, 'showModerators');
146  }
147 
148  $frm_properties = ilForumProperties::getInstance(ilObject::_lookupObjId($this->forum->getRefId()));
149  $obj_id = $frm_properties->getObjId();
150  $notificaton_type = $frm_properties->getNotificationType();
151  $is_membersip_enabled_parent = $this->forum->isParentMembershipEnabledContainer();
152  $need_participants = $is_membersip_enabled_parent && $notificaton_type !== NotificationType::DEFAULT;
153  $tmp_frm_noti = new ilForumNotification($this->forum->getRefId());
154 
155  $participants_result = $need_participants
156  ? new \ILIAS\Data\Result\Ok(ilParticipants::getInstance($this->forum->getRefId()))
157  : new \ILIAS\Data\Result\Error("Participants not required for ref_id {$this->forum->getRefId()}");
158 
159  foreach ($usr_ids as $usr_id) {
160  $this->oForumModerators->detachModeratorRole($usr_id);
161  $participants_result->map(function (ilParticipants $participants) use ($tmp_frm_noti, $usr_id, $obj_id) {
162  if (!$participants->isAssigned($usr_id)) {
163  $tmp_frm_noti->setUserId($usr_id);
164  $tmp_frm_noti->setForumId($obj_id);
165  $tmp_frm_noti->deleteAdminForce();
166  }
167  });
168  }
169 
170  $this->tpl->setOnScreenMessage('success', $this->lng->txt('frm_moderators_detached_role_successfully'), true);
171  $this->ctrl->redirect($this, 'showModerators');
172  }
173 
174  public function showModerators(): void
175  {
177  $this,
178  $this->toolbar,
179  [
180  'auto_complete_name' => $this->lng->txt('user'),
181  'submit_name' => $this->lng->txt('add'),
182  'add_search' => true,
183  'add_from_container' => $this->oForumModerators->getRefId()
184  ]
185  );
186 
187  $tbl = new \ILIAS\Forum\Moderation\ForumModeratorsTable(
188  $this->oForumModerators,
189  $this->ctrl,
190  $this->lng,
191  $this->http,
192  $this->ui_factory
193  );
194 
195  $this->tpl->setContent($this->ui_renderer->render($tbl->getComponent()));
196  }
197 
198  private function handleModeratorActions(): void
199  {
200  $action = $this->http_wrapper->query()->retrieve(
201  'frm_moderators_table_action',
202  $this->refinery->byTrying([
203  $this->refinery->kindlyTo()->string(),
204  $this->refinery->always('')
205  ])
206  );
207  match ($action) {
208  'detachModeratorRole' => $this->detachModeratorRole(),
209  default => $this->ctrl->redirect($this, 'showModerators'),
210  };
211  }
212 
213  public function getUnsafeGetCommands(): array
214  {
215  return [
216  'handleModeratorActions',
217  ];
218  }
219 
220  public function getSafePostCommands(): array
221  {
222  return [];
223  }
224 }
readonly ilGlobalTemplateInterface $tpl
readonly ILIAS Refinery Factory $refinery
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstance(int $a_ref_id)
readonly ilToolbarGUI $toolbar
readonly ILIAS HTTP Wrapper WrapperFactory $http_wrapper
readonly ilErrorHandling $error
isAssigned(int $a_usr_id)
check if user is assigned
readonly ilCtrlInterface $ctrl
static _lookupObjId(int $ref_id)
readonly ilAccessHandler $access
static getInstance(int $a_obj_id=0)
readonly ILIAS HTTP Services $http
readonly ilForumModerators $oForumModerators
static http()
Fetches the global http state from ILIAS.
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
getSafePostCommands()
This method must return a list of safe POST commands.
global $DIC
Definition: shib_login.php:26
readonly ILIAS UI Factory $ui_factory
static fillAutoCompleteToolbar(object $parent_object, ?ilToolbarGUI $toolbar=null, array $a_options=[], bool $a_sticky=false)
array( auto_complete_name = $lng->txt(&#39;user&#39;), auto_complete_size = 15, user_type = array(ilCoursePar...
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...