ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTestPassesSelector.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
12{
13 protected $db;
14
15 protected $testOBJ;
16
17 private $activeId;
18
19 private $lastFinishedPass = null;
20
21 private $passes = null;
22
23 private $testPassedOnceCache = array();
24
26 {
27 $this->db = $db;
28 $this->testOBJ = $testOBJ;
29 }
30
31 public function getActiveId()
32 {
33 return $this->activeId;
34 }
35
36 public function setActiveId($activeId)
37 {
38 $this->activeId = $activeId;
39 }
40
41 public function getLastFinishedPass()
42 {
44 }
45
47 {
49 $this->lastFinishedPass = $lastFinishedPass;
50 }
51
52 private function passesLoaded()
53 {
54 return is_array($this->passes);
55 }
56
57 private function ensureLoadedPasses()
58 {
59 if (!$this->passesLoaded()) {
60 $this->loadPasses();
61 }
62 }
63 private function loadPasses()
64 {
65 $query = "
66 SELECT DISTINCT tst_pass_result.* FROM tst_pass_result
67 LEFT JOIN tst_test_result
68 ON tst_pass_result.pass = tst_test_result.pass
69 AND tst_pass_result.active_fi = tst_test_result.active_fi
70 WHERE tst_pass_result.active_fi = %s
71 ORDER BY tst_pass_result.pass
72 ";
73
74 $res = $this->db->queryF(
75 $query,
76 array('integer'),
77 array($this->getActiveId())
78 );
79
80 $this->passes = array();
81
82 while ($row = $this->db->fetchAssoc($res)) {
83 $this->passes[$row['pass']] = $row;
84 }
85 }
86
87 private function getLazyLoadedPasses()
88 {
89 $this->ensureLoadedPasses();
90 return $this->passes;
91 }
92
93 public function loadLastFinishedPass()
94 {
95 $query = "
96 SELECT last_finished_pass FROM tst_active WHERE active_id = %s
97 ";
98
99 $res = $this->db->queryF(
100 $query,
101 array('integer'),
102 array($this->getActiveId())
103 );
104
105 while ($row = $this->db->fetchAssoc($res)) {
106 $this->setLastFinishedPass($row['last_finished_pass']);
107 }
108 }
109
110 public function getExistingPasses()
111 {
112 return array_keys($this->getLazyLoadedPasses());
113 }
114
115 public function hasExistingPasses()
116 {
117 return (bool) count($this->getExistingPasses());
118 }
119
120 public function getNumExistingPasses()
121 {
122 return count($this->getExistingPasses());
123 }
124
125 public function getClosedPasses()
126 {
127 $existingPasses = $this->getExistingPasses();
128 $closedPasses = $this->fetchClosedPasses($existingPasses);
129
130 return $closedPasses;
131 }
132
133 public function getReportablePasses()
134 {
135 $existingPasses = $this->getExistingPasses();
136
137 $reportablePasses = $this->fetchReportablePasses($existingPasses);
138
139 return $reportablePasses;
140 }
141
142 public function hasReportablePasses()
143 {
144 return (bool) count($this->getReportablePasses());
145 }
146
147 private function fetchReportablePasses($existingPasses)
148 {
149 $lastPass = $this->fetchLastPass($existingPasses);
150
151 $reportablePasses = array();
152
153 foreach ($existingPasses as $pass) {
154 if ($this->isReportablePass($lastPass, $pass)) {
155 $reportablePasses[] = $pass;
156 }
157 }
158
159 return $reportablePasses;
160 }
161
162 private function fetchClosedPasses($existingPasses)
163 {
164 $closedPasses = array();
165
166 foreach ($existingPasses as $pass) {
167 if ($this->isClosedPass($pass)) {
168 $closedPasses[] = $pass;
169 }
170 }
171
172 return $closedPasses;
173 }
174
175 private function fetchLastPass($existingPasses)
176 {
177 $lastPass = null;
178
179 foreach ($existingPasses as $pass) {
180 if ($lastPass === null || $pass > $lastPass) {
181 $lastPass = $pass;
182 }
183 }
184
185 return $lastPass;
186 }
187
188 private function isReportablePass($lastPass, $pass)
189 {
190 switch ($this->testOBJ->getScoreReporting()) {
192
193 return true;
194
196
197 return $this->isReportingDateReached();
198
200
201 if ($pass < $lastPass) {
202 return true;
203 }
204
205 return $this->isClosedPass($pass);
206
208
209 if (!$this->hasTestPassedOnce($this->getActiveId())) {
210 return false;
211 }
212
213 return $this->isClosedPass($pass);
214 }
215
216 return false;
217 }
218
220 {
221 if ($this->getLastFinishedPass() === null) {
222 throw new ilTestException('invalid object state: last finished pass was not set!');
223 }
224 }
225
226 private function isClosedPass($pass)
227 {
229
230 if ($pass <= $this->getLastFinishedPass()) {
231 return true;
232 }
233
234 if ($this->isProcessingTimeReached($pass)) {
235 return true;
236 }
237
238 return false;
239 }
240
241 private function isReportingDateReached()
242 {
243 $reg = '/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/';
244 $date = $this->testOBJ->getReportingDate();
245 $matches = null;
246
247 if (!preg_match($reg, $date, $matches)) {
248 return false;
249 }
250
251 $repTS = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
252
253 return time() >= $repTS;
254 }
255
257 {
258 if (!$this->testOBJ->getEnableProcessingTime()) {
259 return false;
260 }
261
262 $startingTime = $this->testOBJ->getStartingTimeOfUser($this->getActiveId(), $pass);
263
264 if ($startingTime === false) {
265 return false;
266 }
267
268 return $this->testOBJ->isMaxProcessingTimeReached($startingTime, $this->getActiveId());
269 }
270
275 {
276 if ($this->getLastFinishedPass() === null) {
277 return null;
278 }
279
280 $passes = $this->getLazyLoadedPasses();
281 return $passes[$this->getLastFinishedPass()]['tstamp'];
282 }
283
285 {
286 global $DIC; /* @var ILIAS\DI\Container $DIC */
287
288 if (!isset($this->testPassedOnceCache[$activeId])) {
289 $this->testPassedOnceCache[$activeId] = false;
290
291 $res = $DIC->database()->queryF(
292 "SELECT passed_once FROM tst_result_cache WHERE active_fi = %s",
293 array('integer'),
294 array($activeId)
295 );
296
297 while ($row = $DIC->database()->fetchAssoc($res)) {
298 $this->testPassedOnceCache[$activeId] = (bool) $row['passed_once'];
299 }
300 }
301
302 return $this->testPassedOnceCache[$activeId];
303 }
304}
An exception for terminatinating execution or to throw for unit testing.
const SCORE_REPORTING_IMMIDIATLY
const SCORE_REPORTING_FINISHED
const SCORE_REPORTING_AFTER_PASSED
const SCORE_REPORTING_DATE
Base Exception for all Exceptions relating to Modules/Test.
__construct(ilDBInterface $db, ilObjTest $testOBJ)
setLastFinishedPass($lastFinishedPass)
Interface ilDBInterface.
$row
$query
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res