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