CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 __construct($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
193 function AddSqlConditionForFind($Conditions)
194 {
195 # if caller requested clear
196 if ($Conditions === NULL)
197 {
198 # clear all conditions
199 unset($this->ConditionsForFind);
200 }
201 else
202 {
203 # convert condition to array if only one specified
204 if (!is_array($Conditions)) { $Conditions = array($Conditions); }
205
206 # add conditions to list
207 $this->ConditionsForFind = isset($this->ConditionsForFind)
208 ? array_merge($this->ConditionsForFind, $Conditions)
209 : $Conditions;
210 }
211 }
212
218 function LimitFindToUser($UserId = NULL)
219 {
220 if ($UserId === NULL)
221 {
222 $UserId = $this->UserId;
223 }
224 $this->AddSqlConditionForFind("UserId = ".intval($UserId));
225 }
226
239 function ModifyEvents($EventType, $EventDate, $UserId,
240 $DataOne = NULL, $DataTwo = NULL, $Condition = NULL)
241 {
242 if ($DataOne || $DataTwo)
243 {
244 $this->DB->Query("UPDATE EventLog SET"
245 .($DataOne ? " DataOne = '".addslashes($DataOne)."'" : "")
246 .(($DataOne && $DataTwo) ? ", " : "")
247 .($DataTwo ? " DataTwo = '".addslashes($DataTwo)."'" : "")
248 ." WHERE EventType = '".addslashes($EventType)."'"
249 ." AND EventDate = '".addslashes($EventDate)."'"
250 ." AND UserId = '".addslashes($UserId)."'");
251 }
252 }
253
254
255 # ---- PRIVATE INTERFACE -------------------------------------------------
256
257 private $DB;
258 private $Enabled;
259 private $UserId;
260 private $ExcludedPrivilegesForFind;
261 private $ConditionsForFind;
262
263 }
264
265
266 ?>
EventLog\LimitFindToUser
LimitFindToUser($UserId=NULL)
Limit FindEvents() results to user with specified ID.
Definition: EventLog.php:218
EventLog\FindEvents
FindEvents($StartDate=NULL, $EndDate=NULL, $EventCount=999999999, $EventType)
Retrieve specified range of events.
Definition: EventLog.php:72
EventLog\__construct
__construct($DB, $UserId=-1, $LoggingEnabled=TRUE)
Object constructor.
Definition: EventLog.php:33
EventLog
Class for storing and retrieving event information from database.
Definition: EventLog.php:22
EventLog\Log
Log($Type, $DataOne="", $DataTwo="")
Add event to log.
Definition: EventLog.php:46
PHP
PHP
Definition: RSSClient--Test.php:12
EventLog\ExcludeUsersWithPrivilegesForFind
ExcludeUsersWithPrivilegesForFind($Operator, $Value)
Add privilege to exclude from FindEvents() results.
Definition: EventLog.php:169
EventLog\AddSqlConditionForFind
AddSqlConditionForFind($Conditions)
Add SQL condition to apply to FindEvents().
Definition: EventLog.php:193
EventLog\ModifyEvents
ModifyEvents($EventType, $EventDate, $UserId, $DataOne=NULL, $DataTwo=NULL, $Condition=NULL)
Modify existing events.
Definition: EventLog.php:239