ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChangeEvent.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2007 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 
56 {
72  function _recordWriteEvent($obj_id, $usr_id, $action, $parent_obj_id = null)
73  {
74  global $ilDB;
75 
76  if ($parent_obj_id == null)
77  {
78  $q = "INSERT IGNORE INTO write_event ".
79  "(obj_id, parent_obj_id, usr_id, action, ts) ".
80  "SELECT ".
81  $ilDB->quote($obj_id).",".
82  "r2.obj_id,".
83  $ilDB->quote($usr_id).",".
84  $ilDB->quote($action).",".
85  "NOW() ".
86  "FROM object_reference AS r1 ".
87  "JOIN tree AS t ON t.child = r1.ref_id ".
88  "JOIN object_reference AS r2 ON r2.ref_id = t.parent ".
89  "WHERE r1.obj_id = ".$ilDB->quote($obj_id);
90  }
91  else
92  {
93  $q = "INSERT IGNORE INTO write_event ".
94  "(obj_id, parent_obj_id, usr_id, action, ts) ".
95  "VALUES (".
96  $ilDB->quote($obj_id).",".
97  $ilDB->quote($parent_obj_id).",".
98  $ilDB->quote($usr_id).",".
99  $ilDB->quote($action).",".
100  "NOW()".
101  ")";
102  }
103  $r = $ilDB->query($q);
104  //error_log ('ilChangeEvent::_recordWriteEvent '.$q);
105  }
106 
115  function _recordReadEvent($obj_id, $usr_id, $isCatchupWriteEvents = true)
116  {
117  global $ilDB;
118  include_once('Services/Tracking/classes/class.ilObjUserTracking.php');
119  $validTimeSpan = ilObjUserTracking::_getValidTimeSpan();
120 
121  // Important: In the SQL statement below, it is important that ts
122  // is updated after spent_seconds is updated, because
123  // spent_seconds computes its value from the old value of ts.
124  $q = "INSERT INTO read_event ".
125  "(obj_id, usr_id, first_access, last_access, read_count) ".
126  "VALUES (".
127  $ilDB->quote($obj_id).",".
128  $ilDB->quote($usr_id).",".
129  "NOW(), NOW(), 1".
130  ") ".
131  "ON DUPLICATE KEY ".
132  "UPDATE ".
133  "read_count=read_count+1, ".
134  "spent_seconds = IF (TIME_TO_SEC(TIMEDIFF(NOW(),last_access))<=".$ilDB->quote($validTimeSpan).",spent_seconds+TIME_TO_SEC(TIMEDIFF(NOW(),last_access)),spent_seconds),".
135  "last_access=NOW()".
136  "";
137  $r = $ilDB->query($q);
138  //error_log ('ilChangeEvent::_recordReadEvent '.$q);
139 
140  if ($isCatchupWriteEvents)
141  {
142  ilChangeEvent::_catchupWriteEvents($obj_id, $usr_id);
143  }
144  }
145 
154  function _catchupWriteEvents($obj_id, $usr_id, $timestamp = null)
155  {
156  global $ilDB;
157 
158 
159  $q = "INSERT INTO catch_write_events ".
160  "(obj_id, usr_id, ts) ".
161  "VALUES (".
162  $ilDB->quote($obj_id).",".
163  $ilDB->quote($usr_id).",";
164  if ($timestamp == null)
165  {
166  $q .= "NOW()".
167  ") ON DUPLICATE KEY UPDATE ts=NOW()";
168  }
169  else {
170  $q .= $ilDB->quote($timestamp).
171  ") ON DUPLICATE KEY UPDATE ts=".$ilDB->quote($timestamp);
172  }
173  //error_log ('ilChangeEvent::_catchupWriteEvents '.$q);
174  $r = $ilDB->query($q);
175  }
223  public static function _lookupUncaughtWriteEvents($obj_id, $usr_id)
224  {
225  global $ilDB;
226 
227  $q = "SELECT ts ".
228  "FROM catch_write_events ".
229  "WHERE obj_id=".$ilDB->quote($obj_id)." ".
230  "AND usr_id=".$ilDB->quote($usr_id);
231  $r = $ilDB->query($q);
232  $catchup = null;
233  while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC)) {
234  $catchup = $row['ts'];
235  }
236 
237  $q = "SELECT * ".
238  "FROM write_event ".
239  "WHERE obj_id=".$ilDB->quote($obj_id)." ".
240  "AND usr_id <> ".$ilDB->quote($usr_id)." ".
241  ($catchup == null ? "" : "AND ts > ".$ilDB->quote($catchup))." ".
242  "ORDER BY ts DESC";
243  $r = $ilDB->query($q);
244  $events = array();
245  while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
246  {
247  $events[] = $row;
248  }
249  return $events;
250  }
261  public static function _lookupChangeState($obj_id, $usr_id)
262  {
263  global $ilDB;
264 
265  $q = "SELECT ts ".
266  "FROM catch_write_events ".
267  "WHERE obj_id=".$ilDB->quote($obj_id)." ".
268  "AND usr_id=".$ilDB->quote($usr_id);
269  $r = $ilDB->query($q);
270  $catchup = null;
271  while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC)) {
272  $catchup = $row['ts'];
273  }
274  $q = "SELECT * ".
275  "FROM write_event ".
276  "WHERE obj_id=".$ilDB->quote($obj_id)." ".
277  "AND usr_id <> ".$ilDB->quote($usr_id)." ".
278  ($catchup == null ? "" : "AND ts > ".$ilDB->quote($catchup))." ".
279  //"ORDER BY ts DESC ". --> Note: to improve performance we don't need order by here
280  "LIMIT 1";
281  $r = $ilDB->query($q);
282  $numRows = $r->numRows();
283  if ($numRows > 0)
284  {
285  $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
286  // if we have write events, and user never catched one, report as new (1)
287  // if we have write events, and user catched an old write event, report as changed (2)
288  return ($catchup == null) ? 1 : 2;
289  }
290  else
291  {
292  return 0; // user catched all write events, report as unchanged (0)
293  }
294  }
310  public static function _lookupInsideChangeState($parent_obj_id, $usr_id)
311  {
312  global $ilDB;
313 
314  $q = "SELECT ts ".
315  "FROM catch_write_events ".
316  "WHERE obj_id=".$ilDB->quote($parent_obj_id)." ".
317  "AND usr_id=".$ilDB->quote($usr_id);
318  $r = $ilDB->query($q);
319  $catchup = null;
320  while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC)) {
321  $catchup = $row['ts'];
322  }
323  $q = "SELECT * ".
324  "FROM write_event ".
325  "WHERE parent_obj_id=".$ilDB->quote($parent_obj_id)." ".
326  "AND usr_id <> ".$ilDB->quote($usr_id)." ".
327  ($catchup == null ? "" : "AND ts > ".$ilDB->quote($catchup))." ".
328  //"ORDER BY ts DESC ". --> Note: to improve performance we don't need order by here
329  "LIMIT 1";
330  $r = $ilDB->query($q);
331  $numRows = $r->numRows();
332  if ($numRows > 0)
333  {
334  $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
335  // if we have write events, and user never catched one, report as new (1)
336  // if we have write events, and user catched an old write event, report as changed (2)
337  return ($catchup == null) ? 1 : 2;
338  }
339  else
340  {
341  return 0; // user catched all write events, report as unchanged (0)
342  }
343  }
388  public static function _lookupReadEvents($obj_id, $usr_id = null)
389  {
390  global $ilDB;
391 
392  if ($usr_id == null)
393  {
394  $q = "SELECT * ".
395  "FROM read_event ".
396  "WHERE obj_id=".$ilDB->quote($obj_id)." ".
397  "ORDER BY last_access DESC";
398  }
399  else
400  {
401  $q = "SELECT * ".
402  "FROM read_event ".
403  "WHERE obj_id=".$ilDB->quote($obj_id)." ".
404  "AND usr_id=".$ilDB->quote($usr_id)." ".
405  "ORDER BY last_access DESC";
406  }
407  $r = $ilDB->query($q);
408  $events = array();
409  while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
410  {
411  $events[] = $row;
412  }
413  return $events;
414  }
415 
421  public static function _activate() {
423  {
424  return 'change event tracking is already active';
425  }
426  else
427  {
428  global $ilDB;
429 
430  // Insert initial data into table write_event
431  // We need to do this here, because we need
432  // to catch up write events that occured while the change event tracking was
433  // deactivated.
434  $q = "INSERT IGNORE INTO write_event ".
435  "(obj_id,parent_obj_id,usr_id,action,ts) ".
436  "SELECT r1.obj_id,r2.obj_id,d.owner,'create',d.create_date ".
437  "FROM object_data AS d ".
438  "JOIN object_reference AS r1 ON d.obj_id=r1.obj_id ".
439  "JOIN tree AS t ON t.child=r1.ref_id ".
440  "JOIN object_reference as r2 on r2.ref_id=t.parent ";
441  $r = $ilDB->db->query($q);
442  if ($ilDB->isError($r) || $ilDB->isError($r->result))
443  {
444  return 'couldn\'t insert initial data into table "write_event": '.
445  (($ilDB->isError($r->result)) ? $r->result->getMessage() : $r->getMessage());
446  }
447 
448 
449  global $ilias;
450  $ilias->setSetting('enable_change_event_tracking', '1');
451 
452  return true;
453  }
454  }
455 
461  public static function _deactivate() {
462  global $ilias;
463  $ilias->setSetting('enable_change_event_tracking', '0');
464 
465  }
466 
472  public static function _isActive() {
473  global $ilias;
474  return $ilias->getSetting('enable_change_event_tracking', '0') == '1';
475 
476  }
477 }
478 ?>