ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ParticipantTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Test\Participants;
22 
23 use ILIAS\Test\Results\Data\Factory as ResultsDataFactory;
38 
40 {
41  private const ID = 'pt';
42  private ?iterable $records = null;
43 
44  public function __construct(
45  private readonly UIFactory $ui_factory,
46  private readonly \ilUIService $ui_service,
47  private readonly Language $lng,
48  private readonly \ilTestAccess $test_access,
49  private readonly RequestDataCollector $test_request,
50  private readonly \ilTestParticipantAccessFilterFactory $participant_access_filter,
51  private readonly ParticipantRepository $repository,
52  private readonly ResultsDataFactory $results_data_factory,
53  private readonly ResultsPresentationSettings $results_presentation_settings,
54  private readonly \ilObjUser $current_user,
55  private readonly \ilObjTest $test_object,
56  private readonly ParticipantTableActions $table_actions
57  ) {
58  }
59 
60  public function execute(URLBuilder $url_builder): ?Modal
61  {
62  return $this->table_actions->execute(...$this->acquireParameters($url_builder));
63  }
64 
68  public function getComponents(URLBuilder $url_builder, string $filter_url): array
69  {
70  $filter = $this->getFilterComponent($filter_url, $this->test_request->getRequest());
71  $table = $this->getTableComponent(
72  $this->test_request->getRequest(),
73  $this->ui_service->filter()->getData($filter)
74  );
75 
76  return [
77  $filter,
78  $table->withActions($this->table_actions->getEnabledActions(...$this->acquireParameters($url_builder)))
79  ];
80  }
81 
82  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
83  {
84  return $this->repository->countParticipants($this->test_object->getTestId(), $filter_data);
85  }
86 
87  public function getRows(
88  DataRowBuilder $row_builder,
89  array $visible_column_ids,
90  Range $range,
91  Order $order,
92  ?array $filter_data,
93  ?array $additional_parameters
94  ): \Generator {
95  $processing_time = $this->test_object->getProcessingTimeInSeconds();
96  $reset_time_on_new_attempt = $this->test_object->getResetProcessingTime();
97 
98  $current_user_timezone = new \DateTimeZone($this->current_user->getTimeZone());
99 
101  foreach ($this->getViewControlledRecords($filter_data, $range, $order) as $record) {
102  $total_duration = $record->getTotalDuration($processing_time);
103  $status_of_attempt = $record->getAttemptOverviewInformation()?->getStatusOfAttempt() ?? StatusOfAttempt::NOT_YET_STARTED;
104 
105  $row = [
106  'name' => $this->test_object->buildName($record->getUserId(), $record->getLastname(), $record->getFirstname()),
107  'login' => $record->getLogin(),
108  'matriculation' => $record->getMatriculation(),
109  'total_time_on_task' => $record->getAttemptOverviewInformation()?->getHumanReadableTotalTimeOnTask() ?? '',
110  'status_of_attempt' => $this->lng->txt($status_of_attempt->value),
111  'id_of_attempt' => $record->getAttemptOverviewInformation()?->getExamId(),
112  'ip_range' => $record->getClientIpTo() !== '' || $record->getClientIpFrom() !== ''
113  ? sprintf('%s - %s', $record->getClientIpFrom(), $record->getClientIpTo())
114  : '',
115  'total_attempts' => $record->getAttemptOverviewInformation()?->getNrOfAttempts() ?? 0,
116  'extra_time' => $record->getExtraTime() > 0 ? sprintf('%d min', $record->getExtraTime()) : '',
117  'total_duration' => $total_duration > 0 ? sprintf('%d min', $total_duration / 60) : '',
118  'remaining_duration' => sprintf('%d min', $record->getRemainingDuration($processing_time, $reset_time_on_new_attempt) / 60),
119  ];
120 
121  $first_access = $record->getAttemptOverviewInformation()?->getStartedDate();
122  if ($first_access !== null) {
123  $row['attempt_started_at'] = $first_access->setTimezone($current_user_timezone);
124  }
125 
126  $last_access = $record->getLastAccess();
127  if ($last_access !== null) {
128  $row['last_access'] = $last_access->setTimezone($current_user_timezone);
129  }
130  if ($record->getActiveId() !== null
131  && $this->test_access->checkResultsAccessForActiveId(
132  $record->getActiveId(),
133  $this->test_object->getTestId()
134  ) || $record === null && $this->test_access->checkParticipantsResultsAccess()) {
135  $row['reached_points'] = sprintf(
136  $this->lng->txt('tst_reached_points_of_max'),
137  $record->getAttemptOverviewInformation()?->getReachedPoints(),
138  $record->getAttemptOverviewInformation()?->getAvailablePoints()
139  );
140  $row['nr_of_answered_questions'] = sprintf(
141  $this->lng->txt('tst_answered_questions_of_total'),
142  $record->getAttemptOverviewInformation()?->getNrOfAnsweredQuestions(),
143  $record->getAttemptOverviewInformation()?->getNrOfTotalQuestions()
144  );
145  $row['percent_of_available_points'] = $record->getAttemptOverviewInformation()?->getReachedPointsInPercent();
146  }
147 
148  if ($status_of_attempt->isFinished()) {
149  $row['test_passed'] = $record->getAttemptOverviewInformation()?->hasPassingMark() ?? false;
150  $row['mark'] = $record->getAttemptOverviewInformation()?->getMark();
151  }
152 
153  yield $this->table_actions->onDataRow(
154  $row_builder->buildDataRow((string) $record->getUserId(), $row),
155  $record
156  );
157  }
158  }
159 
160  private function acquireParameters($url_builder): array
161  {
162  return $url_builder->acquireParameters(
163  [self::ID],
167  );
168  }
169 
173  private function getPostLoadFilters(): array
174  {
175  return [
176  'solution' => fn(string $value, Participant $record) =>
177  $value === 'true' ? $record->hasAnsweredQuestionsForScoredAttempt() : !$record->hasAnsweredQuestionsForScoredAttempt(),
178  'status_of_attempt' => fn(string $value, Participant $record) =>
179  ($value === StatusOfAttempt::NOT_YET_STARTED->value && $record->getAttemptOverviewInformation()?->getStatusOfAttempt() === null) ||
180  $value === $record->getAttemptOverviewInformation()?->getStatusOfAttempt()->value,
181  'test_passed' => fn(string $value, Participant $record) => $value === 'true'
182  ? $record->getAttemptOverviewInformation()?->hasPassingMark() === true
183  : $record->getAttemptOverviewInformation()?->hasPassingMark() !== true
184  ];
185  }
186 
190  private function getPostLoadOrderFields(): array
191  {
192  $processing_time = $this->test_object->getProcessingTimeInSeconds();
193  $reset_time_on_new_attempt = $this->test_object->getResetProcessingTime();
194 
195  return [
196  'attempt_started_at' => static fn(Participant $a, Participant $b) => $a->getFirstAccess() <=> $b->getFirstAccess(),
197  'total_duration' => static fn(
198  Participant $a,
199  Participant $b
200  ) => $a->getTotalDuration($processing_time) <=> $b->getTotalDuration($processing_time),
201  'remaining_duration' => static fn(
202  Participant $a,
203  Participant $b
204  ) => $a->getRemainingDuration($processing_time, $reset_time_on_new_attempt)
205  <=> $b->getRemainingDuration($processing_time, $reset_time_on_new_attempt),
206  'last_access' => static fn(Participant $a, Participant $b) => $a->getLastAccess() <=> $b->getLastAccess(),
207  'status_of_attempt' => static fn(
208  Participant $a,
209  Participant $b
210  ) => $a->getAttemptOverviewInformation()?->getStatusOfAttempt()
211  <=> $b->getAttemptOverviewInformation()?->getStatusOfAttempt(),
212  'reached_points' => static fn(
213  Participant $a,
214  Participant $b
215  ) => $a->getAttemptOverviewInformation()?->getReachedPoints()
216  <=> $b->getAttemptOverviewInformation()?->getReachedPoints(),
217  'nr_of_answered_questions' => static fn(
218  Participant $a,
219  Participant $b
220  ) => $a->getAttemptOverviewInformation()?->getNrOfAnsweredQuestions()
221  <=> $b->getAttemptOverviewInformation()?->getNrOfAnsweredQuestions(),
222  'percent_of_available_points' => static fn(
223  Participant $a,
224  Participant $b
225  ) => $a->getAttemptOverviewInformation()?->getReachedPointsInPercent()
226  <=> $b->getAttemptOverviewInformation()?->getReachedPointsInPercent(),
227  'test_passed' => static fn(
228  Participant $a,
229  Participant $b
230  ) => $a->getAttemptOverviewInformation()?->hasPassingMark()
231  <=> $b->getAttemptOverviewInformation()?->hasPassingMark(),
232  'mark' => static fn(
233  Participant $a,
234  Participant $b
235  ) => $a->getAttemptOverviewInformation()?->getMark() <=> $b->getAttemptOverviewInformation()?->getMark(),
236  'matriculation' => static fn(
237  Participant $a,
238  Participant $b
239  ) => $a->getMatriculation() <=> $b->getMatriculation(),
240  'id_of_attempt' => static fn(
241  Participant $a,
242  Participant $b
243  ) => $a->getAttemptOverviewInformation()?->getExamId() <=> $b->getAttemptOverviewInformation()?->getExamId()
244  ];
245  }
246 
247  private function getFilterComponent(string $action, ServerRequestInterface $request): FilterComponent
248  {
249  $filter_inputs = [];
250  $is_input_initially_rendered = [];
251  $field_factory = $this->ui_factory->input()->field();
252 
253  foreach ($this->getFilterFields($field_factory) as $filter_id => $filter) {
254  [$filter_inputs[$filter_id], $is_input_initially_rendered[$filter_id]] = $filter;
255  }
256 
257  return $this->ui_service->filter()->standard(
258  'participant_filter',
259  $action,
260  $filter_inputs,
261  $is_input_initially_rendered,
262  true,
263  true
264  );
265  }
266 
272  private function getFilterFields(FieldFactory $field_factory): array
273  {
274  $yes_no_all_options = [
275  'true' => $this->lng->txt('yes'),
276  'false' => $this->lng->txt('no')
277  ];
278 
279  $solution_options = [
280  'false' => $this->lng->txt('without_solution'),
281  'true' => $this->lng->txt('with_solution')
282  ];
283 
284  $status_of_attempt_options = [
285  StatusOfAttempt::NOT_YET_STARTED->value => $this->lng->txt(StatusOfAttempt::NOT_YET_STARTED->value),
286  StatusOfAttempt::RUNNING->value => $this->lng->txt(StatusOfAttempt::RUNNING->value),
287  StatusOfAttempt::FINISHED_BY_UNKNOWN->value => $this->lng->txt(StatusOfAttempt::FINISHED_BY_UNKNOWN->value),
288  StatusOfAttempt::FINISHED_BY_ADMINISTRATOR->value => $this->lng->txt(StatusOfAttempt::FINISHED_BY_ADMINISTRATOR->value),
289  StatusOfAttempt::FINISHED_BY_CRONJOB->value => $this->lng->txt(StatusOfAttempt::FINISHED_BY_CRONJOB->value),
290  StatusOfAttempt::FINISHED_BY_DURATION->value => $this->lng->txt(StatusOfAttempt::FINISHED_BY_DURATION->value),
291  StatusOfAttempt::FINISHED_BY_PARTICIPANT->value => $this->lng->txt(StatusOfAttempt::FINISHED_BY_PARTICIPANT->value),
292  ];
293 
294  $filters = [
295  'name' => [$field_factory->text($this->lng->txt('name')), true],
296  'login' => [$field_factory->text($this->lng->txt('login')), true],
297  'ip_range' => [$field_factory->text($this->lng->txt('client_ip_range')), true],
298  'solution' => [$field_factory->select($this->lng->txt('solutions'), $solution_options), true],
299  ];
300 
301  if ($this->test_object->getEnableProcessingTime()) {
302  $filters['extra_time'] = [$field_factory->select($this->lng->txt('extratime'), $yes_no_all_options), true];
303  }
304 
305  $filters['status_of_attempt'] = [
306  $field_factory->select($this->lng->txt('status_of_attempt'), $status_of_attempt_options),
307  true
308  ];
309 
310  $filters['test_passed'] = [
311  $field_factory->select($this->lng->txt('tst_passed'), $yes_no_all_options),
312  true
313  ];
314 
315  return $filters;
316  }
317 
318  private function getTableComponent(ServerRequestInterface $request, ?array $filter)
319  {
320  return $this->ui_factory
321  ->table()
322  ->data(
323  $this,
324  $this->lng->txt('list_of_participants'),
325  $this->getColumns(),
326  )
327  ->withId(self::ID)
328  ->withRequest($request)
329  ->withFilter($filter);
330  }
331 
335  private function getColumns(): array
336  {
337  $column_factory = $this->ui_factory->table()->column();
338 
339  $columns = [
340  'name' => $column_factory->text($this->lng->txt('name'))
341  ->withIsSortable(!$this->test_object->getAnonymity())
342  ];
343  if (!$this->test_object->getAnonymity()) {
344  $columns['login'] = $column_factory->text($this->lng->txt('login'))->withIsSortable(true);
345  }
346 
347  $columns += [
348  'matriculation' => $column_factory->text($this->lng->txt('matriculation'))
349  ->withIsOptional(true, false)
350  ->withIsSortable(true),
351  'ip_range' => $column_factory->text($this->lng->txt('client_ip_range'))
352  ->withIsOptional(true, false)
353  ->withIsSortable(true),
354  'attempt_started_at' => $column_factory->date(
355  $this->lng->txt('tst_attempt_started'),
356  $this->current_user->getDateTimeFormat()
357  )->withIsSortable(true),
358  'total_time_on_task' => $column_factory->text($this->lng->txt('working_time'))
359  ->withIsOptional(true, false),
360  'total_attempts' => $column_factory->number($this->lng->txt('total_attempts'))
361  ->withIsOptional(true, false)
362  ->withIsSortable(true),
363  ];
364 
365  if ($this->test_object->getEnableProcessingTime()) {
366  $columns['extra_time'] = $column_factory->text($this->lng->txt('extratime'))
367  ->withIsOptional(true, false);
368  $columns['total_duration'] = $column_factory->text($this->lng->txt('total_duration'))
369  ->withIsOptional(true, false);
370  $columns['remaining_duration'] = $column_factory->text($this->lng->txt('remaining_duration'))
371  ->withIsOptional(true);
372  }
373 
374  $columns['status_of_attempt'] = $column_factory->text($this->lng->txt('status_of_attempt'))
375  ->withIsSortable(true);
376 
377  if ($this->test_object->getMainSettings()->getTestBehaviourSettings()->getExamIdInTestAttemptEnabled()) {
378  $columns['id_of_attempt'] = $column_factory->text($this->lng->txt('exam_id_of_attempt'))
379  ->withIsOptional(true, false)
380  ->withIsSortable(true);
381  }
382 
383  if ($this->test_access->checkParticipantsResultsAccess()) {
384  $columns['reached_points'] = $column_factory->text($this->lng->txt('tst_reached_points'))
385  ->withIsSortable(true);
386  $columns['nr_of_answered_questions'] = $column_factory->text($this->lng->txt('tst_answered_questions'))
387  ->withIsOptional(true, false)
388  ->withIsSortable(true);
389  $columns['percent_of_available_points'] = $column_factory->number($this->lng->txt('tst_percent_solved'))
390  ->withUnit('%')
391  ->withIsOptional(true, false)
392  ->withIsSortable(true);
393  $columns['test_passed'] = $column_factory->boolean(
394  $this->lng->txt('tst_passed'),
395  $this->ui_factory->symbol()->icon()->custom(
396  'assets/images/standard/icon_checked.svg',
397  $this->lng->txt('yes'),
398  'small'
399  ),
400  $this->ui_factory->symbol()->icon()->custom(
401  'assets/images/standard/icon_unchecked.svg',
402  $this->lng->txt('no'),
403  'small'
404  )
405  )->withIsSortable(true)
406  ->withOrderingLabels(
407  "{$this->lng->txt('tst_passed')}, {$this->lng->txt('yes')} {$this->lng->txt('order_option_first')}",
408  "{$this->lng->txt('tst_passed')}, {$this->lng->txt('no')} {$this->lng->txt('order_option_first')}"
409  );
410  $columns['mark'] = $column_factory->text($this->lng->txt('tst_mark'))
411  ->withIsOptional(true, false)
412  ->withIsSortable(true);
413  }
414 
415  $columns['last_access'] = $column_factory->date(
416  $this->lng->txt('last_access'),
417  $this->current_user->getDateTimeFormat()
418  );
419 
420  return $columns;
421  }
422 
423  private function loadRecords(?array $filter, Order $order): iterable
424  {
425  if ($this->records !== null) {
426  return $this->records;
427  }
428 
429  $records = iterator_to_array(
430  $this->repository->getParticipants(
431  $this->test_object->getTestId(),
432  $filter,
433  null,
434  $order
435  )
436  );
437 
438  $access_filter = $this->participant_access_filter->getManageParticipantsUserFilter($this->test_object->getRefId());
439  $filtered_user_ids = $access_filter(array_map(
440  fn(Participant $participant) => $participant->getUserId(),
441  $records
442  ));
443 
444  $this->records = array_filter(
445  $records,
446  fn(Participant $participant) => in_array($participant->getUserId(), $filtered_user_ids),
447  );
448 
449  return $this->records;
450  }
451 
452 
456  private function getViewControlledRecords(?array $filter_data, Range $range, Order $order): iterable
457  {
458  return $this->limitRecords(
459  $this->sortRecords(
460  $this->filterRecords(
461  $this->results_data_factory->addAttemptOverviewInformationToParticipants(
462  $this->results_presentation_settings,
463  $this->test_object,
464  $this->loadRecords($filter_data, $order)
465  ),
466  $filter_data
467  ),
468  $order
469  ),
470  $range
471  );
472  }
473 
474  private function filterRecords(iterable $records, ?array $filter_data): iterable
475  {
476  foreach ($records as $record) {
477  if ($this->matchFilter($record, $filter_data)) {
478  yield $record;
479  }
480  }
481  }
482 
483  private function matchFilter(Participant $record, ?array $filter): bool
484  {
485  if ($filter === null) {
486  return true;
487  }
488 
489  $post_load_filters = $this->getPostLoadFilters();
490  $allow = true;
491 
492  foreach ($filter as $key => $value) {
493  if (!$value) {
494  continue;
495  }
496 
497  $post_load_filter = $post_load_filters[$key] ?? fn() => true;
498  $allow = $allow && $post_load_filter($value, $record);
499  }
500 
501  return $allow;
502  }
503 
504  private function sortRecords(iterable $records, Order $order): array
505  {
506  $post_load_order_fields = $this->getPostLoadOrderFields();
507  $records = iterator_to_array($records);
508 
509  uasort($records, static function (Participant $a, Participant $b) use ($order, $post_load_order_fields) {
510  foreach ($order->get() as $subject => $direction) {
511  $post_load_order_field = $post_load_order_fields[$subject] ?? fn() => 0;
512  $index = $post_load_order_field($a, $b);
513 
514  if ($index !== 0) {
515  return $direction === 'DESC' ? $index * -1 : $index;
516  }
517  }
518 
519  return 0;
520  });
521 
522  return $records;
523  }
524 
525  private function limitRecords(array $records, Range $range): array
526  {
527  return array_slice($records, $range->getStart(), $range->getLength());
528  }
529 }
This describes commonalities between the different modals.
Definition: Modal.php:34
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Participant.php:21
repository()
description: > Example for rendering a repository card
Definition: repository.php:33
getTotalDuration(?int $processing_time)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
matchFilter(Participant $record, ?array $filter)
getViewControlledRecords(?array $filter_data, Range $range, Order $order)
getComponents(URLBuilder $url_builder, string $filter_url)
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
buildDataRow(string $id, array $record)
limitRecords(array $records, Range $range)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getFilterFields(FieldFactory $field_factory)
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...
loadRecords(?array $filter, Order $order)
sortRecords(iterable $records, Order $order)
getTableComponent(ServerRequestInterface $request, ?array $filter)
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 UIFactory $ui_factory, private readonly \ilUIService $ui_service, private readonly Language $lng, private readonly \ilTestAccess $test_access, private readonly RequestDataCollector $test_request, private readonly \ilTestParticipantAccessFilterFactory $participant_access_filter, private readonly ParticipantRepository $repository, private readonly ResultsDataFactory $results_data_factory, private readonly ResultsPresentationSettings $results_presentation_settings, private readonly \ilObjUser $current_user, private readonly \ilObjTest $test_object, private readonly ParticipantTableActions $table_actions)
global $lng
Definition: privfeed.php:31
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
URLBuilder.
Definition: URLBuilder.php:40
filterRecords(iterable $records, ?array $filter_data)
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
getRemainingDuration(int $processing_time, bool $reset_time_on_new_attempt)
getFilterComponent(string $action, ServerRequestInterface $request)