ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
DataRetrieval.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data\Factory as DataFactory;
29use ILIAS\UI\Factory as UIFactory;
30use ILIAS\UI\Renderer as UIRenderer;
31
33{
34 public function __construct(
35 protected readonly \ilObjTest $test_obj,
36 protected readonly TestTopListRepository $repository,
37 protected readonly \ilLanguage $lng,
38 protected readonly \ilObjUser $user,
39 protected readonly UIFactory $ui_factory,
40 protected readonly UIRenderer $ui_renderer,
41 protected readonly DataFactory $data_factory,
42 protected readonly TopListType $list_type,
43 protected readonly TopListOrder $order_by,
44 protected readonly ParticipantRepository $participant_repository
45 ) {
46 }
47
48 public function getColumns(): array
49 {
50 $column_factory = $this->ui_factory->table()->column();
51 $iconActor = $this->ui_factory->symbol()->icon()->standard(Icon::USR, 'me');
52
53 $columns = [
54 'is_actor' => $column_factory->boolean('', $iconActor, ''),
55 'rank' => $column_factory->text($this->lng->txt('toplist_col_rank')),
56 'participant' => $column_factory->text($this->lng->txt('toplist_col_participant')),
57 'achieved' => $column_factory->date(
58 $this->lng->txt('toplist_col_achieved'),
59 $this->data_factory->dateFormat()->withTime24($this->data_factory->dateFormat()->standard())
60 ),
61 'score' => $column_factory->text($this->lng->txt('toplist_col_score')),
62 'percentage' => $column_factory->number($this->lng->txt('toplist_col_percentage'))->withUnit('%'),
63 'workingtime' => $column_factory->text($this->lng->txt('toplist_col_wtime')),
64 ];
65
66 $optional_columns = [
67 'achieved' => $this->test_obj->getHighscoreAchievedTS(),
68 'score' => $this->test_obj->getHighscoreScore(),
69 'percentage' => $this->test_obj->getHighscorePercentage(),
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,
87 Order $order,
88 mixed $additional_viewcontrol_data,
89 mixed $filter_data,
90 mixed $additional_parameters
91 ): \Generator {
92 foreach ($this->loadToplistData() as $row) {
93 $item = $this->buildBasicItemFromRowArray($row);
94
95 if (isset($row['tstamp']) && in_array('achieved', $visible_column_ids, true)) {
96 $item['achieved'] = new \DateTimeImmutable('@' . $row['tstamp']);
97 }
98 if (isset($row['reached_points']) && in_array('score', $visible_column_ids, true)) {
99 $item['score'] = $row['reached_points'] . ' / ' . $row['max_points'];
100 }
101 if (isset($row['percentage']) && in_array('percentage', $visible_column_ids, true)) {
102 $item['percentage'] = $row['percentage'];
103 }
104 if (isset($row['workingtime']) && in_array('workingtime', $visible_column_ids, true)) {
105 $item['workingtime'] = $this->formatTime($row['workingtime']);
106 }
107
108 yield $row_builder->buildDataRow((string) $row['rank'], $item);
109 }
110 }
111
112 public function getTotalRowCount(
113 mixed $additional_viewcontrol_data,
114 mixed $filter_data,
115 mixed $additional_parameters
116 ): ?int {
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::GENERAL) {
124 return $this->repository->getGeneralToplist($this->test_obj, $this->order_by);
125 }
126
127 if ($this->order_by === TopListOrder::BY_SCORE) {
128 return $this->repository->getUserToplistByPercentage($this->test_obj, $this->user->getId());
129 }
130
131 return $this->repository->getUserToplistByWorkingtime($this->test_obj, $this->user->getId());
132 }
133
134 public function formatTime(int $seconds): string
135 {
136 $hours = intdiv($seconds, 3600);
137 $minutes = intdiv($seconds % 3600, 60);
138 $seconds = $seconds % 60;
139
140 return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
141 }
142
143 private function buildBasicItemFromRowArray(array $row): array
144 {
145 if ($row['rank'] === '...') {
146 return [
147 'rank' => '...',
148 'is_actor' => false
149 ];
150 }
151
152 return [
153 'rank' => "{$row['rank']}.",
154 'participant' => $this->test_obj->isHighscoreAnon() && (int) $row['usr_id'] !== $this->user->getId()
155 ? '-, -'
156 : $this->participant_repository->getParticipantByActiveId($this->test_obj->getTestId(), $row['active_id'])->getDisplayName($this->lng),
157 'is_actor' => isset($row['usr_id']) && ((int) $row['usr_id'] === $this->user->getId())
158 ];
159 }
160}
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
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
__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, protected readonly ParticipantRepository $participant_repository)
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
language handling
User class.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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