-
Notifications
You must be signed in to change notification settings - Fork 6
/
rate.php
124 lines (110 loc) · 4.68 KB
/
rate.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
<?php
/*
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.
*/
/**
* wgGallery module for xoops
*
* @copyright module for xoops
* @license GPL 3.0 or later
* @package wggallery
* @since 1.0
* @min_xoops 2.5.7
* @author TDM XOOPS - Email:<[email protected]> - Website:<https://xoops.org>
* @version $Id: 1.0 rate.php 13070 Wed 2016-12-14 22:22:38Z XOOPS Development Team $
*/
use Xmf\Request;
use XoopsModules\Wggallery\Constants;
include __DIR__ . '/header.php';
$op = Request::getString('op', 'default');
switch ($op) {
case 'save-imglist':
case 'save-imgshow':
// Security Check
if ($GLOBALS['xoopsSecurity']->check()) {
\redirect_header('index.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
}
$itemid = Request::getInt('img_id');
$rating = Request::getInt('rating');
$source = Request::getInt('source'); //source 1 = image / source 2 = album rating TODO
// Checking permissions
$rate_allowed = false;
$groups = (isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser'])) ? $GLOBALS['xoopsUser']->getGroups() : \XOOPS_GROUP_ANONYMOUS;
foreach ($groups as $group) {
if (\XOOPS_GROUP_ADMIN == $group || \in_array($group, $helper->getConfig('ratingbar_groups'))) {
$rate_allowed = true;
break;
}
}
if (!$rate_allowed) {
\redirect_header(\WGGALLERY_URL . '/index.php?img_id=' . $itemid . '#item' . $itemid, 2, \_MA_WGGALLERY_RATING_NOPERM);
}
$redir = $_SERVER['HTTP_REFERER'];
if ('save-imglist' === $op) {
$redir = $_SERVER['HTTP_REFERER'] . '#imglist_' . $itemid;
}
if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')) {
if ($rating > 5 || $rating < 1) {
\redirect_header($redir, 2, \_MA_WGGALLERY_RATING_VOTE_BAD);
exit();
}
} elseif (Constants::RATING_10STARS === (int)$helper->getConfig('ratingbars') || Constants::RATING_10NUM === (int)$helper->getConfig('ratingbars')) {
if ($rating > 10 || $rating < 1) {
\redirect_header($redir, 2, \_MA_WGGALLERY_RATING_VOTE_BAD);
exit();
}
} else {
if ($rating > 1 || $rating < -1) {
\redirect_header($redir, 2, \_MA_WGGALLERY_RATING_VOTE_BAD);
exit();
}
}
$itemrating = $ratingsHandler->getItemRating($itemid, 1);
if ($itemrating['voted']) {
// \redirect_header($redir, 2, \_MA_WGGALLERY_RATING_VOTE_ALREADY);
$ratingsObj = $ratingsHandler->get($itemrating['id']);
} else {
$ratingsObj = $ratingsHandler->create();
}
$ratingsObj->setVar('rate_source', $source);
$ratingsObj->setVar('rate_itemid', $itemid);
$ratingsObj->setVar('rate_value', $rating);
$ratingsObj->setVar('rate_uid', $itemrating['uid']);
$ratingsObj->setVar('rate_ip', $itemrating['ip']);
$ratingsObj->setVar('rate_date', \time());
// Insert Data
if ($ratingsHandler->insert($ratingsObj)) {
// update table wggallery_images
$nb_ratings = 0;
$avg_rate_value = 0;
$ratingObjs = $helper->getHandler('ratings')->getObjects();
$count = \count($ratingObjs);
$current_rating = 0;
foreach ($ratingObjs as $ratingObj) {
$current_rating += $ratingObj->getVar('rate_value');
}
unset($ratingObj);
if ($count > 0) {
$avg_rate_value = number_format($current_rating / $count, 2);
}
$imagesObj = $imagesHandler->get($itemid);
// Set Vars
$imagesObj->setVar('img_ratinglikes', $avg_rate_value);
$imagesObj->setVar('img_votes', $count);
// Insert Data
$imagesHandler->insert($imagesObj);
unset($imagesObj);
\redirect_header($redir, 2, \_MA_WGGALLERY_RATING_VOTE_THANKS);
}
echo '<br>error:' . $ratingsObj->getHtmlErrors();
break;
case 'default':
default:
echo \_MA_WGGALLERY_RATING_VOTE_BAD . ' (invalid parameter)';
break;
}