ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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(str_replace('\\', '', self::class) . '_' . $this->forum->getId())
89 ->withRange(new Range(0, 50))
90 ->withRequest($this->request);
91 }
92
96 private function getColumns(): array
97 {
98 $columns = [
99 'ranking' => $this->ui_factory->table()->column()->number(
100 $this->lng->txt('frm_statistics_ranking')
101 )->withIsSortable(true),
102 'login' => $this->ui_factory->table()->column()->text(
103 $this->lng->txt('login')
104 )->withIsSortable(true),
105 'lastname' => $this->ui_factory->table()->column()->text(
106 $this->lng->txt('lastname')
107 )->withIsSortable(true),
108 'firstname' => $this->ui_factory->table()->column()->text(
109 $this->lng->txt('firstname')
110 )->withIsSortable(true),
111 ];
112 if ($this->has_active_lp && $this->has_general_lp_access) {
113 $columns['progress'] = $this->ui_factory->table()->column()->status(
114 $this->lng->txt('learning_progress')
115 )->withIsSortable(false);
116 }
117
118 return $columns;
119 }
120
121 private function initRecords(): void
122 {
123 if ($this->records === null) {
124 $this->records = [];
125 $data = $this->forum->Forum->getUserStatistics($this->obj_properties->isPostActivationEnabled());
126 $counter = 0;
127 foreach ($data as $row) {
128 $this->records[$counter]['usr_id'] = $row['usr_id'];
129 $this->records[$counter]['ranking'] = $row['num_postings'];
130 $this->records[$counter]['login'] = $row['login'];
131 $this->records[$counter]['lastname'] = $row['lastname'];
132 $this->records[$counter]['firstname'] = $row['firstname'];
133 if ($this->has_active_lp && $this->has_general_lp_access) {
134 $this->records[$counter]['progress'] = $this->getProgressStatus($row['usr_id']);
135 }
136 ++$counter;
137 }
138 }
139 }
140
145 private function sortedRecords(array $records, Order $order): array
146 {
147 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
148
149 usort($records, static function ($left, $right) use ($order_field): int {
150 if ($order_field === 'ranking') {
151 return $left[$order_field] <=> $right[$order_field];
152 }
153
154 return ilStr::strCmp($left[$order_field], $right[$order_field]);
155 });
156
157 if ($order_direction === 'DESC') {
158 $records = array_reverse($records);
159 }
160
161 return $records;
162 }
163
167 private function getRecords(Range $range, Order $order): array
168 {
169 $this->initRecords();
170 $records = $this->sortedRecords($this->records, $order);
171
172 return $this->limitRecords($records, $range);
173 }
174
179 private function limitRecords(array $records, Range $range): array
180 {
181 return \array_slice($records, $range->getStart(), $range->getLength());
182 }
183
184 public function getRows(
185 \ILIAS\UI\Component\Table\DataRowBuilder $row_builder,
186 array $visible_column_ids,
188 Order $order,
189 mixed $additional_viewcontrol_data,
190 mixed $filter_data,
191 mixed $additional_parameters,
192 ): \Generator {
193 $records = $this->getRecords($range, $order);
194 foreach ($records as $record) {
195 $row_id = (string) $record['usr_id'];
196 yield $row_builder->buildDataRow($row_id, $record);
197 }
198 }
199
200 public function getTotalRowCount(
201 mixed $additional_viewcontrol_data,
202 mixed $filter_data,
203 mixed $additional_parameters
204 ): ?int {
205 $this->initRecords();
206
207 return \count($this->records);
208 }
209
210 private function getProgressStatus(int $user_id): string
211 {
212 $icon = '';
213 if ($this->has_active_lp &&
214 $this->has_general_lp_access &&
215 ($this->has_rbac_or_position_access || $this->actor->getId() === $user_id)) {
216 $icon = match (true) {
217 \in_array($user_id, $this->completed, false) => $this->icons->renderIconForStatus(
219 ),
220 \in_array($user_id, $this->in_progress, false) => $this->icons->renderIconForStatus(
222 ),
223 \in_array($user_id, $this->failed, false) => $this->icons->renderIconForStatus(
225 ),
226 default => $this->icons->renderIconForStatus(ilLPStatus::LP_STATUS_NOT_ATTEMPTED_NUM),
227 };
228 }
229
230 return $icon;
231 }
232}
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
__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, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters,)
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...
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:33
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31
$counter