File indexing completed on 2024-12-22 05:33:28

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  **/
0022 class Backend_Model_Reports
0023 {
0024 
0025     public function getReportsForComments($startIndex = 0, $pageSize = 10, $orderBy = 'reports_comment.created_at DESC')
0026     {
0027         $sql = "select 
0028                     reports_comment.report_id,
0029                     reports_comment.project_id,
0030                     reports_comment.comment_id,
0031                     max(reports_comment.created_at) as last_reported_at,
0032                     comments.comment_active,
0033                     comments.comment_text,
0034                     comments.comment_created_at,
0035                     comments.comment_deleted_at,
0036                     count(reports_comment.comment_id) as counter
0037 
0038                 from reports_comment
0039                 straight_join comments on comments.comment_id = reports_comment.comment_id
0040                 straight_join member on member.member_id = comments.comment_member_id and member.is_active = 1
0041                 straight_join project on project.project_id = comments.comment_target_id and project.`status` = 100
0042                 group by reports_comment.comment_id
0043               ";
0044         $limit = ' limit ' . (int)$startIndex . ',' . (int)$pageSize;
0045         if(!isset($orderBy) || count($orderBy) == 0) {
0046             $orderBy = 'reports_comment.created_at DESC';
0047         }
0048         $orderBy = ' order by ' . $orderBy;
0049 
0050         $rowSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql.$orderBy.$limit);
0051         if (0 == count($rowSet)) {
0052             return array();
0053         }
0054 
0055         return $rowSet;
0056     }
0057 
0058     public function getTotalCountForReportedComments()
0059     {
0060         $sql = "select '1'
0061                 from reports_comment
0062                 straight_join comments on comments.comment_id = reports_comment.comment_id
0063                 group by reports_comment.comment_id
0064               ";
0065 
0066         $rowSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0067 
0068         return count($rowSet);
0069     }
0070 
0071     public function getReportsForProjects($startIndex = 0, $pageSize = 10, $orderBy = 'counter DESC, reports_project.project_id', $filterDeleted = 0)
0072     {
0073         $sql = "select reports_project.*, project.status, count(reports_project.project_id) as counter, max(reports_project.created_at) as last_report_date
0074                 from reports_project
0075                 straight_join project on project.project_id = reports_project.project_id
0076                 where reports_project.is_deleted = :deleted
0077                 group by reports_project.project_id
0078         ";
0079 
0080         $limit = ' limit ' . (int)$startIndex . ',' . (int)$pageSize;
0081         $orderBy = ' order by ' . $orderBy;
0082 
0083         $rowSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql.$orderBy.$limit, array('deleted' => $filterDeleted));
0084         if (0 == count($rowSet)) {
0085             return array();
0086         }
0087 
0088         return $rowSet;
0089     }
0090 
0091     public function getTotalCountForReportedProject($filterDeleted = 0)
0092     {
0093         $sql = "select '1'
0094                 from reports_project
0095                 straight_join project on project.project_id = reports_project.project_id
0096                 where reports_project.is_deleted = :deleted
0097                 group by reports_project.project_id
0098               ";
0099 
0100         $rowSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql, array('deleted' => $filterDeleted));
0101 
0102         return count($rowSet);
0103     }
0104 
0105     public function setDelete($projectId, $onlySpam = true)
0106     {
0107         $sql = "update reports_project set is_deleted = 1 where project_id = :projectId";
0108         
0109         if($onlySpam) {
0110             $sql .= " and report_type = 0";
0111         }
0112 
0113         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('projectId' => $projectId))->execute();
0114         $this->updateMaterializedView($projectId);
0115         return $result;
0116     }
0117     
0118     
0119     public function setReportAsValid($reportId)
0120     {
0121         $sql = "update reports_project set is_valid = 1 where report_id = :reportId";
0122 
0123         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('reportId' => $reportId))->execute();
0124         return $result;
0125     }
0126     
0127     public function setReportAsDeleted($reportId)
0128     {
0129         $sql = "update reports_project set is_deleted = 1 where report_id = :reportId";
0130 
0131         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('reportId' => $reportId))->execute();
0132         return $result;
0133     }
0134 
0135     private function updateMaterializedView($project_id)
0136     {
0137         $sql = "update stat_projects set amount_reports = 0 where project_id = :project_id";
0138         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('project_id' => $project_id))->execute();
0139     }
0140     
0141     public function saveNewFraud($project_id, $_authMemeber) 
0142     {
0143         $sql = "INSERT INTO reports_project (project_id, report_type, reported_by, is_valid, text) VALUES (:project_id, 1, :member_id, 1, :text)";
0144 
0145         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('project_id' => $project_id, 'member_id' => $_authMemeber->member_id, 'text' => 'Admin: moved from spam to misuse'));
0146         return $result;
0147     }
0148 
0149 }