ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCourseParticipantsGroupsTableGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Data\Factory as ilDataFactory;
22use ILIAS\DI\UIServices as ilUIServices;
23use ILIAS\HTTP\Services as ilHTTPServices;
24use ILIAS\Refinery\Factory as ilRefineryFactory;
25use ILIAS\UI\Component\Table\Data as ilDataTable;
28use ILIAS\UI\URLBuilderToken as ilURLBuilderToken;
29use JetBrains\PhpStorm\NoReturn;
31
33{
34 public const string TABLE_COL_NAME = 'name';
35 public const string TABLE_COL_LOGIN = 'login';
36 public const string TABLE_COL_GROUP_NUMBER = 'groups_number';
37 public const string TABLE_COL_GROUPS = 'groups';
38 public const string TABLE_FILTER_NAME = 'filter_name';
39 public const string TABLE_FILTER_GROUPS = 'filter_groups';
40 protected const string ALL_OBJECTS = 'ALL_OBJECTS';
41 protected const string TABLE_ID = 'crsprtcpntsgrpstbl';
42 protected const string FILTER_ID = 'crsprtcpntsgrpstbl_filter';
43 protected const string ROW_ID = 'row_ids';
44 protected const string TABLE_ACTION_ID = 'table_action';
45 protected const string TABLE_ACTION_ADD_TO_GROUP = 'add_to_group';
46 protected const string LNG_TABLE_COL_NAME = 'name';
47 protected const string LNG_TABLE_COL_LOGIN = 'login';
48 protected const string LNG_TABLE_COL_GROUP_NUMBER = 'crs_groups_nr';
49 protected const string LNG_TABLE_COL_GROUPS = 'groups';
50 protected const string LNG_TABLE_ACTION_CONFIRM_UNSUBSCRIBE = 'grp_unsubscribe';
51 protected const string LNG_TABLE_TITLE = 'crs_grp_assignments';
52 protected const string LNG_ADD_TO_GROUP = 'crs_add_to_group';
53
55 protected ilURLBuilderToken $action_parameter_token;
56 protected ilURLBuilderToken $row_id_token;
57 protected ilDataTable $table;
58 protected ilFilter $filter;
59
60 public function __construct(
61 protected ilCourseParticipantsGroupsTableDataRetrieval $data_retrieval,
62 protected ilUIServices $ui_services,
63 protected ilUIService $ui_service,
64 protected ilHTTPServices $http_services,
65 protected ilRefineryFactory $refinery,
66 protected ilLanguage $lng,
67 protected ilCtrl $ctrl,
68 protected ilDataFactory $data_factory,
69 protected ilGlobalTemplateInterface $tpl,
70 protected ilAccess $access,
71 protected ilObjectDataCache $object_data_cache
72 ) {
73 $this->lng->loadLanguageModule('grp');
74 }
75
76 protected function buildAddToGroupString(int $group_ref_id): string
77 {
78 return self::TABLE_ACTION_ADD_TO_GROUP . '_' . $group_ref_id;
79 }
80
81 protected function getColumns(): array
82 {
83 return [
84 self::TABLE_COL_NAME => $this->ui_services->factory()->table()->column()->text(
85 $this->lng->txt(self::LNG_TABLE_COL_NAME)
86 ),
87 self::TABLE_COL_LOGIN => $this->ui_services->factory()->table()->column()->text(
88 $this->lng->txt(self::LNG_TABLE_COL_LOGIN)
89 ),
90 self::TABLE_COL_GROUP_NUMBER => $this->ui_services->factory()->table()->column()->number(
91 $this->lng->txt(self::LNG_TABLE_COL_GROUP_NUMBER)
92 ),
93 self::TABLE_COL_GROUPS => $this->ui_services->factory()->table()->column()->text(
94 $this->lng->txt(self::LNG_TABLE_COL_GROUPS)
95 )->withIsSortable(false)
96 ];
97 }
98
99 protected function getActions(): array
100 {
101 $this->url_builder = new URLBuilder($this->data_factory->uri($this->http_services->request()->getUri()->__toString()));
102 list($this->url_builder, $this->action_parameter_token, $this->row_id_token) =
103 $this->url_builder->acquireParameters(
104 ['datatable', self::TABLE_ID],
105 self::TABLE_ACTION_ID,
106 self::ROW_ID
107 );
108 $actions = [];
109 foreach ($this->data_retrieval->getSelectableGroups() as $ref_id => $group_name) {
110 $action_id = $this->buildAddToGroupString((int) $ref_id);
111 $actions[$action_id] = $this->ui_services->factory()->table()->action()->multi(
112 $this->lng->txt(self::LNG_ADD_TO_GROUP) . ': ' . $group_name,
113 $this->url_builder->withParameter($this->action_parameter_token, $action_id),
114 $this->row_id_token
115 );
116 $action_id = $this->data_retrieval->buildConfirmUnsubscribeActionId((int) $ref_id);
117 $actions[$action_id] = $this->ui_services->factory()->table()->action()->single(
118 $group_name . ' ' . $this->lng->txt(self::LNG_TABLE_ACTION_CONFIRM_UNSUBSCRIBE),
119 $this->url_builder->withParameter($this->action_parameter_token, $action_id),
120 $this->row_id_token
121 )->withAsync(true);
122 }
123 return $actions;
124 }
125
126 private function initFilter(): void
127 {
128 if (isset($this->filter)) {
129 return;
130 }
131 $filter_fields = $this->getFilterFields();
132 $this->filter = $this->ui_service->filter()->standard(
133 self::FILTER_ID,
134 $this->ctrl->getLinkTargetByClass(ilCourseParticipantsGroupsGUI::class, 'show'),
135 $filter_fields,
136 array_fill(0, count($filter_fields), true),
137 true,
138 true
139 );
140 }
141
145 protected function getFilterFields(): array
146 {
147 return [
148 self::TABLE_FILTER_NAME => $this->ui_services->factory()->input()->field()->text(
149 $this->lng->txt(self::LNG_TABLE_COL_NAME)
150 ),
151 self::TABLE_FILTER_GROUPS => $this->ui_services->factory()->input()->field()->select(
152 $this->lng->txt(self::TABLE_COL_GROUPS),
153 $this->data_retrieval->getSelectableGroups()
154 )
155 ];
156 }
157
158 protected function initTable(): void
159 {
160 if (isset($this->table)) {
161 return;
162 }
163 $this->table = $this->ui_services->factory()->table()->data(
164 $this->data_retrieval,
165 $this->lng->txt(self::LNG_TABLE_TITLE),
166 $this->getColumns()
167 )
168 ->withId(self::TABLE_ID)
169 ->withActions($this->getActions())
170 ->withRequest($this->http_services->request());
171 }
172
173 protected function readIdsFromQuery(): array
174 {
175 $tokens = $this->http_services->wrapper()->query()->retrieve(
176 $this->row_id_token->getName(),
177 $this->refinery->custom()->transformation(fn($v) => $v)
178 );
179 return is_null($tokens) ? [] : (is_array($tokens) ? $tokens : [$tokens]);
180 }
181
182 protected function addToGroup(array $user_ids, int $group_ref_id): void
183 {
184 if (!$this->access->checkRbacOrPositionPermissionAccess('manage_members', 'manage_members', $group_ref_id)) {
185 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
186 $this->ctrl->redirectByClass(ilCourseParticipantsGroupsGUI::class, 'show');
187 return;
188 }
189 $members_obj = ilGroupParticipants::_getInstanceByObjId($this->object_data_cache->lookupObjId($group_ref_id));
190 $rejected_count = 0;
191 foreach ($user_ids as $new_member) {
192 if (!$members_obj->add((int) $new_member, ilParticipants::IL_GRP_MEMBER)) {
193 $rejected_count++;
194 continue;
195 }
196 $members_obj->sendNotification(
198 (int) $new_member
199 );
200 }
201 if ($rejected_count === 0) {
202 $message = $this->lng->txt('grp_msg_member_assigned');
203 } else {
204 $accepted_count = count($user_ids) - $rejected_count;
205 $message = sprintf(
206 $this->lng->txt('grp_not_all_users_assigned_msg'),
207 $accepted_count,
208 $rejected_count
209 );
210 }
211 $this->tpl->setOnScreenMessage('success', $message, true);
212 $this->ctrl->redirectByClass(ilCourseParticipantsGroupsGUI::class, 'show');
213 }
214
215 protected function unsubscribe(array $user_ids, int $group_id): void
216 {
217 foreach ($user_ids as $user_id) {
218 if (!$this->access->checkRbacOrPositionPermissionAccess('manage_members', 'manage_members', $group_id)) {
219 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("permission_denied"), true);
220 $this->ctrl->redirectByClass(ilCourseParticipantsGroupsGUI::class, 'show');
221 return;
222 }
223 $members_obj = ilGroupParticipants::_getInstanceByObjId($this->object_data_cache->lookupObjId($group_id));
224 $members_obj->delete((int) $user_id);
225 // Send notification
226 $members_obj->sendNotification(
228 (int) $user_id
229 );
230 }
231 $this->tpl->setOnScreenMessage('success', $this->lng->txt("grp_msg_membership_annulled"), true);
232 $this->ctrl->redirectByClass(ilCourseParticipantsGroupsGUI::class, "show");
233 }
234
235 #[NoReturn] protected function showConfirmUnsubscribeModal(array $user_ids, int $group_ref_id): void
236 {
237 $items = [];
238 foreach ($user_ids as $user_id) {
239 $items[] = $this->ui_services->factory()->modal()->interruptiveItem()->standard(
240 $user_id . '',
241 ilUserUtil::getNamePresentation($user_id, false, false, "", true),
242 $this->ui_services->factory()->image()->standard(ilUtil::getImagePath('standard/icon_usr.svg'), '')
243 );
244 }
245 echo($this->ui_services->renderer()->renderAsync([
246 $this->ui_services->factory()->modal()->interruptive(
247 $this->lng->txt('confirm'),
248 $this->lng->txt('grp_dismiss_member'),
249 (string) $this->url_builder
250 ->withParameter(
251 $this->action_parameter_token,
252 $this->data_retrieval->buildUnsubscribeActionId($group_ref_id)
253 )->withParameter(
254 $this->row_id_token,
255 $user_ids
256 )->buildURI()
257 )->withAffectedItems($items)
258 ]));
259 exit();
260 }
261
262 public function getHTML(): string
263 {
264 $this->initTable();
265 $this->initFilter();
266 return $this->ui_services->renderer()->render([
267 $this->filter,
268 $this->table->withFilter($this->filter->getInputs())
269 ]);
270 }
271
272 public function handleCommands(): void
273 {
274 $this->initTable();
275 if (!$this->http_services->wrapper()->query()->has($this->action_parameter_token->getName())) {
276 return;
277 }
278 $action = $this->http_services->wrapper()->query()->retrieve(
279 $this->action_parameter_token->getName(),
280 $this->refinery->to()->string()
281 );
282 $tokens = $this->http_services->wrapper()->query()->retrieve(
283 $this->row_id_token->getName(),
284 $this->refinery->custom()->transformation(fn($v) => $v)
285 );
286 $all_entries = ($tokens[0] ?? "") === self::ALL_OBJECTS;
287 $user_ids = [];
288 if ($all_entries) {
289 $user_ids = $this->data_retrieval->getAllUserIds();
290 }
291 if (!$all_entries) {
292 $user_ids = $this->readIdsFromQuery();
293 }
294 foreach ($this->data_retrieval->getSelectableGroups() as $ref_id => $group_name) {
295 if ($action === $this->buildAddToGroupString((int) $ref_id)) {
296 $this->addToGroup($user_ids, $ref_id);
297 break;
298 }
299 if ($action === $this->data_retrieval->buildUnsubscribeActionId($ref_id)) {
300 $this->unsubscribe($user_ids, $ref_id);
301 break;
302 }
303 if ($action === $this->data_retrieval->buildConfirmUnsubscribeActionId($ref_id)) {
304 $this->showConfirmUnsubscribeModal($user_ids, $ref_id);
305 break;
306 }
307 }
308 }
309}
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
Class ilAccessHandler Checks access for ILIAS objects.
__construct(protected ilCourseParticipantsGroupsTableDataRetrieval $data_retrieval, protected ilUIServices $ui_services, protected ilUIService $ui_service, protected ilHTTPServices $http_services, protected ilRefineryFactory $refinery, protected ilLanguage $lng, protected ilCtrl $ctrl, protected ilDataFactory $data_factory, protected ilGlobalTemplateInterface $tpl, protected ilAccess $access, protected ilObjectDataCache $object_data_cache)
Class ilCtrl provides processing control methods.
static _getInstanceByObjId(int $a_obj_id)
Get singleton instance.
language handling
class ilObjectDataCache
Filter service.
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path=null)
Default behaviour is:
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
exit
This describes commonalities between all filters.
Definition: Filter.php:34
This describes a standard filter.
Definition: Standard.php:27
This describes a Data Table.
Definition: Data.php:31
$ref_id
Definition: ltiauth.php:66
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)
global $lng
Definition: privfeed.php:31
$message
Definition: xapiexit.php:31