Open Monograph Press  3.3.0
ViewsDAO.inc.php
1 <?php
2 
16 define('RECORD_VIEW_RESULT_FAIL', 0);
17 define('RECORD_VIEW_RESULT_EXISTING', 1);
18 define('RECORD_VIEW_RESULT_INSERTED', 2);
19 
20 class ViewsDAO extends DAO {
21 
29  public function recordView($assocType, $assocId, $userId) {
30  return $this->replace(
31  'item_views',
32  array(
33  'date_last_viewed' => strftime('%Y-%m-%d %H:%M:%S'),
34  'assoc_type' => (int) $assocType,
35  'assoc_id' => $assocId,
36  'user_id' => (int) $userId
37  ),
38  array('assoc_type', 'assoc_id', 'user_id')
39  );
40  }
41 
49  public function getLastViewDate($assocType, $assocId, $userId = null) {
50  $params = array((int)$assocType, $assocId);
51  if ($userId) $params[] = (int)$userId;
52  $result = $this->retrieve(
53  'SELECT date_last_viewed
54  FROM item_views
55  WHERE assoc_type = ?
56  AND assoc_id = ?' .
57  ($userId ? ' AND user_id = ?' : ''),
58  $params
59  );
60  return (isset($result->fields[0])) ? $result->fields[0] : false;
61  }
62 
69  public function moveViews($assocType, $oldAssocId, $newAssocId) {
70  return $this->update(
71  'UPDATE item_views SET assoc_id = ? WHERE assoc_type = ? AND assoc_id = ?',
72  array($newAssocId, (int)$assocType, $oldAssocId)
73  );
74  }
75 
81  public function deleteViews($assocType, $assocId) {
82  return $this->update(
83  'DELETE FROM item_views WHERE assoc_type = ? AND assoc_id = ?',
84  array((int)$assocType, $assocId)
85  );
86  }
87 }
88 
ViewsDAO\deleteViews
deleteViews($assocType, $assocId)
Definition: ViewsDAO.inc.php:81
ViewsDAO\getLastViewDate
getLastViewDate($assocType, $assocId, $userId=null)
Definition: ViewsDAO.inc.php:49
ViewsDAO\moveViews
moveViews($assocType, $oldAssocId, $newAssocId)
Definition: ViewsDAO.inc.php:69
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
ViewsDAO\recordView
recordView($assocType, $assocId, $userId)
Definition: ViewsDAO.inc.php:29
ViewsDAO
Class for keeping track of item views.
Definition: ViewsDAO.inc.php:20
DAO\replace
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:243
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31