ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
WorkingTime.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Test\Presentation;
22 
24 use ilDateTime;
27 use ilLanguage;
28 use ilObjTest;
29 use ilTemplate;
30 
32 {
33  public function __construct(
34  private readonly ilLanguage $lng,
35  private readonly Factory $ui_factory,
36  private readonly Renderer $ui_renderer,
37  private readonly int $starting_time,
38  private readonly int $processing_time
39  ) {
40  }
41 
43  ilObjTest $object,
44  array $date,
45  string $check_url,
46  string $redirect_url
47  ): ilTemplate {
48  [$processing_time_minutes, $processing_time_seconds] = $this->getUserProcessingTimeMinutesAndSeconds();
49  $template = new ilTemplate('tpl.workingtime.js', true, true, 'components/ILIAS/Test');
50  $template->setVariable('STRING_MINUTE', $this->lng->txt('minute'));
51  $template->setVariable('STRING_MINUTES', $this->lng->txt('minutes'));
52  $template->setVariable('STRING_SECOND', $this->lng->txt('second'));
53  $template->setVariable('STRING_SECONDS', $this->lng->txt('seconds'));
54  $template->setVariable('STRING_TIMELEFT', $this->lng->txt('tst_time_already_spent_left'));
55  $template->setVariable('AND', strtolower($this->lng->txt('and')));
56  $template->setVariable('YEAR', $date['year']);
57  $template->setVariable('MONTH', $date['mon'] - 1);
58  $template->setVariable('DAY', $date['mday']);
59  $template->setVariable('HOUR', $date['hours']);
60  $template->setVariable('MINUTE', $date['minutes']);
61  $template->setVariable('SECOND', $date['seconds']);
62  if ($object->isEndingTimeEnabled()) {
63  $date_time = new ilDateTime($object->getEndingTime(), IL_CAL_UNIX);
64  preg_match('/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $date_time->get(IL_CAL_TIMESTAMP), $matches);
65  if ($matches !== []) {
66  $template->setVariable('ENDYEAR', $matches[1]);
67  $template->setVariable('ENDMONTH', $matches[2] - 1);
68  $template->setVariable('ENDDAY', $matches[3]);
69  $template->setVariable('ENDHOUR', $matches[4]);
70  $template->setVariable('ENDMINUTE', $matches[5]);
71  $template->setVariable('ENDSECOND', $matches[6]);
72  }
73  }
74 
75  $datenow = getdate();
76  $template->setVariable('YEARNOW', $datenow['year']);
77  $template->setVariable('MONTHNOW', $datenow['mon'] - 1);
78  $template->setVariable('DAYNOW', $datenow['mday']);
79  $template->setVariable('HOURNOW', $datenow['hours']);
80  $template->setVariable('MINUTENOW', $datenow['minutes']);
81  $template->setVariable('SECONDNOW', $datenow['seconds']);
82  $template->setVariable('PTIME_M', $processing_time_minutes);
83  $template->setVariable('PTIME_S', $processing_time_seconds);
84  $template->setVariable('REDIRECT_URL', $redirect_url);
85  $template->setVariable('CHECK_URL', $check_url);
86 
87  return $template;
88  }
89 
90  public function getMessageBox(bool $verbose): string
91  {
92  $message_text = $verbose
93  ? $this->getUserProcessingTimeString() . ' <span id="timeleft">' . $this->getUserRemainingTimeString() . '</span>'
94  : '<div class="ilTstWorkingFormBlock_WorkingTime"><span id="timeleft" class="ilTstWorkingFormInfo_ProcessTimeLeft">' . $this->getUserRemainingTimeString() . '</span></div>';
95  return $this->ui_renderer->render($this->ui_factory->messageBox()->info($message_text));
96  }
97 
98  private function getUserProcessingTimeMinutesAndSeconds(): array
99  {
100  $processing_time_minutes = floor($this->processing_time / 60);
101  $processing_time_seconds = $this->processing_time - $processing_time_minutes * 60;
102 
103  return [$processing_time_minutes, $processing_time_seconds];
104  }
105 
106  private function getUserProcessingTimeString(): string
107  {
108  [$processing_time_minutes, $processing_time_seconds] = $this->getUserProcessingTimeMinutesAndSeconds();
109 
110  $str_processing_time = '';
111  if ($processing_time_minutes > 0) {
112  $str_processing_time = "$processing_time_minutes {$this->lng->txt($processing_time_minutes === 1 ? 'minute' : 'minutes')}";
113  }
114 
115  if ($processing_time_seconds > 0) {
116  if ($str_processing_time !== '') {
117  $str_processing_time .= " {$this->lng->txt('and')} ";
118  }
119  $str_processing_time .= "$processing_time_seconds {$this->lng->txt($processing_time_seconds === 1 ? 'second' : 'seconds')}";
120  }
121 
122  return sprintf(
123  $this->lng->txt('tst_time_already_spent'),
124  ilDatePresentation::formatDate(new ilDateTime(getdate($this->starting_time), IL_CAL_FKT_GETDATE)),
125  $str_processing_time
126  );
127  }
128 
129  private function getUserRemainingTimeString(): string
130  {
131  $time_left = $this->starting_time + $this->processing_time - time();
132  $time_left_minutes = floor($time_left / 60);
133  $time_left_seconds = $time_left - $time_left_minutes * 60;
134  $str_time_left = '';
135  if ($time_left_minutes > 0) {
136  $str_time_left = "$time_left_minutes {$this->lng->txt($time_left_minutes === 1 ? 'minute' : 'minutes')}";
137  }
138  if ($time_left < 300 && $time_left_seconds > 0) {
139  if ($str_time_left !== '') {
140  $str_time_left .= " {$this->lng->txt('and')} ";
141  }
142  $str_time_left .= "$time_left_seconds {$this->lng->txt($time_left_seconds === 1 ? 'second' : 'seconds')}";
143  }
144 
145  return sprintf($this->lng->txt('tst_time_already_spent_left'), $str_time_left);
146  }
147 }
prepareWorkingTimeJsTemplate(ilObjTest $object, array $date, string $check_url, string $redirect_url)
Definition: WorkingTime.php:42
const IL_CAL_UNIX
Builds data types.
Definition: Factory.php:35
const IL_CAL_FKT_GETDATE
global $lng
Definition: privfeed.php:31
__construct(private readonly ilLanguage $lng, private readonly Factory $ui_factory, private readonly Renderer $ui_renderer, private readonly int $starting_time, private readonly int $processing_time)
Definition: WorkingTime.php:33
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
const IL_CAL_TIMESTAMP