1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Publisher; |
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
|
|
|
* Publisher module for xoops |
17
|
|
|
* |
18
|
|
|
* @copyright module for xoops |
19
|
|
|
* @license GPL 3.0 or later |
20
|
|
|
* @since 1.0 |
21
|
|
|
* @min_xoops 2.5.10 |
22
|
|
|
* @author XOOPS Development Team |
23
|
|
|
*/ |
24
|
|
|
\defined('XOOPS_ROOT_PATH') || exit('Restricted access'); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Object RatingsHandler |
28
|
|
|
*/ |
29
|
|
|
class RatingsHandler extends \XoopsPersistableObjectHandler |
30
|
|
|
{ |
31
|
|
|
private const TABLE = 'publisher_liking'; |
32
|
|
|
private const ENTITY = Ratings::class; |
33
|
|
|
private const ENTITYNAME = 'Ratings'; |
34
|
|
|
private const KEYNAME = 'rate_id'; |
35
|
|
|
private const IDENTIFIER = 'rate_itemid'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor |
39
|
|
|
*/ |
40
|
|
|
public function __construct(?\XoopsDatabase $db) |
41
|
|
|
{ |
42
|
|
|
$this->db = $db; |
43
|
|
|
parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param bool $isNew |
48
|
|
|
* |
49
|
|
|
* @return \XoopsObject |
50
|
|
|
*/ |
51
|
|
|
public function create($isNew = true) |
52
|
|
|
{ |
53
|
|
|
return parent::create($isNew); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* retrieve a field |
58
|
|
|
* |
59
|
|
|
* @param int $i field id |
60
|
|
|
* @param array $fields |
61
|
|
|
* @return mixed reference to the {@link Get} object |
62
|
|
|
*/ |
63
|
|
|
public function get($i = null, $fields = null) |
64
|
|
|
{ |
65
|
|
|
return parent::get($i, $fields); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* get inserted id |
70
|
|
|
* |
71
|
|
|
* @return int reference to the {@link Get} object |
72
|
|
|
*/ |
73
|
|
|
public function getInsertId() |
74
|
|
|
{ |
75
|
|
|
return $this->db->getInsertId(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get Rating per item in the database |
80
|
|
|
* @param int $itemId |
81
|
|
|
* @param int $source |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getItemRating($itemId = 0, $source = 0) |
85
|
|
|
{ |
86
|
|
|
$helper = \XoopsModules\Publisher\Helper::getInstance(); |
87
|
|
|
|
88
|
|
|
$itemRating = []; |
89
|
|
|
$itemRating['nb_ratings'] = 0; |
90
|
|
|
$uid = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
91
|
|
|
$voted = false; |
92
|
|
|
$ip = \getenv('REMOTE_ADDR'); |
93
|
|
|
$currentRating = 0; |
94
|
|
|
$count = 0; |
95
|
|
|
|
96
|
|
|
if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars') |
97
|
|
|
|| Constants::RATING_10STARS === (int)$helper->getConfig('ratingbars') |
98
|
|
|
|| Constants::RATING_10NUM === (int)$helper->getConfig('ratingbars')) { |
99
|
|
|
$rating_unitwidth = 25; |
100
|
|
|
if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')) { |
101
|
|
|
$max_units = 5; |
102
|
|
|
} else { |
103
|
|
|
$max_units = 10; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$criteria = new \CriteriaCompo(); |
107
|
|
|
$criteria->add(new \Criteria('rate_itemid', $itemId)); |
108
|
|
|
$criteria->add(new \Criteria('rate_source', $source)); |
109
|
|
|
|
110
|
|
|
$ratingObjs = $helper->getHandler('ratings') |
111
|
|
|
->getObjects($criteria); |
112
|
|
|
$count = \count($ratingObjs); |
113
|
|
|
$itemRating['nb_ratings'] = $count; |
114
|
|
|
|
115
|
|
|
foreach ($ratingObjs as $ratingObj) { |
116
|
|
|
$currentRating += $ratingObj->getVar('rate_value'); |
117
|
|
|
if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
118
|
|
|
$voted = true; |
|
|
|
|
119
|
|
|
$itemRating['id'] = $ratingObj->getVar('rate_id'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
unset($criteria); |
123
|
|
|
|
124
|
|
|
$itemRating['avg_rate_value'] = 0; |
125
|
|
|
if ($count > 0) { |
126
|
|
|
$itemRating['avg_rate_value'] = \number_format((float)$currentRating / $count, 2); |
127
|
|
|
} |
128
|
|
|
if (1 == $count) { |
129
|
|
|
$text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_1); |
130
|
|
|
$shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_1); |
131
|
|
|
} else { |
132
|
|
|
$text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_X); |
133
|
|
|
$shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_X); |
134
|
|
|
} |
135
|
|
|
$text = \str_replace('%m', (string)$max_units, $text); |
136
|
|
|
$text = \str_replace('%t', (string)$itemRating['nb_ratings'], $text); |
137
|
|
|
$shorttext = \str_replace('%t', (string)$itemRating['nb_ratings'], $shorttext); |
138
|
|
|
$itemRating['text'] = $text; |
139
|
|
|
$itemRating['shorttext'] = $shorttext; |
140
|
|
|
$itemRating['size'] = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px'; |
141
|
|
|
$itemRating['maxsize'] = ($max_units * $rating_unitwidth) . 'px'; |
142
|
|
|
} elseif (Constants::RATING_LIKES === (int)$helper->getConfig('ratingbars')) { |
143
|
|
|
$criteria = new \CriteriaCompo(); |
144
|
|
|
$criteria->add(new \Criteria('rate_itemid', $itemId)); |
145
|
|
|
$criteria->add(new \Criteria('rate_source', $source)); |
146
|
|
|
$criteria->add(new \Criteria('rate_value', 0, '<')); |
147
|
|
|
|
148
|
|
|
$ratingObjs = $helper->getHandler('Ratings') |
149
|
|
|
->getObjects($criteria); |
150
|
|
|
$count = \count($ratingObjs); |
151
|
|
|
|
152
|
|
|
foreach ($ratingObjs as $ratingObj) { |
153
|
|
|
$currentRating += $ratingObj->getVar('rate_value'); |
154
|
|
|
if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
155
|
|
|
$voted = true; |
156
|
|
|
$itemRating['id'] = $ratingObj->getVar('rate_id'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
unset($criteria); |
160
|
|
|
$itemRating['dislikes'] = $count; |
161
|
|
|
|
162
|
|
|
$criteria = new \CriteriaCompo(); |
163
|
|
|
$criteria->add(new \Criteria('rate_itemid', $itemId)); |
164
|
|
|
$criteria->add(new \Criteria('rate_source', $source)); |
165
|
|
|
$criteria->add(new \Criteria('rate_value', 0, '>')); |
166
|
|
|
|
167
|
|
|
$ratingObjs = $helper->getHandler('ratings') |
168
|
|
|
->getObjects($criteria); |
169
|
|
|
$count = \count($ratingObjs); |
170
|
|
|
$currentRating = 0; |
171
|
|
|
foreach ($ratingObjs as $ratingObj) { |
172
|
|
|
$currentRating += $ratingObj->getVar('rate_value'); |
173
|
|
|
if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
174
|
|
|
$voted = true; |
175
|
|
|
$itemRating['id'] = $ratingObj->getVar('rate_id'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
unset($criteria); |
179
|
|
|
$itemRating['likes'] = $count; |
180
|
|
|
|
181
|
|
|
$count = $itemRating['likes'] + $itemRating['dislikes']; |
|
|
|
|
182
|
|
|
// Facebook Reactions ========================================== |
183
|
|
|
} elseif (Constants::RATING_REACTION === (int)$helper->getConfig('ratingbars')) { |
184
|
|
|
$criteria = new \CriteriaCompo(); |
185
|
|
|
$criteria->add(new \Criteria('rate_itemid', $itemId)); |
186
|
|
|
$criteria->add(new \Criteria('rate_source', $source)); |
187
|
|
|
$criteria->add(new \Criteria('rate_value', 0, '<')); |
188
|
|
|
|
189
|
|
|
$ratingObjs = $helper->getHandler('ratings') |
190
|
|
|
->getObjects($criteria); |
191
|
|
|
$count = \count($ratingObjs); |
192
|
|
|
$itemRating['nb_ratings'] = $count; |
193
|
|
|
|
194
|
|
|
foreach ($ratingObjs as $ratingObj) { |
195
|
|
|
$currentRating += $ratingObj->getVar('rate_value'); |
196
|
|
|
if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
197
|
|
|
$voted = true; |
198
|
|
|
$itemRating['id'] = $ratingObj->getVar('rate_id'); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
unset($criteria); |
202
|
|
|
$itemRating['dislikes'] = $count; |
203
|
|
|
|
204
|
|
|
$criteria = new \CriteriaCompo(); |
205
|
|
|
$criteria->add(new \Criteria('rate_itemid', $itemId)); |
206
|
|
|
$criteria->add(new \Criteria('rate_source', $source)); |
207
|
|
|
$criteria->add(new \Criteria('rate_value', 0, '>')); |
208
|
|
|
|
209
|
|
|
$ratingObjs = $helper->getHandler('ratings') |
210
|
|
|
->getObjects($criteria); |
211
|
|
|
$count = \count($ratingObjs); |
212
|
|
|
$currentRating = 0; |
213
|
|
|
foreach ($ratingObjs as $ratingObj) { |
214
|
|
|
$currentRating += $ratingObj->getVar('rate_value'); |
215
|
|
|
if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
216
|
|
|
$voted = true; |
217
|
|
|
$itemRating['id'] = $ratingObj->getVar('rate_id'); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
unset($criteria); |
221
|
|
|
$itemRating['likes'] = $count; |
222
|
|
|
|
223
|
|
|
$count = $itemRating['likes'] + $itemRating['dislikes']; |
224
|
|
|
} else { |
225
|
|
|
$itemRating['uid'] = $uid; |
226
|
|
|
$itemRating['nb_ratings'] = $count; |
227
|
|
|
$itemRating['voted'] = $voted; |
228
|
|
|
$itemRating['ip'] = $ip; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $itemRating; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* delete ratings of given item |
236
|
|
|
* @param mixed $itemId |
237
|
|
|
* @param mixed $source |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
|
|
public function deleteAllRatings($itemId, $source) |
241
|
|
|
{ |
242
|
|
|
$criteria = new \CriteriaCompo(); |
243
|
|
|
$criteria->add(new \Criteria('rate_itemid', $itemId)); |
244
|
|
|
$criteria->add(new \Criteria('rate_source', $source)); |
245
|
|
|
|
246
|
|
|
return $this->deleteAll($criteria); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|