ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilTestSession.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
14 {
15  const ACCESS_CODE_SESSION_INDEX = "tst_access_code";
16 
17  const ACCESS_CODE_CHAR_DOMAIN = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
18 
19  const ACCESS_CODE_LENGTH = 5;
20 
27 
33  var $user_id;
34 
41 
47  var $test_id;
48 
55 
60 
67 
73  var $tstamp;
74 
81 
83 
85 
94  public function __construct()
95  {
96  $this->active_id = 0;
97  $this->user_id = 0;
98  $this->anonymous_id = 0;
99  $this->test_id = 0;
100  $this->lastsequence = 0;
101  $this->lastPresentationMode = null;
102  $this->submitted = FALSE;
103  $this->submittedTimestamp = "";
104  $this->pass = 0;
105  $this->ref_id = 0;
106  $this->tstamp = 0;
107 
108  $this->lastFinishedPass = null;
109  $this->objectiveOrientedContainerId = 0;
110  }
111 
117  function setRefId($a_val)
118  {
119  $this->ref_id = $a_val;
120  }
121 
127  function getRefId()
128  {
129  return $this->ref_id;
130  }
131 
132  protected function activeIDExists($user_id, $test_id)
133  {
134  global $ilDB;
135 
136  if ($_SESSION["AccountId"] != ANONYMOUS_USER_ID)
137  {
138  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
139  array('integer','integer'),
140  array($user_id, $test_id)
141  );
142  if ($result->numRows())
143  {
144  $row = $ilDB->fetchAssoc($result);
145  $this->active_id = $row["active_id"];
146  $this->user_id = $row["user_fi"];
147  $this->anonymous_id = $row["anonymous_id"];
148  $this->test_id = $row["test_fi"];
149  $this->lastsequence = $row["lastindex"];
150  $this->pass = $row["tries"];
151  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
152  $this->submittedTimestamp = $row["submittimestamp"];
153  $this->tstamp = $row["tstamp"];
154 
155  $this->setLastFinishedPass($row['last_finished_pass']);
156  $this->setObjectiveOrientedContainerId((int)$row['objective_container']);
157 
158  return true;
159  }
160  }
161  return false;
162  }
163 
164  function increaseTestPass()
165  {
166  global $ilDB, $ilLog;
167 
168  $this->increasePass();
169  $this->setLastSequence(0);
170  $submitted = ($this->isSubmitted()) ? 1 : 0;
171  // 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)
172  if (time() - $_SESSION['tst_last_increase_pass'] > 10)
173  {
174  $_SESSION['tst_last_increase_pass'] = time();
175  $this->tstamp = time();
176  if ($this->active_id > 0)
177  {
178  $ilDB->update('tst_active',
179  array(
180  'lastindex' => array('integer', $this->getLastSequence()),
181  'tries' => array('integer', $this->getPass()),
182  'submitted' => array('integer', $submitted),
183  'submittimestamp' => array('timestamp', strlen($this->getSubmittedTimestamp()) ? $this->getSubmittedTimestamp() : NULL),
184  'tstamp' => array('integer', time()),
185  'last_finished_pass' => array('integer', $this->getLastFinishedPass()),
186  'objective_container' => array('integer', (int)$this->getObjectiveOrientedContainerId())
187  ),
188  array(
189  'active_id' => array('integer', $this->getActiveId())
190  )
191  );
192 
193  // update learning progress
194  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
195  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
197  ilObjTestAccess::_getParticipantId($this->active_id));
198  }
199  else
200  {
201  if (!$this->activeIDExists($this->getUserId(), $this->getTestId()))
202  {
203  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : NULL;
204  $submittedTs = (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL;
205  $next_id = $ilDB->nextId('tst_active');
206 
207  $ilDB->insert('tst_active', array(
208  'active_id' => array('integer', $next_id),
209  'user_fi' => array('integer', $this->getUserId()),
210  'anonymous_id' => array('text', $anonymous_id),
211  'test_fi' => array('integer', $this->getTestId()),
212  'lastindex' => array('integer', $this->getLastSequence()),
213  'tries' => array('integer', $this->getPass()),
214  'submitted' => array('integer', $submitted),
215  'submittimestamp' => array('timestamp', $submittedTs),
216  'tstamp' => array('integer', time()),
217  'objective_container' => array('integer', (int)$this->getObjectiveOrientedContainerId()),
218  ));
219 
220  $this->active_id = $next_id;
221 
222  // update learning progress
223  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
224  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
226  $this->getUserId());
227  }
228  }
229  }
230  }
231 
232  function saveToDb()
233  {
234  global $ilDB, $ilLog;
235 
236  $submitted = ($this->isSubmitted()) ? 1 : 0;
237  if ($this->active_id > 0)
238  {
239  $ilDB->update('tst_active',
240  array(
241  'lastindex' => array('integer', $this->getLastSequence()),
242  'tries' => array('integer', $this->getPass()),
243  'submitted' => array('integer', $submitted),
244  'submittimestamp' => array('timestamp', (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL),
245  'tstamp' => array('integer', time()-10),
246  'last_finished_pass' => array('integer', $this->getLastFinishedPass()),
247  'objective_container' => array('integer', (int)$this->getObjectiveOrientedContainerId())
248  ),
249  array(
250  'active_id' => array('integer', $this->getActiveId())
251  )
252  );
253 
254  // update learning progress
255  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
256  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
259  }
260  else
261  {
262  if (!$this->activeIDExists($this->getUserId(), $this->getTestId()))
263  {
264  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : NULL;
265 
266  $next_id = $ilDB->nextId('tst_active');
267  $ilDB->insert('tst_active',
268  array(
269  'active_id' => array('integer', $next_id),
270  'user_fi' => array('integer', $this->getUserId()),
271  'anonymous_id' => array('text', $anonymous_id),
272  'test_fi' => array('integer', $this->getTestId()),
273  'lastindex' => array('integer', $this->getLastSequence()),
274  'tries' => array('integer', $this->getPass()),
275  'submitted' => array('integer', $submitted),
276  'submittimestamp' => array('timestamp', (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL),
277  'tstamp' => array('integer', time()-10),
278  'last_finished_pass' => array('integer', $this->getLastFinishedPass()),
279  'objective_container' => array('integer', (int)$this->getObjectiveOrientedContainerId())
280  )
281  );
282  $this->active_id = $next_id;
283 
284  // update learning progress
285  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
286  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
288  $this->getUserId());
289  }
290  }
291  include_once './Modules/Test/classes/class.ilObjTestAccess.php';
292  include_once("./Services/Tracking/classes/class.ilLearningProgress.php");
295  $this->getRefId(),
296  'tst');
297  }
298 
299  function loadTestSession($test_id, $user_id = "", $anonymous_id = "")
300  {
301  global $ilDB;
302  global $ilUser;
303 
304  if (!$user_id)
305  {
306  $user_id = $ilUser->getId();
307  }
308  if (($_SESSION["AccountId"] == ANONYMOUS_USER_ID) && $this->doesAccessCodeInSessionExists())
309  {
310  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
311  array('integer','integer','text'),
312  array($user_id, $test_id, $this->getAccessCodeFromSession())
313  );
314  }
315  else if (strlen($anonymous_id))
316  {
317  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
318  array('integer','integer','text'),
319  array($user_id, $test_id, $anonymous_id)
320  );
321  }
322  else
323  {
324  if ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)
325  {
326  return NULL;
327  }
328  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
329  array('integer','integer'),
330  array($user_id, $test_id)
331  );
332  }
333  if ($result->numRows())
334  {
335  $row = $ilDB->fetchAssoc($result);
336  $this->active_id = $row["active_id"];
337  $this->user_id = $row["user_fi"];
338  $this->anonymous_id = $row["anonymous_id"];
339  $this->test_id = $row["test_fi"];
340  $this->lastsequence = $row["lastindex"];
341  $this->pass = $row["tries"];
342  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
343  $this->submittedTimestamp = $row["submittimestamp"];
344  $this->tstamp = $row["tstamp"];
345 
346  $this->setLastFinishedPass($row['last_finished_pass']);
347  $this->setObjectiveOrientedContainerId((int)$row['objective_container']);
348  }
349  elseif( $this->doesAccessCodeInSessionExists() )
350  {
351  $this->unsetAccessCodeInSession();
352  }
353  }
354 
360  public function loadFromDb($active_id)
361  {
362  global $ilDB;
363  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE active_id = %s",
364  array('integer'),
365  array($active_id)
366  );
367  if ($result->numRows())
368  {
369  $row = $ilDB->fetchAssoc($result);
370  $this->active_id = $row["active_id"];
371  $this->user_id = $row["user_fi"];
372  $this->anonymous_id = $row["anonymous_id"];
373  $this->test_id = $row["test_fi"];
374  $this->lastsequence = $row["lastindex"];
375  $this->pass = $row["tries"];
376  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
377  $this->submittedTimestamp = $row["submittimestamp"];
378  $this->tstamp = $row["tstamp"];
379 
380  $this->setLastFinishedPass($row['last_finished_pass']);
381  $this->setObjectiveOrientedContainerId((int)$row['objective_container']);
382  }
383  }
384 
385  function getActiveId()
386  {
387  return $this->active_id;
388  }
389 
390  function setUserId($user_id)
391  {
392  $this->user_id = $user_id;
393  }
394 
395  function getUserId()
396  {
397  return $this->user_id;
398  }
399 
400  function setTestId($test_id)
401  {
402  $this->test_id = $test_id;
403  }
404 
405  function getTestId()
406  {
407  return $this->test_id;
408  }
409 
410  function setAnonymousId($anonymous_id)
411  {
412  $this->anonymous_id = $anonymous_id;
413  }
414 
415  function getAnonymousId()
416  {
417  return $this->anonymous_id;
418  }
419 
420  public function setLastSequence($lastsequence)
421  {
422  $this->lastsequence = $lastsequence;
423  }
424 
425  public function getLastSequence()
426  {
427  return $this->lastsequence;
428  }
429 
430  function setPass($pass)
431  {
432  $this->pass = $pass;
433  }
434 
435  function getPass()
436  {
437  return $this->pass;
438  }
439 
440  function increasePass()
441  {
442  $this->pass += 1;
443  }
444 
445  function isSubmitted()
446  {
447  return $this->submitted;
448  }
449 
450  function setSubmitted()
451  {
452  $this->submitted = TRUE;
453  }
454 
456  {
458  }
459 
461  {
462  $this->submittedTimestamp = strftime("%Y-%m-%d %H:%M:%S");
463  }
464 
466  {
467  $this->lastFinishedPass = $lastFinishedPass;
468  }
469 
470  public function getLastFinishedPass()
471  {
473  }
474 
475  public function setObjectiveOrientedContainerId($objectiveOriented)
476  {
477  $this->objectiveOrientedContainerId = $objectiveOriented;
478  }
479 
481  {
483  }
484 
485  public function isObjectiveOriented()
486  {
487  return (bool)$this->getObjectiveOrientedContainerId();
488  }
489 
490  public function persistTestStartLock($testStartLock)
491  {
492  global $ilDB;
493 
494  $ilDB->update(
495  'tst_active',
496  array('start_lock' => array('text', $testStartLock)),
497  array('active_id' => array('integer', $this->getActiveId()))
498  );
499  }
500 
501  public function lookupTestStartLock()
502  {
503  global $ilDB;
504 
505  $res = $ilDB->queryF(
506  "SELECT start_lock FROM tst_active WHERE active_id = %s",
507  array('integer'), array($this->getActiveId())
508  );
509 
510  while($row = $ilDB->fetchAssoc($res))
511  {
512  return $row['start_lock'];
513  }
514 
515  return null;
516  }
517 
518  public function setAccessCodeToSession($access_code)
519  {
520  if (!is_array($_SESSION[self::ACCESS_CODE_SESSION_INDEX]))
521  {
522  $_SESSION[self::ACCESS_CODE_SESSION_INDEX] = array();
523  }
524 
525  $_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()] = $access_code;
526  }
527 
528  public function unsetAccessCodeInSession()
529  {
530  unset($_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()]);
531  }
532 
533  public function getAccessCodeFromSession()
534  {
535  if( !is_array($_SESSION[self::ACCESS_CODE_SESSION_INDEX]) )
536  {
537  return null;
538  }
539 
540  if( !isset($_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()]) )
541  {
542  return null;
543  }
544 
545  return $_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()];
546  }
547 
549  {
550  if( !is_array($_SESSION[self::ACCESS_CODE_SESSION_INDEX]) )
551  {
552  return false;
553  }
554 
555  return isset($_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()]);
556  }
557 
558  public function createNewAccessCode()
559  {
560  do
561  {
562  $code = $this->buildAccessCode();
563  }
564  while( $this->isAccessCodeUsed($code) );
565 
566  return $code;
567  }
568 
569  public function isAccessCodeUsed($code)
570  {
571  global $ilDB;
572 
573  $query = "SELECT anonymous_id FROM tst_active WHERE test_fi = %s AND anonymous_id = %s";
574 
575  $result = $ilDB->queryF(
576  $query, array('integer', 'text'), array($this->getTestId(), $code)
577  );
578 
579  return ($result->numRows() > 0);
580  }
581 
582  private function buildAccessCode()
583  {
584  // create a 5 character code
585  $codestring = self::ACCESS_CODE_CHAR_DOMAIN;
586 
587  mt_srand();
588 
589  $code = "";
590 
591  for($i = 1; $i <= self::ACCESS_CODE_LENGTH; $i++)
592  {
593  $index = mt_rand(0, strlen($codestring)-1);
594  $code .= substr($codestring, $index, 1);
595  }
596 
597  return $code;
598  }
599 
600  public function isAnonymousUser()
601  {
602  return $this->getUserId() == ANONYMOUS_USER_ID;
603  }
604 }
$_SESSION["AccountId"]
$result
$code
Definition: example_050.php:99
setObjectiveOrientedContainerId($objectiveOriented)
setRefId($a_val)
Set Ref id.
static _updateStatus($a_obj_id, $a_usr_id, $a_obj=null, $a_percentage=false, $a_force_raise=false)
Update status.
__construct()
ilTestSession constructor
static _tracProgress($a_user_id, $a_obj_id, $a_ref_id, $a_obj_type='')
setAnonymousId($anonymous_id)
_lookupObjIdForTestId($a_test_id)
Lookup object id for test id.
setAccessCodeToSession($access_code)
loadFromDb($active_id)
Loads the session data for a given active id.
setLastFinishedPass($lastFinishedPass)
getRefId()
Get Ref id.
_getParticipantId($active_id)
Get user id for active id.
Test session handler.
global $ilUser
Definition: imgupload.php:15
persistTestStartLock($testStartLock)
$ref_id
Definition: sahs_server.php:39
global $ilDB
setLastSequence($lastsequence)
activeIDExists($user_id, $test_id)
loadTestSession($test_id, $user_id="", $anonymous_id="")