Passed
Pull Request — master (#18)
by Michael
04:31
created

LogHandler::deleteByPollId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace XoopsModules\Xoopspoll;
4
5
/*
6
               XOOPS - PHP Content Management System
7
                   Copyright (c) 2000-2020 XOOPS.org
8
                      <https://xoops.org>
9
 This program is free software; you can redistribute it and/or modify
10
 it under the terms of the GNU General Public License as published by
11
 the Free Software Foundation; either version 2 of the License, or
12
 (at your option) any later version.
13
14
 You may not change or alter any portion of this comment or credits
15
 of supporting developers from this source code or any supporting
16
 source code which is considered copyrighted (c) material of the
17
 original comment or credit authors.
18
19
 This program is distributed in the hope that it will be useful,
20
 but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 GNU General Public License for more details.
23
24
 You should have received a copy of the GNU General Public License
25
 along with this program; if not, write to the Free Software
26
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
27
*/
28
/**
29
 * Log class for the XoopsPoll Module
30
 *
31
 * @copyright ::  {@link https://xoops.org/ XOOPS Project}
32
 * @license   ::  {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
33
 * @package   ::  xoopspoll
34
 * @subpackage::  class
35
 * @since     ::  1.40
36
 * @author    ::  {@link http://www.myweb.ne.jp/ Kazumi Ono (AKA onokazu)}
37
 **/
38
39
40
41
use XoopsModules\Xoopspoll;
42
43
/**
44
 * Class LogHandler
45
 */
46
class LogHandler extends \XoopsPersistableObjectHandler
47
{
48
    /**
49
     * LogHandler::__construct()
50
     *
51
     * @param null|\XoopsDatabase $db
52
     **/
53
    public function __construct(\XoopsDatabase $db = null)
54
    {
55
        parent::__construct($db, 'xoopspoll_log', Log::class, 'log_id');
56
    }
57
58
    /**
59
     * LogHandler::LogHandler()
60
     *
61
     * @param mixed $db
62
     **/
63
    public function LogHandler($db)
64
    {
65
        $this->__construct($db);
66
    }
67
68
    /**
69
     * Delete all log entries by Option ID
70
     * @param int $option_id
71
     * @return bool $success
72
     */
73
    public function deleteByOptionId($option_id)
74
    {
75
        $criteria = new \Criteria('option_id', $option_id, '=');
76
        $success  = $this->deleteAll($criteria) ? true : false;
77
78
        return $success;
79
    }
80
81
    /**
82
     * Delete all log entries by Poll ID
83
     * @param int $pid
84
     * @return bool $success
85
     * @uses CriteriaCompo
86
     */
87
    public function deleteByPollId($pid)
88
    {
89
        $criteria = new \Criteria('poll_id', (int)$pid, '=');
90
        $success  = $this->deleteAll($criteria) ? true : false;
91
92
        return $success;
93
    }
94
95
    /**
96
     * Gets all log entries by Poll ID
97
     * @param int    $pid
98
     * @param string $sortby  sort all results by this field
99
     * @param string $orderby sort order (ASC, DESC)
100
     * @return array $success
101
     * @uses CriteriaCompo
102
     */
103
    public function getAllByPollId($pid, $sortby = 'time', $orderby = 'ASC')
104
    {
105
        $ret      = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
106
        $criteria = new \CriteriaCompo();
107
        $criteria->add(new \Criteria('poll_id', (int)$pid, '='));
108
        $criteria->setSort($sortby);
109
        $criteria->setOrder($orderby);
110
        $ret = &$this->getAll($criteria);
111
112
        return $ret;
113
    }
114
115
    /**
116
     * Get the total number of votes by the Poll ID
117
     * @param int $pid
118
     * @return int
119
     * @uses CriteriaCompo
120
     */
121
    public function getTotalVotesByPollId($pid)
122
    {
123
        $criteria = new \Criteria('poll_id', (int)$pid, '=');
124
        $numVotes = $this->getCount($criteria);
125
126
        return $numVotes;
127
    }
128
129
    /**
130
     * Get the total number of voters for a specific Poll
131
     * @param int $pid
132
     * @return int
133
     * @uses CriteriaCompo
134
     */
135
    public function getTotalVotersByPollId($pid)
136
    {
137
        $criteria = new \CriteriaCompo();
138
        $criteria->add(new \Criteria('poll_id', (int)$pid, '='));
139
        $criteria->setGroupBy('ip');
140
        $voterGrps = $this->getCount($criteria);
141
        $numVoters = \count($voterGrps);
0 ignored issues
show
Bug introduced by
$voterGrps of type integer is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
        $numVoters = \count(/** @scrutinizer ignore-type */ $voterGrps);
Loading history...
142
143
        return $numVoters;
144
    }
145
146
    /**
147
     * Get the total number of votes for an option
148
     * @param int $option_id
149
     * @return int
150
     * @uses CriteriaCompo
151
     */
152
    public function getTotalVotesByOptionId($option_id)
153
    {
154
        $criteria = new \Criteria('option_id', (int)$option_id, '=');
155
        $votes    = $this->getCount($criteria);
156
157
        return $votes;
158
    }
159
160
    /**
161
     * hasVoted indicates if user (logged in or not) has voted in a poll
162
     * @param int    $pid of the poll the check
163
     * @param string $ip  the ip address for this voter
164
     * @param int    $uid the XOOPS user id of this voter (0 for anon)
165
     * @return bool
166
     * @uses $_COOKIE
167
     */
168
    public function hasVoted($pid, $ip, $uid = 0)
169
    {
170
        $uid   = (int)$uid;
171
        $pid   = (int)$pid;
172
        $voted = true;
173
        //        xoops_load('pollUtility', 'xoopspoll');
174
        $voted_polls = Xoopspoll\Utility::getVoteCookie();
175
        //        $voted_polls = [];  //TESTING HACK TO BYPASS COOKIES
176
        $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll');
177
        $pollObj     = $pollHandler->get($pid);
178
        if ($pollObj) {
0 ignored issues
show
introduced by
$pollObj is of type XoopsObject, thus it always evaluated to true.
Loading history...
179
            $pollStarttime = $pollObj->getVar('start_time');
180
            $criteria      = new \CriteriaCompo();
181
            $criteria->add(new \Criteria('poll_id', $pid, '='));
182
            if ($uid > 0) {
183
                /**
184
                 *  {@internal check to see if vote was from before poll was started
185
                 *  and if so allow voting. This allows voting if poll is restarted
186
                 *  with new start date or if module is uninstalled and re-installed.}
187
                 */
188
                $criteria->add(new \Criteria('user_id', $uid, '='));
189
                $criteria->add(new \Criteria('time', (int)$pollStarttime, '>='));
190
                $vCount = $this->getCount($criteria);
191
                $voted  = $vCount > 0;
192
            } elseif (!empty($ip) && \filter_var($ip, \FILTER_VALIDATE_IP)) {
193
                $criteria->add(new \Criteria('ip', $ip, '='));
194
                $criteria->add(new \Criteria('time', (int)$pollStarttime, '>='));
195
                $criteria->add(new \Criteria('user_id', 0, '='));
196
                $vCount = $this->getCount($criteria);
197
                $voted  = $vCount > 0;
198
            } else {
199
                /* Check cookie to see if someone from this system has voted before */
200
                if (\array_key_exists($pid, $voted_polls) && ((int)$voted_polls[$pid] >= $pollStarttime)) {
201
                    $criteria = new \CriteriaCompo();
202
                    $criteria->add(new \Criteria('poll_id', $pid, '='));
203
                    $criteria->add(new \Criteria('time', (int)$pollStarttime, '>='));
204
                    $vCount = $this->getCount($criteria);
205
                    $voted  = $vCount > 0;
206
                } else {
207
                    $voted = false;
208
                }
209
            }
210
        }
211
212
        return $voted;
213
    }
214
}
215