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