RatingHandler::already_rated()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 4
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php namespace XoopsModules\Smartobject;
2
3
/*
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright    XOOPS Project https://xoops.org/
15
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @package
17
 * @since
18
 * @author     XOOPS Development Team
19
 */
20
21
use XoopsModules\Smartobject;
22
23
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
24
25
//require_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobject.php';
26
//require_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartplugins.php';
27
28
29
/**
30
 * Class SmartobjectRatingHandler
31
 */
32
class RatingHandler extends Smartobject\PersistableObjectHandler
33
{
34
    public $_rateOptions = [];
35
    public $_moduleList  = false;
36
    public $pluginsObject;
37
38
    /**
39
     * SmartobjectRatingHandler constructor.
40
     * @param \XoopsDatabase $db
41
     */
42
    public function __construct(\XoopsDatabase $db)
43
    {
44
        parent::__construct($db, Rating::class, 'ratingid', 'rate', '', 'smartobject');
45
        $this->generalSQL = 'SELECT * FROM ' . $this->table . ' AS ' . $this->_itemname . ' INNER JOIN ' . $this->db->prefix('users') . ' AS user ON ' . $this->_itemname . '.uid=user.uid';
0 ignored issues
show
Documentation Bug introduced by
The property $generalSQL was declared of type boolean, but 'SELECT * FROM ' . $this...mname . '.uid=user.uid' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
46
47
        $this->_rateOptions[1] = 1;
48
        $this->_rateOptions[2] = 2;
49
        $this->_rateOptions[3] = 3;
50
        $this->_rateOptions[4] = 4;
51
        $this->_rateOptions[5] = 5;
52
53
        $this->pluginsObject = new PluginHandler();
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public function getModuleList()
60
    {
61
        if (!$this->_moduleList) {
62
            $moduleArray          = $this->pluginsObject->getPluginsArray();
63
            $this->_moduleList[0] = _CO_SOBJECT_MAKE_SELECTION;
64
            foreach ($moduleArray as $k => $v) {
65
                $this->_moduleList[$k] = $v;
66
            }
67
        }
68
69
        return $this->_moduleList;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getRateList()
76
    {
77
        return $this->_rateOptions;
78
    }
79
80
    /**
81
     * @param $itemid
82
     * @param $dirname
83
     * @param $item
84
     * @return int
85
     */
86
    public function getRatingAverageByItemId($itemid, $dirname, $item)
87
    {
88
        $sql    = 'SELECT AVG(rate), COUNT(ratingid) FROM ' . $this->table . " WHERE itemid=$itemid AND dirname='$dirname' AND item='$item' GROUP BY itemid";
89
        $result = $this->db->query($sql);
90
        if (!$result) {
91
            return 0;
92
        }
93
        list($average, $sum) = $this->db->fetchRow($result);
94
        $ret['average'] = isset($average) ? $average : 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
95
        $ret['sum']     = isset($sum) ? $sum : 0;
96
97
        return $ret;
98
    }
99
100
    /**
101
     * @param $item
102
     * @param $itemid
103
     * @param $dirname
104
     * @param $uid
105
     * @return bool
106
     */
107
    public function already_rated($item, $itemid, $dirname, $uid)
108
    {
109
        $criteria = new \CriteriaCompo();
110
        $criteria->add(new \Criteria('item', $item));
111
        $criteria->add(new \Criteria('itemid', $itemid));
112
        $criteria->add(new \Criteria('dirname', $dirname));
113
        $criteria->add(new \Criteria('user.uid', $uid));
114
115
        $ret =& $this->getObjects($criteria);
116
117
        if (!$ret) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ret of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
118
            return false;
119
        } else {
120
            return $ret[0];
121
        }
122
    }
123
}
124