ILIAS  eassessment Revision 61809
 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  function increaseTestPass()
119  {
120  global $ilDB, $ilLog;
121 
122  $this->increasePass();
123  $this->setLastSequence(0);
124  $submitted = ($this->isSubmitted()) ? 1 : 0;
125  // 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)
126  if (time() - $_SESSION['tst_last_increase_pass'] > 10)
127  {
128  $_SESSION['tst_last_increase_pass'] = time();
129  $this->tstamp = time();
130  if ($this->active_id > 0)
131  {
132  $affectedRows = $ilDB->manipulateF("UPDATE tst_active SET lastindex = %s, tries = %s, submitted = %s, submittimestamp = %s, tstamp = %s WHERE active_id = %s",
133  array('integer', 'integer', 'integer', 'timestamp', 'integer', 'integer'),
134  array(
135  $this->getLastSequence(),
136  $this->getPass(),
137  $submitted,
138  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
139  time(),
140  $this->getActiveId()
141  )
142  );
143 
144  // update learning progress
145  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
146  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
148  ilObjTestAccess::_getParticipantId($this->active_id));
149 
150  }
151  else
152  {
153  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : NULL;
154  $next_id = $ilDB->nextId('tst_active');
155  $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)",
156  array('integer', 'integer', 'text', 'integer', 'integer', 'integer', 'integer', 'timestamp', 'integer'),
157  array(
158  $next_id,
159  $this->getUserId(),
161  $this->getTestId(),
162  $this->getLastSequence(),
163  $this->getPass(),
164  $submitted,
165  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
166  time()
167  )
168  );
169  $this->active_id = $next_id;
170 
171  // update learning progress
172  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
173  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
175  $this->getUserId());
176 
177  }
178  }
179  }
180 
181  function saveToDb()
182  {
183  global $ilDB, $ilLog;
184 
185  $submitted = ($this->isSubmitted()) ? 1 : 0;
186  if ($this->active_id > 0)
187  {
188  $affectedRows = $ilDB->manipulateF("UPDATE tst_active SET lastindex = %s, tries = %s, submitted = %s, submittimestamp = %s, tstamp = %s WHERE active_id = %s",
189  array('integer', 'integer', 'integer', 'timestamp', 'integer', 'integer'),
190  array(
191  $this->getLastSequence(),
192  $this->getPass(),
193  $submitted,
194  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
195  time()-10,
196  $this->getActiveId()
197  )
198  );
199 
200  // update learning progress
201  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
202  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
205  }
206  else
207  {
208  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : NULL;
209  $next_id = $ilDB->nextId('tst_active');
210  $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)",
211  array('integer', 'integer', 'text', 'integer', 'integer', 'integer', 'integer', 'timestamp', 'integer'),
212  array(
213  $next_id,
214  $this->getUserId(),
216  $this->getTestId(),
217  $this->getLastSequence(),
218  $this->getPass(),
219  $submitted,
220  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
221  time()-10
222  )
223  );
224  $this->active_id = $next_id;
225 
226  // update learning progress
227  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
228  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
230  $this->getUserId());
231 
232  }
233 
234  include_once("./Services/Tracking/classes/class.ilLearningProgress.php");
237  $this->getRefId(),
238  'tst');
239  }
240 
242  {
243  global $ilDB;
244  global $ilUser;
245 
246  if (!$user_id)
247  {
248  $user_id = $ilUser->getId();
249  }
250  if (($_SESSION["AccountId"] == ANONYMOUS_USER_ID) && (strlen($_SESSION["tst_access_code"][$test_id])))
251  {
252  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
253  array('integer','integer','text'),
254  array($user_id, $test_id, $_SESSION["tst_access_code"][$test_id])
255  );
256  }
257  else if (strlen($anonymous_id))
258  {
259  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
260  array('integer','integer','text'),
261  array($user_id, $test_id, $anonymous_id)
262  );
263  }
264  else
265  {
266  if ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)
267  {
268  return NULL;
269  }
270  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
271  array('integer','integer'),
272  array($user_id, $test_id)
273  );
274  }
275  if ($result->numRows())
276  {
277  $row = $ilDB->fetchAssoc($result);
278  $this->active_id = $row["active_id"];
279  $this->user_id = $row["user_fi"];
280  $this->anonymous_id = $row["anonymous_id"];
281  $this->test_id = $row["test_fi"];
282  $this->lastsequence = $row["lastindex"];
283  $this->pass = $row["tries"];
284  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
285  $this->submittedTimestamp = $row["submittimestamp"];
286  $this->tstamp = $row["tstamp"];
287  }
288  }
289 
296  private function loadFromDb($active_id)
297  {
298  global $ilDB;
299  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE active_id = %s",
300  array('integer'),
301  array($active_id)
302  );
303  if ($result->numRows())
304  {
305  $row = $ilDB->fetchAssoc($result);
306  $this->active_id = $row["active_id"];
307  $this->user_id = $row["user_fi"];
308  $this->anonymous_id = $row["anonymous_id"];
309  $this->test_id = $row["test_fi"];
310  $this->lastsequence = $row["lastindex"];
311  $this->pass = $row["tries"];
312  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
313  $this->submittedTimestamp = $row["submittimestamp"];
314  $this->tstamp = $row["tstamp"];
315  }
316  }
317 
318  function getActiveId()
319  {
320  return $this->active_id;
321  }
322 
323  function setUserId($user_id)
324  {
325  $this->user_id = $user_id;
326  }
327 
328  function getUserId()
329  {
330  return $this->user_id;
331  }
332 
333  function setTestId($test_id)
334  {
335  $this->test_id = $test_id;
336  }
337 
338  function getTestId()
339  {
340  return $this->test_id;
341  }
342 
344  {
345  $this->anonymous_id = $anonymous_id;
346  }
347 
348  function getAnonymousId()
349  {
350  return $this->anonymous_id;
351  }
352 
354  {
355  $this->lastsequence = $lastsequence;
356  }
357 
358  function getLastSequence()
359  {
360  return $this->lastsequence;
361  }
362 
363  function setPass($pass)
364  {
365  $this->pass = $pass;
366  }
367 
368  function getPass()
369  {
370  return $this->pass;
371  }
372 
373  function increasePass()
374  {
375  $this->pass += 1;
376  }
377 
378  function isSubmitted()
379  {
380  return $this->submitted;
381  }
382 
383  function setSubmitted()
384  {
385  $this->submitted = TRUE;
386  }
387 
389  {
391  }
392 
394  {
395  $this->submittedTimestamp = date('c'); // ISO standard timestamp
396  }
397 }
398 
399 ?>