ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
ParticipantTableActions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use ILIAS\Refinery\Factory as Refinery;
30use ILIAS\UI\Factory as UIFactory;
31use ILIAS\UI\Renderer as UIRenderer;
34
36{
37 public const ROW_ID_PARAMETER = 'p_id';
38 public const ACTION_PARAMETER = 'action';
39 public const ACTION_TYPE_PARAMETER = 'action_type';
40 public const SHOW_ACTION = 'showTableAction';
41 public const SUBMIT_ACTION = 'submitTableAction';
45 public function __construct(
46 protected readonly \ilCtrlInterface $ctrl,
47 protected readonly Language $lng,
48 protected readonly \ilGlobalTemplateInterface $tpl,
49 protected readonly UIFactory $ui_factory,
50 protected readonly UIRenderer $ui_renderer,
51 protected readonly Refinery $refinery,
52 protected readonly RequestDataCollector $test_request,
53 protected readonly ResponseHandler $test_response,
54 protected readonly ParticipantRepository $repository,
55 protected readonly \ilObjTest $test_obj,
56 protected readonly array $actions
57 ) {
58 }
59
60 public function getEnabledActions(
61 URLBuilder $url_builder,
62 URLBuilderToken $row_id_token,
63 URLBuilderToken $action_token,
64 URLBuilderToken $action_type_token
65 ): array {
66 return array_filter(
67 array_map(
68 function (TableAction $action) use (
69 $url_builder,
70 $row_id_token,
71 $action_token,
72 $action_type_token
73 ): ?Action {
74 if (!$action->isAvailable()) {
75 return null;
76 }
77
78 return $action->getTableAction(
79 $url_builder,
80 $row_id_token,
81 $action_token,
82 $action_type_token
83 );
84 },
85 $this->actions
86 )
87 );
88 }
89
90 public function getAction(string $action_id): ?TableAction
91 {
92 return $this->actions[$action_id] ?? null;
93 }
94
95 public function execute(
96 URLBuilder $url_builder,
97 URLBuilderToken $row_id_token,
98 URLBuilderToken $action_token,
99 URLBuilderToken $action_type_token
100 ): ?Modal {
101 return match($this->test_request->strVal($action_type_token->getName())) {
102 self::SUBMIT_ACTION => $this->submit(
103 $url_builder,
104 $row_id_token,
105 $action_token,
106 $action_type_token
107 ),
108 default => $this->showModal(
109 $url_builder,
110 $row_id_token,
111 $action_token,
112 $action_type_token
113 ),
114 };
115 }
116
117 public function onDataRow(DataRow $row, mixed $record): DataRow
118 {
119 return array_reduce(
120 array_keys($this->actions),
121 fn(DataRow $c, string $v): DataRow => $this->actions[$v]->allowActionForRecord($record)
122 ? $c
123 : $c->withDisabledAction($v),
124 $row
125 );
126 }
127
128 protected function showModal(
129 URLBuilder $url_builder,
130 URLBuilderToken $row_id_token,
131 URLBuilderToken $action_token,
132 URLBuilderToken $action_type_token
133 ): void {
134 $action = $this->actions[$this->test_request->strVal($action_token->getName())];
135 $selected_participants_from_request = $this->test_request
136 ->getMultiSelectionIds($row_id_token->getName());
137 $selected_participants = $this->resolveSelectedParticipants(
138 $action,
139 $selected_participants_from_request
140 );
141
142 if ($selected_participants === []) {
143 $error_message = $action->getSelectionErrorMessage() ?? $this->lng->txt('no_valid_participant_selection');
144 $this->test_response->sendAsync(
145 $this->ui_renderer->renderAsync(
146 $this->ui_factory->messageBox()->failure(
147 $error_message
148 )
149 )
150 );
151 }
152
153 $this->test_response->sendAsync(
154 $this->ui_renderer->renderAsync(
155 $action->getModal(
156 $url_builder
157 ->withParameter($row_id_token, $selected_participants_from_request)
158 ->withParameter($action_token, $action->getActionId())
159 ->withParameter($action_type_token, self::SUBMIT_ACTION),
160 $selected_participants,
161 $selected_participants_from_request === 'ALL_OBJECTS'
162 )
163 )
164 );
165 }
166
167 protected function submit(
168 URLBuilder $url_builder,
169 URLBuilderToken $row_id_token,
170 URLBuilderToken $action_token,
171 URLBuilderToken $action_type_token
172 ): ?Modal {
173 $action = $this->actions[$this->test_request->strVal($action_token->getName())];
174 $selected_participants_from_request = $this->test_request
175 ->getMultiSelectionIds($row_id_token->getName());
176 $selected_participants = $this->resolveSelectedParticipants(
177 $action,
178 $selected_participants_from_request
179 );
180
181 if ($selected_participants === []) {
182 $this->tpl->setOnScreenMessage(
184 $this->lng->txt('no_valid_participant_selection'),
185 true
186 );
187 }
188
189 return $action->onSubmit(
190 $url_builder
191 ->withParameter($row_id_token, $selected_participants_from_request)
192 ->withParameter($action_token, $action->getActionId())
193 ->withParameter($action_type_token, self::SUBMIT_ACTION),
194 $this->test_request->getRequest(),
195 $selected_participants,
196 $selected_participants_from_request === 'ALL_OBJECTS'
197 );
198 }
199
200 protected function resolveSelectedParticipants(
201 TableAction $action,
202 array|string $selected_participants
203 ): array {
204 if ($selected_participants === 'ALL_OBJECTS') {
205 return array_filter(
206 iterator_to_array($this->repository->getParticipants($this->test_obj->getTestId())),
207 static fn(Participant $participant): bool => $action->allowActionForRecord($participant)
208 );
209 }
210
211 if (!is_array($selected_participants)) {
212 return [];
213 }
214
215 return array_filter(
216 array_map(
217 function (string $ids_string): ?Participant {
218 [$user_id, $active_id] = array_map(
219 'intval',
220 explode('_', $ids_string)
221 );
222
223 if ($user_id === ANONYMOUS_USER_ID) {
224 return $this->repository->getParticipantByActiveId(
225 $this->test_obj->getTestId(),
226 $active_id
227 );
228 }
229
230 return $this->repository->getParticipantByUserId(
231 $this->test_obj->getTestId(),
233 );
234
235 },
236 $selected_participants
237 ),
238 static fn(?Participant $participant): bool => $participant instanceof Participant && $action->allowActionForRecord($participant)
239 );
240 }
241}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
submit(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
getEnabledActions(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
execute(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
__construct(protected readonly \ilCtrlInterface $ctrl, protected readonly Language $lng, protected readonly \ilGlobalTemplateInterface $tpl, protected readonly UIFactory $ui_factory, protected readonly UIRenderer $ui_renderer, protected readonly Refinery $refinery, protected readonly RequestDataCollector $test_request, protected readonly ResponseHandler $test_response, protected readonly ParticipantRepository $repository, protected readonly \ilObjTest $test_obj, protected readonly array $actions)
resolveSelectedParticipants(TableAction $action, array|string $selected_participants)
showModal(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
getName()
Get the full name of the token including its namespace.
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter's value if the supplied token is valid.
Definition: URLBuilder.php:166
const ANONYMOUS_USER_ID
Definition: constants.php:27
$c
Definition: deliver.php:25
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
allowActionForRecord(Participant $record)
This describes commonalities between the different modals.
Definition: Modal.php:35
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Participant.php:21
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))