ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ForumStatisticsTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\UI\Component\Table\Data as DataTable;
24use ILIAS\UI\Factory as UIFactory;
25use Psr\Http\Message\ServerRequestInterface as HttpRequest;
29use ilLPStatus;
30use ilStr;
32use ilObjectLP;
34use ilLanguage;
35use ilObjUser;
37use ilObjForum;
38
40{
41 private bool $has_active_lp = false;
43 private array $completed = [];
45 private array $failed = [];
47 private array $in_progress = [];
51 private ?array $records = null;
52 private readonly ilLPStatusIcons $icons;
53
54 public function __construct(
55 private readonly ilObjForum $forum,
56 private readonly ilForumProperties $obj_properties,
57 private readonly bool $has_general_lp_access,
58 private readonly bool $has_rbac_or_position_access,
59 private readonly ilObjUser $actor,
60 private readonly UIFactory $ui_factory,
61 private readonly HttpRequest $request,
62 private readonly ilLanguage $lng,
63 ) {
65
66 $lp = ilObjectLP::getInstance($forum->getId());
67 if ($lp->isActive()) {
68 $this->has_active_lp = true;
69 }
70
71 if ($this->has_active_lp && $this->has_general_lp_access) {
72 $this->lng->loadLanguageModule('trac');
73 $this->completed = ilLPStatusWrapper::_lookupCompletedForObject($forum->getId());
74 $this->in_progress = ilLPStatusWrapper::_lookupInProgressForObject($forum->getId());
75 $this->failed = ilLPStatusWrapper::_lookupFailedForObject($forum->getId());
76 }
77 }
78
79 public function getComponent(): DataTable
80 {
81 return $this->ui_factory
82 ->table()
83 ->data(
84 $this,
85 $this->lng->txt('frm_moderators'),
86 $this->getColumns()
87 )
88 ->withId(self::class . '_' . $this->forum->getId())
89 ->withRequest($this->request);
90 }
91
95 private function getColumns(): array
96 {
97 $columns = [
98 'ranking' => $this->ui_factory->table()->column()->number(
99 $this->lng->txt('frm_statistics_ranking')
100 )->withIsSortable(true),
101 'login' => $this->ui_factory->table()->column()->text(
102 $this->lng->txt('login')
103 )->withIsSortable(true),
104 'lastname' => $this->ui_factory->table()->column()->text(
105 $this->lng->txt('lastname')
106 )->withIsSortable(true),
107 'firstname' => $this->ui_factory->table()->column()->text(
108 $this->lng->txt('firstname')
109 )->withIsSortable(true),
110 ];
111 if ($this->has_active_lp && $this->has_general_lp_access) {
112 $columns['progress'] = $this->ui_factory->table()->column()->status(
113 $this->lng->txt('learning_progress')
114 )->withIsSortable(false);
115 }
116
117 return $columns;
118 }
119
120 private function initRecords(): void
121 {
122 if ($this->records === null) {
123 $this->records = [];
124 $data = $this->forum->Forum->getUserStatistics($this->obj_properties->isPostActivationEnabled());
125 $counter = 0;
126 foreach ($data as $row) {
127 $this->records[$counter]['usr_id'] = $row['usr_id'];
128 $this->records[$counter]['ranking'] = $row['num_postings'];
129 $this->records[$counter]['login'] = $row['login'];
130 $this->records[$counter]['lastname'] = $row['lastname'];
131 $this->records[$counter]['firstname'] = $row['firstname'];
132 if ($this->has_active_lp && $this->has_general_lp_access) {
133 $this->records[$counter]['progress'] = $this->getProgressStatus($row['usr_id']);
134 }
135 ++$counter;
136 }
137 }
138 }
139
144 private function sortedRecords(array $records, Order $order): array
145 {
146 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
147
148 usort($records, static function ($left, $right) use ($order_field): int {
149 if ($order_field === 'ranking') {
150 return $left[$order_field] <=> $right[$order_field];
151 }
152
153 return ilStr::strCmp($left[$order_field], $right[$order_field]);
154 });
155
156 if ($order_direction === 'DESC') {
157 $records = array_reverse($records);
158 }
159
160 return $records;
161 }
162
166 private function getRecords(Range $range, Order $order): array
167 {
168 $this->initRecords();
169 $records = $this->sortedRecords($this->records, $order);
170
171 return $this->limitRecords($records, $range);
172 }
173
178 private function limitRecords(array $records, Range $range): array
179 {
180 return \array_slice($records, $range->getStart(), $range->getLength());
181 }
182
183 public function getRows(
184 \ILIAS\UI\Component\Table\DataRowBuilder $row_builder,
185 array $visible_column_ids,
187 Order $order,
188 ?array $filter_data,
189 ?array $additional_parameters,
190 ): \Generator {
191 $records = $this->getRecords($range, $order);
192 foreach ($records as $record) {
193 $row_id = (string) $record['usr_id'];
194 yield $row_builder->buildDataRow($row_id, $record);
195 }
196 }
197
198 public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
199 {
200 $this->initRecords();
201
202 return \count($this->records);
203 }
204
205 private function getProgressStatus(int $user_id): string
206 {
207 $icon = '';
208 if ($this->has_active_lp &&
209 $this->has_general_lp_access &&
210 ($this->has_rbac_or_position_access || $this->actor->getId() === $user_id)) {
211 $icon = match (true) {
212 \in_array($user_id, $this->completed, false) => $this->icons->renderIconForStatus(
214 ),
215 \in_array($user_id, $this->in_progress, false) => $this->icons->renderIconForStatus(
217 ),
218 \in_array($user_id, $this->failed, false) => $this->icons->renderIconForStatus(
220 ),
221 default => $this->icons->renderIconForStatus(ilLPStatus::LP_STATUS_NOT_ATTEMPTED_NUM),
222 };
223 }
224
225 return $icon;
226 }
227}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
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...
__construct(private readonly ilObjForum $forum, private readonly ilForumProperties $obj_properties, private readonly bool $has_general_lp_access, private readonly bool $has_rbac_or_position_access, private readonly ilObjUser $actor, private readonly UIFactory $ui_factory, private readonly HttpRequest $request, private readonly ilLanguage $lng,)
getRows(\ILIAS\UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters,)
Definition: UI.php:24
Caches and supplies the paths to the learning progress status images.
static getInstance(int $variant=ilLPStatusIcons::ICON_VARIANT_DEFAULT, ?\ILIAS\UI\Renderer $renderer=null, ?\ILIAS\UI\Factory $factory=null)
Class ilLPStatusWrapper This class is wrapper for all ilLPStatus classes.
static _lookupCompletedForObject(int $a_obj_id, ?array $a_user_ids=null)
static _lookupFailedForObject(int $a_obj_id, ?array $a_user_ids=null)
static _lookupInProgressForObject(int $a_obj_id, ?array $a_user_ids=null)
Abstract class ilLPStatus for all learning progress modes E.g ilLPStatusManual, ilLPStatusObjectives ...
const LP_STATUS_COMPLETED_NUM
const LP_STATUS_IN_PROGRESS_NUM
const LP_STATUS_NOT_ATTEMPTED_NUM
const LP_STATUS_FAILED_NUM
language handling
Class ilObjForum.
User class.
Base class for object lp connectors.
static getInstance(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.ilStr.php:20
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
This describes a Data Table.
Definition: Data.php:31
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31
$counter