ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ForumModeratorsTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data;
24use ILIAS\UI;
25use Psr\Http\Message\ServerRequestInterface;
26use ilArrayUtil;
27use ilObjUser;
29use ilLanguage;
31
33{
34 protected ServerRequestInterface $request;
35 protected Data\Factory $data_factory;
39 private ?array $records = null;
40
41 public function __construct(
42 private readonly ilForumModerators $forum_moderators,
43 private readonly ilLanguage $lng,
44 \ILIAS\HTTP\Services $http,
45 private readonly \ILIAS\UI\Factory $ui_factory,
46 private readonly string $action_url
47 ) {
48 $this->request = $http->request();
49 $this->data_factory = new Data\Factory();
50 }
51
52 public function getComponent(): UI\Component\Table\Data
53 {
54 $columns = $this->getColumns();
55 $actions = $this->getActions();
56
57 return $this->ui_factory
58 ->table()
59 ->data($this, $this->lng->txt('frm_moderators'), $columns)
60 ->withId(self::class . '_' . $this->forum_moderators->getRefId())
61 ->withOrder(new \ILIAS\Data\Order('login', \ILIAS\Data\Order::ASC))
62 ->withActions($actions)
63 ->withRequest($this->request);
64 }
65
69 private function getColumns(): array
70 {
71 return [
72 'login' => $this->ui_factory
73 ->table()
74 ->column()
75 ->text($this->lng->txt('login'))
76 ->withIsSortable(true),
77 'firstname' => $this->ui_factory
78 ->table()
79 ->column()
80 ->text($this->lng->txt('firstname'))
81 ->withIsSortable(true),
82
83 'lastname' => $this->ui_factory
84 ->table()
85 ->column()
86 ->text($this->lng->txt('lastname'))
87 ->withIsSortable(true),
88 ];
89 }
90
94 protected function getActions(): array
95 {
96 $query_params_namespace = ['frm', 'moderators', 'table'];
97
98 $uri_detach = $this->data_factory->uri($this->action_url);
99
100 $url_builder_detach = new UI\URLBuilder($uri_detach);
101 [
102 $url_builder_detach,
103 $action_parameter_token_copy,
104 $row_id_token_detach
105 ] = $url_builder_detach->acquireParameters(
106 $query_params_namespace,
107 'action',
108 'usr_ids'
109 );
110
111 return [
112 'detachModeratorRole' => $this->ui_factory->table()->action()->single(
113 $this->lng->txt('remove'),
114 $url_builder_detach->withParameter($action_parameter_token_copy, 'detachModeratorRole'),
115 $row_id_token_detach
116 ),
117 ];
118 }
119
120 private function initRecords(): void
121 {
122 if ($this->records === null) {
123 $this->records = [];
124 $i = 0;
125 $entries = $this->forum_moderators->getCurrentModerators();
126 foreach ($entries as $usr_id) {
128 $user = ilObjectFactory::getInstanceByObjId($usr_id, false);
129 if (!($user instanceof ilObjUser)) {
130 $this->forum_moderators->detachModeratorRole($usr_id);
131 continue;
132 }
133
134 $this->records[$i]['usr_id'] = $user->getId();
135 $this->records[$i]['login'] = $user->getLogin();
136 $this->records[$i]['firstname'] = $user->getFirstname();
137 $this->records[$i]['lastname'] = $user->getLastname();
138 ++$i;
139 }
140 }
141 }
142
143 public function getRows(
144 UI\Component\Table\DataRowBuilder $row_builder,
145 array $visible_column_ids,
147 Data\Order $order,
148 ?array $filter_data,
149 ?array $additional_parameters
150 ): \Generator {
151 $records = $this->getRecords($range, $order);
152
153 foreach ($records as $record) {
154 $row_id = (string) $record['usr_id'];
155 yield $row_builder->buildDataRow($row_id, $record);
156 }
157 }
158
159 public function getTotalRowCount(
160 ?array $filter_data,
161 ?array $additional_parameters
162 ): ?int {
163 $this->initRecords();
164
165 return \count($this->records);
166 }
167
171 private function sortedRecords(Data\Order $order): array
172 {
173 $records = $this->records;
174 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
175
176 return ilArrayUtil::stableSortArray($records, $order_field, strtolower($order_direction));
177 }
178
182 private function getRecords(Data\Range $range, Data\Order $order): array
183 {
184 $this->initRecords();
185
186 $records = $this->sortedRecords($order);
187
188 $records = \array_slice($records, $range->getStart(), $range->getLength());
189
190 return $records;
191 }
192}
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
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
getRecords(Data\Range $range, Data\Order $order)
getRows(UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Data\Range $range, Data\Order $order, ?array $filter_data, ?array $additional_parameters)
__construct(private readonly ilForumModerators $forum_moderators, private readonly ilLanguage $lng, \ILIAS\HTTP\Services $http, private readonly \ILIAS\UI\Factory $ui_factory, private readonly string $action_url)
Definition: UI.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stableSortArray(array $array, string $a_array_sortby, string $a_array_sortorder="asc", bool $a_numeric=false)
Sort an aray using a stable sort algorithm, which preveserves the sequence of array elements which ha...
Class ilForumModerators.
language handling
User class.
Class ilObjectFactory This class offers methods to get instances of the type-specific object classes ...
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
$http
Definition: deliver.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31