57 if ($parent_obj_id == null)
59 $pset = $ilDB->query(
'SELECT r2.obj_id par_obj_id FROM object_reference r1 '.
60 'JOIN tree t ON t.child = r1.ref_id '.
61 'JOIN object_reference r2 ON r2.ref_id = t.parent '.
62 'WHERE r1.obj_id = '.$ilDB->quote($obj_id,
'integer'));
64 while ($prec = $ilDB->fetchAssoc($pset))
66 $nid = $ilDB->nextId(
"write_event");
67 $query = sprintf(
'INSERT INTO write_event '.
68 '(write_id, obj_id, parent_obj_id, usr_id, action, ts) VALUES '.
69 '(%s, %s, %s, %s, %s, '.$ilDB->now().
')',
70 $ilDB->quote($nid,
'integer'),
71 $ilDB->quote($obj_id,
'integer'),
72 $ilDB->quote($prec[
"par_obj_id"],
'integer'),
73 $ilDB->quote(
$usr_id,
'integer'),
74 $ilDB->quote($action,
'text'));
76 $aff = $ilDB->manipulate(
$query);
81 $nid = $ilDB->nextId(
"write_event");
82 $query = sprintf(
'INSERT INTO write_event '.
83 '(write_id, obj_id, parent_obj_id, usr_id, action, ts) '.
84 'VALUES (%s,%s,%s,%s,%s,'.$ilDB->now().
')',
85 $ilDB->quote($nid,
'integer'),
86 $ilDB->quote($obj_id,
'integer'),
87 $ilDB->quote($parent_obj_id,
'integer'),
88 $ilDB->quote(
$usr_id,
'integer'),
89 $ilDB->quote($action,
'text'));
90 $aff = $ilDB->manipulate(
$query);
105 $isCatchupWriteEvents =
true, $a_ext_rc =
false, $a_ext_time =
false)
109 include_once(
'Services/Tracking/classes/class.ilObjUserTracking.php');
112 $query = sprintf(
'SELECT * FROM read_event '.
113 'WHERE obj_id = %s '.
115 $ilDB->quote($obj_id,
'integer'),
116 $ilDB->quote(
$usr_id,
'integer'));
118 $row = $ilDB->fetchObject($res);
121 if ($a_ext_rc !==
false)
123 $read_count =
'read_count = '.$ilDB->quote($a_ext_rc,
"integer").
", ";
124 $read_count_init = max(1, (
int) $a_ext_rc);
125 $read_count_diff = max(1, (
int) $a_ext_rc) -
$row->read_count;
129 $read_count =
'read_count = read_count + 1, ';
130 $read_count_init = 1;
131 $read_count_diff = 1;
137 if ($a_ext_time !==
false)
139 $time = (int) $a_ext_time;
143 $time = $ilDB->quote((time() -
$row->last_access) <= $validTimeSpan
144 ?
$row->spent_seconds + time() -
$row->last_access
145 :
$row->spent_seconds,
'integer');
147 $time_diff = $time - (int)
$row->spent_seconds;
150 $query = sprintf(
'UPDATE read_event SET '.
152 'spent_seconds = %s, '.
154 'WHERE obj_id = %s '.
157 $ilDB->quote(time(),
'integer'),
158 $ilDB->quote($obj_id,
'integer'),
159 $ilDB->quote(
$usr_id,
'integer'));
160 $aff = $ilDB->manipulate(
$query);
164 if ($a_ext_time !==
false)
166 $time = (int) $a_ext_time;
173 $time_diff = $time - (int)
$row->spent_seconds;
175 $query = sprintf(
'INSERT INTO read_event (obj_id,usr_id,last_access,read_count,spent_seconds,first_access) '.
176 'VALUES (%s,%s,%s,%s,%s,'.$ilDB->now().
') ',
177 $ilDB->quote($obj_id,
'integer'),
178 $ilDB->quote(
$usr_id,
'integer'),
179 $ilDB->quote(time(),
'integer'),
180 $ilDB->quote($read_count_init,
'integer'),
181 $ilDB->quote($time,
'integer'));
183 $aff = $ilDB->manipulate(
$query);
186 if ($isCatchupWriteEvents)
192 if (!in_array($a_type, array(
"cat",
"root",
"crs")))
194 if ($tree->isInTree($a_ref_id))
196 $path = $tree->getPathId($a_ref_id);
198 foreach (
$path as $p)
203 if (($p != $a_ref_id) && (in_array($obj2_type, array(
"crs",
"fold",
"grp"))))
205 $query = sprintf(
'SELECT * FROM read_event '.
206 'WHERE obj_id = %s '.
208 $ilDB->quote($obj2_id,
'integer'),
209 $ilDB->quote(
$usr_id,
'integer'));
210 $res2 = $ilDB->query(
$query);
211 if ($row2 = $ilDB->fetchAssoc($res2))
215 $query = sprintf(
'UPDATE read_event SET '.
216 'childs_read_count = childs_read_count + %s ,'.
217 'childs_spent_seconds = childs_spent_seconds + %s '.
218 'WHERE obj_id = %s '.
220 $ilDB->quote((
int) $read_count_diff,
'integer'),
221 $ilDB->quote((
int) $time_diff,
'integer'),
222 $ilDB->quote($obj2_id,
'integer'),
223 $ilDB->quote(
$usr_id,
'integer'));
224 $aff = $ilDB->manipulate(
$query);
229 $query = sprintf(
'INSERT INTO read_event (obj_id,usr_id,last_access,read_count,spent_seconds,first_access,'.
230 'childs_read_count, childs_spent_seconds) '.
231 'VALUES (%s,%s,%s,%s,%s,'.$ilDB->now().
', %s, %s) ',
232 $ilDB->quote($obj2_id,
'integer'),
233 $ilDB->quote(
$usr_id,
'integer'),
234 $ilDB->quote(time(),
'integer'),
235 $ilDB->quote(1,
'integer'),
236 $ilDB->quote($time,
'integer'),
237 $ilDB->quote((
int) $read_count_diff,
'integer'),
238 $ilDB->quote((
int) $time_diff,
'integer')
240 $aff = $ilDB->manipulate(
$query);
265 $query =
"SELECT obj_id FROM catch_write_events ".
266 "WHERE obj_id = ".$ilDB->quote($obj_id ,
'integer').
" ".
267 "AND usr_id = ".$ilDB->quote(
$usr_id ,
'integer');
271 $query =
"UPDATE catch_write_events ".
272 "SET ts = ".($timestamp == null ? $ilDB->now() : $ilDB->quote(
$timestamp,
'timestamp')).
" ".
273 "WHERE usr_id = ".$ilDB->quote(
$usr_id ,
'integer').
" ".
274 "AND obj_id = ".$ilDB->quote($obj_id ,
'integer');
279 $query =
"INSERT INTO catch_write_events (ts,obj_id,usr_id) ".
282 $ilDB->quote($obj_id,
'integer').
", ".
283 $ilDB->quote(
$usr_id,
'integer').
" ".
360 "FROM catch_write_events ".
361 "WHERE obj_id=".$ilDB->quote($obj_id ,
'integer').
" ".
362 "AND usr_id=".$ilDB->quote(
$usr_id ,
'integer');
363 $r = $ilDB->query($q);
366 $catchup =
$row[
'ts'];
371 $query = sprintf(
'SELECT * FROM write_event '.
372 'WHERE obj_id = %s '.
375 $ilDB->quote($obj_id,
'integer'),
376 $ilDB->quote(
$usr_id,
'integer'));
381 $query = sprintf(
'SELECT * FROM write_event '.
382 'WHERE obj_id = %s '.
386 $ilDB->quote($obj_id,
'integer'),
387 $ilDB->quote(
$usr_id,
'integer'),
388 $ilDB->quote($catchup,
'timestamp'));
392 while(
$row = $ilDB->fetchAssoc(
$res))
413 "FROM catch_write_events ".
414 "WHERE obj_id=".$ilDB->quote($obj_id ,
'integer').
" ".
415 "AND usr_id=".$ilDB->quote(
$usr_id ,
'integer');
416 $r = $ilDB->query($q);
419 $catchup =
$row[
'ts'];
425 $query = sprintf(
'SELECT * FROM write_event '.
426 'WHERE obj_id = %s '.
428 $ilDB->quote($obj_id,
'integer'),
429 $ilDB->quote(
$usr_id,
'integer'));
435 $query = sprintf(
'SELECT * FROM write_event '.
436 'WHERE obj_id = %s '.
439 $ilDB->quote($obj_id,
'integer'),
440 $ilDB->quote(
$usr_id,
'integer'),
441 $ilDB->quote($catchup,
'timestamp'));
445 $numRows =
$res->numRows();
451 return ($catchup == null) ? 1 : 2;
478 "FROM catch_write_events ".
479 "WHERE obj_id=".$ilDB->quote($parent_obj_id).
" ".
480 "AND usr_id=".$ilDB->quote(
$usr_id);
481 $r = $ilDB->query($q);
484 $catchup =
$row[
'ts'];
490 $query = sprintf(
'SELECT * FROM write_event '.
491 'WHERE parent_obj_id = %s '.
493 $ilDB->quote($parent_obj_id,
'integer'),
494 $ilDB->quote(
$usr_id,
'integer'));
500 $query = sprintf(
'SELECT * FROM write_event '.
501 'WHERE parent_obj_id = %s '.
504 $ilDB->quote($parent_obj_id,
'integer'),
505 $ilDB->quote(
$usr_id,
'integer'),
506 $ilDB->quote($catchup,
'timestamp'));
509 $numRows =
$res->numRows();
515 return ($catchup == null) ? 1 : 2;
572 $query = sprintf(
'SELECT * FROM read_event '.
573 'WHERE obj_id = %s '.
574 'ORDER BY last_access DESC',
575 $ilDB->quote($obj_id,
'integer'));
580 $query = sprintf(
'SELECT * FROM read_event '.
581 'WHERE obj_id = %s '.
583 'ORDER BY last_access DESC',
584 $ilDB->quote($obj_id,
'integer'),
585 $ilDB->quote(
$usr_id,
'integer'));
590 while (
$row = $ilDB->fetchAssoc(
$res))
592 $events[$counter][
'obj_id'] =
$row[
'obj_id'];
593 $events[$counter][
'usr_id'] =
$row[
'usr_id'];
594 $events[$counter][
'last_access'] =
$row[
'last_access'];
595 $events[$counter][
'read_count'] =
$row[
'read_count'];
596 $events[$counter][
'spent_seconds'] =
$row[
'spent_seconds'];
597 $events[$counter][
'first_access'] =
$row[
'first_access'];
602 return $events ? $events : array();
615 $query = sprintf(
'SELECT DISTINCT(usr_id) usr FROM read_event '.
616 'WHERE obj_id = %s ',
617 $ilDB->quote($a_obj_id,
'integer'));
619 while(
$row = $ilDB->fetchObject($res))
621 $users[] =
$row->usr;
623 return $users ? $users : array();
633 $set = $ilDB->query(
"SELECT usr_id FROM read_event WHERE ".
634 "obj_id = ".$ilDB->quote($a_obj_id,
"integer").
" AND ".
635 "usr_id = ".$ilDB->quote($a_usr_id,
"integer")
637 if ($rec = $ilDB->fetchAssoc($set))
652 return 'change event tracking is already active';
664 $set = $ilDB->query(sprintf(
'SELECT r1.obj_id,r2.obj_id p,d.owner,%s,d.create_date '.
665 'FROM object_data d '.
666 'LEFT JOIN write_event w ON d.obj_id = w.obj_id '.
667 'JOIN object_reference r1 ON d.obj_id=r1.obj_id '.
668 'JOIN tree t ON t.child=r1.ref_id '.
669 'JOIN object_reference r2 on r2.ref_id=t.parent '.
670 'WHERE w.obj_id IS NULL',
671 $ilDB->quote(
'create',
'text')));
672 while ($rec = $ilDB->fetchAssoc($set))
674 $nid = $ilDB->nextId(
"write_event");
675 $query =
'INSERT INTO write_event '.
676 '(write_id, obj_id,parent_obj_id,usr_id,action,ts) VALUES ('.
677 $ilDB->quote($nid,
"integer").
",".
678 $ilDB->quote($rec[
"obj_id"],
"integer").
",".
679 $ilDB->quote($rec[
"p"],
"integer").
",".
680 $ilDB->quote($rec[
"owner"],
"integer").
",".
681 $ilDB->quote(
"create",
"text").
",".
682 $ilDB->quote($rec[
"create_date"],
"timestamp").
687 if ($ilDB->isError(
$res) || $ilDB->isError(
$res->result))
689 return 'couldn\'t insert initial data into table "write_event": '.
690 (($ilDB->isError($r->result)) ? $r->result->getMessage() : $r->getMessage());
695 $ilias->setSetting(
'enable_change_event_tracking',
'1');
708 $ilias->setSetting(
'enable_change_event_tracking',
'0');
719 return $ilias->getSetting(
'enable_change_event_tracking',
'0') ==
'1';
733 $query = sprintf(
'DELETE FROM write_event WHERE obj_id = %s ',
734 $ilDB->quote($a_obj_id,
'integer'));
735 $aff = $ilDB->manipulate(
$query);
737 $query = sprintf(
'DELETE FROM read_event WHERE obj_id = %s ',
738 $ilDB->quote($a_obj_id,
'integer'));
739 $aff = $ilDB->manipulate(
$query);