XOOPS  2.6.0
Stats.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Core\Kernel\Model;
13 
16 
29 {
37  public function getCount(CriteriaElement $criteria = null)
38  {
39  $qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder();
40  $eb = $qb->expr();
41 
42  $field = '';
43  $groupby = false;
44  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
45  $temp = $criteria->getGroupby();
46  if (!empty($temp)) {
47  $qb->select($temp);
48  $groupby = true;
49  }
50  }
51  if (!$groupby) {
52  $qb->select('COUNT(*)');
53  } else {
54  $qb->addSelect('COUNT(*)');
55  }
56 
57  $qb->from($this->handler->table, null);
58  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
59  $qb = $criteria->renderQb($qb);
60  }
61  try {
62  $result = $qb->execute();
63  if (!$result) {
64  return 0;
65  }
66  } catch (\Exception $e) {
67  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
68  return 0;
69  }
70 
71  if ($groupby == false) {
72  list ($count) = $result->fetch(\PDO::FETCH_NUM);
73  return $count;
74  } else {
75  $ret = array();
76  while (list ($id, $count) = $result->fetch(\PDO::FETCH_NUM)) {
77  $ret[$id] = $count;
78  }
79  return $ret;
80  }
81  }
82 
90  public function getCounts(CriteriaElement $criteria = null)
91  {
92  $qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder();
93  $eb = $qb->expr();
94 
95  $ret = array();
96  $sql_where = '';
97  $limit = null;
98  $start = null;
99  $groupby_key = $this->handler->keyName;
100  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
101  if ($groupby = $criteria->getGroupby()) {
102  $groupby_key = $groupby;
103  }
104  }
105  $qb->select($groupby_key)
106  ->addSelect('COUNT(*)')
107  ->from($this->handler->table, null);
108 
109  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
110  $qb = $criteria->renderQb($qb);
111  }
112  $result = $qb->execute();
113  if (!$result) {
114  return $ret;
115  }
116  while (list ($id, $count) = $result->fetch(\PDO::FETCH_NUM)) {
117  $ret[$id] = $count;
118  }
119  return $ret;
120  }
121 }
static getInstance()
Definition: Xoops.php:160
$result
Definition: pda.php:33
getCounts(CriteriaElement $criteria=null)
Definition: Stats.php:90
getCount(CriteriaElement $criteria=null)
Definition: Stats.php:37
$id
Definition: admin_menu.php:36
$limit
Definition: findusers.php:202
$criteria
$start