ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTracking.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
12 class ilTracking {
13 
14  var $objId;
15  var $userId;
18  var $clientIp;
20  var $accObjId;
22  var $accSubId;
23  var $lanugage;
24  var $browser;
26  var $acc_time;
27 
28  var $db;
29 
30  function ilTracking()
31  {
32  global $ilias,$tpl,$lng,$ilDB;
33 
34  $this->ilias =& $ilias;
35  $this->tpl =& $tpl;
36  $this->lng =& $lng;
37  $this->db = $ilDB;
38 
39  }
40 
44  function _getLastAccess()
45  {
46  global $ilUser, $ilDB;
47 
48  $ilDB->setLimit(1);
49  $q = "SELECT * from ut_access "
50  ." WHERE "
51  ." user_id = ".$ilDB->quote($ilUser->getId(),'integer')
52  ." ORDER BY acc_time DESC";
53  $res = $ilDB->query($q);
54  return $res->fetchRow(DB_FETCHMODE_ASSOC);
55  }
56 
57  function _hasEntry($a_obj_id, $a_obj_type,$a_sub_id = 0, $a_sub_type = "")
58  {
59  global $ilDB;
60 
61  // We query for the session_id since it is more unique than the user_id.
62 
63  $query = "SELECT COUNT(id) as num_entries FROM ut_access ".
64  "WHERE session_id = ".$ilDB->quote(session_id(), "text")." ".
65  "AND acc_obj_id = ".$ilDB->quote($a_obj_id, "integer")." ".
66  "AND acc_sub_id = ".$ilDB->quote($a_sub_id, "text")." ";
67  $res = $ilDB->query($query);
68  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
69 
70  return $row->num_entries ? true : false;
71  }
72 
82  function _trackAccess($a_obj_id, $a_obj_type,$a_sub_id = 0, $a_sub_type = "", $a_action_type = "read")
83  {
84  global $ilUser, $ilDB;
85 
86 
87  include_once("Services/Tracking/classes/class.ilObjUserTracking.php");
89  {
90  return false;
91  }
92 
93  include_once 'Services/Tracking/classes/class.ilLearningProgress.php';
94  ilLearningProgress::_tracProgress($ilUser->getId(),$a_obj_id,$a_obj_type);
95 
97  {
98  $user_id = $ilUser->getId();
99  }
100  else
101  {
102  $user_id = 0;
103  }
104 
105  $client_ip = getenv("REMOTE_ADDR");
106  $script = substr($_SERVER["SCRIPT_FILENAME"], strlen(IL_ABSOLUTE_PATH) - 1,
107  strlen($_SERVER["SCRIPT_FILENAME"]) - strlen(IL_ABSOLUTE_PATH) + 1);
108  $language = $ilUser->getLanguage();
109  $session_id = session_id();
110 
111  #$last_access = ilTracking::_getLastAccess();
112 
113 
114  if(ilTracking::_hasEntry($a_obj_id, $a_obj_type,$a_sub_id, $a_sub_type))
115  {
116  return true;
117  }
118  $q = "INSERT INTO ut_access ("
119  ."id,"
120  ."user_id, action_type, php_script, client_ip,"
121  ."acc_obj_type, acc_obj_id, acc_sub_type, acc_sub_id,"
122  ."language, browser, session_id, acc_time, ut_month"
123  .") VALUES ("
124  .$ilDB->quote($ilDB->nextId('ut_access'),'integer').','
125  .$ilDB->quote($user_id, "integer").","
126  .$ilDB->quote($a_action_type, "text").","
127  .$ilDB->quote($script, "text").","
128  .$ilDB->quote($client_ip, "text").","
129  .$ilDB->quote($a_obj_type, "text").","
130  .$ilDB->quote($a_obj_id, "integer").","
131  .$ilDB->quote($a_sub_type, "text").","
132  .$ilDB->quote($a_sub_id, "integer").","
133  .$ilDB->quote($language, "text").","
134  .$ilDB->quote(substr($_SERVER["HTTP_USER_AGENT"],0.255)).","
135  .$ilDB->quote($session_id, "text").", "
136  .$ilDB->quote(ilUtil::now(), "timestamp").", "
137  .$ilDB->quote(substr(ilUtil::now(), 0, 7), "text")
138  .")";
139  $ilDB->manipulate($q);
140 
141  /*
142  if(($session_id == $last_access["session_id"]) &&
143  ($a_obj_id == $last_access["acc_obj_id"]) &&
144  ($a_obj_type == $last_access["acc_obj_type"]) &&
145  ($a_sub_id == $last_access["acc_sub_id"]) &&
146  ($a_sub_type == $last_access["acc_sub_type"])
147  )
148  {
149  return true;
150  }
151  else
152  {
153  $q = "INSERT INTO ut_access ("
154  ."user_id, action_type, php_script, client_ip,"
155  ."acc_obj_type, acc_obj_id, acc_sub_type, acc_sub_id,"
156  ."language, browser, session_id, acc_time"
157  .") VALUES ("
158  .$ilDB->quote($user_id).","
159  .$ilDB->quote($a_action_type).","
160  .$ilDB->quote($script).","
161  .$ilDB->quote($client_ip).","
162  .$ilDB->quote($a_obj_type).","
163  .$ilDB->quote($a_obj_id).","
164  .$ilDB->quote($a_sub_type).","
165  .$ilDB->quote($a_sub_id).","
166  .$ilDB->quote($language).","
167  .$ilDB->quote($_SERVER["HTTP_USER_AGENT"]).","
168  .$ilDB->quote($session_id).", now()"
169  .")";
170  $ilDB->query($q);
171  }
172  */
173  }
174  function TestTitle($user_id)
175  {
176  global $ilDB;
177 
178  $q = " SELECT title from object_data "
179  ." WHERE type = ".$ilDB->quote("tst", "text")
180  ." AND owner = ".$ilDB->quote($user_id ,'integer');
181  $res = $this->ilias->db->query($q);
182  for($i=0;$i<$res->numRows();$i++)
183  {
184  $result[$i]=$res->fetchRow();
185  }
186  return $result;
187  }
188 
189 
190 
191  function numDay($from,$to)
192  {
193  $from = strtotime($from);
194  $to = strtotime($to);
195  $dayf = date ("d",$from);
196  $dayt = date ("d",$to);
197  $yearf = date ("Y",$from);
198  $yeart = date ("Y",$to);
199  $montht = date ("m",$to);
200  $monthf = date ("m",$from);
201  $ret = ( mktime(0,0,0,$montht,$dayt,$yeart) - mktime(0,0,0,$monthf,$dayf,$yearf))/(3600*24);
202  return $ret;
203  }
204  function numHour($from,$to)
205  {
206  $from = strtotime($from);
207  $to = strtotime($to);
208  $dayf = date ("d",$from);
209  $dayt = date ("d",$to);
210  $yearf = date ("Y",$from);
211  $yeart = date ("Y",$to);
212  $montht = date ("m",$to);
213  $monthf = date ("m",$from);
214  $hourt = date ("h",$to);
215  $hourf = date ("h",$from);
216  $ret = (mktime($hourt,0,0,$montht,$dayt,$yeart)-mktime($hourf,0,0,$monthf,$dayf,$yearf))/3600;
217  $ret = strftime($ret);
218  return $ret;
219  }
220  function addHour($time)
221  {
222  $time = strtotime($time);
223  $day = date("d",$time);
224  $month = date("m",$time);
225  $year = date("Y",$time);
226  $hour = date("H",$time);
227  $min = date("i",$time);
228  $sec = date("s",$time);
229  $hour = $hour+1;
230  $ret = date("H:i:s", mktime($hour,$min,$sec,$month,$day,$year));
231  return $ret;
232  }
233  function addDay($time)
234  {
235  $time = strtotime($time);
236  $day = date("d",$time);
237  $month = date("m",$time);
238  $year = date("y",$time);
239  $min = date("i",$time);
240  $hour = date("h",$time);
241  $sec = date("s",$time);
242  $day = $day + 1;
243  $ret = date ("Y-m-d", mktime($hour,$min,$sec,$month,$day,$year));
244  return $ret;
245  }
246 
247  function getSubId($id)
248  {
249  global $ilDB;
250 
251  $q = "SELECT obj_id from object_data "
252  ." where type = ".$ilDB->quote("lm", "text")." and "
253  ." owner = ".$ilDB->quote($id, "integer");
254  $res = $this->ilias->db->query($q);
255  for($i=0;$i<$res->numRows();$i++)
256  {
257  $result[$i]=$res->fetchRow();
258  }
259  return $result;
260  }
261  function getSubTest($id)
262  {
263  global $ilDB;
264 
265  $q = "SELECT obj_id from object_data "
266  ." where type = ".$ilDB->quote("tst", "text")." and "
267  ." owner = ".$ilDB->quote($id, "integer");
268  $res = $this->ilias->db->query($q);
269  for($i=0;$i<$res->numRows();$i++)
270  {
271  $result[$i]=$res->fetchRow();
272  }
273  return $result;
274  }
275 
276  function getTestId($id)
277  {
278  global $ilDB;
279 
280  $q = "select obj_id from object_data "
281  ." where type = ".$ilDB->quote("tst", "text")." and "
282  ." owner = ".$ilDB->quote($id, "integer");
283  $res = $this->ilias->db->query($q);
284  for ($i=0;$i<$res->numRows();$i++)
285  {
286  $result[$i]=$res->fetchRow();
287  }
288  return $result;
289  }
290 
291  function countResults($condition)
292  {
293  global $ilDB;
294 
295  $q = "SELECT count(*) from ut_access "
296  ." WHERE "
297  .$condition;
298  $res = $this->ilias->db->query($q);
299  $result = $res->fetchRow();
300  return $result[0];
301  }
302 
303  function searchResults($condition)
304  {
305  global $ilDB;
306 
307  $q = "SELECT a.login,b.acc_obj_type,b.language,b.client_ip,b.acc_time "
308  ." FROM usr_data a,ut_access b "
309  ." WHERE a.usr_id=b.user_id "
310  ." AND ".$condition;
311 //echo $q;
312  $res = $this->ilias->db->query($q);
313  for($i=0;$i<$res->numRows();$i++)
314  {
315  $result[$i]=$res->fetchRow();
316  }
317  return $result;
318  }
319 
320  function searchTestResults($condition)
321  {
322  global $ilDB;
323 
324  $q = "SELECT a.login,b.acc_obj_type,b.client_ip,b.acc_time "
325  ." FROM usr_data a,ut_access b "
326  ." WHERE a.usr_id=b.user_id "
327  ." AND ".$condition;
328 //echo $q;
329  $res = $this->ilias->db->query($q);
330  for($i=0;$i<$res->numRows();$i++)
331  {
332  $result[$i]=$res->fetchRow();
333  }
334  return $result;
335  }
336 
337  function searchUserId($condition)
338  {
339  global $ilDB;
340 
341  $q = "SELECT user_id from ut_access where ".$condition;
342 //echo $q;
343  $res = $this->ilias->db->query($q);
344  for($i=0;$i<$res->numRows();$i++)
345  {
346  $result[$i]=$res->fetchRow();
347  }
348  return $result;
349  }
350 
351  function searchTestId($condition)
352  {
353  global $ilDB;
354 
355  $q = "select user_fi from tst_active where ".$condition;
356  $res = $this->ilias->db->query($q);
357  for($i=0;$i<$res->numRows();$i++)
358  {
359  $result[$i]=$res->fetchRow();
360  }
361  return $result;
362  }
363 
364  function getPerTestId($test)
365  {
366  global $ilDB;
367 
368  $q = "select obj_id from object_data where type = ".
369  $ilDB->quote("tst", "text")." and title = ".$ilDB->quote($test, "text");
370  $res = $this->ilias->db->query($q);
371  $result = $res->fetchRow();
372  return $result[0];
373  }
374 
375  function countNum($from,$from1,$condition)
376  {
377  global $ilDB;
378 
379  $q = "SELECT count(*) from ut_access "
380  ." WHERE (acc_time > ".$ilDB->quote($from, "timestamp")
381  ." AND acc_time <= ".$ilDB->quote($from1, "integer").")"
382  ." AND ".$condition;
383  //echo $condition;echo "<br>";
384 //echo $q;
385  $res = $this->ilias->db->query($q);
386  $result = $res->fetchRow();
387  return $result[0];
388  }
389 
390  function selectTime($from,$to,$condition)
391  {
392  global $ilDB;
393 
394  $q = "SELECT acc_time from ut_access "
395  ." WHERE (acc_time > ".$ilDB->quote($from, "timestamp")
396  ." AND acc_time <= ".$ilDB->quote($to, "integer").")"
397  ." AND ".$condition;
398 //echo $q;
399 //echo "<br>";
400  $res = $this->ilias->db->query($q);
401  for($i=0;$i<$res->numRows();$i++)
402  {
403  $result[$i]=$res->fetchRow();
404  }
405  return $result;
406  }
407 
408  function getTest($id)
409  {
410  global $ilDB;
411 
412  $q = "SELECT title from object_data "
413  ." WHERE "
414  ." type = ".$ilDB->quote("tst", "text")
415  ." and "
416  ." owner = ".$ilDB->quote($id, "integer");
417  $res = $this->ilias->db->query($q);
418  for($i=0;$i<$res->numRows();$i++)
419  {
420  $result[$i]=$res->fetchRow();
421  }
422  return $result;
423  }
424 }
425 ?>