ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DataRetrieval.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data\Factory as DataFactory;
28use ILIAS\UI\Factory as UIFactory;
29use ILIAS\UI\Renderer as UIRenderer;
30
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->text($this->lng->txt('toplist_col_rank')),
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,
85 Order $order,
86 ?array $filter_data,
87 ?array $additional_parameters
88 ): \Generator {
89 foreach ($this->loadToplistData() as $row) {
90 $item = $this->buildBasicItemFromRowArray($row);
91
92 if (isset($row['tstamp']) && in_array('achieved', $visible_column_ids, true)) {
93 $item['achieved'] = new \DateTimeImmutable('@' . $row['tstamp']);
94 }
95 if (isset($row['reached_points']) && in_array('score', $visible_column_ids, true)) {
96 $item['score'] = $row['reached_points'] . ' / ' . $row['max_points'];
97 }
98 if (isset($row['percentage']) && in_array('percentage', $visible_column_ids, true)) {
99 $item['percentage'] = $row['percentage'];
100 }
101 if (isset($row['workingtime']) && in_array('workingtime', $visible_column_ids, true)) {
102 $item['workingtime'] = $this->formatTime($row['workingtime']);
103 }
104
105 yield $row_builder->buildDataRow((string) $row['rank'], $item);
106 }
107 }
108
109 public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
110 {
111 // return 0 here to avoid pagination in the table. This is the same behavior as in Ilias 8/9
112 return 0;
113 }
114
115 private function loadToplistData(): \Generator
116 {
117 if ($this->list_type === TopListType::GENERAL) {
118 return $this->repository->getGeneralToplist($this->test_obj, $this->order_by);
119 }
120
121 if ($this->order_by === TopListOrder::BY_SCORE) {
122 return $this->repository->getUserToplistByPercentage($this->test_obj, $this->user->getId());
123 }
124
125 return $this->repository->getUserToplistByWorkingtime($this->test_obj, $this->user->getId());
126 }
127
128 public function formatTime(int $seconds): string
129 {
130 $hours = intdiv($seconds, 3600);
131 $minutes = intdiv($seconds % 3600, 60);
132 $seconds = $seconds % 60;
133
134 return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
135 }
136
137 private function buildBasicItemFromRowArray(array $row): array
138 {
139 if ($row['rank'] === '...') {
140 return [
141 'rank' => '...',
142 'is_actor' => false
143 ];
144 }
145
146 return [
147 'rank' => "{$row['rank']}.",
148 'participant' => $this->test_obj->isHighscoreAnon() && (int) $row['usr_id'] !== $this->user->getId()
149 ? '-, -'
150 : $row['lastname'] . ', ' . $row['firstname'],
151 'is_actor' => isset($row['usr_id']) && ((int) $row['usr_id'] === $this->user->getId())
152 ];
153 }
154}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
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....
__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)
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...
language handling
User class.
This describes the specific behavior of an ILIAS standard icon.
Definition: Standard.php:27
buildDataRow(string $id, array $record)
An entity that renders components to a string output.
Definition: Renderer.php:31
global $lng
Definition: privfeed.php:31