RatingHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wfdownloads;
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
 * Wfdownloads module
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @package         wfdownload
21
 * @since           3.23
22
 * @author          Xoops Development Team
23
 */
24
25
require_once \dirname(__DIR__) . '/include/common.php';
26
27
/**
28
 * Class RatingHandler
29
 */
30
class RatingHandler extends \XoopsPersistableObjectHandler
31
{
32
    /**
33
     * @access public
34
     */
35
    public $helper;
36
37
    /**
38
     * @param \XoopsDatabase|null $db
39
     */
40
    public function __construct(\XoopsDatabase $db = null)
41
    {
42
        parent::__construct($db, 'wfdownloads_votedata', Rating::class, 'ratingid');
43
        $this->helper = Helper::getInstance();
44
    }
45
46
    /**
47
     * Get average ratings of users matching a condition
48
     *
49
     * @param \CriteriaElement|\CriteriaCompo $criteria {@link \CriteriaElement} to match
50
     *
51
     * @return array|int
52
     */
53
    public function getUserAverage($criteria = null)
54
    {
55
        $groupby = false;
56
        $field   = '';
57
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
58
            if ('' != $criteria->groupby) {
59
                $groupby = true;
60
                $field   = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used
61
            }
62
        }
63
        $sql = "SELECT {$field} AVG(rating), count(*)";
64
        $sql .= " FROM {$this->table}";
65
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
66
            $sql .= ' ' . $criteria->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

66
            $sql .= ' ' . $criteria->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            if ('' != $criteria->groupby) {
68
                $sql .= $criteria->getGroupby();
69
            }
70
        }
71
        $result = $this->db->query($sql);
72
        if (!$result) {
73
            return 0;
74
        }
75
        if (false === $groupby) {
76
            [$average, $count] = $this->db->fetchRow($result);
77
78
            return [
79
                'avg'   => $average,
80
                'count' => $count,
81
            ];
82
        }
83
        $ret = [];
84
        while (list($id, $average, $count) = $this->db->fetchRow($result)) {
85
            $ret[$id] = [
86
                'avg'   => $average,
87
                'count' => $count,
88
            ];
89
        }
90
91
        return $ret;
92
    }
93
}
94