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