ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTestSession.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
15 {
22 
28  var $user_id;
29 
36 
42  var $test_id;
43 
50 
57 
63  var $tstamp;
64 
71 
80  function ilTestSession($active_id = "")
81  {
82  $this->active_id = 0;
83  $this->user_id = 0;
84  $this->anonymous_id = 0;
85  $this->test_id = 0;
86  $this->lastsequence = 0;
87  $this->submitted = FALSE;
88  $this->submittedTimestamp = "";
89  $this->pass = 0;
90  $this->ref_id = 0;
91  $this->tstamp = 0;
92  if ($active_id > 0)
93  {
94  $this->loadFromDb($active_id);
95  }
96  }
97 
103  function setRefId($a_val)
104  {
105  $this->ref_id = $a_val;
106  }
107 
113  function getRefId()
114  {
115  return $this->ref_id;
116  }
117 
118  private function activeIDExists($user_id, $test_id)
119  {
120  global $ilDB;
121 
122  if ($_SESSION["AccountId"] != ANONYMOUS_USER_ID)
123  {
124  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
125  array('integer','integer'),
126  array($user_id, $test_id)
127  );
128  if ($result->numRows())
129  {
130  $row = $ilDB->fetchAssoc($result);
131  $this->active_id = $row["active_id"];
132  $this->active_id = $row["active_id"];
133  $this->user_id = $row["user_fi"];
134  $this->anonymous_id = $row["anonymous_id"];
135  $this->test_id = $row["test_fi"];
136  $this->lastsequence = $row["lastindex"];
137  $this->pass = $row["tries"];
138  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
139  $this->submittedTimestamp = $row["submittimestamp"];
140  $this->tstamp = $row["tstamp"];
141  return true;
142  }
143  }
144  return false;
145  }
146 
147  function increaseTestPass()
148  {
149  global $ilDB, $ilLog;
150 
151  $this->increasePass();
152  $this->setLastSequence(0);
153  $submitted = ($this->isSubmitted()) ? 1 : 0;
154  // there has to be at least 10 seconds between new test passes (to ensure that noone double clicks the finish button and increases the test pass by more than 1)
155  if (time() - $_SESSION['tst_last_increase_pass'] > 10)
156  {
157  $_SESSION['tst_last_increase_pass'] = time();
158  $this->tstamp = time();
159  if ($this->active_id > 0)
160  {
161  $affectedRows = $ilDB->manipulateF("UPDATE tst_active SET lastindex = %s, tries = %s, submitted = %s, submittimestamp = %s, tstamp = %s WHERE active_id = %s",
162  array('integer', 'integer', 'integer', 'timestamp', 'integer', 'integer'),
163  array(
164  $this->getLastSequence(),
165  $this->getPass(),
166  $submitted,
167  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
168  time(),
169  $this->getActiveId()
170  )
171  );
172 
173  // update learning progress
174  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
175  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
177  ilObjTestAccess::_getParticipantId($this->active_id));
178  }
179  else
180  {
181  if (!$this->activeIDExists($this->getUserId(), $this->getTestId()))
182  {
183  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : NULL;
184  $next_id = $ilDB->nextId('tst_active');
185  $affectedRows = $ilDB->manipulateF("INSERT INTO tst_active (active_id, user_fi, anonymous_id, test_fi, lastindex, tries, submitted, submittimestamp, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
186  array('integer', 'integer', 'text', 'integer', 'integer', 'integer', 'integer', 'timestamp', 'integer'),
187  array(
188  $next_id,
189  $this->getUserId(),
191  $this->getTestId(),
192  $this->getLastSequence(),
193  $this->getPass(),
194  $submitted,
195  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
196  time()
197  )
198  );
199  $this->active_id = $next_id;
200 
201  // update learning progress
202  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
203  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
205  $this->getUserId());
206  }
207  }
208  }
209  }
210 
211  function saveToDb()
212  {
213  global $ilDB, $ilLog;
214 
215  $submitted = ($this->isSubmitted()) ? 1 : 0;
216  if ($this->active_id > 0)
217  {
218  $affectedRows = $ilDB->manipulateF("UPDATE tst_active SET lastindex = %s, tries = %s, submitted = %s, submittimestamp = %s, tstamp = %s WHERE active_id = %s",
219  array('integer', 'integer', 'integer', 'timestamp', 'integer', 'integer'),
220  array(
221  $this->getLastSequence(),
222  $this->getPass(),
223  $submitted,
224  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
225  time()-10,
226  $this->getActiveId()
227  )
228  );
229 
230  // update learning progress
231  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
232  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
235  }
236  else
237  {
238  if (!$this->activeIDExists($this->getUserId(), $this->getTestId()))
239  {
240  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : NULL;
241  $next_id = $ilDB->nextId('tst_active');
242  $affectedRows = $ilDB->manipulateF("INSERT INTO tst_active (active_id, user_fi, anonymous_id, test_fi, lastindex, tries, submitted, submittimestamp, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
243  array('integer', 'integer', 'text', 'integer', 'integer', 'integer', 'integer', 'timestamp', 'integer'),
244  array(
245  $next_id,
246  $this->getUserId(),
248  $this->getTestId(),
249  $this->getLastSequence(),
250  $this->getPass(),
251  $submitted,
252  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
253  time()-10
254  )
255  );
256  $this->active_id = $next_id;
257 
258  // update learning progress
259  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
260  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
262  $this->getUserId());
263  }
264  }
265 
266  include_once("./Services/Tracking/classes/class.ilLearningProgress.php");
269  $this->getRefId(),
270  'tst');
271  }
272 
274  {
275  global $ilDB;
276  global $ilUser;
277 
278  if (!$user_id)
279  {
280  $user_id = $ilUser->getId();
281  }
282  if (($_SESSION["AccountId"] == ANONYMOUS_USER_ID) && (strlen($_SESSION["tst_access_code"][$test_id])))
283  {
284  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
285  array('integer','integer','text'),
286  array($user_id, $test_id, $_SESSION["tst_access_code"][$test_id])
287  );
288  }
289  else if (strlen($anonymous_id))
290  {
291  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
292  array('integer','integer','text'),
293  array($user_id, $test_id, $anonymous_id)
294  );
295  }
296  else
297  {
298  if ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)
299  {
300  return NULL;
301  }
302  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
303  array('integer','integer'),
304  array($user_id, $test_id)
305  );
306  }
307  if ($result->numRows())
308  {
309  $row = $ilDB->fetchAssoc($result);
310  $this->active_id = $row["active_id"];
311  $this->user_id = $row["user_fi"];
312  $this->anonymous_id = $row["anonymous_id"];
313  $this->test_id = $row["test_fi"];
314  $this->lastsequence = $row["lastindex"];
315  $this->pass = $row["tries"];
316  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
317  $this->submittedTimestamp = $row["submittimestamp"];
318  $this->tstamp = $row["tstamp"];
319  }
320  }
321 
328  private function loadFromDb($active_id)
329  {
330  global $ilDB;
331  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE active_id = %s",
332  array('integer'),
333  array($active_id)
334  );
335  if ($result->numRows())
336  {
337  $row = $ilDB->fetchAssoc($result);
338  $this->active_id = $row["active_id"];
339  $this->user_id = $row["user_fi"];
340  $this->anonymous_id = $row["anonymous_id"];
341  $this->test_id = $row["test_fi"];
342  $this->lastsequence = $row["lastindex"];
343  $this->pass = $row["tries"];
344  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
345  $this->submittedTimestamp = $row["submittimestamp"];
346  $this->tstamp = $row["tstamp"];
347  }
348  }
349 
350  function getActiveId()
351  {
352  return $this->active_id;
353  }
354 
355  function setUserId($user_id)
356  {
357  $this->user_id = $user_id;
358  }
359 
360  function getUserId()
361  {
362  return $this->user_id;
363  }
364 
365  function setTestId($test_id)
366  {
367  $this->test_id = $test_id;
368  }
369 
370  function getTestId()
371  {
372  return $this->test_id;
373  }
374 
376  {
377  $this->anonymous_id = $anonymous_id;
378  }
379 
380  function getAnonymousId()
381  {
382  return $this->anonymous_id;
383  }
384 
386  {
387  $this->lastsequence = $lastsequence;
388  }
389 
390  function getLastSequence()
391  {
392  return $this->lastsequence;
393  }
394 
395  function setPass($pass)
396  {
397  $this->pass = $pass;
398  }
399 
400  function getPass()
401  {
402  return $this->pass;
403  }
404 
405  function increasePass()
406  {
407  $this->pass += 1;
408  }
409 
410  function isSubmitted()
411  {
412  return $this->submitted;
413  }
414 
415  function setSubmitted()
416  {
417  $this->submitted = TRUE;
418  }
419 
421  {
423  }
424 
426  {
427  $this->submittedTimestamp = strftime("%Y-%m-%d %H:%M:%S");
428  }
429 }
430 
431 ?>