ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
All Data Structures Namespaces Files Functions Variables Typedefs Modules Pages
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 
26  public $active_id;
27 
33  public $user_id;
34 
40  public $anonymous_id;
41 
47  public $test_id;
48 
54  public $lastsequence;
55 
60 
66  public $submitted;
67 
73  public $tstamp;
74 
81 
83 
85 
87 
96  public function __construct()
97  {
98  $this->active_id = 0;
99  $this->user_id = 0;
100  $this->anonymous_id = 0;
101  $this->test_id = 0;
102  $this->lastsequence = 0;
103  $this->lastPresentationMode = null;
104  $this->submitted = false;
105  $this->submittedTimestamp = "";
106  $this->pass = 0;
107  $this->ref_id = 0;
108  $this->tstamp = 0;
109 
110  $this->lastStartedPass = null;
111  $this->lastFinishedPass = null;
112  $this->objectiveOrientedContainerId = 0;
113  }
114 
120  public function setRefId($a_val)
121  {
122  $this->ref_id = $a_val;
123  }
124 
130  public function getRefId()
131  {
132  return $this->ref_id;
133  }
134 
135  protected function activeIDExists($user_id, $test_id)
136  {
137  global $ilDB;
138 
139  if ($GLOBALS['DIC']['ilUser']->getId() != ANONYMOUS_USER_ID) {
140  $result = $ilDB->queryF(
141  "SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
142  array('integer','integer'),
144  );
145  if ($result->numRows()) {
146  $row = $ilDB->fetchAssoc($result);
147  $this->active_id = $row["active_id"];
148  $this->user_id = $row["user_fi"];
149  $this->anonymous_id = $row["anonymous_id"];
150  $this->test_id = $row["test_fi"];
151  $this->lastsequence = $row["lastindex"];
152  $this->pass = $row["tries"];
153  $this->submitted = ($row["submitted"]) ? true : false;
154  $this->submittedTimestamp = $row["submittimestamp"];
155  $this->tstamp = $row["tstamp"];
156 
157  $this->setLastStartedPass($row['last_started_pass']);
158  $this->setLastFinishedPass($row['last_finished_pass']);
159  $this->setObjectiveOrientedContainerId((int) $row['objective_container']);
160 
161  return true;
162  }
163  }
164  return false;
165  }
166 
167  public function increaseTestPass()
168  {
169  global $ilDB, $ilLog;
170 
171  if (!$this->active_id) {
172  require_once 'Modules/Test/exceptions/class.ilTestException.php';
173  throw new ilTestException('missing active id on test pass increase!');
174  }
175 
176  $this->increasePass();
177  $this->setLastSequence(0);
178  $submitted = ($this->isSubmitted()) ? 1 : 0;
179 
180  if (!isset($_SESSION[$this->active_id]['tst_last_increase_pass'])) {
181  $_SESSION[$this->active_id]['tst_last_increase_pass'] = 0;
182  }
183 
184  // 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)
185  if (time() - $_SESSION[$this->active_id]['tst_last_increase_pass'] > 10) {
186  $_SESSION[$this->active_id]['tst_last_increase_pass'] = time();
187  $this->tstamp = time();
188  $ilDB->update(
189  'tst_active',
190  array(
191  'lastindex' => array('integer', $this->getLastSequence()),
192  'tries' => array('integer', $this->getPass()),
193  'submitted' => array('integer', $submitted),
194  'submittimestamp' => array('timestamp', strlen($this->getSubmittedTimestamp()) ? $this->getSubmittedTimestamp() : null),
195  'tstamp' => array('integer', time()),
196  'last_finished_pass' => array('integer', $this->getLastFinishedPass()),
197  'last_started_pass' => array('integer', $this->getLastStartedPass()),
198  'objective_container' => array('integer', (int) $this->getObjectiveOrientedContainerId())
199  ),
200  array(
201  'active_id' => array('integer', $this->getActiveId())
202  )
203  );
204  }
205  }
206 
207  public function saveToDb()
208  {
209  global $ilDB, $ilLog;
210 
211  $submitted = ($this->isSubmitted()) ? 1 : 0;
212  if ($this->active_id > 0) {
213  $ilDB->update(
214  'tst_active',
215  array(
216  'lastindex' => array('integer', $this->getLastSequence()),
217  'tries' => array('integer', $this->getPass()),
218  'submitted' => array('integer', $submitted),
219  'submittimestamp' => array('timestamp', (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : null),
220  'tstamp' => array('integer', time()-10),
221  'last_finished_pass' => array('integer', $this->getLastFinishedPass()),
222  'last_started_pass' => array('integer', $this->getPass()),
223  'objective_container' => array('integer', (int) $this->getObjectiveOrientedContainerId())
224  ),
225  array(
226  'active_id' => array('integer', $this->getActiveId())
227  )
228  );
229  } else {
230  if (!$this->activeIDExists($this->getUserId(), $this->getTestId())) {
231  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : null;
232 
233  $next_id = $ilDB->nextId('tst_active');
234  $ilDB->insert(
235  'tst_active',
236  array(
237  'active_id' => array('integer', $next_id),
238  'user_fi' => array('integer', $this->getUserId()),
239  'anonymous_id' => array('text', $anonymous_id),
240  'test_fi' => array('integer', $this->getTestId()),
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  'last_started_pass' => array('integer', $this->getPass()),
248  'objective_container' => array('integer', (int) $this->getObjectiveOrientedContainerId())
249  )
250  );
251  $this->active_id = $next_id;
252  }
253  }
254  }
255 
256  public function loadTestSession($test_id, $user_id = "", $anonymous_id = "")
257  {
258  global $ilDB;
259  global $ilUser;
260 
261  if (!$user_id) {
262  $user_id = $ilUser->getId();
263  }
264  if (($GLOBALS['DIC']['ilUser']->getId() == ANONYMOUS_USER_ID) && $this->doesAccessCodeInSessionExists()) {
265  $result = $ilDB->queryF(
266  "SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
267  array('integer','integer','text'),
269  );
270  } elseif (strlen($anonymous_id)) {
271  $result = $ilDB->queryF(
272  "SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
273  array('integer','integer','text'),
275  );
276  } else {
277  if ($GLOBALS['DIC']['ilUser']->getId() == ANONYMOUS_USER_ID) {
278  return null;
279  }
280  $result = $ilDB->queryF(
281  "SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
282  array('integer','integer'),
284  );
285  }
286 
287  // TODO bheyser: Refactor
288  $this->user_id = $user_id;
289 
290  if ($result->numRows()) {
291  $row = $ilDB->fetchAssoc($result);
292  $this->active_id = $row["active_id"];
293  $this->user_id = $row["user_fi"];
294  $this->anonymous_id = $row["anonymous_id"];
295  $this->test_id = $row["test_fi"];
296  $this->lastsequence = $row["lastindex"];
297  $this->pass = $row["tries"];
298  $this->submitted = ($row["submitted"]) ? true : false;
299  $this->submittedTimestamp = $row["submittimestamp"];
300  $this->tstamp = $row["tstamp"];
301 
302  $this->setLastStartedPass($row['last_started_pass']);
303  $this->setLastFinishedPass($row['last_finished_pass']);
304  $this->setObjectiveOrientedContainerId((int) $row['objective_container']);
305  } elseif ($this->doesAccessCodeInSessionExists()) {
306  $this->unsetAccessCodeInSession();
307  }
308  }
309 
315  public function loadFromDb($active_id)
316  {
317  global $ilDB;
318  $result = $ilDB->queryF(
319  "SELECT * FROM tst_active WHERE active_id = %s",
320  array('integer'),
322  );
323  if ($result->numRows()) {
324  $row = $ilDB->fetchAssoc($result);
325  $this->active_id = $row["active_id"];
326  $this->user_id = $row["user_fi"];
327  $this->anonymous_id = $row["anonymous_id"];
328  $this->test_id = $row["test_fi"];
329  $this->lastsequence = $row["lastindex"];
330  $this->pass = $row["tries"];
331  $this->submitted = ($row["submitted"]) ? true : false;
332  $this->submittedTimestamp = $row["submittimestamp"];
333  $this->tstamp = $row["tstamp"];
334 
335  $this->setLastStartedPass($row['last_started_pass']);
336  $this->setLastFinishedPass($row['last_finished_pass']);
337  $this->setObjectiveOrientedContainerId((int) $row['objective_container']);
338  }
339  }
340 
341  public function getActiveId()
342  {
343  return $this->active_id;
344  }
345 
346  public function setUserId($user_id)
347  {
348  $this->user_id = $user_id;
349  }
350 
351  public function getUserId()
352  {
353  return $this->user_id;
354  }
355 
356  public function setTestId($test_id)
357  {
358  $this->test_id = $test_id;
359  }
360 
361  public function getTestId()
362  {
363  return $this->test_id;
364  }
365 
366  public function setAnonymousId($anonymous_id)
367  {
368  $this->anonymous_id = $anonymous_id;
369  }
370 
371  public function getAnonymousId()
372  {
373  return $this->anonymous_id;
374  }
375 
377  {
378  $this->lastsequence = $lastsequence;
379  }
380 
381  public function getLastSequence()
382  {
383  return $this->lastsequence;
384  }
385 
386  public function setPass($pass)
387  {
388  $this->pass = $pass;
389  }
390 
391  public function getPass()
392  {
393  return $this->pass;
394  }
395 
396  public function increasePass()
397  {
398  $this->pass += 1;
399  }
400 
401  public function isSubmitted()
402  {
403  return $this->submitted;
404  }
405 
406  public function setSubmitted()
407  {
408  $this->submitted = true;
409  }
410 
411  public function getSubmittedTimestamp()
412  {
414  }
415 
416  public function setSubmittedTimestamp()
417  {
418  $this->submittedTimestamp = strftime("%Y-%m-%d %H:%M:%S");
419  }
420 
422  {
423  $this->lastFinishedPass = $lastFinishedPass;
424  }
425 
426  public function getLastFinishedPass()
427  {
429  }
430 
431  public function setObjectiveOrientedContainerId($objectiveOriented)
432  {
433  $this->objectiveOrientedContainerId = $objectiveOriented;
434  }
435 
437  {
439  }
440 
444  public function getLastStartedPass()
445  {
446  return $this->lastStartedPass;
447  }
448 
453  {
454  $this->lastStartedPass = $lastStartedPass;
455  }
456 
457  public function isObjectiveOriented()
458  {
459  return (bool) $this->getObjectiveOrientedContainerId();
460  }
461 
462  public function persistTestStartLock($testStartLock)
463  {
464  global $ilDB;
465 
466  $ilDB->update(
467  'tst_active',
468  array('start_lock' => array('text', $testStartLock)),
469  array('active_id' => array('integer', $this->getActiveId()))
470  );
471  }
472 
473  public function lookupTestStartLock()
474  {
475  global $ilDB;
476 
477  $res = $ilDB->queryF(
478  "SELECT start_lock FROM tst_active WHERE active_id = %s",
479  array('integer'),
480  array($this->getActiveId())
481  );
482 
483  while ($row = $ilDB->fetchAssoc($res)) {
484  return $row['start_lock'];
485  }
486 
487  return null;
488  }
489 
490  public function setAccessCodeToSession($access_code)
491  {
492  if (!is_array($_SESSION[self::ACCESS_CODE_SESSION_INDEX])) {
493  $_SESSION[self::ACCESS_CODE_SESSION_INDEX] = array();
494  }
495 
496  $_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()] = $access_code;
497  }
498 
499  public function unsetAccessCodeInSession()
500  {
501  unset($_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()]);
502  }
503 
504  public function getAccessCodeFromSession()
505  {
506  if (!is_array($_SESSION[self::ACCESS_CODE_SESSION_INDEX])) {
507  return null;
508  }
509 
510  if (!isset($_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()])) {
511  return null;
512  }
513 
514  return $_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()];
515  }
516 
518  {
519  if (!is_array($_SESSION[self::ACCESS_CODE_SESSION_INDEX])) {
520  return false;
521  }
522 
523  return isset($_SESSION[self::ACCESS_CODE_SESSION_INDEX][$this->getTestId()]);
524  }
525 
526  public function createNewAccessCode()
527  {
528  do {
529  $code = $this->buildAccessCode();
530  } while ($this->isAccessCodeUsed($code));
531 
532  return $code;
533  }
534 
535  public function isAccessCodeUsed($code)
536  {
537  global $ilDB;
538 
539  $query = "SELECT anonymous_id FROM tst_active WHERE test_fi = %s AND anonymous_id = %s";
540 
541  $result = $ilDB->queryF(
542  $query,
543  array('integer', 'text'),
544  array($this->getTestId(), $code)
545  );
546 
547  return ($result->numRows() > 0);
548  }
549 
550  private function buildAccessCode()
551  {
552  // create a 5 character code
553  $codestring = self::ACCESS_CODE_CHAR_DOMAIN;
554 
555  mt_srand();
556 
557  $code = "";
558 
559  for ($i = 1; $i <= self::ACCESS_CODE_LENGTH; $i++) {
560  $index = mt_rand(0, strlen($codestring)-1);
561  $code .= substr($codestring, $index, 1);
562  }
563 
564  return $code;
565  }
566 
567  public function isAnonymousUser()
568  {
569  return $this->getUserId() == ANONYMOUS_USER_ID;
570  }
571 }
$_SESSION["AccountId"]
$result
$code
Definition: example_050.php:99
setObjectiveOrientedContainerId($objectiveOriented)
setRefId($a_val)
Set Ref id.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
__construct()
ilTestSession constructor
$index
Definition: metadata.php:60
setAnonymousId($anonymous_id)
Base Exception for all Exceptions relating to Modules/Test.
setAccessCodeToSession($access_code)
loadFromDb($active_id)
Loads the session data for a given active id.
setLastFinishedPass($lastFinishedPass)
foreach($_POST as $key=> $value) $res
setLastStartedPass($lastStartedPass)
getRefId()
Get Ref id.
$ilUser
Definition: imgupload.php:18
Test session handler.
$query
Create styles array
The data for the language used.
persistTestStartLock($testStartLock)
global $ilDB
setLastSequence($lastsequence)
$i
Definition: disco.tpl.php:19
activeIDExists($user_id, $test_id)
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
loadTestSession($test_id, $user_id="", $anonymous_id="")