59 if ($parent_obj_id == null)
61 $pset = $ilDB->query(
'SELECT r2.obj_id par_obj_id FROM object_reference r1 '.
62 'JOIN tree t ON t.child = r1.ref_id '.
63 'JOIN object_reference r2 ON r2.ref_id = t.parent '.
64 'WHERE r1.obj_id = '.$ilDB->quote($obj_id,
'integer'));
66 while ($prec = $ilDB->fetchAssoc($pset))
68 $nid = $ilDB->nextId(
"write_event");
69 $query = sprintf(
'INSERT INTO write_event '.
70 '(write_id, obj_id, parent_obj_id, usr_id, action, ts) VALUES '.
71 '(%s, %s, %s, %s, %s, '.$ilDB->now().
')',
72 $ilDB->quote($nid,
'integer'),
73 $ilDB->quote($obj_id,
'integer'),
74 $ilDB->quote($prec[
"par_obj_id"],
'integer'),
75 $ilDB->quote(
$usr_id,
'integer'),
76 $ilDB->quote($action,
'text'));
78 $aff = $ilDB->manipulate(
$query);
83 $nid = $ilDB->nextId(
"write_event");
84 $query = sprintf(
'INSERT INTO write_event '.
85 '(write_id, obj_id, parent_obj_id, usr_id, action, ts) '.
86 'VALUES (%s,%s,%s,%s,%s,'.$ilDB->now().
')',
87 $ilDB->quote($nid,
'integer'),
88 $ilDB->quote($obj_id,
'integer'),
89 $ilDB->quote($parent_obj_id,
'integer'),
90 $ilDB->quote(
$usr_id,
'integer'),
91 $ilDB->quote($action,
'text'));
92 $aff = $ilDB->manipulate(
$query);
107 $isCatchupWriteEvents =
true, $a_ext_rc =
false, $a_ext_time =
false)
111 include_once(
'Services/Tracking/classes/class.ilObjUserTracking.php');
114 $query = sprintf(
'SELECT * FROM read_event '.
115 'WHERE obj_id = %s '.
117 $ilDB->quote($obj_id,
'integer'),
118 $ilDB->quote(
$usr_id,
'integer'));
120 $row = $ilDB->fetchObject($res);
123 if ($a_ext_rc !==
false)
125 $read_count =
'read_count = '.$ilDB->quote($a_ext_rc,
"integer").
", ";
126 $read_count_init = max(1, (
int) $a_ext_rc);
127 $read_count_diff = max(1, (
int) $a_ext_rc) -
$row->read_count;
131 $read_count =
'read_count = read_count + 1, ';
132 $read_count_init = 1;
133 $read_count_diff = 1;
139 if ($a_ext_time !==
false)
141 $time = (int) $a_ext_time;
145 $time = $ilDB->quote((time() -
$row->last_access) <= $validTimeSpan
146 ?
$row->spent_seconds + time() -
$row->last_access
147 :
$row->spent_seconds,
'integer');
149 $time_diff = $time - (int)
$row->spent_seconds;
152 $query = sprintf(
'UPDATE read_event SET '.
154 'spent_seconds = %s, '.
156 'WHERE obj_id = %s '.
159 $ilDB->quote(time(),
'integer'),
160 $ilDB->quote($obj_id,
'integer'),
161 $ilDB->quote(
$usr_id,
'integer'));
162 $aff = $ilDB->manipulate(
$query);
166 if ($a_ext_time !==
false)
168 $time = (int) $a_ext_time;
175 $time_diff = $time - (int)
$row->spent_seconds;
177 $query = sprintf(
'INSERT INTO read_event (obj_id,usr_id,last_access,read_count,spent_seconds,first_access) '.
178 'VALUES (%s,%s,%s,%s,%s,'.$ilDB->now().
') ',
179 $ilDB->quote($obj_id,
'integer'),
180 $ilDB->quote(
$usr_id,
'integer'),
181 $ilDB->quote(time(),
'integer'),
182 $ilDB->quote($read_count_init,
'integer'),
183 $ilDB->quote($time,
'integer'));
185 $aff = $ilDB->manipulate(
$query);
187 self::$has_accessed[$obj_id][
$usr_id] =
true;
190 if ($isCatchupWriteEvents)
196 if (!in_array($a_type, array(
"cat",
"root",
"crs")))
198 if ($tree->isInTree($a_ref_id))
200 $path = $tree->getPathId($a_ref_id);
202 foreach (
$path as $p)
207 if (($p != $a_ref_id) && (in_array($obj2_type, array(
"crs",
"fold",
"grp"))))
209 $query = sprintf(
'SELECT * FROM read_event '.
210 'WHERE obj_id = %s '.
212 $ilDB->quote($obj2_id,
'integer'),
213 $ilDB->quote(
$usr_id,
'integer'));
214 $res2 = $ilDB->query(
$query);
215 if ($row2 = $ilDB->fetchAssoc($res2))
219 $query = sprintf(
'UPDATE read_event SET '.
220 'childs_read_count = childs_read_count + %s ,'.
221 'childs_spent_seconds = childs_spent_seconds + %s '.
222 'WHERE obj_id = %s '.
224 $ilDB->quote((
int) $read_count_diff,
'integer'),
225 $ilDB->quote((
int) $time_diff,
'integer'),
226 $ilDB->quote($obj2_id,
'integer'),
227 $ilDB->quote(
$usr_id,
'integer'));
228 $aff = $ilDB->manipulate(
$query);
234 $query = sprintf(
'INSERT INTO read_event (obj_id,usr_id,last_access,read_count,spent_seconds,first_access,'.
235 'childs_read_count, childs_spent_seconds) '.
236 'VALUES (%s,%s,%s,%s,%s,'.$ilDB->now().
', %s, %s) ',
237 $ilDB->quote($obj2_id,
'integer'),
238 $ilDB->quote(
$usr_id,
'integer'),
239 $ilDB->quote(time(),
'integer'),
240 $ilDB->quote(1,
'integer'),
241 $ilDB->quote($time,
'integer'),
242 $ilDB->quote((
int) $read_count_diff,
'integer'),
243 $ilDB->quote((
int) $time_diff,
'integer')
245 $aff = $ilDB->manipulate(
$query);
247 self::$has_accessed[$obj2_id][
$usr_id] =
true;
272 $query =
"SELECT obj_id FROM catch_write_events ".
273 "WHERE obj_id = ".$ilDB->quote($obj_id ,
'integer').
" ".
274 "AND usr_id = ".$ilDB->quote(
$usr_id ,
'integer');
278 $query =
"UPDATE catch_write_events ".
279 "SET ts = ".($timestamp == null ? $ilDB->now() : $ilDB->quote(
$timestamp,
'timestamp')).
" ".
280 "WHERE usr_id = ".$ilDB->quote(
$usr_id ,
'integer').
" ".
281 "AND obj_id = ".$ilDB->quote($obj_id ,
'integer');
286 $query =
"INSERT INTO catch_write_events (ts,obj_id,usr_id) ".
289 $ilDB->quote($obj_id,
'integer').
", ".
290 $ilDB->quote(
$usr_id,
'integer').
" ".
367 "FROM catch_write_events ".
368 "WHERE obj_id=".$ilDB->quote($obj_id ,
'integer').
" ".
369 "AND usr_id=".$ilDB->quote(
$usr_id ,
'integer');
370 $r = $ilDB->query($q);
373 $catchup =
$row[
'ts'];
378 $query = sprintf(
'SELECT * FROM write_event '.
379 'WHERE obj_id = %s '.
382 $ilDB->quote($obj_id,
'integer'),
383 $ilDB->quote(
$usr_id,
'integer'));
388 $query = sprintf(
'SELECT * FROM write_event '.
389 'WHERE obj_id = %s '.
393 $ilDB->quote($obj_id,
'integer'),
394 $ilDB->quote(
$usr_id,
'integer'),
395 $ilDB->quote($catchup,
'timestamp'));
399 while(
$row = $ilDB->fetchAssoc(
$res))
420 "FROM catch_write_events ".
421 "WHERE obj_id=".$ilDB->quote($obj_id ,
'integer').
" ".
422 "AND usr_id=".$ilDB->quote(
$usr_id ,
'integer');
423 $r = $ilDB->query($q);
426 $catchup =
$row[
'ts'];
432 $query = sprintf(
'SELECT * FROM write_event '.
433 'WHERE obj_id = %s '.
435 $ilDB->quote($obj_id,
'integer'),
436 $ilDB->quote(
$usr_id,
'integer'));
442 $query = sprintf(
'SELECT * FROM write_event '.
443 'WHERE obj_id = %s '.
446 $ilDB->quote($obj_id,
'integer'),
447 $ilDB->quote(
$usr_id,
'integer'),
448 $ilDB->quote($catchup,
'timestamp'));
452 $numRows =
$res->numRows();
458 return ($catchup == null) ? 1 : 2;
485 "FROM catch_write_events ".
486 "WHERE obj_id=".$ilDB->quote($parent_obj_id).
" ".
487 "AND usr_id=".$ilDB->quote(
$usr_id);
488 $r = $ilDB->query($q);
491 $catchup =
$row[
'ts'];
497 $query = sprintf(
'SELECT * FROM write_event '.
498 'WHERE parent_obj_id = %s '.
500 $ilDB->quote($parent_obj_id,
'integer'),
501 $ilDB->quote(
$usr_id,
'integer'));
507 $query = sprintf(
'SELECT * FROM write_event '.
508 'WHERE parent_obj_id = %s '.
511 $ilDB->quote($parent_obj_id,
'integer'),
512 $ilDB->quote(
$usr_id,
'integer'),
513 $ilDB->quote($catchup,
'timestamp'));
516 $numRows =
$res->numRows();
522 return ($catchup == null) ? 1 : 2;
579 $query = sprintf(
'SELECT * FROM read_event '.
580 'WHERE obj_id = %s '.
581 'ORDER BY last_access DESC',
582 $ilDB->quote($obj_id,
'integer'));
587 $query = sprintf(
'SELECT * FROM read_event '.
588 'WHERE obj_id = %s '.
590 'ORDER BY last_access DESC',
591 $ilDB->quote($obj_id,
'integer'),
592 $ilDB->quote(
$usr_id,
'integer'));
597 while (
$row = $ilDB->fetchAssoc(
$res))
599 $events[$counter][
'obj_id'] =
$row[
'obj_id'];
600 $events[$counter][
'usr_id'] =
$row[
'usr_id'];
601 $events[$counter][
'last_access'] =
$row[
'last_access'];
602 $events[$counter][
'read_count'] =
$row[
'read_count'];
603 $events[$counter][
'spent_seconds'] =
$row[
'spent_seconds'];
604 $events[$counter][
'first_access'] =
$row[
'first_access'];
609 return $events ? $events : array();
622 $query = sprintf(
'SELECT DISTINCT(usr_id) usr FROM read_event '.
623 'WHERE obj_id = %s ',
624 $ilDB->quote($a_obj_id,
'integer'));
626 while(
$row = $ilDB->fetchObject($res))
628 $users[] =
$row->usr;
630 return $users ? $users : array();
640 if (isset(self::$has_accessed[$a_obj_id][$a_usr_id]))
642 return self::$has_accessed[$a_obj_id][$a_usr_id];
645 $set = $ilDB->query(
"SELECT usr_id FROM read_event WHERE ".
646 "obj_id = ".$ilDB->quote($a_obj_id,
"integer").
" AND ".
647 "usr_id = ".$ilDB->quote($a_usr_id,
"integer")
649 if ($rec = $ilDB->fetchAssoc($set))
651 return self::$has_accessed[$a_obj_id][$a_usr_id] =
true;
653 return self::$has_accessed[$a_obj_id][$a_usr_id] =
false;
664 return 'change event tracking is already active';
676 $set = $ilDB->query(sprintf(
'SELECT r1.obj_id,r2.obj_id p,d.owner,%s,d.create_date '.
677 'FROM object_data d '.
678 'LEFT JOIN write_event w ON d.obj_id = w.obj_id '.
679 'JOIN object_reference r1 ON d.obj_id=r1.obj_id '.
680 'JOIN tree t ON t.child=r1.ref_id '.
681 'JOIN object_reference r2 on r2.ref_id=t.parent '.
682 'WHERE w.obj_id IS NULL',
683 $ilDB->quote(
'create',
'text')));
684 while ($rec = $ilDB->fetchAssoc($set))
686 $nid = $ilDB->nextId(
"write_event");
687 $query =
'INSERT INTO write_event '.
688 '(write_id, obj_id,parent_obj_id,usr_id,action,ts) VALUES ('.
689 $ilDB->quote($nid,
"integer").
",".
690 $ilDB->quote($rec[
"obj_id"],
"integer").
",".
691 $ilDB->quote($rec[
"p"],
"integer").
",".
692 $ilDB->quote($rec[
"owner"],
"integer").
",".
693 $ilDB->quote(
"create",
"text").
",".
694 $ilDB->quote($rec[
"create_date"],
"timestamp").
699 if ($ilDB->isError(
$res) || $ilDB->isError(
$res->result))
701 return 'couldn\'t insert initial data into table "write_event": '.
702 (($ilDB->isError($r->result)) ? $r->result->getMessage() : $r->getMessage());
707 $ilias->setSetting(
'enable_change_event_tracking',
'1');
720 $ilias->setSetting(
'enable_change_event_tracking',
'0');
731 return $ilias->getSetting(
'enable_change_event_tracking',
'0') ==
'1';
745 $query = sprintf(
'DELETE FROM write_event WHERE obj_id = %s ',
746 $ilDB->quote($a_obj_id,
'integer'));
747 $aff = $ilDB->manipulate(
$query);
749 $query = sprintf(
'DELETE FROM read_event WHERE obj_id = %s ',
750 $ilDB->quote($a_obj_id,
'integer'));
751 $aff = $ilDB->manipulate(
$query);