Open Monograph Press  3.3.0
EventLogDAO.inc.php
1 <?php
2 
18 import ('lib.pkp.classes.log.EventLogEntry');
19 
20 class EventLogDAO extends DAO {
21 
29  function getById($logId, $assocType = null, $assocId = null) {
30  $params = array((int) $logId);
31  if (isset($assocType)) {
32  $params[] = (int) $assocType;
33  $params[] = (int) $assocId;
34  }
35 
36  $result = $this->retrieve(
37  'SELECT * FROM event_log WHERE log_id = ?' .
38  (isset($assocType)?' AND assoc_type = ? AND assoc_id = ?':''),
39  $params
40  );
41 
42  $returner = null;
43  if ($result->RecordCount() != 0) {
44  $returner =& $this->build($result->GetRowAssoc(false));
45  }
46 
47  $result->Close();
48  return $returner;
49  }
50 
58  function getByAssoc($assocType, $assocId, $rangeInfo = null) {
59  $result = $this->retrieveRange(
60  'SELECT * FROM event_log WHERE assoc_type = ? AND assoc_id = ? ORDER BY log_id DESC',
61  array((int) $assocType, (int) $assocId),
62  $rangeInfo
63  );
64 
65  return new DAOResultFactory($result, $this, 'build');
66  }
67 
72  function newDataObject() {
73  return new EventLogEntry();
74  }
75 
81  function build($row) {
82  $entry = $this->newDataObject();
83  $entry->setId($row['log_id']);
84  $entry->setUserId($row['user_id']);
85  $entry->setDateLogged($this->datetimeFromDB($row['date_logged']));
86  $entry->setEventType($row['event_type']);
87  $entry->setAssocType($row['assoc_type']);
88  $entry->setAssocId($row['assoc_id']);
89  $entry->setMessage($row['message']);
90  $entry->setIsTranslated($row['is_translated']);
91 
92  $result = $this->retrieve('SELECT * FROM event_log_settings WHERE log_id = ?', array((int) $entry->getId()));
93  $params = array();
94  while (!$result->EOF) {
95  $r = $result->getRowAssoc(false);
96  $params[$r['setting_name']] = $this->convertFromDB(
97  $r['setting_value'],
98  $r['setting_type']
99  );
100  $result->MoveNext();
101  }
102  $result->Close();
103  $entry->setParams($params);
104 
105  HookRegistry::call('EventLogDAO::build', array(&$entry, &$row));
106 
107  return $entry;
108  }
109 
114  function insertObject($entry) {
115  $this->update(
116  sprintf('INSERT INTO event_log
117  (user_id, date_logged, event_type, assoc_type, assoc_id, message, is_translated)
118  VALUES
119  (?, %s, ?, ?, ?, ?, ?)',
120  $this->datetimeToDB($entry->getDateLogged())),
121  array(
122  (int) $entry->getUserId(),
123  (int) $entry->getEventType(),
124  (int) $entry->getAssocType(),
125  (int) $entry->getAssocId(),
126  $entry->getMessage(),
127  (int) $entry->getIsTranslated()
128  )
129  );
130  $entry->setId($this->getInsertId());
131 
132  // Add name => value entries into the settings table
133  $params = $entry->getParams();
134  if (is_array($params)) foreach ($params as $key => $value) {
135  $type = null;
136  $value = $this->convertToDB($value, $type);
137  $params = array(
138  (int) $entry->getId(),
139  $key, $value, $type
140  );
141  $this->update(
142  'INSERT INTO event_log_settings (log_id, setting_name, setting_value, setting_type) VALUES (?, ?, ?, ?)',
143  $params
144  );
145  }
146 
147  return $entry->getId();
148  }
149 
154  function deleteById($logId, $assocType = null, $assocId = null) {
155  $params = array((int) $logId);
156  if ($assocType !== null) {
157  $params[] = (int) $assocType;
158  $params[] = (int) $assocId;
159  }
160  $this->update(
161  'DELETE FROM event_log WHERE log_id = ?' .
162  ($assocType !== null?' AND assoc_type = ? AND assoc_id = ?':''),
163  $params
164  );
165  if ($this->getAffectedRows()) $this->update('DELETE FROM event_log_settings WHERE log_id = ?', array((int) $logId));
166  }
167 
173  function deleteByAssoc($assocType, $assocId) {
174  $entries = $this->getByAssoc($assocType, $assocId);
175  while ($entry = $entries->next()) {
176  $this->deleteById($entry->getId());
177  }
178  }
179 
185  function changeUser($oldUserId, $newUserId) {
186  return $this->update(
187  'UPDATE event_log SET user_id = ? WHERE user_id = ?',
188  array((int) $newUserId, (int) $oldUserId)
189  );
190  }
191 
196  function getInsertId() {
197  return $this->_getInsertId('event_log', 'log_id');
198  }
199 }
200 
201 
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
DAO\retrieveRange
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
EventLogDAO\insertObject
insertObject($entry)
Definition: EventLogDAO.inc.php:114
DAO\convertToDB
convertToDB($value, &$type)
Definition: DAO.inc.php:401
EventLogDAO\deleteById
deleteById($logId, $assocType=null, $assocId=null)
Definition: EventLogDAO.inc.php:154
DAO\getAffectedRows
getAffectedRows()
Definition: DAO.inc.php:264
EventLogDAO\getByAssoc
getByAssoc($assocType, $assocId, $rangeInfo=null)
Definition: EventLogDAO.inc.php:58
EventLogDAO\changeUser
changeUser($oldUserId, $newUserId)
Definition: EventLogDAO.inc.php:185
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
DAO\convertFromDB
convertFromDB($value, $type)
Definition: DAO.inc.php:341
EventLogDAO
Class for inserting/accessing event log entries.
Definition: EventLogDAO.inc.php:20
DAO\datetimeFromDB
datetimeFromDB($dt)
Definition: DAO.inc.php:319
EventLogDAO\build
build($row)
Definition: EventLogDAO.inc.php:81
EventLogDAO\getInsertId
getInsertId()
Definition: EventLogDAO.inc.php:196
EventLogDAO\newDataObject
newDataObject()
Definition: EventLogDAO.inc.php:72
EventLogDAO\deleteByAssoc
deleteByAssoc($assocType, $assocId)
Definition: EventLogDAO.inc.php:173
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
DAO\datetimeToDB
datetimeToDB($dt)
Definition: DAO.inc.php:299
DAO\_getInsertId
_getInsertId($table='', $id='')
Definition: DAO.inc.php:255
EventLogDAO\getById
getById($logId, $assocType=null, $assocId=null)
Definition: EventLogDAO.inc.php:29
EventLogEntry
Describes an entry in the event log.
Definition: EventLogEntry.inc.php:21
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31