Passed
Push — master ( 753352...529310 )
by Goffy
04:13
created

RatingsHandler::getRatingsCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Modulebuilder;
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
 * Modulebuilder module for xoops
17
 *
18
 * @copyright      module for xoops
19
 * @license        GPL 3.0 or later
20
 * @package        Modulebuilder
21
 * @since          1.0
22
 * @min_xoops      2.5.10
23
 * @author         XOOPS Development Team
24
 */
25
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
26
27
/**
28
 * Class Object RatingsHandler
29
 */
30
class RatingsHandler extends \XoopsPersistableObjectHandler
31
{
32
    /**
33
     * Constructor
34
     * @param \XoopsDatabase $db
35
     */
36
    public function __construct(\XoopsDatabase $db)
37
    {
38
        parent::__construct($db, 'modulebuilder_ratings', Ratings::class, 'rate_id', 'rate_itemid');
39
    }
40
41
    /**
42
     * @param bool $isNew
43
     *
44
     * @return Object
45
     */
46
    public function create($isNew = true)
47
    {
48
        return parent::create($isNew);
49
    }
50
51
    /**
52
     * retrieve a field
53
     *
54
     * @param int   $i field id
55
     * @param array $fields
56
     * @return mixed reference to the {@link Get} object
57
     */
58
    public function get($i = null, $fields = null)
59
    {
60
        return parent::get($i, $fields);
61
    }
62
63
    /**
64
     * get inserted id
65
     *
66
     * @param null
67
     * @return int reference to the {@link Get} object
68
     */
69
    public function getInsertId()
70
    {
71
        return $this->db->getInsertId();
72
    }
73
74
    /**
75
     * Get Rating per item in the database
76
     * @param int $itemid
77
     * @param int $source
78
     * @return array
79
     */
80
    public function getItemRating($itemid = 0, $source = 0)
81
    {
82
        $helper = \XoopsModules\Modulebuilder\Helper::getInstance();
83
84
        $ItemRating               = [];
85
        $ItemRating['nb_ratings'] = 0;
86
        $uid                      = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
87
        $voted                    = false;
88
        $ip                       = getenv('REMOTE_ADDR');
89
        $current_rating           = 0;
90
91
        if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')
0 ignored issues
show
Bug introduced by
The type XoopsModules\Modulebuilder\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
            || Constants::RATING_10STARS === (int)$helper->getConfig('ratingbars')
93
            || Constants::RATING_10NUM === (int)$helper->getConfig('ratingbars')) {
94
            $rating_unitwidth = 25;
95
            if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')) {
96
                $max_units = 5;
97
            } else {
98
                $max_units = 10;
99
            }
100
101
            $criteria = new \CriteriaCompo();
102
            $criteria->add(new \Criteria('rate_itemid', $itemid));
103
            $criteria->add(new \Criteria('rate_source', $source));
104
105
            $ratingObjs               = $helper->getHandler('ratings')->getObjects($criteria);
106
            $count                    = count($ratingObjs);
107
            $ItemRating['nb_ratings'] = $count;
108
109
            foreach ($ratingObjs as $ratingObj) {
110
                $current_rating += $ratingObj->getVar('rate_value');
111
                if (($ratingObj->getVar('rate_ip') == $ip && $uid == 0) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
112
                    $voted            = true;
113
                    $ItemRating['id'] = $ratingObj->getVar('rate_id');
114
                }
115
            }
116
            unset($ratingObj);
117
            unset($criteria);
118
119
            $ItemRating['avg_rate_value'] = 0;
120
            if ($count > 0) {
121
                $ItemRating['avg_rate_value'] = number_format($current_rating / $count, 2);
122
            }
123
            if (1 == $count) {
124
                $text      = str_replace('%c', $ItemRating['avg_rate_value'], _MA_MODULEBUILDER_RATING_CURRENT_1);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Modulebuild...UILDER_RATING_CURRENT_1 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
125
                $shorttext = str_replace('%c', $ItemRating['avg_rate_value'], _MA_MODULEBUILDER_RATING_CURRENT_SHORT_1);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Modulebuild..._RATING_CURRENT_SHORT_1 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
126
            } else {
127
                $text      = str_replace('%c', $ItemRating['avg_rate_value'], _MA_MODULEBUILDER_RATING_CURRENT_X);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Modulebuild...UILDER_RATING_CURRENT_X was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
                $shorttext = str_replace('%c', $ItemRating['avg_rate_value'], _MA_MODULEBUILDER_RATING_CURRENT_SHORT_X);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Modulebuild..._RATING_CURRENT_SHORT_X was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
129
            }
130
            $text                    = str_replace('%m', $max_units, $text);
131
            $text                    = str_replace('%t', $ItemRating['nb_ratings'], $text);
132
            $shorttext               = str_replace('%t', $ItemRating['nb_ratings'], $shorttext);
133
            $ItemRating['text']      = $text;
134
            $ItemRating['shorttext'] = $shorttext;
135
            $ItemRating['size']      = ($ItemRating['avg_rate_value'] * $rating_unitwidth) . 'px';
136
            $ItemRating['maxsize']   = ($max_units * $rating_unitwidth) . 'px';
137
        } else {
138
            $criteria = new \CriteriaCompo();
139
            $criteria->add(new \Criteria('rate_itemid', $itemid));
140
            $criteria->add(new \Criteria('rate_source', $source));
141
            $criteria->add(new \Criteria('rate_value', 0, '<'));
142
143
            $ratingObjs = $helper->getHandler('ratings')->getObjects($criteria);
144
            $count      = count($ratingObjs);
145
146
            foreach ($ratingObjs as $ratingObj) {
147
                $current_rating += $ratingObj->getVar('rate_value');
148
                if (($ratingObj->getVar('rate_ip') == $ip && $uid == 0) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
149
                    $voted            = true;
150
                    $ItemRating['id'] = $ratingObj->getVar('rate_id');
151
                }
152
            }
153
            unset($ratingObj);
154
            unset($criteria);
155
            $ItemRating['dislikes'] = $count;
156
157
            $criteria = new \CriteriaCompo();
158
            $criteria->add(new \Criteria('rate_itemid', $itemid));
159
            $criteria->add(new \Criteria('rate_source', $source));
160
            $criteria->add(new \Criteria('rate_value', 0, '>'));
161
162
            $ratingObjs     = $helper->getHandler('ratings')->getObjects($criteria);
163
            $count          = count($ratingObjs);
164
            $current_rating = 0;
165
            foreach ($ratingObjs as $ratingObj) {
166
                $current_rating += $ratingObj->getVar('rate_value');
167
                if (($ratingObj->getVar('rate_ip') == $ip && $uid == 0) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
168
                    $voted            = true;
169
                    $ItemRating['id'] = $ratingObj->getVar('rate_id');
170
                }
171
            }
172
            unset($ratingObj);
173
            unset($criteria);
174
            $ItemRating['likes'] = $count;
175
176
            $count = $ItemRating['likes'] + $ItemRating['dislikes'];
177
        }
178
179
        $ItemRating['uid']        = $uid;
180
        $ItemRating['nb_ratings'] = $count;
181
        $ItemRating['voted']      = $voted;
182
        $ItemRating['ip']         = $ip;
183
184
        return $ItemRating;
185
    }
186
187
    /**
188
     * delete ratings of given item
189
     * @param mixed $itemid
190
     * @param mixed $source
191
     * @return bool
192
     */
193
    public function deleteAllRatings($itemid, $source)
194
    {
195
        $criteria = new \CriteriaCompo();
196
        $criteria->add(new \Criteria('rate_itemid', $itemid));
197
        $criteria->add(new \Criteria('rate_source', $source));
198
199
        return $this->deleteAll($criteria);
200
    }
201
}
202