ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
DataRetrieval.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 
31 class DataRetrieval implements \ILIAS\UI\Component\Table\DataRetrieval
32 {
33  public function __construct(
34  protected readonly \ilObjTest $test_obj,
35  protected readonly TestTopListRepository $repository,
36  protected readonly \ilLanguage $lng,
37  protected readonly \ilObjUser $user,
38  protected readonly UIFactory $ui_factory,
39  protected readonly UIRenderer $ui_renderer,
40  protected readonly DataFactory $data_factory,
41  protected readonly TopListType $list_type,
42  protected readonly TopListOrder $order_by
43  ) {
44  }
45 
46  public function getColumns(): array
47  {
48  $column_factory = $this->ui_factory->table()->column();
49  $iconActor = $this->ui_factory->symbol()->icon()->standard(Icon::USR, 'me');
50 
51  $columns = [
52  'is_actor' => $column_factory->boolean('', $iconActor, ''),
53  'rank' => $column_factory->number($this->lng->txt('toplist_col_rank'))->withUnit('.'),
54  'participant' => $column_factory->text($this->lng->txt('toplist_col_participant')),
55  'achieved' => $column_factory->date(
56  $this->lng->txt('toplist_col_achieved'),
57  $this->data_factory->dateFormat()->withTime24($this->data_factory->dateFormat()->standard())
58  ),
59  'score' => $column_factory->text($this->lng->txt('toplist_col_score')),
60  'percentage' => $column_factory->number($this->lng->txt('toplist_col_percentage'))->withUnit('%'),
61  'workingtime' => $column_factory->text($this->lng->txt('toplist_col_wtime')),
62  ];
63 
64  $optional_columns = [
65  'achieved' => $this->test_obj->getHighscoreAchievedTS(),
66  'score' => $this->test_obj->getHighscoreScore(),
67  'percentage' => $this->test_obj->getHighscorePercentage(),
68  'workingtime' => $this->test_obj->getHighscoreWTime()
69  ];
70 
71  $list = [];
72  foreach ($columns as $key => $column) {
73  if (isset($optional_columns[$key]) && !$optional_columns[$key]) {
74  continue;
75  }
76  $list[$key] = $column->withIsOptional(false, true)->withIsSortable(false);
77  }
78  return $list;
79  }
80 
81  public function getRows(
82  DataRowBuilder $row_builder,
83  array $visible_column_ids,
84  Range $range,
85  Order $order,
86  ?array $filter_data,
87  ?array $additional_parameters
88  ): \Generator {
89  foreach ($this->loadToplistData() as $i => $row) {
90  $item = [
91  'rank' => ($i + 1),
92  'participant' => $this->test_obj->isHighscoreAnon() && (int) $row['usr_id'] !== $this->user->getId()
93  ? '-, -'
94  : $row['lastname'] . ', ' . $row['firstname'],
95  'is_actor' => ((int) $row['usr_id'] === $this->user->getId())
96  ];
97 
98  if (in_array('achieved', $visible_column_ids, true)) {
99  $item['achieved'] = new \DateTimeImmutable('@' . $row['tstamp']);
100  }
101  if (in_array('score', $visible_column_ids, true)) {
102  $item['score'] = $row['reached_points'] . ' / ' . $row['max_points'];
103  }
104  if (in_array('percentage', $visible_column_ids, true)) {
105  $item['percentage'] = $row['percentage'];
106  }
107  if (in_array('workingtime', $visible_column_ids, true)) {
108  $item['workingtime'] = $this->formatTime($row['workingtime']);
109  }
110 
111  yield $row_builder->buildDataRow((string) $i, $item);
112  }
113  }
114 
115  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
116  {
117  // return 0 here to avoid pagination in the table. This is the same behavior as in Ilias 8/9
118  return 0;
119  }
120 
121  private function loadToplistData(): \Generator
122  {
123  if ($this->list_type === TopListType::USER) {
124  return $this->order_by === TopListOrder::BY_SCORE
125  ? $this->repository->getUserToplistByPercentage($this->test_obj, $this->user->getId())
126  : $this->repository->getUserToplistByWorkingtime($this->test_obj, $this->user->getId());
127  } else {
128  return $this->repository->getGeneralToplist($this->test_obj, $this->order_by);
129  }
130  }
131 
132  public function formatTime(int $seconds): string
133  {
134  $hours = intdiv($seconds, 3600);
135  $minutes = intdiv($seconds % 3600, 60);
136  $seconds = $seconds % 60;
137 
138  return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
139  }
140 }
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
repository()
description: > Example for rendering a repository card
Definition: repository.php:33
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
buildDataRow(string $id, array $record)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e...
global $lng
Definition: privfeed.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
__construct(protected readonly \ilObjTest $test_obj, protected readonly TestTopListRepository $repository, protected readonly \ilLanguage $lng, protected readonly \ilObjUser $user, protected readonly UIFactory $ui_factory, protected readonly UIRenderer $ui_renderer, protected readonly DataFactory $data_factory, protected readonly TopListType $list_type, protected readonly TopListOrder $order_by)