ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
base.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
13 
20 function base()
21 {
22  global $DIC;
23  $f = $DIC['ui.factory'];
24  $r = $DIC['ui.renderer'];
25  $df = new \ILIAS\Data\Factory();
26  $refinery = $DIC['refinery'];
27  $request = $DIC->http()->request();
28 
34  $columns = [
35  'usr_id' => $f->table()->column()->number("User ID")
36  ->withIsSortable(false),
37  'login' => $f->table()->column()->text("Login")
38  ->withHighlight(true),
39  'email' => $f->table()->column()->eMail("eMail"),
40  'last' => $f->table()->column()->date("last login", $df->dateFormat()->germanLong()),
41  'achieve' => $f->table()->column()->statusIcon("progress")
42  ->withIsOptional(true),
43  'achieve_txt' => $f->table()->column()->status("success")
44  ->withIsSortable(true)
45  ->withIsOptional(true, false),
46  'repeat' => $f->table()->column()->boolean("repeat", 'yes', 'no')
47  ->withIsSortable(false),
48  'fee' => $f->table()->column()->number("Fee")
49  ->withDecimals(2)
50  ->withUnit('£', I\Column\Number::UNIT_POSITION_FORE)
51  ->withOrderingLabels('cheapest first', 'most expensive first'),
52  'failure_txt' => $f->table()->column()->status("failure")
53  ->withIsSortable(false)
54  ->withIsOptional(true, false),
55  ];
56 
66  $here_uri = $df->uri($DIC->http()->request()->getUri()->__toString());
67 
72  $url_builder = new URLBuilder($here_uri);
73  $query_params_namespace = ['datatable', 'example'];
74 
81  list($url_builder, $action_parameter_token, $row_id_token) =
82  $url_builder->acquireParameters(
83  $query_params_namespace,
84  "table_action", //this is the actions's parameter name
85  "student_ids" //this is the parameter name to be used for row-ids
86  );
87 
91  $actions = [
92  'edit' => $f->table()->action()->single( //never in multi actions
94  'Properties',
96  $url_builder->withParameter($action_parameter_token, "edit"),
98  $row_id_token
99  ),
100  'compare' => $f->table()->action()->multi( //never in single row
101  'Add to Comparison',
102  $url_builder->withParameter($action_parameter_token, "compare"),
103  $row_id_token
104  ),
105  'delete' =>
106  $f->table()->action()->standard( //in both
107  'Remove Student',
108  $url_builder->withParameter($action_parameter_token, "delete"),
109  $row_id_token
110  )
117  ->withAsync(),
118  'info' =>
119  $f->table()->action()->standard( //in both
120  'Info',
121  $url_builder->withParameter($action_parameter_token, "info"),
122  $row_id_token
123  )
124  ->withAsync()
125  ];
126 
127 
128 
138  $data_retrieval = new class ($f, $r) implements I\DataRetrieval {
139  public function __construct(
140  protected \ILIAS\UI\Factory $ui_factory,
141  protected \ILIAS\UI\Renderer $ui_renderer
142  ) {
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('assets/images/standard/icon_checked.svg', '', 'small'),
162  $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_unchecked.svg', '', 'small'),
163  $this->ui_factory->symbol()->icon()->custom('assets/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 
237  //these are initial settings that apply if the according view control
238  //has not been operated, yet
239  ->withRange(new Range(0, 2))
240  ->withOrder(new Order('achieve', Order::DESC))
241 
242  ->withRequest($request);
243 
247  $out = [$table];
248 
253  $query = $DIC->http()->wrapper()->query();
254  if ($query->has($action_parameter_token->getName())) {
255  $action = $query->retrieve($action_parameter_token->getName(), $refinery->to()->string());
257  $ids = $query->retrieve($row_id_token->getName(), $refinery->custom()->transformation(fn($v) => $v));
258  $listing = $f->listing()->characteristicValue()->text([
259  'table_action' => $action,
260  'id' => print_r($ids, true),
261  ]);
262 
264  if ($action === 'delete') {
265  $items = [];
266  foreach ($ids as $id) {
267  $items[] = $f->modal()->interruptiveItem()->keyValue($id, $row_id_token->getName(), $id);
268  }
269  echo($r->renderAsync([
270  $f->modal()->interruptive(
271  'Deletion',
272  'You are about to delete items!',
273  '#'
274  )->withAffectedItems($items)
275  ->withAdditionalOnLoadCode(static fn($id): string => "console.log('ASYNC JS');")
276  ]));
277  exit();
278  }
279  if ($action === 'info') {
280  echo(
281  $r->render($f->messageBox()->info('an info message: <br><li>' . implode('<li>', $ids)))
282  . '<script data-replace-marker="script">console.log("ASYNC JS, too");</script>'
283  );
284  exit();
285  }
286 
288  $out[] = $f->divider()->horizontal();
289  $out[] = $listing;
290  }
291 
292  return $r->render($out);
293 }
join($init, callable $fn)
Definition: Order.php:59
Interface Observer Contains several chained tasks and infos about them.
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:12
This is how the factory for UI elements looks.
Definition: Factory.php:37
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
$out
Definition: buildRTE.php:24
global $DIC
Definition: shib_login.php:25
base()
expected output: > ILIAS shows the rendered Component.
Definition: base.php:20
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
$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 naive range of whole positive numbers.
Definition: Range.php:28
$r