Issues (3083)

htdocs/class/model/stats.php (1 issue)

1
<?php
2
/**
3
 * Object stats handler class.
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
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
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @subpackage          model
16
 * @since               2.3.0
17
 * @author              Taiwen Jiang <[email protected]>
18
 */
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Object stats handler class.
23
 *
24
 * @author Taiwen Jiang <[email protected]>
25
 *
26
 * {@link XoopsModelAbstract}
27
 */
28
class XoopsModelStats extends XoopsModelAbstract
29
{
30
    /**
31
     * count objects matching a condition
32
     *
33
     * @param  CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} to match
34
     * @return int|array    count of objects
35
     */
36
    public function getCount(CriteriaElement $criteria = null)
37
    {
38
        $field   = '';
39
        $groupby = false;
40
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
41
            if ($criteria->groupby != '') {
42
                $groupby = true;
43
                $field   = $criteria->groupby . ', ';
44
            }
45
        }
46
        $sql = "SELECT {$field} COUNT(*) FROM `{$this->handler->table}`";
47
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
48
            $sql .= ' ' . $criteria->renderWhere();
49
            $sql .= $criteria->getGroupby();
50
        }
51
        $result = $this->handler->db->query($sql);
52
        if (!$this->handler->db->isResultSet($result)) {
53
            return 0;
54
        }
55
        if ($groupby == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
56
            list($count) = $this->handler->db->fetchRow($result);
57
58
            return (int) $count;
59
        } else {
60
            $ret = array();
61
            while (false !== (list($id, $count) = $this->handler->db->fetchRow($result))) {
62
                $ret[$id] = (int) $count;
63
            }
64
65
            return $ret;
66
        }
67
    }
68
69
    /**
70
     * get counts matching a condition
71
     *
72
     * @param  CriteriaElement|CriteriaCompo  $criteria {@link CriteriaElement} to match
73
     * @return array  of counts
74
     */
75
    public function getCounts(CriteriaElement $criteria = null)
76
    {
77
        $ret         = array();
78
        $sql_where   = '';
79
        $limit       = null;
80
        $start       = null;
81
        $groupby_key = $this->handler->keyName;
82
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
83
            $sql_where = $criteria->renderWhere();
84
            $limit     = $criteria->getLimit();
85
            $start     = $criteria->getStart();
86
            if ($groupby = $criteria->groupby) {
87
                $groupby_key = $groupby;
88
            }
89
        }
90
        $sql = "SELECT {$groupby_key}, COUNT(*) AS count" . " FROM `{$this->handler->table}`" . " {$sql_where}" . " GROUP BY {$groupby_key}";
91
        $result = $this->handler->db->query($sql, $limit, $start);
92
        if (!$this->handler->db->isResultSet($result)) {
93
            return $ret;
94
        }
95
        while (false !== (list($id, $count) = $this->handler->db->fetchRow($result))) {
96
            $ret[$id] = (int) $count;
97
        }
98
99
        return $ret;
100
    }
101
}
102