ILIAS  Release_4_2_x_branch Revision 61807
 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  var $extended_data = null;
28 
29  // BEGIN ChangeEvent
34  // BEGIN ChangeEvent
35 
39 
40 
47  function ilObjUserTracking($a_id = 0,$a_call_by_reference = true)
48  {
49  $this->type = "trac";
50  $this->ilObject($a_id,$a_call_by_reference);
51 
52  define("DEFAULT_TIME_SPAN",60*5);
53  $this->__readSettings();
54  }
55 
56 
57 
58  function setActivationStatus($a_status)
59  {
60  $this->status = $a_status;
61  }
62 
64  {
65  return $this->status;
66  }
67 
71  function enableTracking($a_enable)
72  {
73  echo 'deprecated';
74 
75  $this->tracking_enabled = (bool) $a_enable;
76 
77  return true;
78  }
79 
80  function enabledTracking()
81  {
82  return ($this->status == UT_ACTIVE_UT) || ($this->status == UT_ACTIVE_BOTH);
83  }
84 
88  function _enabledTracking()
89  {
90  global $ilias;
91 
92  $status = $ilias->getSetting("enable_tracking");
93 
94  return ($status == UT_ACTIVE_UT) || ($status == UT_ACTIVE_BOTH);
95  }
96 
98  {
99  return ($this->status == UT_ACTIVE_LP) || ($this->status == UT_ACTIVE_BOTH);
100  }
101 
106  {
107  global $ilias;
108 
109  $status = $ilias->getSetting("enable_tracking");
110 
111  return ($status == UT_ACTIVE_LP) || ($status == UT_ACTIVE_BOTH);
112  }
113 
117  function enableUserRelatedData($a_enable)
118  {
119  $this->tracking_user_related = (bool) $a_enable;
120  }
121 
123  {
124  return $this->tracking_user_related ? true : false;
125  }
126 
127 
132  {
133  global $ilSetting;
134  return (boolean) $ilSetting->get('save_user_related_data');
135  }
136 
141  {
142  global $ilSetting;
143  return (boolean) $ilSetting->get('object_statistics');
144  }
145 
146  function setValidTimeSpan($a_time_span)
147  {
148  $this->valid_time = (int) $a_time_span;
149 
150  return true;
151  }
152 
153  function getValidTimeSpan()
154  {
155  return (int) $this->valid_time;
156  }
157 
158  function _getValidTimeSpan()
159  {
160  global $ilias;
161 
162  return (int) $ilias->getSetting("tracking_time_span",DEFAULT_TIME_SPAN);
163  }
164 
165  // BEGIN ChangeEvent
172  public function setChangeEventTrackingEnabled($newValue)
173  {
174  $this->is_change_event_tracking_enabled = $newValue;
175  }
182  {
184  }
185  // END ChangeEvent
186 
193  public function setObjectStatisticsEnabled($newValue)
194  {
195  $this->object_statistics_enabled = (bool)$newValue;
196  }
202  public function isObjectStatisticsEnabled()
203  {
204  return $this->object_statistics_enabled;
205  }
206 
207  function updateSettings()
208  {
209  global $ilias;
210 
211  $ilias->setSetting("enable_tracking",$this->getActivationStatus());
212  $ilias->setSetting("save_user_related_data",$this->enabledUserRelatedData() ? 1 : 0);
213  $ilias->setSetting("tracking_time_span",$this->getValidTimeSpan());
214  $ilias->setSetting("lp_extended_data", $this->extended_data);
215  $ilias->setSetting("object_statistics", $this->object_statistics_enabled);
216 
217  // BEGIN ChangeEvent
218  require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
219  if ($this->is_change_event_tracking_enabled != ilChangeEvent::_isActive())
220  {
221  if ($this->is_change_event_tracking_enabled)
222  {
224  }
225  else
226  {
228  }
229  }
230  // END ChangeEvent
231 
232  return true;
233  }
234 
235  function validateSettings()
236  {
237  if(!is_numeric($time = $this->getValidTimeSpan()) or
238  $time < 1 or
239  $time > 9999)
240  {
241  return false;
242  }
243  return true;
244  }
245 
249  function getRecordsTotal()
250  {
251  global $ilDB;
252 
253  $q = "SELECT count(*) AS cnt FROM ut_access";
254  $cnt_set = $ilDB->query($q);
255 
256  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
257 
258  return $cnt_rec["cnt"];
259  }
260 
265  {
266  global $ilDB;
267 
268  $q = "SELECT count(*) as cnt, count(distinct user_id) as user_cnt, ut_month FROM ut_access".
269  " GROUP BY ut_month ORDER BY ut_month DESC";
270  $min_set = $ilDB->query($q);
271  $months = array();
272  while ($min_rec = $min_set->fetchRow(DB_FETCHMODE_ASSOC))
273  {
274  $months[] = array("month" => $min_rec["ut_month"],
275  "cnt" => $min_rec["cnt"], "user_cnt" => $min_rec["user_cnt"]);
276  }
277  return $months;
278  }
279 
283  function getTotalOlderThanMonth($a_month)
284  {
285  global $ilDB;
286 
287  $q = "SELECT count(*) as cnt, count(ut_month) as d FROM ut_access WHERE acc_time <= ".
288  $ilDB->quote($this->increaseMonth($a_month)."-01", "timestamp");
289 
290  $cnt_set = $ilDB->query($q);
291  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
292 //echo "cnt:".$cnt_rec["cnt"].":date:".$cnt_rec["d"].":";
293 
294  return $cnt_rec["cnt"];
295  }
296 
300  function getAccessTotalPerUser($a_condition, $a_searchTermsCondition="",$a_objectCondition="")
301  {
302  global $ilDB;
303 
304  $q = "SELECT count(*) as cnt, user_id FROM ut_access "
305  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
306  .$a_condition
307  .$a_objectCondition
308  ." GROUP BY user_id";
309 
310  $cnt_set = $ilDB->query($q);
311 
312  $acc = array();
313  while ($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
314  {
315  $name = ilObjUser::_lookupName($cnt_rec["user_id"]);
316 
317  if ($cnt_rec["user_id"] != 0)
318  {
319  $acc[] = array("user_id" => $cnt_rec["user_id"],
320  "name" => $name["lastname"].", ".$name["firstname"],
321  "cnt" => $cnt_rec["cnt"]);
322  }
323  }
324  return $acc;
325  }
326 
330  function getAccessTotalPerObj($a_condition, $a_searchTermsCondition="")
331  {
332  global $ilDB;
333  $q = "SELECT count(acc_obj_id) AS cnt, acc_obj_id FROM ut_access "
334  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
335  .$a_condition
336  ." GROUP BY acc_obj_id";
337  $cnt_set = $ilDB->query($q);
338  //echo "q:".$q;
339 
340  $acc = array();
341  while ($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
342  {
343  if ($cnt_rec["cnt"] != "")
344  {
345 
346  $acc[] = array("id" => $cnt_rec["acc_obj_id"],
347  "title" => ilObject::_lookupTitle($cnt_rec["acc_obj_id"]),
348  "author" => $this->getOwnerName($cnt_rec["acc_obj_id"]),
349  "duration" => $this->getDuration($cnt_rec["acc_obj_id"]),
350  "cnt" => $cnt_rec["cnt"]);
351  }
352  }
353  return $acc;
354  }
355 
356  function getDuration($a_obj_id)
357  {
358  global $ilDB;
359 
360  $query = sprintf('SELECT AVG(spent_seconds) FROM read_event '.
361  'WHERE obj_id = %s '.
362  'GROUP BY obj_id ',
363  $ilDB->quote($a_obj_id,'integer'));
364  $res = $ilDB->query($query);
365  $data = $ilDB->fetchAssoc($res);
366  return $data["spent_seconds"];
367  }
368 
372  function getAccessPerUserDetail($a_condition, $a_searchTermsCondition="",$a_objectCondition="")
373  {
374  global $ilDB;
375 
376  $q = "SELECT id, user_id,client_ip,acc_obj_id,language ,acc_time FROM ut_access "
377  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
378  .$a_condition
379  .$a_objectCondition
380  ." GROUP BY id";
381 
382  $cnt_set = $ilDB->query($q);
383  $acc = array();
384  while($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
385  {
386  $name = ilObjUser::_lookupName($cnt_rec["user_id"]);
387 
388  if ($cnt_rec["user_id"] != 0)
389  {
390  $acc[] = array("user_id" => $cnt_rec["user_id"],
391  "name" => $name["lastname"].", ".$name["firstname"],
392  "client_ip" => $cnt_rec["client_ip"],
393  "acc_obj_id" => ilObject::_lookupTitle($cnt_rec["acc_obj_id"]),
394  "language" => $cnt_rec["language"],
395  "acc_time" => $cnt_rec["acc_time"]
396  );
397  }
398  }
399 
400  return $acc;
401  }
405  function deleteTrackingDataBeforeMonth($a_month)
406  {
407  global $ilDB;
408 
409  $q = "DELETE FROM ut_access WHERE acc_time < ".
410  $ilDB->quote($this->increaseMonth($a_month)."-01", "timestamp");
411 
412  $ilDB->manipulate($q);
413  }
414 
421  function increaseMonth($a_month)
422  {
423  $year = (int) substr($a_month, 0, 4);
424  $month = (int) substr($a_month, 5);
425  $month += 1;
426  if ($month == 13)
427  {
428  $year += 1;
429  $month = 1;
430  }
431  return $year."-".str_pad($month, 2, "0", STR_PAD_LEFT);
432  }
433 
437  function allAuthor($a_type,$type)
438  {
439  global $ilDB;
440 
441  $q = "SELECT DISTINCT A.obj_id,A.type,A.title FROM object_data A,object_data B WHERE A.type = ".
442  $ilDB->quote($a_type ,'text')." AND A.obj_id = B.owner AND B.type=".$ilDB->quote($type ,'text')." ".
443  "GROUP BY A.obj_id";
444  //echo $q;
445  $author = $ilDB->query($q);
446  $all = array();
447  while ($aauthor = $author->fetchRow(DB_FETCHMODE_ASSOC))
448  {
449  $all[] = array("title" => $aauthor["title"],
450  "obj_id" =>$aauthor["obj_id"]);
451  }
452  return $all;
453  }
454 
458  function authorLms($id,$type)
459  {
460  global $ilDB;
461 
462  $q = "SELECT title,obj_id FROM object_data WHERE owner = ".$ilDB->quote($id ,'integer')." and type=".$ilDB->quote($type ,'text');
463  //echo $q."<br>";
464  $lms = $ilDB->query($q);
465  $all = array();
466  while ($alms = $lms->fetchRow(DB_FETCHMODE_ASSOC))
467  {
468  $all[] = array("title" => $alms["title"],
469  "obj_id" =>$alms["obj_id"]);
470  }
471  return $all;
472 
473  }
474 
479  {
480  global $ilDB;
481  $q ="SELECT obj_id FROM object_data WHERE type = ".$ilDB->quote($type ,'text')." and title=".$ilDB->quote($title ,'text');
482  $id = $ilDB->query($q);
483  $obj_id = $id->fetchRow(DB_FETCHMODE_ASSOC);
484  return $obj_id["obj_id"];
485  }
486 
490  function getTestId($id)
491  {
492  $q = "select obj_id from object_data "
493  ." where type = 'tst' and "
494  ." owner = ".$ilDB->quote($id ,'integer');
495  $res = $this->ilias->db->query($q);
496  for ($i=0;$i<$res->numRows();$i++)
497  {
498  $result[$i]=$res->fetchRow();
499  }
500  return $result;
501  }
502 
506  function countResults($condition)
507  {
508  $q = "SELECT count(*) from ut_access "
509  ." WHERE "
510  .$condition;
511  $res = $this->ilias->db->query($q);
512  $result = $res->fetchRow();
513  return $result[0];
514  }
515 
519  function getOwnerName($id)
520  {
521  global $ilDB;
522 
523  $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');
524  $res = $this->ilias->db->query($q);
525  $result = $res->fetchRow();
526  return $result[0];
527  }
528 
529  // PRIVATE
530  function __readSettings()
531  {
532  global $ilias;
533 
534  #$this->enableTracking($ilias->getSetting("enable_tracking",0));
535  $this->status = $ilias->getSetting('enable_tracking',UT_INACTIVE_BOTH);
536  $this->enableUserRelatedData($ilias->getSetting("save_user_related_data",0));
537  $this->setValidTimeSpan($ilias->getSetting("tracking_time_span",DEFAULT_TIME_SPAN));#
538 
539  // BEGIN ChangeEvent
540  require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
541  $this->is_change_event_tracking_enabled = ilChangeEvent::_isActive();
542  // END ChangeEvent
543 
544  $this->setExtendedData($ilias->getSetting("lp_extended_data"),0);
545  $this->setObjectStatisticsEnabled($ilias->getSetting("object_statistics"),0);
546 
547  return true;
548  }
549 
550  function _deleteUser($a_usr_id)
551  {
552  global $ilDB;
553 
554  $query = "DELETE FROM ut_access WHERE user_id = ".$ilDB->quote($a_usr_id, "integer")."";
555  $ilDB->manipulate($query);
556 
557  $query = sprintf('DELETE FROM read_event WHERE usr_id = %s ',
558  $ilDB->quote($a_usr_id,'integer'));
559  $aff = $ilDB->manipulate($query);
560 
561  $query = sprintf('DELETE FROM write_event WHERE usr_id = %s ',
562  $ilDB->quote($a_usr_id,'integer'));
563  $aff = $ilDB->manipulate($query);
564 
565  $query = "DELETE FROM ut_lp_marks WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ";
566  $res = $ilDB->manipulate($query);
567 
568  $ilDB->manipulate("DELETE FROM ut_online WHERE usr_id = ".
569  $ilDB->quote($a_usr_id, "integer"));
570 
571  return true;
572  }
573 
574  function setExtendedData($a_value)
575  {
576  $this->extended_data = $a_value;
577  }
578 
579  function hasExtendedData($a_code)
580  {
581  return $this->extended_data & $a_code;
582  }
583 
584 } // END class.ilObjUserTracking
585 ?>