ILIAS  Release_4_1_x_branch Revision 61804
 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 
137  function setValidTimeSpan($a_time_span)
138  {
139  $this->valid_time = (int) $a_time_span;
140 
141  return true;
142  }
143 
144  function getValidTimeSpan()
145  {
146  return (int) $this->valid_time;
147  }
148 
149  function _getValidTimeSpan()
150  {
151  global $ilias;
152 
153  return (int) $ilias->getSetting("tracking_time_span",DEFAULT_TIME_SPAN);
154  }
155 
156  // BEGIN ChangeEvent
163  public function setChangeEventTrackingEnabled($newValue)
164  {
165  $this->is_change_event_tracking_enabled = $newValue;
166  }
173  {
175  }
176  // END ChangeEvent
177 
178  function updateSettings()
179  {
180  global $ilias;
181 
182  $ilias->setSetting("enable_tracking",$this->getActivationStatus());
183  $ilias->setSetting("save_user_related_data",$this->enabledUserRelatedData() ? 1 : 0);
184  $ilias->setSetting("tracking_time_span",$this->getValidTimeSpan());
185  $ilias->setSetting("lp_extended_data", $this->extended_data);
186 
187  // BEGIN ChangeEvent
188  require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
189  if ($this->is_change_event_tracking_enabled != ilChangeEvent::_isActive())
190  {
191  if ($this->is_change_event_tracking_enabled)
192  {
194  }
195  else
196  {
198  }
199  }
200  // END ChangeEvent
201 
202 
203  return true;
204  }
205 
206  function validateSettings()
207  {
208  if(!is_numeric($time = $this->getValidTimeSpan()) or
209  $time < 1 or
210  $time > 9999)
211  {
212  return false;
213  }
214  return true;
215  }
216 
220  function getRecordsTotal()
221  {
222  global $ilDB;
223 
224  $q = "SELECT count(*) AS cnt FROM ut_access";
225  $cnt_set = $ilDB->query($q);
226 
227  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
228 
229  return $cnt_rec["cnt"];
230  }
231 
236  {
237  global $ilDB;
238 
239  $q = "SELECT count(*) as cnt, count(distinct user_id) as user_cnt, ut_month FROM ut_access".
240  " GROUP BY ut_month ORDER BY ut_month DESC";
241  $min_set = $ilDB->query($q);
242  $months = array();
243  while ($min_rec = $min_set->fetchRow(DB_FETCHMODE_ASSOC))
244  {
245  $months[] = array("month" => $min_rec["ut_month"],
246  "cnt" => $min_rec["cnt"], "user_cnt" => $min_rec["user_cnt"]);
247  }
248  return $months;
249  }
250 
254  function getTotalOlderThanMonth($a_month)
255  {
256  global $ilDB;
257 
258  $q = "SELECT count(*) as cnt, count(ut_month) as d FROM ut_access WHERE acc_time <= ".
259  $ilDB->quote($this->increaseMonth($a_month)."-01", "timestamp");
260 
261  $cnt_set = $ilDB->query($q);
262  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
263 //echo "cnt:".$cnt_rec["cnt"].":date:".$cnt_rec["d"].":";
264 
265  return $cnt_rec["cnt"];
266  }
267 
271  function getAccessTotalPerUser($a_condition, $a_searchTermsCondition="",$a_objectCondition="")
272  {
273  global $ilDB;
274 
275  $q = "SELECT count(*) as cnt, user_id FROM ut_access "
276  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
277  .$a_condition
278  .$a_objectCondition
279  ." GROUP BY user_id";
280 
281  $cnt_set = $ilDB->query($q);
282 
283  $acc = array();
284  while ($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
285  {
286  $name = ilObjUser::_lookupName($cnt_rec["user_id"]);
287 
288  if ($cnt_rec["user_id"] != 0)
289  {
290  $acc[] = array("user_id" => $cnt_rec["user_id"],
291  "name" => $name["lastname"].", ".$name["firstname"],
292  "cnt" => $cnt_rec["cnt"]);
293  }
294  }
295  return $acc;
296  }
297 
301  function getAccessTotalPerObj($a_condition, $a_searchTermsCondition="")
302  {
303  global $ilDB;
304  $q = "SELECT count(acc_obj_id) AS cnt, acc_obj_id FROM ut_access "
305  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
306  .$a_condition
307  ." GROUP BY acc_obj_id";
308  $cnt_set = $ilDB->query($q);
309  //echo "q:".$q;
310 
311  $acc = array();
312  while ($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
313  {
314  if ($cnt_rec["cnt"] != "")
315  {
316 
317  $acc[] = array("id" => $cnt_rec["acc_obj_id"],
318  "title" => ilObject::_lookupTitle($cnt_rec["acc_obj_id"]),
319  "author" => $this->getOwnerName($cnt_rec["acc_obj_id"]),
320  "duration" => $this->getDuration($cnt_rec["acc_obj_id"]),
321  "cnt" => $cnt_rec["cnt"]);
322  }
323  }
324  return $acc;
325  }
326 
327  function getDuration($a_obj_id)
328  {
329  global $ilDB;
330 
331  $query = sprintf('SELECT AVG(spent_seconds) FROM read_event '.
332  'WHERE obj_id = %s '.
333  'GROUP BY obj_id ',
334  $ilDB->quote($a_obj_id,'integer'));
335  $res = $ilDB->query($query);
336  $data = $ilDB->fetchAssoc($res);
337  return $data["spent_seconds"];
338  }
339 
343  function getAccessPerUserDetail($a_condition, $a_searchTermsCondition="",$a_objectCondition="")
344  {
345  global $ilDB;
346 
347  $q = "SELECT id, user_id,client_ip,acc_obj_id,language ,acc_time FROM ut_access "
348  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
349  .$a_condition
350  .$a_objectCondition
351  ." GROUP BY id";
352 
353  $cnt_set = $ilDB->query($q);
354  $acc = array();
355  while($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
356  {
357  $name = ilObjUser::_lookupName($cnt_rec["user_id"]);
358 
359  if ($cnt_rec["user_id"] != 0)
360  {
361  $acc[] = array("user_id" => $cnt_rec["user_id"],
362  "name" => $name["lastname"].", ".$name["firstname"],
363  "client_ip" => $cnt_rec["client_ip"],
364  "acc_obj_id" => ilObject::_lookupTitle($cnt_rec["acc_obj_id"]),
365  "language" => $cnt_rec["language"],
366  "acc_time" => $cnt_rec["acc_time"]
367  );
368  }
369  }
370 
371  return $acc;
372  }
376  function deleteTrackingDataBeforeMonth($a_month)
377  {
378  global $ilDB;
379 
380  $q = "DELETE FROM ut_access WHERE acc_time < ".
381  $ilDB->quote($this->increaseMonth($a_month)."-01", "timestamp");
382 
383  $ilDB->manipulate($q);
384  }
385 
392  function increaseMonth($a_month)
393  {
394  $year = (int) substr($a_month, 0, 4);
395  $month = (int) substr($a_month, 5);
396  $month += 1;
397  if ($month == 13)
398  {
399  $year += 1;
400  $month = 1;
401  }
402  return $year."-".str_pad($month, 2, "0", STR_PAD_LEFT);
403  }
404 
408  function allAuthor($a_type,$type)
409  {
410  global $ilDB;
411 
412  $q = "SELECT DISTINCT A.obj_id,A.type,A.title FROM object_data A,object_data B WHERE A.type = ".
413  $ilDB->quote($a_type ,'text')." AND A.obj_id = B.owner AND B.type=".$ilDB->quote($type ,'text')." ".
414  "GROUP BY A.obj_id";
415  //echo $q;
416  $author = $ilDB->query($q);
417  $all = array();
418  while ($aauthor = $author->fetchRow(DB_FETCHMODE_ASSOC))
419  {
420  $all[] = array("title" => $aauthor["title"],
421  "obj_id" =>$aauthor["obj_id"]);
422  }
423  return $all;
424  }
425 
429  function authorLms($id,$type)
430  {
431  global $ilDB;
432 
433  $q = "SELECT title,obj_id FROM object_data WHERE owner = ".$ilDB->quote($id ,'integer')." and type=".$ilDB->quote($type ,'text');
434  //echo $q."<br>";
435  $lms = $ilDB->query($q);
436  $all = array();
437  while ($alms = $lms->fetchRow(DB_FETCHMODE_ASSOC))
438  {
439  $all[] = array("title" => $alms["title"],
440  "obj_id" =>$alms["obj_id"]);
441  }
442  return $all;
443 
444  }
445 
450  {
451  global $ilDB;
452  $q ="SELECT obj_id FROM object_data WHERE type = ".$ilDB->quote($type ,'text')." and title=".$ilDB->quote($title ,'text');
453  $id = $ilDB->query($q);
454  $obj_id = $id->fetchRow(DB_FETCHMODE_ASSOC);
455  return $obj_id["obj_id"];
456  }
457 
461  function getTestId($id)
462  {
463  $q = "select obj_id from object_data "
464  ." where type = 'tst' and "
465  ." owner = ".$ilDB->quote($id ,'integer');
466  $res = $this->ilias->db->query($q);
467  for ($i=0;$i<$res->numRows();$i++)
468  {
469  $result[$i]=$res->fetchRow();
470  }
471  return $result;
472  }
473 
477  function countResults($condition)
478  {
479  $q = "SELECT count(*) from ut_access "
480  ." WHERE "
481  .$condition;
482  $res = $this->ilias->db->query($q);
483  $result = $res->fetchRow();
484  return $result[0];
485  }
486 
490  function getOwnerName($id)
491  {
492  global $ilDB;
493 
494  $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');
495  $res = $this->ilias->db->query($q);
496  $result = $res->fetchRow();
497  return $result[0];
498  }
499 
500  // PRIVATE
501  function __readSettings()
502  {
503  global $ilias;
504 
505  #$this->enableTracking($ilias->getSetting("enable_tracking",0));
506  $this->status = $ilias->getSetting('enable_tracking',UT_INACTIVE_BOTH);
507  $this->enableUserRelatedData($ilias->getSetting("save_user_related_data",0));
508  $this->setValidTimeSpan($ilias->getSetting("tracking_time_span",DEFAULT_TIME_SPAN));#
509 
510  // BEGIN ChangeEvent
511  require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
512  $this->is_change_event_tracking_enabled = ilChangeEvent::_isActive();
513  // END ChangeEvent
514 
515  $this->setExtendedData($ilias->getSetting("lp_extended_data"),0);
516 
517  return true;
518  }
519 
520  function _deleteUser($a_usr_id)
521  {
522  global $ilDB;
523 
524  $query = "DELETE FROM ut_access WHERE user_id = ".$ilDB->quote($a_usr_id, "integer")."";
525  $ilDB->manipulate($query);
526 
527  $query = sprintf('DELETE FROM read_event WHERE usr_id = %s ',
528  $ilDB->quote($a_usr_id,'integer'));
529  $aff = $ilDB->manipulate($query);
530 
531  $query = sprintf('DELETE FROM write_event WHERE usr_id = %s ',
532  $ilDB->quote($a_usr_id,'integer'));
533  $aff = $ilDB->manipulate($query);
534 
535  $query = "DELETE FROM ut_lp_marks WHERE usr_id = ".$ilDB->quote($a_usr_id ,'integer')." ";
536  $res = $ilDB->manipulate($query);
537 
538  $ilDB->manipulate("DELETE FROM ut_online WHERE usr_id = ".
539  $ilDB->quote($a_usr_id, "integer"));
540 
541  return true;
542  }
543 
544  function setExtendedData($a_value)
545  {
546  $this->extended_data = $a_value;
547  }
548 
549  function hasExtendedData($a_code)
550  {
551  return $this->extended_data & $a_code;
552  }
553 
554 } // END class.ilObjUserTracking
555 ?>