File indexing completed on 2024-05-26 05:59:14

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Default_Model_DbTable_MediaViews extends Local_Model_Table
0024 {
0025     const MEDIA_TYPE_VIDEO = 1;
0026     const MEDIA_TYPE_MUSIC = 2;
0027     const MEDIA_TYPE_BOOK = 3;
0028     /** @var  Zend_Cache_Core */
0029     protected $cache;
0030     protected $_name = "media_views";
0031     protected $_keyColumnsForRow = array('media_view_id');
0032     protected $_key = 'media_view_id';
0033 
0034     public function getNewId()
0035     {
0036         $result = $this->getAdapter()->query('SELECT UUID_SHORT()')->fetch();
0037 
0038         return $result['UUID_SHORT()'];
0039     }
0040 
0041     /**
0042      * @inheritDoc
0043      */
0044     public function init()
0045     {
0046         parent::init(); // TODO: Change the autogenerated stub
0047         $this->cache = Zend_Registry::get('cache');
0048     }
0049 
0050     public function fetchCountViewsTodayForFile($collection_id, $file_id)
0051     {
0052         if (empty($collection_id)) {
0053             return 0;
0054         }
0055 
0056         $today = (new DateTime())->modify('-1 day');
0057         $filterDownloadToday = $today->format("Y-m-d H:i:s");
0058 
0059         $sql = "    SELECT COUNT(1) AS cnt
0060                     FROM media_views f
0061                     WHERE f.collection_id = " . $collection_id . "
0062                     AND f.file_id = " . $file_id . "
0063                     AND f.start_timestamp >= '" . $filterDownloadToday . "'               
0064                    ";
0065         $result = $this->_db->query($sql)->fetchAll();
0066 
0067         return $result[0]['cnt'];
0068     }
0069 
0070 
0071     public function fetchCountViewsForFileAllTime($collectionId, $file_id)
0072     {
0073         if (empty($file_id) || empty($collectionId)) {
0074             return 0;
0075         }
0076 
0077         $sql = "    SELECT COUNT(1)  AS cnt
0078                     FROM media_views f
0079                     WHERE f.collection_id = " . $collectionId . " 
0080                     AND f.file_id = " . $file_id . "
0081                    ";
0082         $result = $this->_db->query($sql)->fetchAll();
0083 
0084         return $result[0]['cnt'];
0085     }
0086 
0087     public function fetchCountViewsTodayForProject($project_id)
0088     {
0089         if (empty($project_id)) {
0090             return 0;
0091         }
0092 
0093         $today = (new DateTime())->modify('-1 day');
0094         $filterDownloadToday = $today->format("Y-m-d H:i:s");
0095 
0096         $sql = "    SELECT COUNT(1) AS cnt
0097                     FROM media_views f
0098                     WHERE f.project_id = " . $project_id . "
0099                     AND f.start_timestamp >= '" . $filterDownloadToday . "'               
0100                    ";
0101         $result = $this->_db->query($sql)->fetchAll();
0102 
0103         return $result[0]['cnt'];
0104     }
0105 
0106 
0107     public function fetchCountViewsForProjectAllTime($project_id)
0108     {
0109         if (empty($project_id)) {
0110             return 0;
0111         }
0112 
0113         $sql = "    SELECT COUNT(1)  AS cnt
0114                     FROM media_views f
0115                     WHERE f.project_id = " . $project_id . "
0116                    ";
0117         $result = $this->_db->query($sql)->fetchAll();
0118 
0119         return $result[0]['cnt'];
0120     }
0121 
0122 }