ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjUserTracking.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
17 define('UT_INACTIVE_BOTH',0);
18 define('UT_ACTIVE_BOTH',1);
19 define('UT_ACTIVE_UT',2);
20 define('UT_ACTIVE_LP',3);
21 
22 include_once "classes/class.ilObject.php";
23 
25 {
26  var $valid_time_span = null;
27 
28  // BEGIN ChangeEvent
33  // BEGIN ChangeEvent
34 
35 
42  function ilObjUserTracking($a_id = 0,$a_call_by_reference = true)
43  {
44  $this->type = "trac";
45  $this->ilObject($a_id,$a_call_by_reference);
46 
47  define("DEFAULT_TIME_SPAN",60*5);
48  $this->__readSettings();
49  }
50 
51 
52 
53  function setActivationStatus($a_status)
54  {
55  $this->status = $a_status;
56  }
57 
59  {
60  return $this->status;
61  }
62 
66  function enableTracking($a_enable)
67  {
68  echo 'deprecated';
69 
70  $this->tracking_enabled = (bool) $a_enable;
71 
72  return true;
73  }
74 
75  function enabledTracking()
76  {
77  return ($this->status == UT_ACTIVE_UT) || ($this->status == UT_ACTIVE_BOTH);
78  }
79 
83  function _enabledTracking()
84  {
85  global $ilias;
86 
87  $status = $ilias->getSetting("enable_tracking");
88 
89  return ($status == UT_ACTIVE_UT) || ($status == UT_ACTIVE_BOTH);
90  }
91 
93  {
94  return ($this->status == UT_ACTIVE_LP) || ($this->status == UT_ACTIVE_BOTH);
95  }
96 
101  {
102  global $ilias;
103 
104  $status = $ilias->getSetting("enable_tracking");
105 
106  return ($status == UT_ACTIVE_LP) || ($status == UT_ACTIVE_BOTH);
107  }
108 
112  function enableUserRelatedData($a_enable)
113  {
114  $this->tracking_user_related = (bool) $a_enable;
115  }
116 
118  {
119  return $this->tracking_user_related ? true : false;
120  }
121 
122 
127  {
128  global $ilSetting;
129  return (boolean) $ilSetting->get('save_user_related_data');
130  }
131 
132  function setValidTimeSpan($a_time_span)
133  {
134  $this->valid_time = (int) $a_time_span;
135 
136  return true;
137  }
138 
139  function getValidTimeSpan()
140  {
141  return (int) $this->valid_time;
142  }
143 
144  function _getValidTimeSpan()
145  {
146  global $ilias;
147 
148  return (int) $ilias->getSetting("tracking_time_span",DEFAULT_TIME_SPAN);
149  }
150 
151  // BEGIN ChangeEvent
158  public function setChangeEventTrackingEnabled($newValue)
159  {
160  $this->is_change_event_tracking_enabled = $newValue;
161  }
168  {
170  }
171  // END ChangeEvent
172 
173  function updateSettings()
174  {
175  global $ilias;
176 
177  $ilias->setSetting("enable_tracking",$this->getActivationStatus());
178  $ilias->setSetting("save_user_related_data",$this->enabledUserRelatedData() ? 1 : 0);
179  $ilias->setSetting("tracking_time_span",$this->getValidTimeSpan());
180 
181  // BEGIN ChangeEvent
182  require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
183  if ($this->is_change_event_tracking_enabled != ilChangeEvent::_isActive())
184  {
185  if ($this->is_change_event_tracking_enabled)
186  {
188  }
189  else
190  {
192  }
193  }
194  // END ChangeEvent
195 
196 
197  return true;
198  }
199 
200  function validateSettings()
201  {
202  if(!is_numeric($time = $this->getValidTimeSpan()) or
203  $time < 1 or
204  $time > 9999)
205  {
206  return false;
207  }
208  return true;
209  }
210 
214  function getRecordsTotal()
215  {
216  global $ilDB;
217 
218  $q = "SELECT count(*) AS cnt FROM ut_access";
219  $cnt_set = $ilDB->query($q);
220 
221  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
222 
223  return $cnt_rec["cnt"];
224  }
225 
230  {
231  global $ilDB;
232 
233  $q = "SELECT count(*) as cnt, count(distinct user_id) as user_cnt, ut_month FROM ut_access".
234  " GROUP BY ut_month ORDER BY ut_month DESC";
235  $min_set = $ilDB->query($q);
236  $months = array();
237  while ($min_rec = $min_set->fetchRow(DB_FETCHMODE_ASSOC))
238  {
239  $months[] = array("month" => $min_rec["ut_month"],
240  "cnt" => $min_rec["cnt"], "user_cnt" => $min_rec["user_cnt"]);
241  }
242  return $months;
243  }
244 
248  function getTotalOlderThanMonth($a_month)
249  {
250  global $ilDB;
251 
252  $q = "SELECT count(*) as cnt, count(ut_month) as d FROM ut_access WHERE acc_time <= ".
253  $ilDB->quote($this->increaseMonth($a_month)."-01", "timestamp");
254 
255  $cnt_set = $ilDB->query($q);
256  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
257 //echo "cnt:".$cnt_rec["cnt"].":date:".$cnt_rec["d"].":";
258 
259  return $cnt_rec["cnt"];
260  }
261 
265  function getAccessTotalPerUser($a_condition, $a_searchTermsCondition="",$a_objectCondition="")
266  {
267  global $ilDB;
268 
269  $q = "SELECT count(*) as cnt, user_id FROM ut_access "
270  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
271  .$a_condition
272  .$a_objectCondition
273  ." GROUP BY user_id";
274 
275  $cnt_set = $ilDB->query($q);
276 
277  $acc = array();
278  while ($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
279  {
280  $name = ilObjUser::_lookupName($cnt_rec["user_id"]);
281 
282  if ($cnt_rec["user_id"] != 0)
283  {
284  $acc[] = array("user_id" => $cnt_rec["user_id"],
285  "name" => $name["lastname"].", ".$name["firstname"],
286  "cnt" => $cnt_rec["cnt"]);
287  }
288  }
289  return $acc;
290  }
291 
295  function getAccessTotalPerObj($a_condition, $a_searchTermsCondition="")
296  {
297  global $ilDB;
298  $q = "SELECT count(acc_obj_id) AS cnt, acc_obj_id FROM ut_access "
299  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
300  .$a_condition
301  ." GROUP BY acc_obj_id";
302  $cnt_set = $ilDB->query($q);
303  //echo "q:".$q;
304 
305  $acc = array();
306  while ($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
307  {
308  if ($cnt_rec["cnt"] != "")
309  {
310 
311  $acc[] = array("id" => $cnt_rec["acc_obj_id"],
312  "title" => ilObject::_lookupTitle($cnt_rec["acc_obj_id"]),
313  "author" => $this->getOwnerName($cnt_rec["acc_obj_id"]),
314  "duration" => $this->getDuration($cnt_rec["acc_obj_id"]),
315  "cnt" => $cnt_rec["cnt"]);
316  }
317  }
318  return $acc;
319  }
320 
321  function getDuration($a_obj_id)
322  {
323  global $ilDB;
324 
325  $query = sprintf('SELECT AVG(spent_seconds) FROM read_event '.
326  'WHERE obj_id = %s '.
327  'GROUP BY obj_id ',
328  $ilDB->quote($a_obj_id,'integer'));
329  $res = $ilDB->query($query);
330  $data = $ilDB->fetchAssoc($res);
331  return $data["spent_seconds"];
332  }
333 
337  function getAccessPerUserDetail($a_condition, $a_searchTermsCondition="",$a_objectCondition="")
338  {
339  global $ilDB;
340 
341  $q = "SELECT id, user_id,client_ip,acc_obj_id,language ,acc_time FROM ut_access "
342  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
343  .$a_condition
344  .$a_objectCondition
345  ." GROUP BY id";
346 
347  $cnt_set = $ilDB->query($q);
348  $acc = array();
349  while($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
350  {
351  $name = ilObjUser::_lookupName($cnt_rec["user_id"]);
352 
353  if ($cnt_rec["user_id"] != 0)
354  {
355  $acc[] = array("user_id" => $cnt_rec["user_id"],
356  "name" => $name["lastname"].", ".$name["firstname"],
357  "client_ip" => $cnt_rec["client_ip"],
358  "acc_obj_id" => ilObject::_lookupTitle($cnt_rec["acc_obj_id"]),
359  "language" => $cnt_rec["language"],
360  "acc_time" => $cnt_rec["acc_time"]
361  );
362  }
363  }
364 
365  return $acc;
366  }
370  function deleteTrackingDataBeforeMonth($a_month)
371  {
372  global $ilDB;
373 
374  $q = "DELETE FROM ut_access WHERE acc_time < ".
375  $ilDB->quote($this->increaseMonth($a_month)."-01", "timestamp");
376 
377  $ilDB->manipulate($q);
378  }
379 
386  function increaseMonth($a_month)
387  {
388  $year = (int) substr($a_month, 0, 4);
389  $month = (int) substr($a_month, 5);
390  $month += 1;
391  if ($month == 13)
392  {
393  $year += 1;
394  $month = 1;
395  }
396  return $year."-".str_pad($month, 2, "0", STR_PAD_LEFT);
397  }
398 
402  function allAuthor($a_type,$type)
403  {
404  global $ilDB;
405 
406  $q = "SELECT DISTINCT A.obj_id,A.type,A.title FROM object_data A,object_data B WHERE A.type = ".
407  $ilDB->quote($a_type ,'text')." AND A.obj_id = B.owner AND B.type=".$ilDB->quote($type ,'text')." ".
408  "GROUP BY A.obj_id";
409  //echo $q;
410  $author = $ilDB->query($q);
411  $all = array();
412  while ($aauthor = $author->fetchRow(DB_FETCHMODE_ASSOC))
413  {
414  $all[] = array("title" => $aauthor["title"],
415  "obj_id" =>$aauthor["obj_id"]);
416  }
417  return $all;
418  }
419 
423  function authorLms($id,$type)
424  {
425  global $ilDB;
426 
427  $q = "SELECT title,obj_id FROM object_data WHERE owner = ".$ilDB->quote($id ,'integer')." and type=".$ilDB->quote($type ,'text');
428  //echo $q."<br>";
429  $lms = $ilDB->query($q);
430  $all = array();
431  while ($alms = $lms->fetchRow(DB_FETCHMODE_ASSOC))
432  {
433  $all[] = array("title" => $alms["title"],
434  "obj_id" =>$alms["obj_id"]);
435  }
436  return $all;
437 
438  }
439 
444  {
445  global $ilDB;
446  $q ="SELECT obj_id FROM object_data WHERE type = ".$ilDB->quote($type ,'text')." and title=".$ilDB->quote($title ,'text');
447  $id = $ilDB->query($q);
448  $obj_id = $id->fetchRow(DB_FETCHMODE_ASSOC);
449  return $obj_id["obj_id"];
450  }
451 
455  function getTestId($id)
456  {
457  $q = "select obj_id from object_data "
458  ." where type = 'tst' and "
459  ." owner = ".$ilDB->quote($id ,'integer');
460  $res = $this->ilias->db->query($q);
461  for ($i=0;$i<$res->numRows();$i++)
462  {
463  $result[$i]=$res->fetchRow();
464  }
465  return $result;
466  }
467 
471  function countResults($condition)
472  {
473  $q = "SELECT count(*) from ut_access "
474  ." WHERE "
475  .$condition;
476  $res = $this->ilias->db->query($q);
477  $result = $res->fetchRow();
478  return $result[0];
479  }
480 
484  function getOwnerName($id)
485  {
486  global $ilDB;
487 
488  $q =" select A.login from usr_data A, object_data B where A.usr_id=B.owner and B.obj_id = ".$ilDB->quote($id ,'integer');
489  $res = $this->ilias->db->query($q);
490  $result = $res->fetchRow();
491  return $result[0];
492  }
493 
494  // PRIVATE
495  function __readSettings()
496  {
497  global $ilias;
498 
499  #$this->enableTracking($ilias->getSetting("enable_tracking",0));
500  $this->status = $ilias->getSetting('enable_tracking',UT_INACTIVE_BOTH);
501  $this->enableUserRelatedData($ilias->getSetting("save_user_related_data",0));
502  $this->setValidTimeSpan($ilias->getSetting("tracking_time_span",DEFAULT_TIME_SPAN));
503 
504  // BEGIN ChangeEvent
505  require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
506  $this->is_change_event_tracking_enabled = ilChangeEvent::_isActive();
507  // END ChangeEvent
508 
509  return true;
510  }
511 
512  function _deleteUser($a_usr_id)
513  {
514  global $ilDB;
515 
516  $query = "DELETE FROM ut_access WHERE user_id = ".$ilDB->quote($a_usr_id, "integer")."";
517  $ilDB->manipulate($query);
518 
519  $query = sprintf('DELETE FROM read_event WHERE usr_id = %s ',
520  $ilDB->quote($a_usr_id,'integer'));
521  $aff = $ilDB->manipulate($query);
522 
523  $query = sprintf('DELETE FROM write_event WHERE usr_id = %s ',
524  $ilDB->quote($a_usr_id,'integer'));
525  $aff = $ilDB->manipulate($query);
526 
527  $query = "DELETE FROM ut_lp_filter WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer');
528  $res = $ilDB->manipulate($query);
529 
530  $query = "DELETE FROM ut_lp_marks WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ";
531  $res = $ilDB->manipulate($query);
532 
533  $ilDB->manipulate("DELETE FROM ut_online WHERE usr_id = ".
534  $ilDB->quote($a_usr_id, "integer"));
535 
536  return true;
537  }
538 
539 
540 } // END class.ilObjUserTracking
541 ?>