ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTestSession.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +----------------------------------------------------------------------------+
22 */
23 
34 {
41 
47  var $user_id;
48 
55 
61  var $test_id;
62 
69 
76 
82  var $tstamp;
83 
90 
99  function ilTestSession($active_id = "")
100  {
101  $this->active_id = 0;
102  $this->user_id = 0;
103  $this->anonymous_id = 0;
104  $this->test_id = 0;
105  $this->lastsequence = 0;
106  $this->submitted = FALSE;
107  $this->submittedTimestamp = "";
108  $this->pass = 0;
109  $this->tstamp = 0;
110  if ($active_id > 0)
111  {
112  $this->loadFromDb($active_id);
113  }
114  }
115 
116  function increaseTestPass()
117  {
118  global $ilDB, $ilLog;
119 
120  $this->increasePass();
121  $this->setLastSequence(0);
122  $submitted = ($this->isSubmitted()) ? 1 : 0;
123  // 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)
124  if (time() > $this->tstamp + 10)
125  {
126  $this->tstamp = time();
127  if ($this->active_id > 0)
128  {
129  $affectedRows = $ilDB->manipulateF("UPDATE tst_active SET lastindex = %s, tries = %s, submitted = %s, submittimestamp = %s, tstamp = %s WHERE active_id = %s",
130  array('integer', 'integer', 'integer', 'timestamp', 'integer', 'integer'),
131  array(
132  $this->getLastSequence(),
133  $this->getPass(),
134  $submitted,
135  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
136  time(),
137  $this->getActiveId()
138  )
139  );
140  }
141  else
142  {
143  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : NULL;
144  $next_id = $ilDB->nextId('tst_active');
145  $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)",
146  array('integer', 'integer', 'text', 'integer', 'integer', 'integer', 'integer', 'timestamp', 'integer'),
147  array(
148  $next_id,
149  $this->getUserId(),
151  $this->getTestId(),
152  $this->getLastSequence(),
153  $this->getPass(),
154  $submitted,
155  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
156  time()
157  )
158  );
159  $this->active_id = $next_id;
160  }
161  }
162  }
163 
164  function saveToDb()
165  {
166  global $ilDB, $ilLog;
167 
168  $submitted = ($this->isSubmitted()) ? 1 : 0;
169  if ($this->active_id > 0)
170  {
171  $affectedRows = $ilDB->manipulateF("UPDATE tst_active SET lastindex = %s, tries = %s, submitted = %s, submittimestamp = %s, tstamp = %s WHERE active_id = %s",
172  array('integer', 'integer', 'integer', 'timestamp', 'integer', 'integer'),
173  array(
174  $this->getLastSequence(),
175  $this->getPass(),
176  $submitted,
177  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
178  time()-10,
179  $this->getActiveId()
180  )
181  );
182  }
183  else
184  {
185  $anonymous_id = ($this->getAnonymousId()) ? $this->getAnonymousId() : NULL;
186  $next_id = $ilDB->nextId('tst_active');
187  $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)",
188  array('integer', 'integer', 'text', 'integer', 'integer', 'integer', 'integer', 'timestamp', 'integer'),
189  array(
190  $next_id,
191  $this->getUserId(),
193  $this->getTestId(),
194  $this->getLastSequence(),
195  $this->getPass(),
196  $submitted,
197  (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : NULL,
198  time()-10
199  )
200  );
201  $this->active_id = $next_id;
202  }
203  }
204 
206  {
207  global $ilDB;
208  global $ilUser;
209 
210  if (!$user_id)
211  {
212  $user_id = $ilUser->getId();
213  }
214  if (($_SESSION["AccountId"] == ANONYMOUS_USER_ID) && (strlen($_SESSION["tst_access_code"][$test_id])))
215  {
216  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
217  array('integer','integer','text'),
218  array($user_id, $test_id, $_SESSION["tst_access_code"][$test_id])
219  );
220  }
221  else if (strlen($anonymous_id))
222  {
223  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
224  array('integer','integer','text'),
225  array($user_id, $test_id, $anonymous_id)
226  );
227  }
228  else
229  {
230  if ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)
231  {
232  return NULL;
233  }
234  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
235  array('integer','integer'),
236  array($user_id, $test_id)
237  );
238  }
239  if ($result->numRows())
240  {
241  $row = $ilDB->fetchAssoc($result);
242  $this->active_id = $row["active_id"];
243  $this->user_id = $row["user_fi"];
244  $this->anonymous_id = $row["anonymous_id"];
245  $this->test_id = $row["test_fi"];
246  $this->lastsequence = $row["lastindex"];
247  $this->pass = $row["tries"];
248  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
249  $this->submittedTimestamp = $row["submittimestamp"];
250  $this->tstamp = $row["tstamp"];
251  }
252  }
253 
260  private function loadFromDb($active_id)
261  {
262  global $ilDB;
263  $result = $ilDB->queryF("SELECT * FROM tst_active WHERE active_id = %s",
264  array('integer'),
265  array($active_id)
266  );
267  if ($result->numRows())
268  {
269  $row = $ilDB->fetchAssoc($result);
270  $this->active_id = $row["active_id"];
271  $this->user_id = $row["user_fi"];
272  $this->anonymous_id = $row["anonymous_id"];
273  $this->test_id = $row["test_fi"];
274  $this->lastsequence = $row["lastindex"];
275  $this->pass = $row["tries"];
276  $this->submitted = ($row["submitted"]) ? TRUE : FALSE;
277  $this->submittedTimestamp = $row["submittimestamp"];
278  $this->tstamp = $row["tstamp"];
279  }
280  }
281 
282  function getActiveId()
283  {
284  return $this->active_id;
285  }
286 
287  function setUserId($user_id)
288  {
289  $this->user_id = $user_id;
290  }
291 
292  function getUserId()
293  {
294  return $this->user_id;
295  }
296 
297  function setTestId($test_id)
298  {
299  $this->test_id = $test_id;
300  }
301 
302  function getTestId()
303  {
304  return $this->test_id;
305  }
306 
308  {
309  $this->anonymous_id = $anonymous_id;
310  }
311 
312  function getAnonymousId()
313  {
314  return $this->anonymous_id;
315  }
316 
318  {
319  $this->lastsequence = $lastsequence;
320  }
321 
322  function getLastSequence()
323  {
324  return $this->lastsequence;
325  }
326 
327  function setPass($pass)
328  {
329  $this->pass = $pass;
330  }
331 
332  function getPass()
333  {
334  return $this->pass;
335  }
336 
337  function increasePass()
338  {
339  $this->pass += 1;
340  }
341 
342  function isSubmitted()
343  {
344  return $this->submitted;
345  }
346 
347  function setSubmitted()
348  {
349  $this->submitted = TRUE;
350  }
351 
353  {
355  }
356 
358  {
359  $this->submittedTimestamp = strftime("%Y-%m-%d %H:%M:%S");
360  }
361 }
362 
363 ?>