ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestScreenGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 use ILIAS\Data\Link;
26 use ILIAS\UI\Component\Launcher\Factory as LauncherFactory;
32 use ILIAS\Style\Content\Service as ContentStyle;
33 
40 {
41  public const DEFAULT_CMD = 'testScreen';
42 
44  private readonly int $ref_id;
46  private readonly ilTestSession $test_session;
47  private readonly DataFactory $data_factory;
49 
50  public function __construct(
51  private readonly ilObjTest $object,
52  private readonly ilObjUser $user,
53  private readonly UIFactory $ui_factory,
54  private readonly UIRenderer $ui_renderer,
55  private readonly ilLanguage $lng,
56  private readonly Refinery $refinery,
57  private readonly ilCtrl $ctrl,
58  private readonly ilGlobalTemplateInterface $tpl,
59  private readonly ContentStyle $content_style,
60  private readonly HTTPServices $http,
61  private readonly ilTabsGUI $tabs,
62  private readonly ilAccessHandler $access,
63  private readonly ilDBInterface $database,
64  private readonly ilRbacSystem $rbac_system
65  ) {
66  $this->ref_id = $this->object->getRefId();
67  $this->main_settings = $this->object->getMainSettings();
68  $this->data_factory = new DataFactory();
69 
70  $this->test_session = (new ilTestSessionFactory($this->object, $this->database, $this->user))->getSession();
71 
72  $this->test_passes_selector = new ilTestPassesSelector($this->database, $this->object);
73  $this->test_passes_selector->setActiveId($this->test_session->getActiveId());
74  $this->test_passes_selector->setLastFinishedPass($this->test_session->getLastFinishedPass());
75  $this->password_checker = new ilTestPasswordChecker($this->rbac_system, $this->user, $this->object, $this->lng);
76  }
77 
78  public function executeCommand(): void
79  {
80  if ($this->access->checkAccess('read', '', $this->ref_id)) {
81  $this->{$this->ctrl->getCmd(self::DEFAULT_CMD)}();
82  return;
83  }
84 
85  if (!$this->object->getMainSettings()->getAdditionalSettings()->getHideInfoTab()) {
86  $this->ctrl->redirectByClass(ilObjTestGUI::class, 'infoScreen');
87  }
88 
89  $this->tpl->setOnScreenMessage('failure', sprintf(
90  $this->lng->txt('msg_no_perm_read_item'),
91  $this->object->getTitle()
92  ), true);
93  $this->ctrl->setParameterByClass('ilrepositorygui', 'ref_id', ROOT_FOLDER_ID);
94  $this->ctrl->redirectByClass('ilrepositorygui');
95  }
96 
97  public function testScreen(): void
98  {
99  $this->tabs->activateTab(ilTestTabsManager::TAB_ID_TEST);
100  $this->tpl->setPermanentLink($this->object->getType(), $this->ref_id);
101 
102  $elements = [];
103 
104  if ($this->areSkillLevelThresholdsMissing()) {
105  $elements = [$this->getSkillLevelThresholdsMissingInfo()];
106  }
107  $elements = $this->handleRenderMessageBox($elements);
108  $elements = $this->handleRenderIntroduction($elements);
109 
110  $this->tpl->setContent(
111  $this->ui_renderer->render(
112  $this->testCanBeStarted() ? $this->handleRenderLauncher($elements) : $elements
113  )
114  );
115  }
116 
117  private function handleRenderMessageBox(array $elements): array
118  {
119  $message_box_message = '';
120  $message_box_message_elements = [];
121 
122  $exam_conditions_enabled = $this->main_settings->getIntroductionSettings()->getExamConditionsCheckboxEnabled();
123  $password_enabled = $this->main_settings->getAccessSettings()->getPasswordEnabled();
124  $test_behaviour_settings = $this->main_settings->getTestBehaviourSettings();
125 
126  if ($exam_conditions_enabled && $password_enabled) {
127  $message_box_message_elements[] = $this->lng->txt('tst_launcher_status_message_conditions_and_password');
128  } elseif ($exam_conditions_enabled) {
129  $message_box_message_elements[] = $this->lng->txt('tst_launcher_status_message_conditions');
130  } elseif ($password_enabled) {
131  $message_box_message_elements[] = $this->lng->txt('tst_launcher_status_message_password');
132  }
133 
134  if ($test_behaviour_settings->getProcessingTimeEnabled() && !$this->isUserOutOfProcessingTime()) {
135  $message_box_message_elements[] = sprintf(
136  $this->lng->txt('tst_time_limit_message'),
137  $test_behaviour_settings->getProcessingTimeAsMinutes()
138  );
139  }
140 
141  $nr_of_tries = $this->object->getNrOfTries();
142 
143  if ($nr_of_tries !== 0) {
144  $message_box_message_elements[] = sprintf($this->lng->txt('tst_attempt_limit_message'), $nr_of_tries);
145  }
146 
147  if ($this->object->isStartingTimeEnabled() && !$this->object->startingTimeReached()) {
148  $message_box_message_elements[] = sprintf(
149  $this->lng->txt('detail_starting_time_not_reached'),
150  ilDatePresentation::formatDate(new ilDateTime($this->object->getStartingTime(), IL_CAL_UNIX))
151  );
152  }
153 
154  if ($this->object->isEndingTimeEnabled() && !$this->object->endingTimeReached()) {
155  $message_box_message_elements[] = sprintf(
156  $this->lng->txt('tst_exam_ending_time_message'),
157  ilDatePresentation::formatDate(new ilDateTime($this->object->getEndingTime(), IL_CAL_UNIX))
158  );
159  }
160 
161  foreach ($message_box_message_elements as $message_box_message_element) {
162  $message_box_message .= ' ' . $message_box_message_element;
163  }
164 
165  if (!empty($message_box_message)) {
166  $elements[] = $this->ui_factory->messageBox()->info($message_box_message);
167  }
168 
169  return $elements;
170  }
171 
172  private function handleRenderIntroduction(array $elements): array
173  {
174  $introduction = $this->object->getIntroduction();
175 
176  if (
177  $this->main_settings->getIntroductionSettings()->getIntroductionEnabled() &&
178  !empty($introduction)
179  ) {
180  $this->content_style->gui()->addCss($this->tpl, $this->ref_id);
181  $elements[] = $this->ui_factory->panel()->standard(
182  $this->lng->txt('tst_introduction'),
183  $this->ui_factory->legacy($introduction),
184  );
185  }
186 
187  return $elements;
188  }
189 
190  private function handleRenderLauncher(array $elements): array
191  {
192  $elements[] = $this->getLauncher();
193  return $elements;
194  }
195 
196  private function getLauncher(): Launcher
197  {
198  $launcher_factory = $this->ui_factory->launcher();
199 
200  if ($this->object->isStartingTimeEnabled() && !$this->object->startingTimeReached()) {
201  return $launcher_factory
202  ->inline($this->data_factory->link('', $this->data_factory->uri($this->http->request()->getUri()->__toString())))
203  ->withButtonLabel(sprintf(
204  $this->lng->txt('detail_starting_time_not_reached'),
205  ilDatePresentation::formatDate(new ilDateTime($this->object->getStartingTime(), IL_CAL_UNIX))
206  ), false)
207  ;
208  }
209 
210  if ($this->object->isEndingTimeEnabled() && $this->object->endingTimeReached()) {
211  return $launcher_factory
212  ->inline($this->data_factory->link('', $this->data_factory->uri($this->http->request()->getUri()->__toString())))
213  ->withButtonLabel(sprintf(
214  $this->lng->txt('detail_ending_time_reached'),
215  ilDatePresentation::formatDate(new ilDateTime($this->object->getEndingTime(), IL_CAL_UNIX))
216  ), false)
217  ;
218  }
219 
220  if ($this->isUserOutOfProcessingTime()) {
221  return $launcher_factory
222  ->inline($this->data_factory->link('', $this->data_factory->uri($this->http->request()->getUri()->__toString())))
223  ->withButtonLabel($this->lng->txt('tst_out_of_time_message'), false)
224  ;
225  }
226 
227  if ($this->object->getFixedParticipants() && $this->object->getInvitedUsers($this->user->getId()) === []) {
228  return $launcher_factory
229  ->inline($this->data_factory->link('', $this->data_factory->uri($this->http->request()->getUri()->__toString())))
230  ->withButtonLabel($this->lng->txt('tst_exam_not_assigned_participant_disclaimer'), false)
231  ;
232  }
233 
234  if (ilObjTestAccess::_lookupOnlineTestAccess($this->object->getId(), $this->user->getId()) !== true) {
235  return $launcher_factory
236  ->inline($this->data_factory->link('', $this->data_factory->uri($this->http->request()->getUri()->__toString())))
237  ->withButtonLabel($this->lng->txt('user_wrong_clientip'), false)
238  ;
239  }
240 
241  if (!$this->hasAvailablePasses()) {
242  return $launcher_factory
243  ->inline($this->data_factory->link('', $this->data_factory->uri($this->http->request()->getUri()->__toString())))
244  ->withButtonLabel($this->lng->txt('tst_launcher_button_label_passes_limit_reached'), false);
245  }
246 
247  if ($this->blockUserAfterHavingPassed()) {
248  return $launcher_factory
249  ->inline($this->data_factory->link('', $this->data_factory->uri($this->http->request()->getUri()->__toString())))
250  ->withButtonLabel($this->lng->txt('tst_already_passed_cannot_retake'), false)
251  ;
252  }
253 
254  $next_pass_allowed_timestamp = 0;
255  if (!$this->object->isNextPassAllowed($this->test_passes_selector, $next_pass_allowed_timestamp)) {
256  return $launcher_factory
257  ->inline($this->data_factory->link('', $this->data_factory->uri($this->http->request()->getUri()->__toString())))
258  ->withButtonLabel(
259  sprintf(
260  $this->lng->txt('wait_for_next_pass_hint_msg'),
261  ilDatePresentation::formatDate(new ilDateTime($next_pass_allowed_timestamp, IL_CAL_UNIX)),
262  ),
263  false
264  )
265  ;
266  }
267 
268  if ($this->lastPassSuspended()) {
269  return $launcher_factory->inline($this->getResumeLauncherLink());
270  }
271 
272  if ($this->isModalLauncherNeeded()) {
273  return $this->buildModalLauncher();
274  }
275  return $launcher_factory->inline($this->getStartLauncherLink());
276  }
277 
278  private function getResumeLauncherLink(): Link
279  {
280  $url = $this->ctrl->getLinkTarget((new ilTestPlayerFactory($this->object))->getPlayerGUI(), ilTestPlayerCommands::RESUME_PLAYER);
281  return $this->data_factory->link($this->lng->txt('tst_resume_test'), $this->data_factory->uri(ILIAS_HTTP_PATH . '/' . $url));
282  }
283 
284  private function buildModalLauncher(): Launcher
285  {
286  $launcher = $this->ui_factory->launcher()->inline($this->getModalLauncherLink())
287  ->withInputs(
288  $this->ui_factory->input()->field()->group($this->getModalLauncherInputs()),
289  function (Result $result) {
290  $this->evaluateLauncherModalForm($result);
291  },
293  )->withModalSubmitLabel($this->lng->txt('continue'));
294 
295  $request = $this->http->request();
296  $key = 'launcher_id';
297  if (array_key_exists($key, $request->getQueryParams())
298  && $request->getQueryParams()[$key] === 'exam_modal') {
299  $launcher = $launcher->withRequest($request);
300  }
301  return $launcher;
302  }
303 
304  private function getModalLauncherLink(): Link
305  {
306  $uri = $this->data_factory->uri($this->http->request()->getUri()->__toString())->withParameter('launcher_id', 'exam_modal');
307  return $this->data_factory->link($this->lng->txt('tst_exam_start'), $uri);
308  }
309 
310  private function getModalLauncherInputs(): array
311  {
312  if ($this->main_settings->getIntroductionSettings()->getExamConditionsCheckboxEnabled()) {
313  $modal_inputs['exam_conditions'] = $this->ui_factory->input()->field()->checkbox(
314  $this->lng->txt('tst_exam_conditions'),
315  $this->lng->txt('tst_exam_conditions_label')
316  )->withRequired(true);
317  }
318 
319  if ($this->main_settings->getAccessSettings()->getPasswordEnabled()) {
320  $modal_inputs['exam_password'] = $this->ui_factory->input()->field()->password(
321  $this->lng->txt('tst_exam_password'),
322  $this->lng->txt('tst_exam_password_label')
323  )->withRevelation(true)
324  ->withRequired(true)
325  ->withAdditionalTransformation(
326  $this->refinery->custom()->transformation(
327  static function (ILIAS\Data\Password $value): string {
328  return $value->toString();
329  }
330  )
331  );
332  }
333 
334  if ($this->user->isAnonymous()) {
335  $access_code_input = $this->ui_factory->input()->field()->text(
336  $this->lng->txt('tst_exam_access_code'),
337  $this->lng->txt('tst_exam_access_code_label')
338  );
339 
340  $access_code_from_session = $this->test_session->getAccessCodeFromSession();
341  if ($access_code_from_session) {
342  $access_code_input = $access_code_input->withValue($access_code_from_session);
343  }
344 
345  $modal_inputs['exam_access_code'] = $access_code_input;
346  }
347 
348  if ($this->main_settings->getParticipantFunctionalitySettings()->getUsePreviousAnswerAllowed()
349  && $this->test_passes_selector->getLastFinishedPass() >= 0) {
350  $modal_inputs['exam_use_previous_answers'] = $this->ui_factory->input()->field()->checkbox(
351  $this->lng->txt('tst_exam_use_previous_answers'),
352  $this->lng->txt('tst_exam_use_previous_answers_label')
353  );
354  }
355 
356  return $modal_inputs ?? [];
357  }
358 
360  {
361  $exam_conditions_enabled = $this->main_settings->getIntroductionSettings()->getExamConditionsCheckboxEnabled();
362  $password_enabled = $this->main_settings->getAccessSettings()->getPasswordEnabled();
363 
364  if ($exam_conditions_enabled && $password_enabled) {
365  $modal_message_box_message = $this->lng->txt('tst_exam_modal_message_conditions_and_password');
366  } elseif ($exam_conditions_enabled) {
367  $modal_message_box_message = $this->lng->txt('tst_exam_modal_message_conditions');
368  } elseif ($password_enabled) {
369  $modal_message_box_message = $this->lng->txt('tst_exam_modal_message_password');
370  }
371 
372  return isset($modal_message_box_message) ? $this->ui_factory->messageBox()->info($modal_message_box_message) : null;
373  }
374 
375  private function getStartLauncherLink(): Link
376  {
377  $url = $this->ctrl->getLinkTarget((new ilTestPlayerFactory($this->object))->getPlayerGUI(), ilTestPlayerCommands::INIT_TEST);
378  return $this->data_factory->link($this->lng->txt('tst_exam_start'), $this->data_factory->uri(ILIAS_HTTP_PATH . '/' . $url));
379  }
380 
381  private function evaluateLauncherModalForm(Result $result): void
382  {
383  if ($result->isOK()) {
384  $conditions_met = true;
385  $message = '';
386  $access_settings_password = $this->main_settings->getAccessSettings()->getPassword();
387  $anonymous = $this->user->isAnonymous();
388  foreach ($result->value() as $key => $value) {
389 
390  switch ($key) {
391  case 'exam_conditions':
392  $exam_conditions_value = (bool) $value;
393  if (!$exam_conditions_value) {
394  $conditions_met = false;
395  $message .= $this->lng->txt('tst_exam_conditions_not_checked_message') . '<br>';
396  }
397  break;
398  case 'exam_password':
399  $password = $value;
400  $exam_password_valid = ($password === $access_settings_password);
401  if (!$exam_password_valid) {
402  $conditions_met = false;
403  $message .= $this->lng->txt('tst_exam_password_invalid_message') . '<br>';
404  }
405  $this->password_checker->setUserEnteredPassword($password);
406  break;
407  case 'exam_access_code':
408  if ($anonymous && !empty($value)) {
409  $this->test_session->setAccessCodeToSession($value);
410  } else {
411  $this->test_session->unsetAccessCodeInSession();
412  }
413  break;
414  case 'exam_use_previous_answers':
415  $exam_use_previous_answers_value = (string) (int) $value;
416  break;
417  }
418  }
419 
420  if ($message !== '') {
421  $this->tpl->setOnScreenMessage(ilGlobalTemplateInterface::MESSAGE_TYPE_FAILURE, $message, true);
422  }
423 
424  if (empty($result->value())) {
425  $this->tpl->setOnScreenMessage(ilGlobalTemplateInterface::MESSAGE_TYPE_FAILURE, $this->lng->txt('tst_exam_required_fields_not_filled_message'), true);
426  } elseif ($conditions_met) {
427  if (
428  !$anonymous &&
429  $this->main_settings->getParticipantFunctionalitySettings()->getUsePreviousAnswerAllowed()
430  ) {
431  $this->user->setPref('tst_use_previous_answers', $exam_use_previous_answers_value ?? '0');
432  $this->user->update();
433  }
434 
435  if (isset($password) && $password === $access_settings_password) {
436  ilSession::set('tst_password_' . $this->object->getTestId(), $password);
437  } else {
438  ilSession::set('tst_password_' . $this->object->getTestId(), '');
439  $this->test_session->setPasswordChecked(false);
440  }
441 
442  $this->ctrl->redirectByClass((new ilTestPlayerFactory($this->object))->getPlayerGUI()::class, ilTestPlayerCommands::INIT_TEST);
443  }
444  } else {
445  $this->tpl->setOnScreenMessage(ilGlobalTemplateInterface::MESSAGE_TYPE_FAILURE, $this->lng->txt('tst_exam_required_fields_not_filled_message'), true);
446  }
447  }
448 
449  private function testCanBeStarted(): bool
450  {
451  if ($this->object->getOfflineStatus()
452  || !$this->object->isComplete($this->object->getQuestionSetConfig())) {
453  return false;
454  }
455 
456  return true;
457  }
458 
459  private function isUserOutOfProcessingTime(): bool
460  {
461  $test_behaviour_settings = $this->object->getMainSettings()->getTestBehaviourSettings();
462  if (!$test_behaviour_settings->getProcessingTimeEnabled()
463  || $test_behaviour_settings->getResetProcessingTime()) {
464  return false;
465  }
466 
467  $active_id = $this->test_passes_selector->getActiveId();
468  $last_started_pass = $this->test_session->getLastStartedPass();
469  return $last_started_pass !== null
470  && $this->object->isMaxProcessingTimeReached(
471  $this->object->getStartingTimeOfUser($active_id, $last_started_pass),
472  $active_id
473  );
474  }
475 
476  private function blockUserAfterHavingPassed(): bool
477  {
478  if ($this->main_settings->getTestBehaviourSettings()->getBlockAfterPassedEnabled()) {
479  return $this->test_passes_selector->getLastFinishedPass() >= 0
480  && $this->test_passes_selector->hasTestPassedOnce($this->test_session->getActiveId());
481  }
482 
483  return false;
484  }
485 
486  private function hasAvailablePasses(): bool
487  {
488  $nr_of_tries = $this->object->getNrOfTries();
489 
490  return $nr_of_tries === 0 || (count($this->test_passes_selector->getExistingPasses()) <= $nr_of_tries && count($this->test_passes_selector->getClosedPasses()) < $nr_of_tries);
491  }
492 
493  private function lastPassSuspended(): bool
494  {
495  return (count($this->test_passes_selector->getExistingPasses()) - count($this->test_passes_selector->getClosedPasses())) === 1;
496  }
497 
498  private function isModalLauncherNeeded(): bool
499  {
500  return (
501  $this->main_settings->getIntroductionSettings()->getExamConditionsCheckboxEnabled()
502  || $this->main_settings->getAccessSettings()->getPasswordEnabled()
503  || $this->main_settings->getParticipantFunctionalitySettings()->getUsePreviousAnswerAllowed()
504  && $this->test_passes_selector->getLastFinishedPass() >= 0
505  || $this->user->isAnonymous()
506  );
507  }
508 
510  {
511  $message = $this->lng->txt('tst_skl_level_thresholds_missing');
512 
513  $link_target = $this->buildLinkTarget(
515  );
516 
517  $link = $this->ui_factory->link()->standard(
518  $this->lng->txt('tst_skl_level_thresholds_link'),
519  $link_target
520  );
521 
522  return $this->ui_factory->messageBox()->failure($message)->withLinks([$link]);
523  }
524 
525  private function areSkillLevelThresholdsMissing(): bool
526  {
527  if (!$this->object->isSkillServiceEnabled()) {
528  return false;
529  }
530 
531  $questionContainerId = $this->object->getId();
532 
533  $assignmentList = new ilAssQuestionSkillAssignmentList($this->database);
534  $assignmentList->setParentObjId($questionContainerId);
535  $assignmentList->loadFromDb();
536 
537  foreach ($assignmentList->getUniqueAssignedSkills() as $data) {
538  foreach ($data['skill']->getLevelData() as $level) {
539  $threshold = new ilTestSkillLevelThreshold($this->database);
540  $threshold->setTestId($this->object->getTestId());
541  $threshold->setSkillBaseId($data['skill_base_id']);
542  $threshold->setSkillTrefId($data['skill_tref_id']);
543  $threshold->setSkillLevelId($level['id']);
544 
545  if (!$threshold->dbRecordExists()) {
546  return true;
547  }
548  }
549  }
550 
551  return false;
552  }
553 
554  private function buildLinkTarget(string $cmd = null): string
555  {
556  $target = array_merge(['ilRepositoryGUI', 'ilObjTestGUI'], ['ilTestSkillAdministrationGUI', 'ilTestSkillLevelThresholdsGUI']);
557  return $this->ctrl->getLinkTargetByClass($target, $cmd);
558  }
559 }
isOK()
Get to know if the result is ok.
readonly ilObjTestMainSettings $main_settings
readonly ilTestSession $test_session
value()
Get the encapsulated value.
Class ilTestScreenGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const ROOT_FOLDER_ID
Definition: constants.php:32
Class ChatMainBarProvider .
readonly DataFactory $data_factory
evaluateLauncherModalForm(Result $result)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
A password is used as part of credentials for authentication.
Definition: Password.php:16
const IL_CAL_UNIX
readonly ilTestPassesSelector $test_passes_selector
static _lookupOnlineTestAccess($a_test_id, $a_user_id)
Checks if a user is allowd to run an online exam.
static http()
Fetches the global http state from ILIAS.
$lng
string $key
Consumer key/client ID value.
Definition: System.php:193
$url
Definition: ltiregstart.php:35
$http
Definition: raiseError.php:7
__construct(private readonly ilObjTest $object, private readonly ilObjUser $user, private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer, private readonly ilLanguage $lng, private readonly Refinery $refinery, private readonly ilCtrl $ctrl, private readonly ilGlobalTemplateInterface $tpl, private readonly ContentStyle $content_style, private readonly HTTPServices $http, private readonly ilTabsGUI $tabs, private readonly ilAccessHandler $access, private readonly ilDBInterface $database, private readonly ilRbacSystem $rbac_system)
buildLinkTarget(string $cmd=null)
handleRenderLauncher(array $elements)
handleRenderMessageBox(array $elements)
ilTestPasswordChecker $password_checker
$message
Definition: xapiexit.php:32
handleRenderIntroduction(array $elements)
static set(string $a_var, $a_val)
Set a value.
Refinery Factory $refinery