Passed
Branch master (d2b70f)
by Michael
12:26
created

ratelike.php (1 issue)

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