ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
24  {
25  $this->db = $db;
26  $this->testOBJ = $testOBJ;
27  }
28 
29  public function getActiveId()
30  {
31  return $this->activeId;
32  }
33 
34  public function setActiveId($activeId)
35  {
36  $this->activeId = $activeId;
37  }
38 
39  public function getLastFinishedPass()
40  {
42  }
43 
45  {
47  $this->lastFinishedPass = $lastFinishedPass;
48  }
49 
50  private function passesLoaded()
51  {
52  return is_array($this->passes);
53  }
54 
55  private function ensureLoadedPasses()
56  {
57  if (!$this->passesLoaded()) {
58  $this->loadPasses();
59  }
60  }
61  private function loadPasses()
62  {
63  $query = "
64  SELECT DISTINCT tst_pass_result.* FROM tst_pass_result
65  LEFT JOIN tst_test_result
66  ON tst_pass_result.pass = tst_test_result.pass
67  AND tst_pass_result.active_fi = tst_test_result.active_fi
68  WHERE tst_pass_result.active_fi = %s
69  ORDER BY tst_pass_result.pass
70  ";
71 
72  $res = $this->db->queryF(
73  $query,
74  array('integer'),
75  array($this->getActiveId())
76  );
77 
78  $this->passes = array();
79 
80  while ($row = $this->db->fetchAssoc($res)) {
81  $this->passes[$row['pass']] = $row;
82  }
83  }
84 
85  private function getLazyLoadedPasses()
86  {
87  $this->ensureLoadedPasses();
88  return $this->passes;
89  }
90 
91  public function loadLastFinishedPass()
92  {
93  $query = "
94  SELECT last_finished_pass FROM tst_active WHERE active_id = %s
95  ";
96 
97  $res = $this->db->queryF(
98  $query,
99  array('integer'),
100  array($this->getActiveId())
101  );
102 
103  while ($row = $this->db->fetchAssoc($res)) {
104  $this->setLastFinishedPass($row['last_finished_pass']);
105  }
106  }
107 
108  public function getExistingPasses()
109  {
110  return array_keys($this->getLazyLoadedPasses());
111  }
112 
113  public function getNumExistingPasses()
114  {
115  return count($this->getExistingPasses());
116  }
117 
118  public function getClosedPasses()
119  {
120  $existingPasses = $this->getExistingPasses();
121  $closedPasses = $this->fetchClosedPasses($existingPasses);
122 
123  return $closedPasses;
124  }
125 
126  public function getReportablePasses()
127  {
128  $existingPasses = $this->getExistingPasses();
129 
130  $reportablePasses = $this->fetchReportablePasses($existingPasses);
131 
132  return $reportablePasses;
133  }
134 
135  private function fetchReportablePasses($existingPasses)
136  {
137  $lastPass = $this->fetchLastPass($existingPasses);
138 
139  $reportablePasses = array();
140 
141  foreach ($existingPasses as $pass) {
142  if ($this->isReportablePass($lastPass, $pass)) {
143  $reportablePasses[] = $pass;
144  }
145  }
146 
147  return $reportablePasses;
148  }
149 
150  private function fetchClosedPasses($existingPasses)
151  {
152  $closedPasses = array();
153 
154  foreach ($existingPasses as $pass) {
155  if ($this->isClosedPass($pass)) {
156  $closedPasses[] = $pass;
157  }
158  }
159 
160  return $closedPasses;
161  }
162 
163  private function fetchLastPass($existingPasses)
164  {
165  $lastPass = null;
166 
167  foreach ($existingPasses as $pass) {
168  if ($lastPass === null || $pass > $lastPass) {
169  $lastPass = $pass;
170  }
171  }
172 
173  return $lastPass;
174  }
175 
176  private function isReportablePass($lastPass, $pass)
177  {
178  switch ($this->testOBJ->getScoreReporting()) {
180 
181  return true;
182 
184 
185  return $this->isReportingDateReached();
186 
188 
189  if ($pass < $lastPass) {
190  return true;
191  }
192 
193  return $this->isClosedPass($pass);
194  }
195 
196  return false;
197  }
198 
200  {
201  if ($this->getLastFinishedPass() === null) {
202  throw new ilTestException('invalid object state: last finished pass was not set!');
203  }
204  }
205 
206  private function isClosedPass($pass)
207  {
209 
210  if ($pass <= $this->getLastFinishedPass()) {
211  return true;
212  }
213 
214  if ($this->isProcessingTimeReached($pass)) {
215  return true;
216  }
217 
218  return false;
219  }
220 
221  private function isReportingDateReached()
222  {
223  $reg = '/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/';
224  $date = $this->testOBJ->getReportingDate();
225  $matches = null;
226 
227  if (!preg_match($reg, $date, $matches)) {
228  return false;
229  }
230 
231  $repTS = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
232 
233  return time() >= $repTS;
234  }
235 
236  private function isProcessingTimeReached($pass)
237  {
238  if (!$this->testOBJ->getEnableProcessingTime()) {
239  return false;
240  }
241 
242  $startingTime = $this->testOBJ->getStartingTimeOfUser($this->getActiveId(), $pass);
243 
244  if ($startingTime === false) {
245  return false;
246  }
247 
248  return $this->testOBJ->isMaxProcessingTimeReached($startingTime, $this->getActiveId());
249  }
250 
255  {
256  if ($this->getLastFinishedPass() === null) {
257  return null;
258  }
259 
260  $passes = $this->getLazyLoadedPasses();
261  return $passes[$this->getLastFinishedPass()]['tstamp'];
262  }
263 }
__construct(ilDBInterface $db, ilObjTest $testOBJ)
Base Exception for all Exceptions relating to Modules/Test.
Interface ilDBInterface.
const SCORE_REPORTING_IMMIDIATLY
foreach($_POST as $key=> $value) $res
setLastFinishedPass($lastFinishedPass)
$query
Create styles array
The data for the language used.
const SCORE_REPORTING_FINISHED
const SCORE_REPORTING_DATE
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.