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