CWIS Developer Documentation
EventLog.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: Scout--EventLog.php
5 #
6 # METHODS PROVIDED:
7 # EventLog()
8 # - constructor
9 # SomeMethod($SomeParameter, $AnotherParameter)
10 # - short description of method
11 #
12 # AUTHOR: Edward Almasy
13 #
14 # Copyright 2007 Internet Scout
15 # http://scout.wisc.edu
16 #
17 
18 
22 class EventLog {
23 
24  # ---- PUBLIC INTERFACE --------------------------------------------------
25 
33  function EventLog($DB, $UserId = -1, $LoggingEnabled = TRUE)
34  {
35  $this->DB = $DB;
36  $this->Enabled = $LoggingEnabled;
37  $this->UserId = intval($UserId);
38  }
39 
46  function Log($Type, $DataOne = "", $DataTwo = "")
47  {
48  # if logging is turned on
49  if ($this->Enabled)
50  {
51  # write event out to log
52  $this->DB->Query("INSERT INTO EventLog"
53  ." (EventType, EventDate, UserId, DataOne, DataTwo) VALUES"
54  ." (".intval($Type).", NOW(), ".$this->UserId.","
55  ." '".addslashes($DataOne)."',"
56  ." '".addslashes($DataTwo)."')");
57  }
58  }
59 
72  function FindEvents(
73  $StartDate = NULL, $EndDate = NULL, $EventCount = 999999999, $EventType)
74  {
75  # retrieve arguments (if supplied)
76  $StartDate = NULL;
77  $EndDate = NULL;
78  $StartIndex = 0;
79  $EventCount = 100;
80  $Types = array();
81  $Args = func_get_args();
82  if (count($Args)) { $StartDate = array_shift($Args); }
83  if (count($Args)) { $EndDate = array_shift($Args); }
84  if (count($Args)) { $StartIndex = array_shift($Args); }
85  if (count($Args)) { $EventCount = array_shift($Args); }
86  while (count($Args))
87  {
88  $Types[] = array_shift($Args);
89  }
90 
91  # add start and/or end date to query condition (if supplied)
92  $Conditions = "";
93  if ($StartDate)
94  {
95  $Conditions .= " EventDate >= '".addslashes($StartDate)."'";
96  }
97  if ($EndDate)
98  {
99  $Conditions .= (strlen($Conditions) ? " AND" : "")
100  ." EventDate <= '".addslashes($EndDate)."'";
101  }
102 
103  # add event types to query condition (if supplied)
104  $SubCondition = "";
105  foreach ($Types as $Type)
106  {
107  $SubCondition .= (strlen($SubCondition) ? " OR" : "")
108  ." EventType = ".intval($Type);
109  }
110  if (strlen($SubCondition))
111  {
112  $Conditions .= (strlen($Conditions) ? " AND" : "")
113  ." (".$SubCondition.")";
114  }
115 
116  # if user privilege exclusions have been specified
117  if (isset($this->ExcludedPrivilegesForFind))
118  {
119  # add beginning of exclusion conditions and subquery
120  $Conditions .= (strlen($Conditions) ? " AND" : "")
121  ." UserId NOT IN (SELECT UserId FROM APUserPrivileges WHERE ";
122 
123  # add subquery condition for each exclusion
124  $Connector = "";
125  foreach ($this->ExcludedPrivilegesForFind as $Exclusion)
126  {
127  $Conditions .= $Connector."Privilege "
128  .$Exclusion["Operator"]." ".$Exclusion["Value"];
129  $Connector = " OR ";
130  }
131 
132  # close out subquery condition
133  $Conditions .= ")";
134  }
135 
136  # if SQL query conditions have been specified
137  if (isset($this->ConditionsForFind))
138  {
139  # add conditions to condition string
140  foreach ($this->ConditionsForFind as $Condition)
141  {
142  $Conditions .= (strlen($Conditions) ? " AND " : " ").$Condition;
143  }
144  }
145 
146  # build event query
147  $Query = "SELECT * FROM EventLog"
148  .(strlen($Conditions) ? " WHERE ".$Conditions : "")
149  ." ORDER BY EventDate DESC LIMIT ".$StartIndex.", ".$EventCount;
150 
151  # run query and retrieve event information
152  $this->DB->Query($Query);
153  $Events = array();
154  while ($EventInfo = $this->DB->FetchRow()) { $Events[] = $EventInfo; }
155 
156  # return event information to caller
157  return $Events;
158  }
159 
169  function ExcludeUsersWithPrivilegesForFind($Operator, $Value)
170  {
171  # if caller requested clear
172  if (($Operator === NULL) && ($Value === NULL))
173  {
174  # clear exclusions
175  unset($this->ExcludedPrivilegesForFind);
176  }
177  else
178  {
179  # add specified exclusion
180  $Exclusion["Operator"] = $Operator;
181  $Exclusion["Value"] = $Value;
182  $this->ExcludedPrivilegesForFind[] = $Exclusion;
183  }
184  }
185 
192  function AddSqlConditionForFind($Conditions)
193  {
194  # if caller requested clear
195  if ($Conditions === NULL)
196  {
197  # clear all conditions
198  unset($this->ConditionsForFind);
199  }
200  else
201  {
202  # convert condition to array if only one specified
203  if (!is_array($Conditions)) { $Conditions = array($Conditions); }
204 
205  # add conditions to list
206  $this->ConditionsForFind = isset($this->ConditionsForFind)
207  ? array_merge($this->ConditionsForFind, $Conditions)
208  : $Conditions;
209  }
210  }
211 
217  function LimitFindToUser($UserId = NULL)
218  {
219  if ($UserId === NULL)
220  {
221  $UserId = $this->UserId;
222  }
223  $this->AddSqlConditionForFind("UserId = ".intval($UserId));
224  }
225 
236  function ModifyEvents($EventType, $EventDate, $UserId,
237  $DataOne = NULL, $DataTwo = NULL, $Condition = NULL)
238  {
239  if ($DataOne || $DataTwo)
240  {
241  $this->DB->Query("UPDATE EventLog SET"
242  .($DataOne ? " DataOne = '".addslashes($DataOne)."'" : "")
243  .(($DataOne && $DataTwo) ? ", " : "")
244  .($DataTwo ? " DataTwo = '".addslashes($DataTwo)."'" : "")
245  ." WHERE EventType = '".addslashes($EventType)."'"
246  ." AND EventDate = '".addslashes($EventDate)."'"
247  ." AND UserId = '".addslashes($UserId)."'");
248  }
249  }
250 
251 
252  # ---- PRIVATE INTERFACE -------------------------------------------------
253 
254  private $DB;
255  private $Enabled;
256  private $UserId;
257  private $ExcludedPrivilegesForFind;
258  private $ConditionsForFind;
259 
260 }
261 
262 
263 ?>
LimitFindToUser($UserId=NULL)
Limit FindEvents() results to user with specified ID.
Definition: EventLog.php:217
FindEvents($StartDate=NULL, $EndDate=NULL, $EventCount=999999999, $EventType)
Retrieve specified range of events.
Definition: EventLog.php:72
Class for storing and retrieving event information from database.
Definition: EventLog.php:22
Log($Type, $DataOne="", $DataTwo="")
Add event to log.
Definition: EventLog.php:46
PHP
Definition: OAIClient.php:39
ExcludeUsersWithPrivilegesForFind($Operator, $Value)
Add privilege to exclude from FindEvents() results.
Definition: EventLog.php:169
AddSqlConditionForFind($Conditions)
Add SQL condition to apply to FindEvents().
Definition: EventLog.php:192
ModifyEvents($EventType, $EventDate, $UserId, $DataOne=NULL, $DataTwo=NULL, $Condition=NULL)
Modify existing events.
Definition: EventLog.php:236
EventLog($DB, $UserId=-1, $LoggingEnabled=TRUE)
Object constructor.
Definition: EventLog.php:33