ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilForumModeratorsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 {
33  private ilLanguage $lng;
34  private ilTabsGUI $tabs;
36  private ilObjUser $user;
40  private \ILIAS\HTTP\Wrapper\WrapperFactory $http_wrapper;
41  private \ILIAS\Refinery\Factory $refinery;
42 
43  public function __construct(private readonly ilObjForum $forum)
44  {
46  global $DIC;
47 
48  $this->ctrl = $DIC->ctrl();
49  $this->tpl = $DIC->ui()->mainTemplate();
50  $this->lng = $DIC->language();
51  $this->access = $DIC->access();
52  $this->tabs = $DIC->tabs();
53  $this->error = $DIC['ilErr'];
54  $this->user = $DIC->user();
55  $this->toolbar = $DIC->toolbar();
56 
57  $this->tabs->activateTab('frm_moderators');
58  $this->lng->loadLanguageModule('search');
59  $this->http_wrapper = $DIC->http()->wrapper();
60  $this->refinery = $DIC->refinery();
61 
62  if (!$this->access->checkAccess('write', '', $this->forum->getRefId())) {
63  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
64  }
65 
66  $this->oForumModerators = new ilForumModerators($this->forum->getRefId());
67  }
68 
69  public function executeCommand(): void
70  {
71  $next_class = $this->ctrl->getNextClass($this);
72  $cmd = $this->ctrl->getCmd();
73 
74  switch (strtolower($next_class)) {
75  case strtolower(ilRepositorySearchGUI::class):
76  $rep_search = new ilRepositorySearchGUI();
77  $rep_search->setCallback($this, 'addModerator');
78  $this->ctrl->setReturn($this, 'showModerators');
79  $this->ctrl->forwardCommand($rep_search);
80  break;
81 
82  default:
83  if (!$cmd) {
84  $cmd = 'showModerators';
85  }
86  $this->$cmd();
87  break;
88  }
89  }
90 
94  public function addModerator(array $users = []): void
95  {
96  if ($users === []) {
97  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('frm_moderators_select_one'));
98  return;
99  }
100 
101  $frm_properties = ilForumProperties::getInstance(ilObject::_lookupObjId($this->forum->getRefId()));
102  $notificaton_type = $frm_properties->getNotificationType();
103  $is_membersip_enabled_parent = $this->forum->isParentMembershipEnabledContainer();
104  $tmp_frm_noti = new ilForumNotification($this->forum->getRefId());
105 
106  foreach ($users as $usr_id) {
107  $this->oForumModerators->addModeratorRole($usr_id);
108  if ($is_membersip_enabled_parent && $notificaton_type !== NotificationType::DEFAULT) {
109  $tmp_frm_noti->setUserId($usr_id);
110  $tmp_frm_noti->setUserIdNoti($this->user->getId());
111  $tmp_frm_noti->setUserToggle($frm_properties->getUserToggleNoti());
112  $tmp_frm_noti->setAdminForce($frm_properties->getAdminForceNoti());
113  $tmp_frm_noti->insertAdminForce();
114  }
115  }
116 
117  $this->tpl->setOnScreenMessage('success', $this->lng->txt('frm_moderator_role_added_successfully'), true);
118  $this->ctrl->redirect($this, 'showModerators');
119  }
120 
121  public function detachModeratorRole(): void
122  {
123  $usr_ids = $this->http_wrapper->post()->retrieve(
124  'usr_id',
125  $this->refinery->byTrying([
126  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
127  $this->refinery->always([])
128  ])
129  );
130 
131  if ($usr_ids === []) {
132  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('frm_moderators_select_at_least_one'));
133  $this->ctrl->redirect($this, 'showModerators');
134  }
135 
136  $entries = $this->oForumModerators->getCurrentModerators();
137  if (count($usr_ids) === count($entries)) {
138  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('frm_at_least_one_moderator'));
139  $this->ctrl->redirect($this, 'showModerators');
140  }
141 
142  $frm_properties = ilForumProperties::getInstance(ilObject::_lookupObjId($this->forum->getRefId()));
143  $obj_id = $frm_properties->getObjId();
144  $notificaton_type = $frm_properties->getNotificationType();
145  $is_membersip_enabled_parent = $this->forum->isParentMembershipEnabledContainer();
146  $need_participants = $is_membersip_enabled_parent && $notificaton_type !== NotificationType::DEFAULT;
147  $tmp_frm_noti = new ilForumNotification($this->forum->getRefId());
148 
149  $participants_result = $need_participants
150  ? new \ILIAS\Data\Result\Ok($this->forum->parentParticipants())
151  : new \ILIAS\Data\Result\Error("Participants not required for ref_id {$this->forum->getRefId()}");
152 
153  foreach ($usr_ids as $usr_id) {
154  $this->oForumModerators->detachModeratorRole($usr_id);
155  $participants_result->map(function (ilParticipants $participants) use ($tmp_frm_noti, $usr_id, $obj_id) {
156  if (!$participants->isAssigned($usr_id)) {
157  $tmp_frm_noti->setUserId($usr_id);
158  $tmp_frm_noti->setForumId($obj_id);
159  $tmp_frm_noti->deleteAdminForce();
160  }
161  });
162  }
163 
164  $this->tpl->setOnScreenMessage('success', $this->lng->txt('frm_moderators_detached_role_successfully'), true);
165  $this->ctrl->redirect($this, 'showModerators');
166  }
167 
168  public function showModerators(): void
169  {
171  $this,
172  $this->toolbar,
173  [
174  'auto_complete_name' => $this->lng->txt('user'),
175  'submit_name' => $this->lng->txt('add'),
176  'add_search' => true,
177  'add_from_container' => $this->oForumModerators->getRefId()
178  ]
179  );
180 
181  $tbl = new ilForumModeratorsTableGUI($this, 'showModerators', $this->forum->getRefId());
182 
183  $entries = $this->oForumModerators->getCurrentModerators();
184  $num = count($entries);
185  $result = [];
186  $i = 0;
187  foreach ($entries as $usr_id) {
189  $user = ilObjectFactory::getInstanceByObjId($usr_id, false);
190  if (!($user instanceof ilObjUser)) {
191  $this->oForumModerators->detachModeratorRole($usr_id);
192  continue;
193  }
194 
195  if ($num > 1) {
196  $result[$i]['check'] = ilLegacyFormElementsUtil::formCheckbox(false, 'usr_id[]', (string) $user->getId());
197  } else {
198  $result[$i]['check'] = '';
199  }
200  $result[$i]['login'] = $user->getLogin();
201  $result[$i]['firstname'] = $user->getFirstname();
202  $result[$i]['lastname'] = $user->getLastname();
203  ++$i;
204  }
205 
206  $tbl->setData($result);
207  $this->tpl->setContent($tbl->getHTML());
208  }
209 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider .
isAssigned(int $a_usr_id)
check if user is assigned
Class ilForumNotification.
static _lookupObjId(int $ref_id)
Class ilForumModeratorsTableGUI.
static getInstance(int $a_obj_id=0)
global $DIC
Definition: feed.php:28
ilGlobalTemplateInterface $tpl
__construct(VocabulariesInterface $vocabularies)
Class ilForumModeratorsGUI.
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...
static formCheckbox(bool $checked, string $varname, string $value, bool $disabled=false)
ILIAS HTTP Wrapper WrapperFactory $http_wrapper
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Base class for course and group participants.
Error Handling & global info handling.
Class ilObjForum.
Class ilForumModerators.
ILIAS Refinery Factory $refinery