ILIAS  release_8 Revision v8.24
class.ilTestPassesSelector.php
Go to the documentation of this file.
1<?php
2
26{
27 protected $db;
28
29 protected $testOBJ;
30
31 private $activeId;
32
33 private $lastFinishedPass = null;
34
35 private $passes = null;
36
37 private $testPassedOnceCache = array();
38
40 {
41 $this->db = $db;
42 $this->testOBJ = $testOBJ;
43 }
44
45 public function getActiveId()
46 {
47 return $this->activeId;
48 }
49
50 public function setActiveId($activeId)
51 {
52 $this->activeId = $activeId;
53 }
54
55 public function getLastFinishedPass()
56 {
58 }
59
61 {
63 $this->lastFinishedPass = $lastFinishedPass;
64 }
65
66 private function passesLoaded(): bool
67 {
68 return is_array($this->passes);
69 }
70
71 private function ensureLoadedPasses()
72 {
73 if (!$this->passesLoaded()) {
74 $this->loadPasses();
75 }
76 }
77 private function loadPasses()
78 {
79 $query = "
80 SELECT DISTINCT tst_pass_result.* FROM tst_pass_result
81 LEFT JOIN tst_test_result
82 ON tst_pass_result.pass = tst_test_result.pass
83 AND tst_pass_result.active_fi = tst_test_result.active_fi
84 WHERE tst_pass_result.active_fi = %s
85 ORDER BY tst_pass_result.pass
86 ";
87
88 $res = $this->db->queryF(
89 $query,
90 array('integer'),
91 array($this->getActiveId())
92 );
93
94 $this->passes = array();
95
96 while ($row = $this->db->fetchAssoc($res)) {
97 $this->passes[$row['pass']] = $row;
98 }
99 }
100
101 private function getLazyLoadedPasses()
102 {
103 $this->ensureLoadedPasses();
104 return $this->passes;
105 }
106
107 public function loadLastFinishedPass()
108 {
109 $query = "
110 SELECT last_finished_pass FROM tst_active WHERE active_id = %s
111 ";
112
113 $res = $this->db->queryF(
114 $query,
115 array('integer'),
116 array($this->getActiveId())
117 );
118
119 while ($row = $this->db->fetchAssoc($res)) {
120 $this->setLastFinishedPass($row['last_finished_pass']);
121 }
122 }
123
124 public function getExistingPasses(): array
125 {
126 return array_keys($this->getLazyLoadedPasses());
127 }
128
129 public function hasExistingPasses(): bool
130 {
131 return (bool) count($this->getExistingPasses());
132 }
133
134 public function getNumExistingPasses(): int
135 {
136 return count($this->getExistingPasses());
137 }
138
139 public function openPassExists(): bool
140 {
141 return count($this->getExistingPasses()) > count($this->getClosedPasses());
142 }
143
144 public function getClosedPasses(): array
145 {
146 $existingPasses = $this->getExistingPasses();
147 $closedPasses = $this->fetchClosedPasses($existingPasses);
148
149 return $closedPasses;
150 }
151
152 public function getReportablePasses(): array
153 {
154 $existingPasses = $this->getExistingPasses();
155
156 $reportablePasses = $this->fetchReportablePasses($existingPasses);
157
158 return $reportablePasses;
159 }
160
161 public function hasReportablePasses(): bool
162 {
163 return (bool) count($this->getReportablePasses());
164 }
165
166 private function fetchReportablePasses($existingPasses): array
167 {
168 $lastPass = $this->fetchLastPass($existingPasses);
169
170 $reportablePasses = array();
171
172 foreach ($existingPasses as $pass) {
173 if ($this->isReportablePass($lastPass, $pass)) {
174 $reportablePasses[] = $pass;
175 }
176 }
177
178 return $reportablePasses;
179 }
180
181 private function fetchClosedPasses($existingPasses): array
182 {
183 $closedPasses = array();
184
185 foreach ($existingPasses as $pass) {
186 if ($this->isClosedPass($pass)) {
187 $closedPasses[] = $pass;
188 }
189 }
190
191 return $closedPasses;
192 }
193
194 private function fetchLastPass($existingPasses)
195 {
196 $lastPass = null;
197
198 foreach ($existingPasses as $pass) {
199 if ($lastPass === null || $pass > $lastPass) {
200 $lastPass = $pass;
201 }
202 }
203
204 return $lastPass;
205 }
206
207 private function isReportablePass($lastPass, $pass): bool
208 {
209 switch ($this->testOBJ->getScoreReporting()) {
211
212 return true;
213
215
216 return $this->isReportingDateReached();
217
219
220 if ($pass < $lastPass) {
221 return true;
222 }
223
224 return $this->isClosedPass($pass);
225
227
228 if (!$this->hasTestPassedOnce($this->getActiveId())) {
229 return false;
230 }
231
232 return $this->isClosedPass($pass);
233 }
234
235 return false;
236 }
237
239 {
240 if ($this->getLastFinishedPass() === null) {
241 throw new ilTestException('invalid object state: last finished pass was not set!');
242 }
243 }
244
245 private function isClosedPass($pass): bool
246 {
248
249 if ($pass <= $this->getLastFinishedPass()) {
250 return true;
251 }
252
253 if ($this->isProcessingTimeReached($pass)) {
254 return true;
255 }
256
257 return false;
258 }
259
260 private function isReportingDateReached(): bool
261 {
262 $reporting_date = $this->testOBJ->getScoreSettings()->getResultSummarySettings()->getReportingDate();
263 return $reporting_date <= new DateTimeImmutable('now', new DateTimeZone('UTC'));
264 }
265
266 private function isProcessingTimeReached($pass): bool
267 {
268 if (!$this->testOBJ->getEnableProcessingTime()) {
269 return false;
270 }
271
272 $startingTime = $this->testOBJ->getStartingTimeOfUser($this->getActiveId(), $pass);
273
274 if ($startingTime === false) {
275 return false;
276 }
277
278 return $this->testOBJ->isMaxProcessingTimeReached($startingTime, $this->getActiveId());
279 }
280
285 {
286 $last_finished_pass = $this->getLastFinishedPass();
287 if ($last_finished_pass === null || $last_finished_pass === -1) {
288 return null;
289 }
290
291 $passes = $this->getLazyLoadedPasses();
292 if (!isset($passes[$last_finished_pass])) {
293 return null;
294 }
295 return $passes[$last_finished_pass]['tstamp'];
296 }
297
298 public function hasTestPassedOnce($activeId): bool
299 {
300 global $DIC; /* @var ILIAS\DI\Container $DIC */
301
302 if (!isset($this->testPassedOnceCache[$activeId])) {
303 $this->testPassedOnceCache[$activeId] = false;
304
305 $res = $DIC->database()->queryF(
306 "SELECT passed_once FROM tst_result_cache WHERE active_fi = %s",
307 array('integer'),
308 array($activeId)
309 );
310
311 while ($row = $DIC->database()->fetchAssoc($res)) {
312 $this->testPassedOnceCache[$activeId] = (bool) $row['passed_once'];
313 }
314 }
315
316 return $this->testPassedOnceCache[$activeId];
317 }
318}
const SCORE_REPORTING_IMMIDIATLY
const SCORE_REPORTING_FINISHED
const SCORE_REPORTING_AFTER_PASSED
const SCORE_REPORTING_DATE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilDBInterface $db, ilObjTest $testOBJ)
setLastFinishedPass($lastFinishedPass)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query