ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
repo_implementation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
11 
19 {
32  global $DIC;
33  $r = $DIC['ui.renderer'];
34 
35  $repo = new DataTableDemoRepo();
36  $table = $repo->getTableForRepresentation();
37 
38  return $r->render(
39  $table->withRequest($DIC->http()->request())
40  );
41 }
42 
43 class DataTableDemoRepo implements I\DataRetrieval
44 {
45  protected \ILIAS\UI\Factory $ui_factory;
46  protected \ILIAS\Data\Factory $df;
47 
48  public function __construct()
49  {
50  global $DIC;
51  $this->ui_factory = $DIC['ui.factory'];
52  $this->df = new \ILIAS\Data\Factory();
53  }
54 
55  //the repo is capable of building its table-view (similar to forms from a repo)
57  {
58  return $this->ui_factory->table()->data(
59  'a data table from a repository',
61  $this
62  );
63  }
64 
65  //implementation of DataRetrieval - accept params and yield rows
66  public function getRows(
67  I\DataRowBuilder $row_builder,
68  array $visible_column_ids,
69  Range $range,
70  Order $order,
71  ?array $filter_data,
72  ?array $additional_parameters
73  ): \Generator {
74  $icons = [
75  $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_checked.svg', '', 'small'),
76  $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_unchecked.svg', '', 'small')
77  ];
78  foreach ($this->doSelect($order, $range) as $idx => $record) {
79  $row_id = (string) $record['usr_id'];
80  $record['achieve_txt'] = $record['achieve'] > 80 ? 'passed' : 'failed';
81  $record['failure_txt'] = "not " . $record["achieve_txt"];
82  $record['repeat'] = $record['achieve'] < 80;
83  $record['achieve_icon'] = $icons[(int) $record['achieve'] > 80];
84  yield $row_builder->buildDataRow($row_id, $record);
85  }
86  }
87 
88  public function getTotalRowCount(
89  ?array $filter_data,
90  ?array $additional_parameters
91  ): ?int {
92  return count($this->dummyrecords());
93  }
94 
95  //do the actual reading - note, that e.g. order and range are easily converted to SQL
96  protected function doSelect(Order $order, Range $range): array
97  {
98  $sql_order_part = $order->join('ORDER BY', fn(...$o) => implode(' ', $o));
99  $sql_range_part = sprintf('LIMIT %2$s OFFSET %1$s', ...$range->unpack());
100  return array_map(
101  fn($rec) => array_merge($rec, ['sql_order' => $sql_order_part, 'sql_range' => $sql_range_part]),
102  $this->dummyrecords()
103  );
104  }
105 
106  //this is how the UI-Table looks - and that's usually quite close to the db-table
107  protected function getColumsForRepresentation(): array
108  {
110  return [
111  'usr_id' => $f->table()->column()->number("User ID")
112  ->withIsSortable(false),
113  'login' => $f->table()->column()->text("Login")
114  ->withHighlight(true),
115  'email' => $f->table()->column()->eMail("eMail"),
116  'last' => $f->table()->column()->date("last login", $this->df->dateFormat()->germanLong()),
117  'achieve' => $f->table()->column()->status("progress")
118  ->withIsOptional(true),
119  'achieve_txt' => $f->table()->column()->status("success")
120  ->withIsSortable(false)
121  ->withIsOptional(true),
122  'failure_txt' => $f->table()->column()->status("failure")
123  ->withIsSortable(false)
124  ->withIsOptional(true, false),
125  'achieve_icon' => $f->table()->column()->statusIcon("achieved")
126  ->withIsOptional(true),
127  'repeat' => $f->table()->column()->boolean("repeat", 'yes', 'no')
128  ->withIsSortable(false),
129  'fee' => $f->table()->column()->number("Fee")
130  ->withDecimals(2)
131  ->withUnit('£', I\Column\Number::UNIT_POSITION_FORE),
132  'sql_order' => $f->table()->column()->text("sql order part")
133  ->withIsSortable(false)
134  ->withIsOptional(true),
135  'sql_range' => $f->table()->column()->text("sql range part")
136  ->withIsSortable(false)
137  ->withIsOptional(true)
138  ];
139  }
140 
141  protected function dummyrecords()
142  {
143  return [
144  ['usr_id' => 123,'login' => 'superuser','email' => 'user@example.com',
145  'last' => new \DateTimeImmutable(),'achieve' => 20,'fee' => 0
146  ],
147  ['usr_id' => 867,'login' => 'student1','email' => 'student1@example.com',
148  'last' => new \DateTimeImmutable(),'achieve' => 90,'fee' => 40
149  ],
150  ['usr_id' => 8923,'login' => 'student2','email' => 'student2@example.com',
151  'last' => new \DateTimeImmutable(),'achieve' => 66,'fee' => 36.789
152  ],
153  ['usr_id' => 8748,'login' => 'student3_longname','email' => 'student3_long_email@example.com',
154  'last' => new \DateTimeImmutable(),'achieve' => 66,'fee' => 36.789
155  ]
156  ];
157  }
158 }
join($init, callable $fn)
Definition: Order.php:59
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:12
repo_implementation()
expected output: > ILIAS shows the rendered Component.
global $DIC
Definition: shib_login.php:25
getRows(I\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
$r