ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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  function _createEntry($a_obj_id, $a_action, $a_info_params = "", $a_obj_type = "",
32  $a_user_comment = "", $a_update_last = false)
33  {
34  global $ilDB, $ilUser;
35 
36  if ($a_obj_type == "")
37  {
38  $a_obj_type = ilObject::_lookupType($a_obj_id);
39  }
40 
41  if (is_array($a_info_params))
42  {
43  foreach($a_info_params as $key => $param)
44  {
45  $a_info_params[$key] = str_replace(",", "&#044;", $param);
46  }
47  $a_info_params = implode(",", $a_info_params);
48  }
49 
50  // get last entry of object
51  $last_entry_sql = "SELECT * FROM history WHERE ".
52  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
53  " obj_type = ".$ilDB->quote($a_obj_type, "text")." ORDER BY hdate DESC";
54  $last_entry_set = $ilDB->query($last_entry_sql);
55  $last_entry = $ilDB->fetchAssoc($last_entry_set);
56 
57  // note: insert is forced if last entry already has a comment and a
58  // new comment is given too OR
59  // if entry should not be updated OR
60  // if current action or user id are not equal with last entry
61  if (($a_user_comment != "" && $last_entry["user_comment"] != "")
62  || !$a_update_last || $a_action != $last_entry["action"]
63  || $ilUser->getId() != $last_entry["usr_id"])
64  {
65  $id = $ilDB->nextId("history");
66  $ilDB->insert("history", array(
67  "id" => array("integer", $id),
68  "obj_id" => array("integer", $a_obj_id),
69  "obj_type" => array("text", $a_obj_type),
70  "action" => array("text", $a_action),
71  "hdate" => array("timestamp", ilUtil::now()),
72  "usr_id" => array("integer", $ilUser->getId()),
73  "info_params" => array("text", $a_info_params),
74  "user_comment" => array("clob", $a_user_comment)
75  ));
76 
77  /*$query = "INSERT INTO history (id, obj_id, obj_type, action, hdate, usr_id, info_params, user_comment) VALUES ".
78  "(".
79  $ilDB->quote($id).", ".
80  $ilDB->quote($a_obj_id).", ".
81  $ilDB->quote($a_obj_type).", ".
82  $ilDB->quote($a_action).", ".
83  "now(), ".
84  $ilDB->quote($ilUser->getId()).", ".
85  $ilDB->quote($a_info_params).", ".
86  $ilDB->quote($a_user_comment).
87  ")";
88  $ilDB->query($query);*/
89  }
90  else
91  {
92  // if entry should be updated, update user comment only
93  // if it is set (this means, user comment has been empty
94  // because if old and new comment are given, an INSERT is forced
95  // see if statement above)
96  //$uc_str = ($a_user_comment != "")
97  // ? ", user_comment = ".$ilDB->quote($a_user_comment)
98  // : "";
99  /*$query = "UPDATE history SET ".
100  " hdate = now() ".
101  $uc_str.
102  " WHERE id = ".$ilDB->quote($last_entry["id"]);
103  $ilDB->query($query);*/
104 
105  $fields = array(
106  "hdate" => array("timestamp", ilUtil::now())
107  );
108  if ($a_user_comment != "")
109  {
110  $fields["user_comment"] = array("clob", $a_user_comment);
111  }
112 
113  $ilDB->update("history", $fields, array(
114  "id" => array("integer", $id)
115  ));
116 
117  }
118 
119  }
120 
129  function _getEntriesForObject($a_obj_id, $a_obj_type = "")
130  {
131  global $ilDB;
132 
133  if ($a_obj_type == "")
134  {
135  $a_obj_type = ilObject::_lookupType($a_obj_id);
136  }
137 
138  $query = "SELECT * FROM history WHERE obj_id = ".
139  $ilDB->quote($a_obj_id, "integer")." AND ".
140  "obj_type = ".$ilDB->quote($a_obj_type, "text").
141  " ORDER BY hdate DESC";
142 
143  $hist_set = $ilDB->query($query);
144 
145  $hist_items = array();
146  while ($hist_rec = $ilDB->fetchAssoc($hist_set))
147  {
148  $hist_items[] = array("date" => $hist_rec["hdate"],
149  "user_id" => $hist_rec["usr_id"],
150  "obj_id" => $hist_rec["obj_id"],
151  "obj_type" => $hist_rec["obj_type"],
152  "action" => $hist_rec["action"],
153  "info_params" => $hist_rec["info_params"],
154  "user_comment" => $hist_rec["user_comment"],
155  "hist_entry_id" => $hist_rec["id"],
156  "title" => $hist_rec["title"]);
157  }
158 
159  if ($a_obj_type == "lm" || $a_obj_type == "dbk")
160  {
161  $query = "SELECT h.*, l.title as title FROM history h, lm_data l WHERE ".
162  " l.lm_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
163  " l.obj_id = h.obj_id AND ".
164  " (h.obj_type=".$ilDB->quote($a_obj_type.":pg", "text")." OR h.obj_type=".$ilDB->quote($a_obj_type.":st", "text").") ".
165  " ORDER BY h.hdate DESC";
166 
167  $hist_set = $ilDB->query($query);
168  while ($hist_rec = $ilDB->fetchAssoc($hist_set))
169  {
170  $hist_items[] = array("date" => $hist_rec["hdate"],
171  "user_id" => $hist_rec["usr_id"],
172  "obj_id" => $hist_rec["obj_id"],
173  "obj_type" => $hist_rec["obj_type"],
174  "action" => $hist_rec["action"],
175  "info_params" => $hist_rec["info_params"],
176  "user_comment" => $hist_rec["user_comment"],
177  "hist_entry_id" => $hist_rec["id"],
178  "title" => $hist_rec["title"]);
179  }
180  usort($hist_items, array("ilHistory", "_compareHistArray"));
181  $hist_items2 = array_reverse($hist_items);
182  return $hist_items2;
183  }
184 
185  return $hist_items;
186  }
187 
188  function _compareHistArray($a, $b)
189  {
190  if ($a["date"] == $b["date"])
191  {
192  return 0;
193  }
194  return ($a["date"] < $b["date"]) ? -1 : 1;
195  }
196 
204  function _removeEntriesForObject($a_obj_id)
205  {
206  global $ilDB;
207 
208  $q = "DELETE FROM history WHERE obj_id = ".
209  $ilDB->quote($a_obj_id, "integer");
210  $r = $ilDB->manipulate($q);
211 
212  return true;
213  }
214 
222  function _copyEntriesForObject($a_src_id,$a_dst_id)
223  {
224  global $ilDB;
225 
226  $q = "SELECT * FROM history WHERE obj_id = ".
227  $ilDB->quote($a_src_id, "integer");
228  $r = $ilDB->query($q);
229 
230  while ($row = $ilDB->fetchObject($r))
231  {
232  $id = $ilDB->nextId("history");
233  $ilDB->insert("history", array(
234  "id" => array("integer", $id),
235  "obj_id" => array("integer", $a_dst_id),
236  "obj_type" => array("text", $row->obj_type),
237  "action" => array("text", $row->action),
238  "hdate" => array("timestamp", ilUtil::now()),
239  "usr_id" => array("integer", $row->usr_id),
240  "info_params" => array("text", $row->info_params),
241  "user_comment" => array("clob", $row->user_comment)
242  ));
243 
244  /*
245  $q = "INSERT INTO history (obj_id, obj_type, action, hdate, usr_id, info_params, user_comment) VALUES ".
246  "(".
247  $ilDB->quote($a_dst_id).", ".
248  $ilDB->quote($row->obj_type).", ".
249  $ilDB->quote($row->action).", ".
250  $ilDB->quote($row->hdate).", ".
251  $ilDB->quote($row->usr_id).", ".
252  $ilDB->quote($row->info_params).", ".
253  $ilDB->quote($row->user_comment).
254  ")";
255 
256  $ilDB->query($q);*/
257  }
258 
259  return true;
260  }
261 
267  function _getEntryByHistoryID($a_hist_entry_id)
268  {
269  global $ilDB;
270 
271  $q = "SELECT * FROM history WHERE id = ".
272  $ilDB->quote($a_hist_entry_id, "integer");
273  $r = $ilDB->query($q);
274 
275  return $ilDB->fetchAssoc($r);
276  }
277 } // END class.ilHistory
278 ?>