ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
base.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use Psr\Http\Message\ServerRequestInterface;
29
36function base()
37{
38 global $DIC;
39 $f = $DIC['ui.factory'];
40 $r = $DIC['ui.renderer'];
41 $df = new \ILIAS\Data\Factory();
42 $refinery = $DIC['refinery'];
43 $request = $DIC->http()->request();
44
50 $columns = [
51 'usr_id' => $f->table()->column()->number("User ID")
52 ->withIsSortable(false),
53 'login' => $f->table()->column()->text("Login")
54 ->withHighlight(true),
55 'email' => $f->table()->column()->eMail("eMail"),
56 'last' => $f->table()->column()->date("last login", $df->dateFormat()->germanLong()),
57 'achieve' => $f->table()->column()->statusIcon("progress")
58 ->withIsOptional(true),
59 'achieve_txt' => $f->table()->column()->status("success")
60 ->withIsSortable(true)
61 ->withIsOptional(true, false),
62 'repeat' => $f->table()->column()->boolean("repeat", 'yes', 'no')
63 ->withIsSortable(false),
64 'fee' => $f->table()->column()->number("Fee")
65 ->withDecimals(2)
66 ->withUnit('£', I\Column\Number::UNIT_POSITION_FORE)
67 ->withOrderingLabels('cheapest first', 'most expensive first'),
68 'failure_txt' => $f->table()->column()->status("failure")
69 ->withIsSortable(false)
70 ->withIsOptional(true, false),
71 ];
72
82 $here_uri = $df->uri($DIC->http()->request()->getUri()->__toString());
83
88 $url_builder = new URLBuilder($here_uri);
89 $query_params_namespace = ['datatable', 'example'];
90
97 list($url_builder, $action_parameter_token, $row_id_token) =
98 $url_builder->acquireParameters(
99 $query_params_namespace,
100 "table_action", //this is the actions's parameter name
101 "student_ids" //this is the parameter name to be used for row-ids
102 );
103
107 $actions = [
108 'edit' => $f->table()->action()->single( //never in multi actions
110 'Properties',
112 $url_builder->withParameter($action_parameter_token, "edit"),
114 $row_id_token
115 ),
116 'compare' => $f->table()->action()->multi( //never in single row
117 'Add to Comparison',
118 $url_builder->withParameter($action_parameter_token, "compare"),
119 $row_id_token
120 ),
121 'delete' =>
122 $f->table()->action()->standard( //in both
123 'Remove Student',
124 $url_builder->withParameter($action_parameter_token, "delete"),
125 $row_id_token
126 )
133 ->withAsync(),
134 'info' =>
135 $f->table()->action()->standard( //in both
136 'Info',
137 $url_builder->withParameter($action_parameter_token, "info"),
138 $row_id_token
139 )
140 ->withAsync()
141 ];
142
143
144
154 $data_retrieval = new class ($f, $r) implements I\DataRetrieval {
155 public function __construct(
156 protected \ILIAS\UI\Factory $ui_factory,
157 protected \ILIAS\UI\Renderer $ui_renderer
158 ) {
159 }
160
161 public function getRows(
162 I\DataRowBuilder $row_builder,
163 array $visible_column_ids,
165 Order $order,
166 ?array $filter_data,
167 ?array $additional_parameters
168 ): \Generator {
169 $records = $this->getRecords($range, $order);
170 foreach ($records as $idx => $record) {
171 $row_id = (string) $record['usr_id'];
172 $record['achieve_txt'] = $record['achieve'] > 80 ? 'passed' : 'failed';
173 $record['failure_txt'] = "not " . $record["achieve_txt"];
174 $record['repeat'] = $record['achieve'] < 80;
175
176 $icons = [
177 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_checked.svg', '', 'small'),
178 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_unchecked.svg', '', 'small'),
179 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_x.svg', '', 'small'),
180 ];
181 $icon = $icons[2];
182 if ($record['achieve'] > 80) {
183 $icon = $icons[0];
184 }
185 if ($record['achieve'] < 30) {
186 $icon = $icons[1];
187 }
188 $record['achieve'] = $icon;
189
190 yield $row_builder->buildDataRow($row_id, $record)
192 ->withDisabledAction('delete', ($record['login'] === 'superuser'));
193 }
194 }
195
196 public function getTotalRowCount(
197 ?array $filter_data,
198 ?array $additional_parameters
199 ): ?int {
200 return count($this->getRecords());
201 }
202
203 protected function getRecords(?Range $range = null, ?Order $order = null): array
204 {
205 $records = [
206 ['usr_id' => 123,'login' => 'superuser','email' => 'user@example.com',
207 'last' => (new \DateTimeImmutable())->modify('-1 day') ,'achieve' => 20,'fee' => 0
208 ],
209 ['usr_id' => 867,'login' => 'student1','email' => 'student1@example.com',
210 'last' => (new \DateTimeImmutable())->modify('-10 day'),'achieve' => 90,'fee' => 40
211 ],
212 ['usr_id' => 8923,'login' => 'student2','email' => 'student2@example.com',
213 'last' => (new \DateTimeImmutable())->modify('-8 day'),'achieve' => 66,'fee' => 36.789
214 ],
215 ['usr_id' => 8748,'login' => 'student3_longname','email' => 'student3_long_email@example.com',
216 'last' => (new \DateTimeImmutable())->modify('-300 day'),'achieve' => 8,'fee' => 36.789
217 ],
218 ['usr_id' => 8749,'login' => 'studentAB','email' => 'studentAB@example.com',
219 'last' => (new \DateTimeImmutable())->modify('-7 day'),'achieve' => 100,'fee' => 114
220 ],
221 ['usr_id' => 8750,'login' => 'student5','email' => 'student5@example.com',
222 'last' => new \DateTimeImmutable(),'achieve' => 76,'fee' => 3.789
223 ],
224 ['usr_id' => 8751,'login' => 'student6','email' => 'student6@example.com',
225 'last' => (new \DateTimeImmutable())->modify('-2 day'),'achieve' => 66,'fee' => 67
226 ]
227 ];
228 if ($order) {
229 list($order_field, $order_direction) = $order->join([], fn($ret, $key, $value) => [$key, $value]);
230 usort($records, fn($a, $b) => $a[$order_field] <=> $b[$order_field]);
231 if ($order_direction === 'DESC') {
232 $records = array_reverse($records);
233 }
234 }
235 if ($range) {
236 $records = array_slice($records, $range->getStart(), $range->getLength());
237 }
238
239 return $records;
240 }
241 };
242
243
248 $table = $f->table()
249 ->data($data_retrieval, 'a data table', $columns)
250 ->withId('example_base')
251 ->withActions($actions)
252
253 //these are initial settings that apply if the according view control
254 //has not been operated, yet
255 ->withRange(new Range(0, 2))
256 ->withOrder(new Order('achieve', Order::DESC))
257
258 ->withRequest($request);
259
263 $out = [$table];
264
269 $query = $DIC->http()->wrapper()->query();
270 if ($query->has($action_parameter_token->getName())) {
271 $action = $query->retrieve($action_parameter_token->getName(), $refinery->to()->string());
273 $ids = $query->retrieve($row_id_token->getName(), $refinery->custom()->transformation(fn($v) => $v));
274 $listing = $f->listing()->characteristicValue()->text([
275 'table_action' => $action,
276 'id' => print_r($ids, true),
277 ]);
278
280 if ($action === 'delete') {
281 $items = [];
282 foreach ($ids as $id) {
283 $items[] = $f->modal()->interruptiveItem()->keyValue($id, $row_id_token->getName(), $id);
284 }
285 echo($r->renderAsync([
286 $f->modal()->interruptive(
287 'Deletion',
288 'You are about to delete items!',
289 '#'
290 )->withAffectedItems($items)
291 ->withAdditionalOnLoadCode(static fn($id): string => "console.log('ASYNC JS');")
292 ]));
293 exit();
294 }
295 if ($action === 'info') {
296 echo(
297 $r->render($f->messageBox()->info('an info message: <br><li>' . implode('<li>', $ids)))
298 . '<script data-replace-marker="script">console.log("ASYNC JS, too");</script>'
299 );
300 exit();
301 }
302
304 $out[] = $f->divider()->horizontal();
305 $out[] = $listing;
306 }
307
308 return $r->render($out);
309}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$out
Definition: buildRTE.php:24
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
const DESC
Definition: Order.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Definition: UI.php:24
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
exit
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26