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