Passed
Branch master (160f79)
by Michael
02:51
created

VoteHandler::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace XoopsModules\Publisher;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * Publisher module for xoops
17
 *
18
 * @copyright      module for xoops
19
 * @license        GPL 3.0 or later
20
 * @package        Publisher
21
 * @since          1.0
22
 * @min_xoops      2.5.10
23
 * @author         XOOPS Development Team
24
 */
25
26
/** @var Helper $helper */
27
28
\defined('XOOPS_ROOT_PATH') || exit('Restricted access');
29
30
/**
31
 * Class Object VoteHandler
32
 */
33
class VoteHandler extends \XoopsPersistableObjectHandler
34
{
35
    private const TABLE = 'publisher_rating';
36
    private const ENTITY = Vote::class;
37
    private const ENTITYNAME = 'Vote';
38
    private const KEYNAME = 'ratingid';
39
    private const IDENTIFIER = 'itemid';
40
    private const SOURCE = 'source';
41
42
    /**
43
     * @var Helper
44
     */
45
    public $helper;
46
47
    /**
48
     * Constructor
49
     * @param \XoopsDatabase $db
50
     * @param Helper $helper
51
     */
52
    public function __construct(\XoopsDatabase $db = null, Helper $helper = null)
53
    {
54
        $this->db = $db;
55
        /** @var Helper $this->helper */
56
        $this->helper = $helper ?? Helper::getInstance();
57
58
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
59
    }
60
61
    /**
62
     * get inserted id
63
     *
64
     * @param null
65
     * @return int reference to the {@link Get} object
66
     */
67
    public function getInsertId(): int
68
    {
69
        return $this->db->getInsertId();
70
    }
71
72
    /**
73
     * Get Rating per item in the database
74
     * @param int|null $itemId
75
     * @param int|null $source
76
     * @return array
77
     */
78
    public function getItemRating($itemId = null, $source = null): array
79
    {
80
        $itemId    = $itemId ?? 0;
81
        $source    = $source ?? 0;
82
        $helper    = Helper::getInstance();
83
        $xoopsUser = $GLOBALS['xoopsUser'];
84
85
        $itemRating            = [];
86
        $itemRating['nb_vote'] = 0;
87
        $uid                   = \is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
88
        $voted                 = false;
89
        $ip                    = \getenv('REMOTE_ADDR');
90
        $currentRating         = 0;
91
        $count                 = 0;
92
93
        $max_units   = 10;
94
        $ratingBars  = (int)$helper->getConfig('ratingbars');
95
        $ratingArray = [Constants::RATING_5STARS, Constants::RATING_10STARS, Constants::RATING_10NUM];
96
97
        if (in_array($ratingBars, $ratingArray)) {
98
            $rating_unitwidth = 25;
99
            if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')) {
100
                $max_units = 5;
101
            }
102
103
            $criteria = new \CriteriaCompo();
104
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
105
            $criteria->add(new \Criteria(static::SOURCE, $source));
106
107
            $voteObjs              = $helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler or XoopsModules\Publisher\PermissionHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

107
            $voteObjs              = $helper->getHandler(static::ENTITYNAME)->/** @scrutinizer ignore-call */ getObjects($criteria);
Loading history...
108
            $count                 = \count($voteObjs);
109
            $itemRating['nb_vote'] = $count;
110
111
            foreach ($voteObjs as $voteObj) {
112
                $currentRating += $voteObj->getVar('rate');
113
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
114
                    $voted            = true;
115
                    $itemRating['id'] = $voteObj->getVar('ratingid');
116
                }
117
            }
118
            unset($criteria);
119
120
            $itemRating['avg_rate_value'] = 0;
121
            if ($count > 0) {
122
                $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2);
123
            }
124
            if (1 == $count) {
125
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_1);
126
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_1);
127
            } else {
128
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_X);
129
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_X);
130
            }
131
            $text                    = \str_replace('%m', $max_units, $text);
132
            $text                    = \str_replace('%t', $itemRating['nb_vote'], $text);
133
            $shorttext               = \str_replace('%t', $itemRating['nb_vote'], $shorttext);
134
            $itemRating['text']      = $text;
135
            $itemRating['shorttext'] = $shorttext;
136
            $itemRating['size']      = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px';
