ILIAS  release_8 Revision v8.24
base.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
6
12use Psr\Http\Message\ServerRequestInterface;
13
14function base()
15{
16 global $DIC;
17 $f = $DIC['ui.factory'];
18 $r = $DIC['ui.renderer'];
19 $df = new \ILIAS\Data\Factory();
20 $refinery = $DIC['refinery'];
21 $request = $DIC->http()->request();
22
28 $columns = [
29 'usr_id' => $f->table()->column()->number("User ID")
30 ->withIsSortable(false),
31 'login' => $f->table()->column()->text("Login")
32 ->withHighlight(true),
33 'email' => $f->table()->column()->eMail("eMail"),
34 'last' => $f->table()->column()->date("last login", $df->dateFormat()->germanLong()),
35 'achieve' => $f->table()->column()->statusIcon("progress")
36 ->withIsOptional(true),
37 'achieve_txt' => $f->table()->column()->status("success")
38 ->withIsSortable(false)
39 ->withIsOptional(true),
40 'repeat' => $f->table()->column()->boolean("repeat", 'yes', 'no')
41 ->withIsSortable(false),
42 'fee' => $f->table()->column()->number("Fee")
43 ->withDecimals(2)
44 ->withUnit('£', I\Column\Number::UNIT_POSITION_FORE)
45 ->withOrderingLabels('cheapest first', 'most expensive first'),
46 'failure_txt' => $f->table()->column()->status("failure")
47 ->withIsSortable(false)
48 ->withIsOptional(true, false),
49 ];
50
60 $here_uri = $df->uri($DIC->http()->request()->getUri()->__toString());
61
66 $url_builder = new URLBuilder($here_uri);
67 $query_params_namespace = ['datatable', 'example'];
68
75 list($url_builder, $action_parameter_token, $row_id_token) =
76 $url_builder->acquireParameters(
77 $query_params_namespace,
78 "table_action", //this is the actions's parameter name
79 "student_ids" //this is the parameter name to be used for row-ids
80 );
81
85 $actions = [
86 'edit' => $f->table()->action()->single( //never in multi actions
88 'Properties',
90 $url_builder->withParameter($action_parameter_token, "edit"),
92 $row_id_token
93 ),
94 'compare' => $f->table()->action()->multi( //never in single row
95 'Add to Comparison',
96 $url_builder->withParameter($action_parameter_token, "compare"),
97 $row_id_token
98 ),
99 'delete' =>
100 $f->table()->action()->standard( //in both
101 'Remove Student',
102 $url_builder->withParameter($action_parameter_token, "delete"),
103 $row_id_token
104 )
111 ->withAsync(),
112 'info' =>
113 $f->table()->action()->standard( //in both
114 'Info',
115 $url_builder->withParameter($action_parameter_token, "info"),
116 $row_id_token
117 )
118 ->withAsync()
119 ];
120
121
122
132 $data_retrieval = new class ($f, $r) implements I\DataRetrieval {
133
134 protected \ILIAS\UI\Factory $ui_factory;
135 protected \ILIAS\UI\Renderer $ui_renderer;
136
137 public function __construct(
138 \ILIAS\UI\Factory $ui_factory,
139 \ILIAS\UI\Renderer $ui_renderer
140 ) {
141 $this->ui_factory = $ui_factory;
142 $this->ui_renderer = $ui_renderer;
143 }
144
145 public function getRows(
146 I\DataRowBuilder $row_builder,
147 array $visible_column_ids,
148 Range $range,
149 Order $order,
150 ?array $filter_data,
151 ?array $additional_parameters
152 ): \Generator {
153 $records = $this->getRecords($range, $order);
154 foreach ($records as $idx => $record) {
155 $row_id = (string)$record['usr_id'];
156 $record['achieve_txt'] = $record['achieve'] > 80 ? 'passed' : 'failed';
157 $record['failure_txt'] = "not " . $record["achieve_txt"];
158 $record['repeat'] = $record['achieve'] < 80;
159
160 $icons = [
161 $this->ui_factory->symbol()->icon()->custom('templates/default/images/standard/icon_checked.svg', '', 'small'),
162 $this->ui_factory->symbol()->icon()->custom('templates/default/images/standard/icon_unchecked.svg', '', 'small'),
163 $this->ui_factory->symbol()->icon()->custom('templates/default/images/standard/icon_x.svg', '', 'small'),
164 ];
165 $icon = $icons[2];
166 if($record['achieve'] > 80) {
167 $icon = $icons[0];
168 }
169 if($record['achieve'] < 30) {
170 $icon = $icons[1];
171 }
172 $record['achieve'] = $icon;
173
174 yield $row_builder->buildDataRow($row_id, $record)
176 ->withDisabledAction('delete', ($record['login'] === 'superuser'));
177 }
178 }
179
180 public function getTotalRowCount(
181 ?array $filter_data,
182 ?array $additional_parameters
183 ): ?int {
184 return count($this->getRecords());
185 }
186
187 protected function getRecords(Range $range = null, Order $order = null): array
188 {
189 $records = [
190 ['usr_id' => 123,'login' => 'superuser','email' => 'user@example.com',
191 'last' => (new \DateTimeImmutable())->modify('-1 day') ,'achieve' => 20,'fee' => 0
192 ],
193 ['usr_id' => 867,'login' => 'student1','email' => 'student1@example.com',
194 'last' => (new \DateTimeImmutable())->modify('-10 day'),'achieve' => 90,'fee' => 40
195 ],
196 ['usr_id' => 8923,'login' => 'student2','email' => 'student2@example.com',
197 'last' => (new \DateTimeImmutable())->modify('-8 day'),'achieve' => 66,'fee' => 36.789
198 ],
199 ['usr_id' => 8748,'login' => 'student3_longname','email' => 'student3_long_email@example.com',
200 'last' => (new \DateTimeImmutable())->modify('-300 day'),'achieve' => 8,'fee' => 36.789
201 ],
202 ['usr_id' => 8749,'login' => 'studentAB','email' => 'studentAB@example.com',
203 'last' => (new \DateTimeImmutable())->modify('-7 day'),'achieve' => 100,'fee' => 114
204 ],
205 ['usr_id' => 8750,'login' => 'student5','email' => 'student5@example.com',
206 'last' => new \DateTimeImmutable(),'achieve' => 76,'fee' => 3.789
207 ],
208 ['usr_id' => 8751,'login' => 'student6','email' => 'student6@example.com',
209 'last' => (new \DateTimeImmutable())->modify('-2 day'),'achieve' => 66,'fee' => 67
210 ]
211 ];
212 if ($order) {
213 list($order_field, $order_direction) = $order->join([], fn ($ret, $key, $value) => [$key, $value]);
214 usort($records, fn ($a, $b) => $a[$order_field] <=> $b[$order_field]);
215 if ($order_direction === 'DESC') {
216 $records = array_reverse($records);
217 }
218 }
219 if ($range) {
220 $records = array_slice($records, $range->getStart(), $range->getLength());
221 }
222
223 return $records;
224 }
225 };
226
227
232 $table = $f->table()
233 ->data('a data table', $columns, $data_retrieval)
234 ->withId('example_base')
235 ->withActions($actions)
236 ->withRequest($request);
237
241 $out = [$table];
242
247 $query = $DIC->http()->wrapper()->query();
248 if ($query->has($action_parameter_token->getName())) {
249 $action = $query->retrieve($action_parameter_token->getName(), $refinery->to()->string());
251 $ids = $query->retrieve($row_id_token->getName(), $refinery->custom()->transformation(fn ($v) => $v));
252 $listing = $f->listing()->characteristicValue()->text([
253 'table_action' => $action,
254 'id' => print_r($ids, true),
255 ]);
256
258 if ($action === 'delete') {
259 $items = [];
260 foreach ($ids as $id) {
261 $items[] = $f->modal()->interruptiveItem(
262 $id,
263 $row_id_token->getName() . ' - ' . $id
264 );
265 }
266 echo($r->renderAsync([
267 $f->modal()->interruptive(
268 'Deletion',
269 'You are about to delete items!',
270 '#'
271 )->withAffectedItems($items)
272 ->withAdditionalOnLoadCode(static fn ($id): string => "console.log('ASYNC JS');")
273 ]));
274 exit();
275 }
276 if ($action === 'info') {
277 echo(
278 $r->render($f->messageBox()->info('an info message: <br><li>' . implode('<li>', $ids)))
279 . '<script data-replace-marker="script">console.log("ASYNC JS, too");</script>'
280 );
281 exit();
282 }
283
285 $out[] = $f->divider()->horizontal();
286 $out[] = $listing;
287 }
288
289 return $r->render($out);
290}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$out
Definition: buildRTE.php:24
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:13
join($init, callable $fn)
Definition: Order.php:59
A simple class to express a range of whole positive numbers.
Definition: Range.php:31
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:62
global $DIC
Definition: feed.php:28
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
exit
Definition: login.php:28
string $key
Consumer key/client ID value.
Definition: System.php:193
Refinery Factory $refinery
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...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$query