/application/models/ContentViews.php

https://github.com/manubamba/site · PHP · 229 lines · 106 code · 34 blank · 89 comment · 11 complexity · c205493141e01d78951705bd3a6185ae MD5 · raw file

  1. <?php
  2. /**
  3. * ContentViews -> ContentViews database model for content views table.
  4. *
  5. * Copyright (c) <2009>, Joel Peltonen
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  11. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
  15. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *
  17. * License text found in /license/
  18. */
  19. /**
  20. * ContentRatings - class
  21. *
  22. * @package models
  23. * @author Joel Peltonen
  24. * @copyright 2009 Joel Peltonen
  25. * @license GPL v2
  26. * @version 1.0
  27. */
  28. class Default_Model_ContentViews extends Zend_Db_Table_Abstract
  29. {
  30. // Table name
  31. protected $_name = 'cnt_views_vws';
  32. // Table reference map
  33. protected $_referenceMap = array(
  34. 'Content' => array(
  35. 'columns' => array('id_cnt_vws'),
  36. 'refTableClass' => 'Default_Model_Content',
  37. 'refColumns' => array('id_cnt')
  38. ),
  39. 'User' => array(
  40. 'columns' => array('id_usr_vws'),
  41. 'refTableClass' => 'Default_Model_User',
  42. 'refColumns' => array('id_usr')
  43. ),
  44. );
  45. /**
  46. * getByContentId
  47. *
  48. * Gets views for specific content
  49. *
  50. * @param integer $id Id value of content, which view count is wanted.
  51. * @return int rating
  52. */
  53. public function getViewsByContentId($id = 0)
  54. {
  55. $return = 0;
  56. // Find rating
  57. $select = $this->select()->from($this, array('viewCount' => 'SUM(views_vws)'))
  58. ->where('id_cnt_vws = ?', $id)
  59. ->group('id_cnt_vws');
  60. $rowset = $this->fetchAll($select);
  61. // If rating found, return value
  62. if(count($rowset) >= 1) {
  63. /*
  64. $views_data = $rowset->toArray();
  65. for ($i = 0; $i<count($rowset); $i++) {
  66. $return += $views_data[$i]['views_vws'];
  67. }
  68. */
  69. $return = $rowset->toArray();
  70. return $return[0]['viewCount'];
  71. // Zend_Debug::dump($return, $label=null, $echo=true); die;
  72. } else {
  73. return 1;
  74. }
  75. }
  76. /**
  77. * getViewsByContentIdAndUserId
  78. *
  79. * Gets views for specific content by content id and user id
  80. *
  81. * @param integer $id Id value of content, which view count is wanted.
  82. * @return int rating
  83. */
  84. public function getViewsByContentIdAndUserId($cnt_id = 0, $usr_id = 0)
  85. {
  86. $return = 0;
  87. // Find rating
  88. $select = $this->select()->where('id_cnt_vws = ?', $cnt_id)
  89. ->where('id_usr_vws = ?', $usr_id);
  90. $rowset = $this->fetchAll($select);
  91. // If rating found, return value
  92. if(count($rowset) == 1) {
  93. $view_data = $rowset->toArray();
  94. $return = $view_data['0']['views_vws'];
  95. }
  96. return $return;
  97. }
  98. /**
  99. * increaseViewCount
  100. *
  101. * Incerease contents view count
  102. *
  103. * @param int $id id of content
  104. * @return boolean success
  105. */
  106. function increaseViewCount($id = 0)
  107. {
  108. $return = false;
  109. // set $userid to represent correct identity or 0 for anonymous
  110. $auth = Zend_Auth::getInstance();
  111. if ($auth->hasIdentity()) {
  112. $userid = (int)$auth->getIdentity()->user_id;
  113. } else {
  114. $userid = 0;
  115. }
  116. // echo $userid . " user ja content: " . $id; die;
  117. // echo $this->getViewsByContentIdAndUserId($id, $userid); die;
  118. // If this is the first view of this user for this content - if not
  119. if ($this->getViewsByContentIdAndUserId($id, $userid) == 0){
  120. $row = $this->createRow();
  121. $row->id_usr_vws = $userid;
  122. $row->id_cnt_vws = $id;
  123. $row->views_vws = 1;
  124. $row->modified_vws = new Zend_Db_Expr('NOW()');
  125. //try {
  126. @$row->save();
  127. /*}
  128. catch (Exception $e)
  129. {
  130. //echo "<pre>";
  131. print_r($e);
  132. //echo "</pre>";
  133. }*/
  134. $return = true;
  135. } else {
  136. $rowset = $this->fetchAll($this
  137. ->select()
  138. ->where('id_cnt_vws = ?', $id)
  139. ->where('id_usr_vws = ?', $userid)
  140. );
  141. $row = $rowset->current();
  142. $row->views_vws += 1;
  143. $row->modified_vws = new Zend_Db_Expr('NOW()');
  144. // Zend_Debug::dump($row, $label=null, $echo=true); die;
  145. $row->save();
  146. $return = true;
  147. }
  148. return $return;
  149. } // end of increaseViewCount
  150. /**
  151. * removeContentViews
  152. * Removes content views row
  153. *
  154. * @param int id_cnt_vws
  155. * @author Mikko Korpinen
  156. */
  157. public function removeContentViews($id_cnt_vws = 0)
  158. {
  159. $where = $this->getAdapter()->quoteInto('id_cnt_vws = ?', $id_cnt_vws);
  160. if ($this->delete($where)) {
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. } // end of removeContentViews
  166. public function getUserViewedContents($id_usr, $limit = 10) {
  167. $select = $this->select()->from($this, 'id_cnt_vws')
  168. ->setIntegrityCheck(false)
  169. ->joinLeft('cnt_has_usr', 'id_cnt_vws = id_cnt', array())
  170. ->where('id_usr != ?', $id_usr)
  171. ->where('id_usr_vws = ?', $id_usr)
  172. ->where('modified_vws is not null')
  173. ->order('modified_vws DESC')
  174. ->limit($limit);
  175. $rowset = $this->fetchAll($select);
  176. $contentModel = new Default_Model_Content();
  177. return $contentModel->getContentRows($rowset->toArray(), 'id_cnt_vws', true);
  178. }
  179. public function getContentViewers($id_cnt, $limit = 10) {
  180. $select = $this->select()->from($this, array('id_usr' => 'id_usr_vws'))
  181. ->where('id_cnt_vws = ?', $id_cnt)
  182. ->where('id_usr_vws != 0')
  183. ->limit($limit);
  184. $rowset = $this->fetchAll($select);
  185. //$results = array();
  186. /*foreach ($rowset as $row) {
  187. $results[] = $row->id_usr_vws;
  188. }*/
  189. $usrModel = new Default_Model_User();
  190. //Zend_Debug::dump($usrModel->getUserInfo($rowset->toArray())); die;
  191. return $usrModel->getUserInfo($rowset->toArray());
  192. }
  193. } // end of class
  194. ?>