137
            $itemRating['maxsize']   = ($max_units * $rating_unitwidth) . 'px';
138
139
            $itemRating['ip']    = $ip;
140
            $itemRating['uid']   = $uid;
141
            $itemRating['voted'] = $voted;
142
            // YouTube Liking  ==========================================
143
        } elseif (Constants::RATING_LIKES === (int)$helper->getConfig('ratingbars')) {
144
            // get count of "dislikes"
145
            $criteria = new \CriteriaCompo();
146
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
147
            $criteria->add(new \Criteria(static::SOURCE, $source));
148
            $criteria->add(new \Criteria('rate', 0, '<'));
149
150
            $voteObjs = $helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
151
            $count    = \count($voteObjs);
152
153
            foreach ($voteObjs as $voteObj) {
154
                $currentRating += $voteObj->getVar('rate');
155
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
156
                    $voted            = true;
157
                    $itemRating['id'] = $voteObj->getVar('ratingid');
158
                }
159
            }
160
            unset($criteria);
161
            $itemRating['dislikes'] = $count;
162
163
            // get count of "likes"
164
            $criteria = new \CriteriaCompo();
165
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
166
            $criteria->add(new \Criteria(static::SOURCE, $source));
167
            $criteria->add(new \Criteria('rate', 0, '>'));
168
169
            $voteObjs      = $helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
170
            $count         = \count($voteObjs);
171
            $currentRating = 0;
172
            foreach ($voteObjs as $voteObj) {
173
                $currentRating += $voteObj->getVar('rate');
174
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
175
                    $voted            = true;
176
                    $itemRating['id'] = $voteObj->getVar('ratingid');
177
                }
178
            }
179
            unset($criteria);
180
            $itemRating['likes'] = $count;
181
182
            $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes'];
183
            $itemRating['ip']      = $ip;
184
            $itemRating['uid']     = $uid;
185
            $itemRating['voted']   = $voted;
186
            // Facebook Reactions  ==========================================
187
        } elseif (Constants::RATING_REACTION === (int)$helper->getConfig('ratingbars')) {
188
            $criteria = new \CriteriaCompo();
189
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
190
            $criteria->add(new \Criteria(static::SOURCE, $source));
191
            $criteria->add(new \Criteria('rate', 0, '<'));
192
193
            $voteObjs              = $helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
194
            $count                 = \count($voteObjs);
195
            $itemRating['nb_vote'] = $count;
196
197
            foreach ($voteObjs as $voteObj) {
198
                $currentRating += $voteObj->getVar('rate');
199
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
200
                    $voted            = true;
201
                    $itemRating['id'] = $voteObj->getVar('ratingid');
202
                }
203
            }
204
            unset($criteria);
205
            $itemRating['dislikes'] = $count;
206
207
            $criteria = new \CriteriaCompo();
208
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
209
            $criteria->add(new \Criteria(static::SOURCE, $source));
210
            $criteria->add(new \Criteria('rate', 0, '>'));
211
212
            $voteObjs      = $helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
213
            $count         = \count($voteObjs);
214
            $currentRating = 0;
215
            foreach ($voteObjs as $voteObj) {
216
                $currentRating += $voteObj->getVar('rate');
217
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
218
                    $voted            = true;
219
                    $itemRating['id'] = $voteObj->getVar('ratingid');
220
                }
221
            }
222
            unset($criteria);
223
            $itemRating['likes'] = $count;
224
225
            $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes'];
226
            $itemRating['ip']      = $ip;
227
            $itemRating['uid']     = $uid;
228
            $itemRating['voted']   = $voted;
229
        } else {
230
            $itemRating['uid']     = $uid;
231
            $itemRating['nb_vote'] = $count;
232
            $itemRating['voted']   = $voted;
233
            $itemRating['ip']      = $ip;
234
        }
235
        return $itemRating;
236
    }
237
238
    /**
239
     * delete vote of given item
240
     * @param mixed $itemId
241
     * @param mixed $source
242
     * @return bool
243
     */
244
    public function deleteAllVote($itemId, $source): bool
245
    {
246
        $criteria = new \CriteriaCompo();
247
        $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
248
        $criteria->add(new \Criteria(static::SOURCE, $source));
249
250
        return $this->deleteAll($criteria);
251
    }
252
}
253