ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilHistory.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
10 class ilHistory
11 {
12 
31  public static function _createEntry(
32  $a_obj_id,
33  $a_action,
34  $a_info_params = "",
35  $a_obj_type = "",
36  $a_user_comment = "",
37  $a_update_last = false
38  ) {
39  global $DIC;
40 
41  $ilDB = $DIC->database();
42  $ilUser = $DIC->user();
43 
44  if ($a_obj_type == "") {
45  $a_obj_type = ilObject::_lookupType($a_obj_id);
46  }
47 
48  if (is_array($a_info_params)) {
49  foreach ($a_info_params as $key => $param) {
50  $a_info_params[$key] = str_replace(",", "&#044;", $param);
51  }
52  $a_info_params = implode(",", $a_info_params);
53  }
54 
55  // get last entry of object
56  $last_entry_sql = "SELECT * FROM history WHERE " .
57  " obj_id = " . $ilDB->quote($a_obj_id, "integer") . " AND " .
58  " obj_type = " . $ilDB->quote($a_obj_type, "text") . " ORDER BY hdate DESC";
59  $last_entry_set = $ilDB->query($last_entry_sql);
60  $last_entry = $ilDB->fetchAssoc($last_entry_set);
61 
62  // note: insert is forced if last entry already has a comment and a
63  // new comment is given too OR
64  // if entry should not be updated OR
65  // if current action or user id are not equal with last entry
66  if (($a_user_comment != "" && $last_entry["user_comment"] != "")
67  || !$a_update_last || $a_action != $last_entry["action"]
68  || $ilUser->getId() != $last_entry["usr_id"]) {
69  $id = $ilDB->nextId("history");
70  $ilDB->insert("history", array(
71  "id" => array("integer", $id),
72  "obj_id" => array("integer", $a_obj_id),
73  "obj_type" => array("text", $a_obj_type),
74  "action" => array("text", $a_action),
75  "hdate" => array("timestamp", ilUtil::now()),
76  "usr_id" => array("integer", $ilUser->getId()),
77  "info_params" => array("text", $a_info_params),
78  "user_comment" => array("clob", $a_user_comment)
79  ));
80 
81  /*$query = "INSERT INTO history (id, obj_id, obj_type, action, hdate, usr_id, info_params, user_comment) VALUES ".
82  "(".
83  $ilDB->quote($id).", ".
84  $ilDB->quote($a_obj_id).", ".
85  $ilDB->quote($a_obj_type).", ".
86  $ilDB->quote($a_action).", ".
87  "now(), ".
88  $ilDB->quote($ilUser->getId()).", ".
89  $ilDB->quote($a_info_params).", ".
90  $ilDB->quote($a_user_comment).
91  ")";
92  $ilDB->query($query);*/
93  } else {
94  // if entry should be updated, update user comment only
95  // if it is set (this means, user comment has been empty
96  // because if old and new comment are given, an INSERT is forced
97  // see if statement above)
98  //$uc_str = ($a_user_comment != "")
99  // ? ", user_comment = ".$ilDB->quote($a_user_comment)
100  // : "";
101  /*$query = "UPDATE history SET ".
102  " hdate = now() ".
103  $uc_str.
104  " WHERE id = ".$ilDB->quote($last_entry["id"]);
105  $ilDB->query($query);*/
106 
107  $fields = array(
108  "hdate" => array("timestamp", ilUtil::now())
109  );
110  if ($a_user_comment != "") {
111  $fields["user_comment"] = array("clob", $a_user_comment);
112  }
113 
114  $ilDB->update("history", $fields, array(
115  "id" => array("integer", $id)
116  ));
117  }
118  }
119 
128  public static function _getEntriesForObject($a_obj_id, $a_obj_type = "")
129  {
130  global $DIC;
131 
132  $ilDB = $DIC->database();
133 
134  if ($a_obj_type == "") {
135  $a_obj_type = ilObject::_lookupType($a_obj_id);
136  }
137  $query = "SELECT * FROM history WHERE obj_id = " .
138  $ilDB->quote($a_obj_id, "integer") . " AND " .
139  "obj_type = " . $ilDB->quote($a_obj_type, "text") .
140  " ORDER BY hdate DESC";
141 
142  $hist_set = $ilDB->query($query);
143  $hist_items = array();
144  while ($hist_rec = $ilDB->fetchAssoc($hist_set)) {
145  $hist_items[] = array("date" => $hist_rec["hdate"],
146  "user_id" => $hist_rec["usr_id"],
147  "obj_id" => $hist_rec["obj_id"],
148  "obj_type" => $hist_rec["obj_type"],
149  "action" => $hist_rec["action"],
150  "info_params" => $hist_rec["info_params"],
151  "user_comment" => $hist_rec["user_comment"],
152  "hist_entry_id" => $hist_rec["id"]);
153  }
154 
155  if ($a_obj_type == "lm") {
156  $query = "SELECT h.*, l.title as title FROM history h, lm_data l WHERE " .
157  " l.lm_id = " . $ilDB->quote($a_obj_id, "integer") . " AND " .
158  " l.obj_id = h.obj_id AND " .
159  " (h.obj_type=" . $ilDB->quote($a_obj_type . ":pg", "text") . " OR h.obj_type=" . $ilDB->quote($a_obj_type . ":st", "text") . ") " .
160  " ORDER BY h.hdate DESC";
161 
162  $hist_set = $ilDB->query($query);
163  while ($hist_rec = $ilDB->fetchAssoc($hist_set)) {
164  $hist_items[] = array("date" => $hist_rec["hdate"],
165  "user_id" => $hist_rec["usr_id"],
166  "obj_id" => $hist_rec["obj_id"],
167  "obj_type" => $hist_rec["obj_type"],
168  "action" => $hist_rec["action"],
169  "info_params" => $hist_rec["info_params"],
170  "user_comment" => $hist_rec["user_comment"],
171  "hist_entry_id" => $hist_rec["id"],
172  "title" => $hist_rec["title"]);
173  }
174  usort($hist_items, array("ilHistory", "_compareHistArray"));
175  $hist_items2 = array_reverse($hist_items);
176  return $hist_items2;
177  }
178 
179  return $hist_items;
180  }
181 
182  public static function _compareHistArray($a, $b)
183  {
184  if ($a["date"] == $b["date"]) {
185  return 0;
186  }
187  return ($a["date"] < $b["date"]) ? -1 : 1;
188  }
189 
197  public static function _removeEntriesForObject($a_obj_id)
198  {
199  global $DIC;
200 
201  $ilDB = $DIC->database();
202 
203  $q = "DELETE FROM history WHERE obj_id = " .
204  $ilDB->quote($a_obj_id, "integer");
205  $r = $ilDB->manipulate($q);
206 
207  return true;
208  }
209 
217  public static function _copyEntriesForObject($a_src_id, $a_dst_id)
218  {
219  global $DIC;
220 
221  $ilDB = $DIC->database();
222 
223  $q = "SELECT * FROM history WHERE obj_id = " .
224  $ilDB->quote($a_src_id, "integer");
225  $r = $ilDB->query($q);
226 
227  while ($row = $ilDB->fetchObject($r)) {
228  $id = $ilDB->nextId("history");
229  $ilDB->insert("history", array(
230  "id" => array("integer", $id),
231  "obj_id" => array("integer", $a_dst_id),
232  "obj_type" => array("text", $row->obj_type),
233  "action" => array("text", $row->action),
234  "hdate" => array("timestamp", ilUtil::now()),
235  "usr_id" => array("integer", $row->usr_id),
236  "info_params" => array("text", $row->info_params),
237  "user_comment" => array("clob", $row->user_comment)
238  ));
239 
240  /*
241  $q = "INSERT INTO history (obj_id, obj_type, action, hdate, usr_id, info_params, user_comment) VALUES ".
242  "(".
243  $ilDB->quote($a_dst_id).", ".
244  $ilDB->quote($row->obj_type).", ".
245  $ilDB->quote($row->action).", ".
246  $ilDB->quote($row->hdate).", ".
247  $ilDB->quote($row->usr_id).", ".
248  $ilDB->quote($row->info_params).", ".
249  $ilDB->quote($row->user_comment).
250  ")";
251 
252  $ilDB->query($q);*/
253  }
254 
255  return true;
256  }
257 
263  public static function _getEntryByHistoryID($a_hist_entry_id)
264  {
265  global $DIC;
266 
267  $ilDB = $DIC->database();
268 
269  $q = "SELECT * FROM history WHERE id = " .
270  $ilDB->quote($a_hist_entry_id, "integer");
271  $r = $ilDB->query($q);
272 
273  return $ilDB->fetchAssoc($r);
274  }
275 
281  public static function _removeEntryByHistoryID($a_hist_entry_id)
282  {
283  global $DIC;
284 
285  $ilDB = $DIC->database();
286 
287  $q = "DELETE FROM history WHERE id = " .
288  $ilDB->quote($a_hist_entry_id, "integer");
289  $ilDB->manipulate($q);
290  }
291 
298  public static function _changeUserId($a_hist_entry_id, $new_user_id)
299  {
300  global $DIC;
301 
302  $ilDB = $DIC->database();
303 
304  $ilDB->update(
305  "history",
306  array("usr_id" => array("integer", $new_user_id)),
307  array("id" => array("integer", $a_hist_entry_id))
308  );
309  }
310 } // END class.ilHistory
static _removeEntryByHistoryID($a_hist_entry_id)
Removes a single entry from the history.
static _changeUserId($a_hist_entry_id, $new_user_id)
Changes the user id of the specified history entry.
static _compareHistArray($a, $b)
static now()
Return current timestamp in Y-m-d H:i:s format.
static _createEntry( $a_obj_id, $a_action, $a_info_params="", $a_obj_type="", $a_user_comment="", $a_update_last=false)
Creates a new history entry for an object.
static _getEntryByHistoryID($a_hist_entry_id)
returns a single history entry
This class methods for maintain history enties for objects.
static _copyEntriesForObject($a_src_id, $a_dst_id)
copy all history entries for an object
$param
Definition: xapitoken.php:29
global $DIC
Definition: goto.php:24
$query
static _getEntriesForObject($a_obj_id, $a_obj_type="")
get all history entries for an object
static _lookupType($a_id, $a_reference=false)
lookup object type
global $ilDB
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
static _removeEntriesForObject($a_obj_id)
remove all history entries for an object
$ilUser
Definition: imgupload.php:18