-
Notifications
You must be signed in to change notification settings - Fork 25
/
vote.php
167 lines (152 loc) · 6.22 KB
/
vote.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* @copyright The XUUPS Project http://sourceforge.net/projects/xuups/
* @license http://www.fsf.org/copyleft/gpl.html GNU public license
* @author trabis <[email protected]>
*/
use Xmf\Request;
use XoopsModules\Publisher\{Constants,
GroupPermHandler,
Helper,
VoteHandler,
Utility
};
/** @var Helper $helper */
/** @var VoteHandler $voteHandler */
require __DIR__ . '/header.php';
$op = Request::getCmd('op', 'list');
$source = Request::getInt('source', 0);
$itemId = Request::getInt('itemid', 0);
$voteHandler = $helper->getHandler('Vote');
$articleHandler = $helper->getHandler('Item');
$xoopsUser = $GLOBALS['xoopsUser'];
switch ($op) {
case 'list':
default:
// default should not happen
\redirect_header('index.php', 3, _NOPERM);
break;
case 'save':
// Security Check
if ($GLOBALS['xoopsSecurity']->check()) {
\redirect_header('index.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
}
$rating = Request::getInt('rating', 0);
$redir = Request::getString('HTTP_REFERER', '', 'SERVER');
if (Constants::TABLE_CATEGORY === $source) {
$redir = 'category.php?op=show&itemid=' . $itemId;
}
if (Constants::TABLE_ARTICLE === $source) {
$redir = 'item.php?op=show&itemid=' . $itemId;
}
// Check permissions
$rateAllowed = false;
$groups = (isset($xoopsUser) && \is_object($xoopsUser)) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
foreach ($groups as $group) {
if (XOOPS_GROUP_ADMIN == $group || \in_array($group, $helper->getConfig('ratingbar_groups'))) {
$rateAllowed = true;
break;
}
}
if (!$rateAllowed) {
\redirect_header('index.php', 3, _MA_PUBLISHER_RATING_NOPERM);
}
// Check rating value
// switch ((int)$helper->getConfig('ratingbars')) {
$articleObj = $articleHandler->get($itemId);
$votingType = (int)$articleObj->votetype();
switch ($votingType) {
case Constants::RATING_NONE:
default:
\redirect_header('index.php', 3, _MA_PUBLISHER_RATING_VOTE_BAD);
case Constants::RATING_LIKES:
if ($rating > 1 || $rating < -1) {
\redirect_header('index.php', 3, _MA_PUBLISHER_RATING_VOTE_BAD);
}
break;
case Constants::RATING_5STARS:
if ($rating > 5 || $rating < 1) {
\redirect_header('index.php', 3, _MA_PUBLISHER_RATING_VOTE_BAD);
}
break;
case Constants::RATING_REACTION:
if ($rating > 6 || $rating < 1) {
\redirect_header('index.php', 3, _MA_PUBLISHER_RATING_VOTE_BAD);
}
break;
case Constants::RATING_10STARS:
case Constants::RATING_10NUM:
if ($rating > 10 || $rating < 1) {
\redirect_header('index.php', 3, _MA_PUBLISHER_RATING_VOTE_BAD);
}
break;
}
// Get existing rating
$itemRating = $voteHandler->getItemRating5($articleObj, $source);
// Set data rating
if ($itemRating['voted'] && !$helper->getConfig('repeat_rating')) {
// If repeat-votingis not allowed, then leave
$helper->redirect('item.php?itemid=' . $itemId, 2, _MD_PUBLISHER_VOTE_ALREADY);
} else {
$voteObj = $voteHandler->create();
}
$voteObj->setVar('source', $source);
$voteObj->setVar('itemid', $itemId);
$voteObj->setVar('rate', $rating);
$voteObj->setVar('uid', $itemRating['uid']);
$voteObj->setVar('ip', $itemRating['ip']);
$voteObj->setVar('date', \time());
$voteObj->setVar('votetype', $votingType);
// Insert Data
if ($voteHandler->insert($voteObj)) {
unset($voteObj);
// Calc average rating value
$nb_vote = 0;
$avg_rate_value = 0;
$currentRating = 0;
$crVote = new \CriteriaCompo();
$crVote->add(new \Criteria('source', $source));
$crVote->add(new \Criteria('itemid', $itemId));
$voteCount = $voteHandler->getCount($crVote);
$voteAll = $voteHandler->getAll($crVote);
foreach (\array_keys($voteAll) as $i) {
$currentRating += $voteAll[$i]->getVar('rate');
}
unset($voteAll);
if ($voteCount > 0) {
$avg_rate_value = number_format($currentRating / $voteCount, 2);
}
// Update related table
if (Constants::TABLE_CATEGORY === $source) {
$tableName = 'category';
$fieldVote = '_vote';
$fieldVotes = '_votes';
$categoryObj = $categoryHandler->get($itemId);
$categoryObj->setVar('rating', $avg_rate_value);
$categoryObj->setVar('votes', $voteCount);
if ($categoryHandler->insert($categoryObj)) {
\redirect_header($redir, 2, _MA_PUBLISHER_RATING_VOTE_THANKS);
} else {
\redirect_header('category.php', 3, _MA_PUBLISHER_RATING_ERROR1);
}
unset($categoryObj);
}
if (Constants::TABLE_ARTICLE === $source) {
\redirect_header($redir, 2, _MA_PUBLISHER_RATING_VOTE_THANKS);
}
\redirect_header('index.php', 2, _MA_PUBLISHER_RATING_VOTE_THANKS);
}
// Get Error
echo 'Error: ' . $voteObj->getHtmlErrors();
break;
}
require __DIR__ . '/footer.php';