ILIAS  trunk Revision v11.0_alpha-1744-gb0451eebef4
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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  'hints' => $column_factory->number($this->lng->txt('toplist_col_hints')),
62  'workingtime' => $column_factory->text($this->lng->txt('toplist_col_wtime')),
63  ];
64 
65  $optional_columns = [
66  'achieved' => $this->test_obj->getHighscoreAchievedTS(),
67  'score' => $this->test_obj->getHighscoreScore(),
68  'percentage' => $this->test_obj->getHighscorePercentage(),
69  'hints' => $this->test_obj->getHighscoreHints(),
70  'workingtime' => $this->test_obj->getHighscoreWTime()
71  ];
72 
73  $list = [];
74  foreach ($columns as $key => $column) {
75  if (isset($optional_columns[$key]) && !$optional_columns[$key]) {
76  continue;
77  }
78  $list[$key] = $column->withIsOptional(false, true)->withIsSortable(false);
79  }
80  return $list;
81  }
82 
83  public function getRows(
84  DataRowBuilder $row_builder,
85  array $visible_column_ids,
86  Range $range,
87  Order $order,
88  ?array $filter_data,
89  ?array $additional_parameters
90  ): \Generator {
91  foreach ($this->loadToplistData() as $i => $row) {
92  $item = [
93  'rank' => ($i + 1),
94  'participant' => $this->test_obj->isHighscoreAnon() && (int) $row['usr_id'] !== $this->user->getId()
95  ? '-, -'
96  : $row['lastname'] . ', ' . $row['firstname'],
97  'is_actor' => ((int) $row['usr_id'] === $this->user->getId())
98  ];
99 
100  if (in_array('achieved', $visible_column_ids, true)) {
101  $item['achieved'] = new \DateTimeImmutable('@' . $row['tstamp']);
102  }
103  if (in_array('score', $visible_column_ids, true)) {
104  $item['score'] = $row['reached_points'] . ' / ' . $row['max_points'];
105  }
106  if (in_array('percentage', $visible_column_ids, true)) {
107  $item['percentage'] = $row['percentage'];
108  }
109  if (in_array('hints', $visible_column_ids, true)) {
110  $item['hints'] = $row['hint_count'];
111  }
112  if (in_array('workingtime', $visible_column_ids, true)) {
113  $item['workingtime'] = $this->formatTime($row['workingtime']);
114  }
115 
116  yield $row_builder->buildDataRow((string) $i, $item);
117  }
118  }
119 
120  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
121  {
122  // return 0 here to avoid pagination in the table. This is the same behavior as in Ilias 8/9
123  return 0;
124  }
125 
126  private function loadToplistData(): \Generator
127  {
128  if ($this->list_type === TopListType::USER) {
129  return $this->order_by === TopListOrder::BY_SCORE
130  ? $this->repository->getUserToplistByPercentage($this->test_obj, $this->user->getId())
131  : $this->repository->getUserToplistByWorkingtime($this->test_obj, $this->user->getId());
132  } else {
133  return $this->repository->getGeneralToplist($this->test_obj, $this->order_by);
134  }
135  }
136 
137  public function formatTime(int $seconds): string
138  {
139  $hours = intdiv($seconds, 3600);
140  $minutes = intdiv($seconds % 3600, 60);
141  $seconds = $seconds % 60;
142 
143  return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
144  }
145 }
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)