ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilLTIConsumerProviderTableGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Message\RequestInterface;
33
43{
44 protected ilLanguage $lng;
46 protected \ILIAS\UI\Renderer $ui_renderer;
48 private ServerRequestInterface|RequestInterface $request;
49 protected \ILIAS\Data\Factory $data_factory;
52 protected \ILIAS\Refinery\Factory $refinery;
53 private array $records;
54 public ?object $parent_obj;
55 public ?string $parent_cmd;
56 private bool $acceptProviderAsGlobal = false;
57 private bool $resetProviderToUserScope = false;
58 private bool $selectProviderForm = false;
59
60 public function __construct(?object $a_parent_obj, ?string $a_parent_cmd)
61 {
62 global $DIC;
63
64 $this->lng = $DIC->language();
65 $this->ui_factory = $DIC->ui()->factory();
66 $this->ui_renderer = $DIC->ui()->renderer();
67 $this->ui_service = $DIC->uiService();
68 $this->request = $DIC->http()->request();
69 $this->data_factory = new \ILIAS\Data\Factory();
70 $this->ctrl = $DIC->ctrl();
71 $this->wrapper = $DIC->http()->wrapper();
72 $this->refinery = $DIC->refinery();
73
74 $this->parent_obj = $a_parent_obj;
75 $this->parent_cmd = $a_parent_cmd;
76 }
77
78 public function enableAcceptProviderAsGlobal(): void
79 {
80 $this->acceptProviderAsGlobal = true;
81 }
82
83 public function enableResetProviderToUserScope(): void
84 {
85 $this->resetProviderToUserScope = true;
86 }
87
88 public function enableSelectProviderForm(): void
89 {
90 $this->selectProviderForm = true;
91 }
92
98 public function getRows(
99 DataRowBuilder $row_builder,
100 array $visible_column_ids,
102 Order $order,
103 mixed $additional_viewcontrol_data,
104 mixed $filter_data,
105 mixed $additional_parameters
106 ): Generator {
107 $records = $this->applyOrdering($this->records, $order, $range);
108 foreach ($records as $record) {
109 $record["icon"] = $record["icon"] ?? "lti";
110 $record["icon"] = $this->ui_factory->symbol()->icon()->standard($record["icon"], $record["icon"], IconAlias::SMALL);
111
112 if ($this->selectProviderForm) {
113 $this->ctrl->setParameter($this->parent_obj, 'provider_id', $record['id']);
114 $record["title"] = $this->ui_factory->link()->standard($record['title'], $this->ctrl->getLinkTarget($this->parent_obj, "save"));
115 } else {
116 $this->ctrl->setParameter($this->parent_obj, 'provider_id', $record['id']);
117 $record["title"] = $this->ui_factory->link()->standard($record['title'], $this->ctrl->getLinkTarget($this->parent_obj, ilLTIConsumerAdministrationGUI::CMD_SHOW_GLOBAL_PROVIDER_FORM));
118 }
119
120 $record["category"] = $this->getCategoryTranslation($record['category']);
121
122 $record["outcome"] = $this->getHasOutcomeFormatted($record['outcome']);
123 $record["internal"] = $this->getIsInternalFormatted(!$record['external']);
124 $record["with_key"] = $this->getIsWithKeyFormatted(!$record['provider_key_customizable']);
125
126 $record["availability"] = $this->getAvailabilityLabel($record);
127 $record["own_provider"] = $this->getOwnProviderLabel($record);
128 $record["provider_creator"] = $this->getProviderCreatorLabel($record);
129
130 yield $row_builder->buildDataRow((string) $record["id"], $record);
131 }
132 }
133
134 public function getTotalRowCount(
135 mixed $additional_viewcontrol_data,
136 mixed $filter_data,
137 mixed $additional_parameters
138 ): ?int {
139 return count($this->records);
140 }
141
142 public function setData(array $data): void
143 {
144 $this->records = $data;
145 }
146
147 protected function applyOrdering(array $records, Order $order, ?Range $range = null): array
148 {
149 [$order_field, $order_direction] = $order->join(
150 [],
151 fn($ret, $key, $value) => [$key, $value]
152 );
153
154 $order_field = (string) $order_field;
155 $sortable_records = array_map(function (array $record) use ($order_field): array {
156 return [
157 'sort_key' => $this->getSortableValue($record, $order_field),
158 'record' => $record,
159 ];
160 }, $records);
161
162 usort($sortable_records, static function (array $left, array $right): int {
163 return ilStr::strCmp($left['sort_key'], $right['sort_key']);
164 });
165
166 $records = array_column($sortable_records, 'record');
167
168 if ($order_direction === Order::DESC) {
169 $records = array_reverse($records);
170 }
171
172 if ($range !== null) {
173 $records = array_slice($records, $range->getStart(), $range->getLength());
174 }
175
176 return $records;
177 }
178
179 protected function getSortableValue(array $record, string $order_field): string
180 {
181 return match ($order_field) {
182 'category' => $this->getCategoryTranslation((string) ($record['category'] ?? '')),
183 'outcome' => $this->getHasOutcomeFormatted((bool) ($record['outcome'] ?? false)),
184 'internal' => $this->getIsInternalFormatted(!(bool) ($record['external'] ?? false)),
185 'with_key' => $this->getIsWithKeyFormatted(!(bool) ($record['provider_key_customizable'] ?? false)),
186 'availability' => $this->getAvailabilityLabel($record),
187 'own_provider' => $this->getOwnProviderLabel($record),
188 'provider_creator' => $this->getProviderCreatorLabel($record),
189 default => (string) ($record[$order_field] ?? ''),
190 };
191 }
192
196 public function getHTML(bool $hasWriteAccess = false): string
197 {
198 $table = $this->ui_factory->table()
199 ->data($this, $this->lng->txt('tbl_provider_header'), $this->getColumns())
200 ->withOrder(new Order('title', Order::ASC))
201 ->withRange(new Range(0, 20))
202 ->withRequest($this->request);
203
204 if ($hasWriteAccess) {
205 $table = $table->withActions($this->getActions());
206 }
207
208 return $this->ui_renderer->render($table);
209 }
210
211 private function getColumns(): array
212 {
213 return [
214 'icon' => $this->ui_factory->table()->column()->statusIcon($this->lng->txt('icon')),
215 'title' => $this->ui_factory->table()->column()->link($this->lng->txt('title')),
216 'description' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_description')),
217 'category' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_category'))->withIsOptional(true),
218 'keywords' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_keywords')),
219 'outcome' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_outcome'))->withIsOptional(true),
220 'internal' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_internal'))->withIsOptional(true),
221 'with_key' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_with_key')),
222 'availability' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_availability')),
223 'own_provider' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_own_provider'))->withIsOptional(true),
224 'provider_creator' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_provider_creator'))->withIsOptional(true),
225 'usages_untrashed' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_usages_untrashed')),
226 'usages_trashed' => $this->ui_factory->table()->column()->text($this->lng->txt('tbl_lti_prov_usages_trashed'))->withIsOptional(true),
227 ];
228 }
229
233 private function getActions(): array
234 {
235 $df = new \ILIAS\Data\Factory();
236 $here_uri = $df->uri($this->request->getUri()->__toString());
237 $url_builder = new URLBuilder($here_uri);
238
239 $query_params_namespace = ['provider', 'table'];
240 list($url_builder, $id_token, $action_token) = $url_builder->acquireParameters(
241 $query_params_namespace,
242 "provider_id",
243 "action"
244 );
245
246 $query = $this->wrapper->query();
247 if ($query->has($action_token->getName())) {
248 $action = $query->retrieve($action_token->getName(), $this->refinery->to()->string());
249 $ids = $query->retrieve($id_token->getName(), $this->refinery->custom()->transformation(fn($v) => $v));
250
251 switch ($action) {
252 case "edit":
253 $id = $ids[0] ?? null;
254 $this->ctrl->setParameter($this->parent_obj, 'provider_id', $id);
255 $this->ctrl->redirect($this->parent_obj, ilLTIConsumerAdministrationGUI::CMD_SHOW_USER_PROVIDER_FORM);
256 break;
257 case "delete_global":
258 if (count($ids) > 1) {
259 $this->ctrl->setParameter($this->parent_obj, 'provider_ids', implode(",", $ids));
261 } else {
262 $id = $ids[0] ?? null;
263 $this->ctrl->setParameter($this->parent_obj, 'provider_id', $id);
264 $this->ctrl->redirect($this->parent_obj, ilLTIConsumerAdministrationGUI::CMD_DELETE_GLOBAL_PROVIDER);
265 }
266 break;
267 case "delete_user":
268 if (count($ids) > 1) {
269 $this->ctrl->setParameter($this->parent_obj, 'provider_ids', implode(",", $ids));
270 $this->ctrl->redirect($this->parent_obj, ilLTIConsumerAdministrationGUI::CMD_DELETE_USER_PROVIDER_MULTI);
271 } else {
272 $id = $ids[0] ?? null;
273 $this->ctrl->setParameter($this->parent_obj, 'provider_id', $id);
274 $this->ctrl->redirect($this->parent_obj, ilLTIConsumerAdministrationGUI::CMD_DELETE_USER_PROVIDER);
275 }
276 break;
277 case "global":
278 if (count($ids) > 1) {
279 $this->ctrl->setParameter($this->parent_obj, 'provider_ids', implode(",", $ids));
281 } else {
282 $id = $ids[0] ?? null;
283 $this->ctrl->setParameter($this->parent_obj, 'provider_id', $id);
284 $this->ctrl->redirect($this->parent_obj, ilLTIConsumerAdministrationGUI::CMD_ACCEPT_PROVIDER_AS_GLOBAL);
285 }
286 break;
287 case "reset":
288 if (count($ids) > 1) {
289 $this->ctrl->setParameter($this->parent_obj, 'provider_ids', implode(",", $ids));
291 } else {
292 $id = $ids[0] ?? null;
293 $this->ctrl->setParameter($this->parent_obj, 'provider_id', $id);
295 }
296 break;
297 }
298 }
299
300
301 $actions = [
302 "edit" => $this->ui_factory->table()->action()->single(
303 $this->lng->txt('lti_action_edit_provider'),
304 $url_builder->withParameter($action_token, "edit"),
305 $id_token
306 ),
307 ];
308
309 if ($this->acceptProviderAsGlobal) {
310 $actions["global"] = $this->ui_factory->table()->action()->standard(
311 $this->lng->txt('lti_action_accept_provider_as_global'),
312 $url_builder->withParameter($action_token, "global"),
313 $id_token
314 );
315 $actions["delete_user"] = $this->ui_factory->table()->action()->standard(
316 $this->lng->txt('lti_delete_provider'),
317 $url_builder->withParameter($action_token, "delete_user"),
318 $id_token
319 );
320 }
321
322 if ($this->resetProviderToUserScope) {
323 $actions["reset"] = $this->ui_factory->table()->action()->standard(
324 $this->lng->txt('lti_action_reset_provider_to_user_scope'),
325 $url_builder->withParameter($action_token, "reset"),
326 $id_token
327 );
328 $actions["delete_global"] = $this->ui_factory->table()->action()->standard(
329 $this->lng->txt('lti_delete_provider'),
330 $url_builder->withParameter($action_token, "delete_global"),
331 $id_token
332 );
333 }
334
335 return $actions;
336 }
337
338 protected function getHasOutcomeFormatted(bool $hasOutcome): string
339 {
340 global $DIC;
341
342 return $hasOutcome ? $DIC->language()->txt('yes') : '';
343 }
344
345 protected function getIsInternalFormatted(bool $isInternal): string
346 {
347 global $DIC;
348
349 return $isInternal ? $DIC->language()->txt('yes') : '';
350 }
351
352 protected function getIsWithKeyFormatted(bool $isWithKey): string
353 {
354 global $DIC;
355
356 return $isWithKey ? $DIC->language()->txt('yes') : '';
357 }
358
359 protected function getCategoryTranslation(string $category): string
360 {
362 return $categories[$category];
363 }
364
365 protected function getAvailabilityLabel(array $data): string
366 {
367 global $DIC;
368
369 return match ($data['availability']) {
370 ilLTIConsumeProvider::AVAILABILITY_CREATE => $DIC->language()->txt('lti_con_prov_availability_create'),
371 ilLTIConsumeProvider::AVAILABILITY_EXISTING => $DIC->language()->txt('lti_con_prov_availability_existing'),
372 ilLTIConsumeProvider::AVAILABILITY_NONE => $DIC->language()->txt('lti_con_prov_availability_non'),
373 default => '',
374 };
375 }
376
377 protected function getOwnProviderLabel(array $data): string
378 {
379 global $DIC;
380
381 if ($data['creator'] == $DIC->user()->getId()) {
382 return $DIC->language()->txt('yes');
383 }
384
385 return '';
386 }
387
392 protected function getProviderCreatorLabel(array $data): string
393 {
394 global $DIC;
395
396 if ($data['creator']) {
397 /* @var ilObjUser $user */
398 $user = ilObjectFactory::getInstanceByObjId($data['creator'], false);
399
400 if ($user) {
401 return $user->getFullname();
402 }
403
404 return $DIC->language()->txt('deleted_user');
405 }
406
407 return '';
408 }
409
413 public function getFilter(): Filter
414 {
415 $filter_inputs = [
416 'title' => $this->ui_factory->input()->field()->text($this->lng->txt("title")),
417 'keywords' => $this->ui_factory->input()->field()->text($this->lng->txt("tbl_lti_prov_keywords")),
418 'outcome' => $this->ui_factory->input()->field()->select($this->lng->txt("tbl_lti_prov_outcome"), [
419 'yes' => $this->lng->txt('yes'),
420 'no' => $this->lng->txt('no'),
421 ]),
422 'internal' => $this->ui_factory->input()->field()->select($this->lng->txt("tbl_lti_prov_internal"), [
423 'yes' => $this->lng->txt('yes'),
424 'no' => $this->lng->txt('no'),
425 ]),
426 'with_key' => $this->ui_factory->input()->field()->select($this->lng->txt("tbl_lti_prov_with_key"), [
427 'yes' => $this->lng->txt('yes'),
428 'no' => $this->lng->txt('no'),
429 ]),
430 'category' => $this->ui_factory->input()->field()->select($this->lng->txt("tbl_lti_prov_category"), ilLTIConsumeProvider::getCategoriesSelectOptions()),
431 ];
432
433 $active = array_fill(0, count($filter_inputs), true);
434
435 return $this->ui_service->filter()->standard(
436 'lti_consumer_provider_table',
437 $this->ctrl->getLinkTarget($this->parent_obj, $this->parent_cmd),
438 $filter_inputs,
439 $active,
440 true
441 );
442 }
443}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
ServerRequestInterface RequestInterface $request
applyOrdering(array $records, Order $order, ?Range $range=null)
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
getSortableValue(array $record, string $order_field)
__construct(?object $a_parent_obj, ?string $a_parent_cmd)
language handling
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
Filter service.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes a standard filter.
Definition: Standard.php:27
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
buildDataRow(string $id, array $record)
This is how the factory for UI elements looks.
Definition: Factory.php:38
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26