ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ParticipantTableExtraTimeAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Test\Participants;
22 
33 
35 {
36  public const ACTION_ID = 'extratime';
37 
38  public function __construct(
39  private readonly Language $lng,
40  private readonly Refinery $refinery,
41  private readonly \ilGlobalTemplateInterface $tpl,
42  private readonly UIFactory $ui_factory,
43  private readonly ParticipantRepository $participant_repository,
44  private readonly \ilObjUser $current_user,
45  private readonly \ilTestAccess $test_access,
46  private readonly \ilObjTest $test_obj
47  ) {
48  }
49 
50  public function getActionId(): string
51  {
52  return self::ACTION_ID;
53  }
54 
55  public function isAvailable(): bool
56  {
57  return $this->test_obj->getEnableProcessingTime()
58  && $this->test_access->checkManageParticipantsAccess();
59  }
60 
61  public function getTableAction(
62  URLBuilder $url_builder,
63  URLBuilderToken $row_id_token,
64  URLBuilderToken $action_token,
65  URLBuilderToken $action_type_token
66  ): Action {
67  return $this->ui_factory->table()->action()->standard(
68  $this->lng->txt(self::ACTION_ID),
69  $url_builder
70  ->withParameter($action_token, self::ACTION_ID)
71  ->withParameter($action_type_token, ParticipantTableActions::SHOW_ACTION),
72  $row_id_token
73  )->withAsync();
74  }
75 
76  public function getModal(
77  URLBuilder $url_builder,
78  array $selected_participants,
79  bool $all_participants_selected
80  ): ?Modal {
81  $has_different_extra_time = $this->resolveHasDifferentExtraTime($selected_participants);
82  $participant_rows = array_map(
83  fn(Participant $participant) => sprintf(
84  '%s, %s (%s)',
85  $participant->getLastname(),
86  $participant->getFirstname(),
87  sprintf($this->lng->txt('already_added_extra_time'), $participant->getExtraTime())
88  ),
89  $selected_participants
90  );
91 
92  return $this->ui_factory->modal()->roundtrip(
93  $this->lng->txt('extratime'),
94  [
95  $this->ui_factory->messageBox()->info(
96  $this->lng->txt(
97  $this->resolveInfoMessage(
98  $selected_participants,
99  $all_participants_selected,
100  $has_different_extra_time
101  )
102  )
103  ),
104  $this->ui_factory->listing()->unordered($participant_rows)
105  ],
106  [
107  'extra_time' => $this->ui_factory->input()->field()->numeric(
108  $this->lng->txt('extratime')
109  )->withRequired(true)
110  ->withAdditionalTransformation($this->refinery->int()->isGreaterThan(0))
111  ->withValue(0)
112  ->withByline(
113  $this->lng->txt('extra_time_byline')
114  )
115  ],
116  $url_builder->buildURI()->__toString()
117  )->withSubmitLabel($this->lng->txt('add'));
118  }
119 
120  public function onSubmit(
121  URLBuilder $url_builder,
122  ServerRequestInterface $request,
123  array $selected_participants,
124  bool $all_participants_selected
125  ): ?Modal {
126  if (!$this->test_access->checkManageParticipantsAccess()) {
127  $this->tpl->setOnScreenMessage(
129  $this->lng->txt('no_permission'),
130  true
131  );
132  return null;
133  }
134  $modal = $this->getModal(
135  $url_builder,
136  $selected_participants,
137  $all_participants_selected
138  )->withRequest($request);
139 
140  $data = $modal->getData();
141  if ($data === null) {
142  return $modal->withOnLoad($modal->getShowSignal());
143  }
144 
145  $this->saveExtraTime($selected_participants, $data['extra_time']);
146 
147  $this->tpl->setOnScreenMessage(
149  $this->lng->txt('extratime_added'),
150  true
151  );
152  return null;
153  }
154 
155  public function allowActionForRecord(Participant $record): bool
156  {
157  return $record->getUserId() !== ANONYMOUS_USER_ID;
158  }
159 
160  private function resolveInfoMessage(
161  array $selected_participants,
162  bool $all_participants_selected,
163  bool $has_different_extra_time
164  ): string {
165  if ($all_participants_selected) {
166  return 'extra_time_for_all_participants';
167  }
168 
169  if (count($selected_participants) === 1) {
170  return 'extra_time_for_single_participant';
171  }
172  if ($has_different_extra_time) {
173  return 'extra_time_for_selected_participants_different';
174  }
175 
176  return 'extra_time_for_selected_participants';
177  }
178 
182  private function resolveHasDifferentExtraTime(array $participants): bool
183  {
184  return count(array_unique(array_map(
185  fn(Participant $participant) => $participant->getExtraTime(),
186  $participants
187  ))) > 1;
188  }
189 
193  public function saveExtraTime(array $participants, int $minutes): void
194  {
195  foreach ($participants as $participant) {
196  $this->participant_repository->updateExtraTime($participant->withAddedExtraTime($minutes));
197  }
198 
199  if ($this->test_obj->getTestLogger()->isLoggingEnabled()) {
200  $this->test_obj->getTestLogger()->logTestAdministrationInteraction(
201  $this->test_obj->getTestLogger()->getInteractionFactory()->buildTestAdministrationInteraction(
202  $this->test_obj->getRefId(),
203  $this->current_user->getId(),
204  TestAdministrationInteractionTypes::EXTRA_TIME_ADDED,
205  [
207  fn(Participant $participant) => $participant->getUserId(),
208  $participants
209  ),
211  ]
212  )
213  );
214  }
215  }
216 
217  public function getSelectionErrorMessage(): ?string
218  {
219  return null;
220  }
221 }
This describes commonalities between the different modals.
Definition: Modal.php:34
const ANONYMOUS_USER_ID
Definition: constants.php:27
buildURI()
Get a URI representation of the full URL including query string and fragment/hash.
Definition: URLBuilder.php:212
onSubmit(URLBuilder $url_builder, ServerRequestInterface $request, array $selected_participants, bool $all_participants_selected)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Participant.php:21
resolveInfoMessage(array $selected_participants, bool $all_participants_selected, bool $has_different_extra_time)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
__construct(private readonly Language $lng, private readonly Refinery $refinery, private readonly \ilGlobalTemplateInterface $tpl, private readonly UIFactory $ui_factory, private readonly ParticipantRepository $participant_repository, private readonly \ilObjUser $current_user, private readonly \ilTestAccess $test_access, private readonly \ilObjTest $test_obj)
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter&#39;s value if the supplied token is valid.
Definition: URLBuilder.php:166
global $lng
Definition: privfeed.php:31
getTableAction(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
getModal(URLBuilder $url_builder, array $selected_participants, bool $all_participants_selected)
URLBuilder.
Definition: URLBuilder.php:40