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'; |
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; |
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) { |
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} else { |
120
|
|
|
return $ret[0]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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.