EventLog.php
Go to the documentation of this file.00001 <?PHP
00002
00003 #
00004 # FILE: Scout--EventLog.php
00005 #
00006 # METHODS PROVIDED:
00007 # EventLog()
00008 # - constructor
00009 # SomeMethod($SomeParameter, $AnotherParameter)
00010 # - short description of method
00011 #
00012 # AUTHOR: Edward Almasy
00013 #
00014 # Copyright 2007 Internet Scout
00015 # http://scout.wisc.edu
00016 #
00017
00018
00022 class EventLog {
00023
00024 # ---- PUBLIC INTERFACE --------------------------------------------------
00025
00033 function EventLog($DB, $UserId = -1, $LoggingEnabled = TRUE)
00034 {
00035 $this->DB = $DB;
00036 $this->Enabled = $LoggingEnabled;
00037 $this->UserId = intval($UserId);
00038 }
00039
00046 function Log($Type, $DataOne = "", $DataTwo = "")
00047 {
00048 # if logging is turned on
00049 if ($this->Enabled)
00050 {
00051 # write event out to log
00052 $this->DB->Query("INSERT INTO EventLog"
00053 ." (EventType, EventDate, UserId, DataOne, DataTwo) VALUES"
00054 ." (".intval($Type).", NOW(), ".$this->UserId.","
00055 ." '".addslashes($DataOne)."',"
00056 ." '".addslashes($DataTwo)."')");
00057 }
00058 }
00059
00072 function FindEvents(
00073 $StartDate = NULL, $EndDate = NULL, $EventCount = 999999999, $EventType)
00074 {
00075 # retrieve arguments (if supplied)
00076 $StartDate = NULL;
00077 $EndDate = NULL;
00078 $StartIndex = 0;
00079 $EventCount = 100;
00080 $Types = array();
00081 $Args = func_get_args();
00082 if (count($Args)) { $StartDate = array_shift($Args); }
00083 if (count($Args)) { $EndDate = array_shift($Args); }
00084 if (count($Args)) { $StartIndex = array_shift($Args); }
00085 if (count($Args)) { $EventCount = array_shift($Args); }
00086 while (count($Args))
00087 {
00088 $Types[] = array_shift($Args);
00089 }
00090
00091 # add start and/or end date to query condition (if supplied)
00092 $Conditions = "";
00093 if ($StartDate)
00094 {
00095 $Conditions .= " EventDate >= '".addslashes($StartDate)."'";
00096 }
00097 if ($EndDate)
00098 {
00099 $Conditions .= (strlen($Conditions) ? " AND" : "")
00100 ." EventDate <= '".addslashes($EndDate)."'";
00101 }
00102
00103 # add event types to query condition (if supplied)
00104 $SubCondition = "";
00105 foreach ($Types as $Type)
00106 {
00107 $SubCondition .= (strlen($SubCondition) ? " OR" : "")
00108 ." EventType = ".intval($Type);
00109 }
00110 if (strlen($SubCondition))
00111 {
00112 $Conditions .= (strlen($Conditions) ? " AND" : "")
00113 ." (".$SubCondition.")";
00114 }
00115
00116 # if user privilege exclusions have been specified
00117 if (isset($this->ExcludedPrivilegesForFind))
00118 {
00119 # add beginning of exclusion conditions and subquery
00120 $Conditions .= (strlen($Conditions) ? " AND" : "")
00121 ." UserId NOT IN (SELECT UserId FROM APUserPrivileges WHERE ";
00122
00123 # add subquery condition for each exclusion
00124 $Connector = "";
00125 foreach ($this->ExcludedPrivilegesForFind as $Exclusion)
00126 {
00127 $Conditions .= $Connector."Privilege "
00128 .$Exclusion["Operator"]." ".$Exclusion["Value"];
00129 $Connector = " OR ";
00130 }
00131
00132 # close out subquery condition
00133 $Conditions .= ")";
00134 }
00135
00136 # if SQL query conditions have been specified
00137 if (isset($this->ConditionsForFind))
00138 {
00139 # add conditions to condition string
00140 foreach ($this->ConditionsForFind as $Condition)
00141 {
00142 $Conditions .= (strlen($Conditions) ? " AND " : " ").$Condition;
00143 }
00144 }
00145
00146 # build event query
00147 $Query = "SELECT * FROM EventLog"
00148 .(strlen($Conditions) ? " WHERE ".$Conditions : "")
00149 ." ORDER BY EventDate DESC LIMIT ".$StartIndex.", ".$EventCount;
00150
00151 # run query and retrieve event information
00152 $this->DB->Query($Query);
00153 $Events = array();
00154 while ($EventInfo = $this->DB->FetchRow()) { $Events[] = $EventInfo; }
00155
00156 # return event information to caller
00157 return $Events;
00158 }
00159
00169 function ExcludeUsersWithPrivilegesForFind($Operator, $Value)
00170 {
00171 # if caller requested clear
00172 if (($Operator === NULL) && ($Value === NULL))
00173 {
00174 # clear exclusions
00175 unset($this->ExcludedPrivilegesForFind);
00176 }
00177 else
00178 {
00179 # add specified exclusion
00180 $Exclusion["Operator"] = $Operator;
00181 $Exclusion["Value"] = $Value;
00182 $this->ExcludedPrivilegesForFind[] = $Exclusion;
00183 }
00184 }
00185
00192 function AddSqlConditionForFind($Conditions)
00193 {
00194 # if caller requested clear
00195 if ($Conditions === NULL)
00196 {
00197 # clear all conditions
00198 unset($this->ConditionsForFind);
00199 }
00200 else
00201 {
00202 # convert condition to array if only one specified
00203 if (!is_array($Conditions)) { $Conditions = array($Conditions); }
00204
00205 # add conditions to list
00206 $this->ConditionsForFind = isset($this->ConditionsForFind)
00207 ? array_merge($this->ConditionsForFind, $Conditions)
00208 : $Conditions;
00209 }
00210 }
00211
00217 function LimitFindToUser($UserId = NULL)
00218 {
00219 if ($UserId === NULL)
00220 {
00221 $UserId = $this->UserId;
00222 }
00223 $this->AddSqlConditionForFind("UserId = ".intval($UserId));
00224 }
00225
00236 function ModifyEvents($EventType, $EventDate, $UserId,
00237 $DataOne = NULL, $DataTwo = NULL, $Condition = NULL)
00238 {
00239 if ($DataOne || $DataTwo)
00240 {
00241 $this->DB->Query("UPDATE EventLog SET"
00242 .($DataOne ? " DataOne = '".addslashes($DataOne)."'" : "")
00243 .(($DataOne && $DataTwo) ? ", " : "")
00244 .($DataTwo ? " DataTwo = '".addslashes($DataTwo)."'" : "")
00245 ." WHERE EventType = '".addslashes($EventType)."'"
00246 ." AND EventDate = '".addslashes($EventDate)."'"
00247 ." AND UserId = '".addslashes($UserId)."'");
00248 }
00249 }
00250
00251
00252 # ---- PRIVATE INTERFACE -------------------------------------------------
00253
00254 private $DB;
00255 private $Enabled;
00256 private $UserId;
00257 private $ExcludedPrivilegesForFind;
00258 private $ConditionsForFind;
00259
00260 }
00261
00262
00263 ?>