ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjUserTracking.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 
37 define('UT_INACTIVE_BOTH',0);
38 define('UT_ACTIVE_BOTH',1);
39 define('UT_ACTIVE_UT',2);
40 define('UT_ACTIVE_LP',3);
41 
42 include_once "classes/class.ilObject.php";
43 
45 {
46  var $valid_time_span = null;
47 
48  // BEGIN ChangeEvent
53  // BEGIN ChangeEvent
54 
55 
62  function ilObjUserTracking($a_id = 0,$a_call_by_reference = true)
63  {
64  $this->type = "trac";
65  $this->ilObject($a_id,$a_call_by_reference);
66 
67  define("DEFAULT_TIME_SPAN",60*5);
68  $this->__readSettings();
69  }
70 
71 
72 
73  function setActivationStatus($a_status)
74  {
75  $this->status = $a_status;
76  }
77 
79  {
80  return $this->status;
81  }
82 
86  function enableTracking($a_enable)
87  {
88  echo 'deprecated';
89 
90  $this->tracking_enabled = (bool) $a_enable;
91 
92  return true;
93  }
94 
95  function enabledTracking()
96  {
97  return ($this->status == UT_ACTIVE_UT) || ($this->status == UT_ACTIVE_BOTH);
98  }
99 
103  function _enabledTracking()
104  {
105  global $ilias;
106 
107  $status = $ilias->getSetting("enable_tracking");
108 
109  return ($status == UT_ACTIVE_UT) || ($status == UT_ACTIVE_BOTH);
110  }
111 
113  {
114  return ($this->status == UT_ACTIVE_LP) || ($this->status == UT_ACTIVE_BOTH);
115  }
116 
121  {
122  global $ilias;
123 
124  $status = $ilias->getSetting("enable_tracking");
125 
126  return ($status == UT_ACTIVE_LP) || ($status == UT_ACTIVE_BOTH);
127  }
128 
132  function enableUserRelatedData($a_enable)
133  {
134  $this->tracking_user_related = (bool) $a_enable;
135  }
136 
138  {
139  return $this->tracking_user_related ? true : false;
140  }
141 
142 
147  {
148  global $ilSetting;
149  return (boolean) $ilSetting->get('save_user_related_data');
150  }
151 
152  function setValidTimeSpan($a_time_span)
153  {
154  $this->valid_time = (int) $a_time_span;
155 
156  return true;
157  }
158 
159  function getValidTimeSpan()
160  {
161  return (int) $this->valid_time;
162  }
163 
164  function _getValidTimeSpan()
165  {
166  global $ilias;
167 
168  return (int) $ilias->getSetting("tracking_time_span",DEFAULT_TIME_SPAN);
169  }
170 
171  // BEGIN ChangeEvent
178  public function setChangeEventTrackingEnabled($newValue)
179  {
180  $this->is_change_event_tracking_enabled = $newValue;
181  }
188  {
190  }
191  // END ChangeEvent
192 
193  function updateSettings()
194  {
195  global $ilias;
196 
197  $ilias->setSetting("enable_tracking",$this->getActivationStatus());
198  $ilias->setSetting("save_user_related_data",$this->enabledUserRelatedData() ? 1 : 0);
199  $ilias->setSetting("tracking_time_span",$this->getValidTimeSpan());
200 
201  // BEGIN ChangeEvent
202  require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
203  if ($this->is_change_event_tracking_enabled != ilChangeEvent::_isActive())
204  {
205  if ($this->is_change_event_tracking_enabled)
206  {
208  }
209  else
210  {
212  }
213  }
214  // END ChangeEvent
215 
216 
217  return true;
218  }
219 
220  function validateSettings()
221  {
222  if(!is_numeric($time = $this->getValidTimeSpan()) or
223  $time < 1 or
224  $time > 9999)
225  {
226  return false;
227  }
228  return true;
229  }
230 
234  function getRecordsTotal()
235  {
236  global $ilDB;
237 
238  $q = "SELECT count(*) AS cnt FROM ut_access";
239  $cnt_set = $ilDB->query($q);
240 
241  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
242 
243  return $cnt_rec["cnt"];
244  }
245 
250  {
251  global $ilDB;
252 
253  $q = "SELECT count(*) as cnt, count(distinct user_id) as user_cnt, date_format(acc_time,'%Y-%m') AS month FROM ut_access".
254  " GROUP BY month ORDER BY month DESC";
255  $min_set = $ilDB->query($q);
256  $months = array();
257  while ($min_rec = $min_set->fetchRow(DB_FETCHMODE_ASSOC))
258  {
259  $months[] = array("month" => $min_rec["month"],
260  "cnt" => $min_rec["cnt"], "user_cnt" => $min_rec["user_cnt"]);
261  }
262  return $months;
263  }
264 
268  function getTotalOlderThanMonth($a_month)
269  {
270  global $ilDB;
271 
272  $q = "SELECT count(*) as cnt, date_add('$a_month-01', INTERVAL 1 MONTH) as d FROM ut_access WHERE acc_time <= ".
273  "date_add('$a_month-01', INTERVAL 1 MONTH)";
274 
275  $cnt_set = $ilDB->query($q);
276  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
277 //echo "cnt:".$cnt_rec["cnt"].":date:".$cnt_rec["d"].":";
278 
279  return $cnt_rec["cnt"];
280  }
281 
285  function getAccessTotalPerUser($a_condition, $a_searchTermsCondition="",$a_objectCondition="")
286  {
287  global $ilDB;
288 
289  $q = "SELECT count(*) as cnt, user_id FROM ut_access "
290  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
291  .$a_condition
292  .$a_objectCondition
293  ." GROUP BY user_id";
294 
295  $cnt_set = $ilDB->query($q);
296 
297  $acc = array();
298  while ($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
299  {
300  $name = ilObjUser::_lookupName($cnt_rec["user_id"]);
301 
302  if ($cnt_rec["user_id"] != 0)
303  {
304  $acc[] = array("user_id" => $cnt_rec["user_id"],
305  "name" => $name["lastname"].", ".$name["firstname"],
306  "cnt" => $cnt_rec["cnt"]);
307  }
308  }
309  return $acc;
310  }
311 
315  function getAccessTotalPerObj($a_condition, $a_searchTermsCondition="")
316  {
317  global $ilDB;
318  $q = "SELECT count(acc_obj_id) AS cnt, acc_obj_id FROM ut_access "
319  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
320  .$a_condition
321  ." GROUP BY acc_obj_id";
322  $cnt_set = $ilDB->query($q);
323  //echo "q:".$q;
324 
325  $acc = array();
326  while ($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
327  {
328  if ($cnt_rec["cnt"] != "")
329  {
330 
331  $acc[] = array("id" => $cnt_rec["acc_obj_id"],
332  "title" => ilObject::_lookupTitle($cnt_rec["acc_obj_id"]),
333  "author" => $this->getOwnerName($cnt_rec["acc_obj_id"]),
334  "duration" => $this->getDuration($cnt_rec["acc_obj_id"]),
335  "cnt" => $cnt_rec["cnt"]);
336  }
337  }
338  return $acc;
339  }
340 
341  function getDuration($a_obj_id)
342  {
343  global $ilDB;
344  $q = "SELECT AVG(spent_seconds) FROM read_event"
345  ." WHERE obj_id = " . $ilDB->quote($a_obj_id)
346  ." GROUP BY obj_id";
347  $res = $ilDB->query($q);
348  $data = $res->fetchRow(DB_FETCHMODE_ASSOC);
349  return $data["spent_seconds"];
350  }
351 
355  function getAccessPerUserDetail($a_condition, $a_searchTermsCondition="",$a_objectCondition="")
356  {
357  global $ilDB;
358 
359  $q = "SELECT id, user_id,client_ip,acc_obj_id,language ,acc_time FROM ut_access "
360  .($a_searchTermsCondition != "" ? $a_searchTermsCondition : " WHERE ")
361  .$a_condition
362  .$a_objectCondition
363  ." GROUP BY id";
364 
365  $cnt_set = $ilDB->query($q);
366  $acc = array();
367  while($cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC))
368  {
369  $name = ilObjUser::_lookupName($cnt_rec["user_id"]);
370 
371  if ($cnt_rec["user_id"] != 0)
372  {
373  $acc[] = array("user_id" => $cnt_rec["user_id"],
374  "name" => $name["lastname"].", ".$name["firstname"],
375  "client_ip" => $cnt_rec["client_ip"],
376  "acc_obj_id" => ilObject::_lookupTitle($cnt_rec["acc_obj_id"]),
377  "language" => $cnt_rec["language"],
378  "acc_time" => $cnt_rec["acc_time"]
379  );
380  }
381  }
382 
383  return $acc;
384  }
388  function deleteTrackingDataBeforeMonth($a_month)
389  {
390  global $ilDB;
391 
392  $q = "DELETE FROM ut_access WHERE acc_time <= ".
393  "date_add('$a_month-01', INTERVAL 1 MONTH)";
394 
395  $ilDB->query($q);
396  }
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 as A,object_data as B WHERE A.type = ".
407  $ilDB->quote($a_type)." AND A.obj_id = B.owner AND B.type=".$ilDB->quote($type);
408  //echo $q;
409  $author = $ilDB->query($q);
410  $all = array();
411  while ($aauthor = $author->fetchRow(DB_FETCHMODE_ASSOC))
412  {
413  $all[] = array("title" => $aauthor["title"],
414  "obj_id" =>$aauthor["obj_id"]);
415  }
416  return $all;
417  }
418 
422  function authorLms($id,$type)
423  {
424  global $ilDB;
425 
426  $q = "SELECT title,obj_id FROM object_data WHERE owner = ".$ilDB->quote($id)." and type=".$ilDB->quote($type);
427  //echo $q."<br>";
428  $lms = $ilDB->query($q);
429  $all = array();
430  while ($alms = $lms->fetchRow(DB_FETCHMODE_ASSOC))
431  {
432  $all[] = array("title" => $alms["title"],
433  "obj_id" =>$alms["obj_id"]);
434  }
435  return $all;
436 
437  }
438 
443  {
444  global $ilDB;
445  $q ="SELECT obj_id FROM object_data WHERE type = ".$ilDB->quote($type)." and title=".$ilDB->quote($title);
446  $id = $ilDB->query($q);
447  $obj_id = $id->fetchRow(DB_FETCHMODE_ASSOC);
448  return $obj_id["obj_id"];
449  }
450 
454  function getTestId($id)
455  {
456  $q = "select obj_id from object_data "
457  ." where type = 'tst' and "
458  ." owner = ".$id;
459  $res = $this->ilias->db->query($q);
460  for ($i=0;$i<$res->numRows();$i++)
461  {
462  $result[$i]=$res->fetchRow();
463  }
464  return $result;
465  }
466 
470  function countResults($condition)
471  {
472  $q = "SELECT count(*) from ut_access "
473  ." WHERE "
474  .$condition;
475  $res = $this->ilias->db->query($q);
476  $result = $res->fetchRow();
477  return $result[0];
478  }
479 
483  function getOwnerName($id)
484  {
485  $q =" select A.login from usr_data as A, object_data as B where A.usr_id=B.owner and B.obj_id = ".$id;
486  $res = $this->ilias->db->query($q);
487  $result = $res->fetchRow();
488  return $result[0];
489  }
490 
491  // PRIVATE
492  function __readSettings()
493  {
494  global $ilias;
495 
496  #$this->enableTracking($ilias->getSetting("enable_tracking",0));
497  $this->status = $ilias->getSetting('enable_tracking',UT_INACTIVE_BOTH);
498  $this->enableUserRelatedData($ilias->getSetting("save_user_related_data",0));
499  $this->setValidTimeSpan($ilias->getSetting("tracking_time_span",DEFAULT_TIME_SPAN));
500 
501  // BEGIN ChangeEvent
502  require_once 'Services/Tracking/classes/class.ilChangeEvent.php';
503  $this->is_change_event_tracking_enabled = ilChangeEvent::_isActive();
504  // END ChangeEvent
505 
506  return true;
507  }
508 
509  function _deleteUser($a_usr_id)
510  {
511  global $ilDB;
512 
513  $query = "DELETE FROM ut_access WHERE user_id = '".$a_usr_id."'";
514  $ilDB->query($query);
515 
516  $query = "DELETE FROM read_event WHERE usr_id = '".$a_usr_id."'";
517  $ilDB->query($query);
518 
519  $query = "DELETE FROM write_event WHERE usr_id = '".$a_usr_id."'";
520  $ilDB->query($query);
521 
522  $query = "DELETE FROM ut_lp_filter WHERE usr_id = '".$a_usr_id."'";
523  $ilDB->query($query);
524 
525  $query = "DELETE FROM ut_lp_marks WHERE usr_id = '".$a_usr_id."'";
526  $ilDB->query($query);
527 
528  $query = "DELETE FROM ut_online WHERE usr_id = '".$a_usr_id."'";
529  $ilDB->query($query);
530 
531  return true;
532  }
533 
534 
535 } // END class.ilObjUserTracking
536 ?>