From 5c20d187b0d4a979b43f8c86d6b74ea9c0c591ba Mon Sep 17 00:00:00 2001 From: mambax7 Date: Fri, 1 Jan 2021 16:58:38 -0500 Subject: [PATCH] updating rating --- class/Item.php | 2 +- class/ItemHandler.php | 39 ++------ class/VoteHandler.php | 182 +++++++++++++++++++++++++++++++++++ config/paths.php | 4 +- include/search.inc.php | 2 +- item.php | 79 ++++++--------- language/english/modinfo.php | 4 + sql/mysql.sql | 53 +--------- templates/publisher_item.tpl | 2 + templates/publisher_vote.tpl | 28 +++--- vote.php | 39 +++----- xoops_version.php | 10 ++ 12 files changed, 274 insertions(+), 170 deletions(-) diff --git a/class/Item.php b/class/Item.php index 9bede0fb..c788ba61 100644 --- a/class/Item.php +++ b/class/Item.php @@ -87,7 +87,7 @@ public function __construct($id = null) $this->initVar('meta_description', \XOBJ_DTYPE_TXTAREA, '', false); $this->initVar('short_url', \XOBJ_DTYPE_TXTBOX, '', false, 255); $this->initVar('item_tag', \XOBJ_DTYPE_TXTAREA, '', false); - $this->initVar('votetype', \XOBJ_DTYPE_INT, 1, false); + $this->initVar('votetype', \XOBJ_DTYPE_INT, 0, false); // Non consistent values $this->initVar('pagescount', \XOBJ_DTYPE_INT, 0, false); if (null !== $id) { diff --git a/class/ItemHandler.php b/class/ItemHandler.php index c62095f1..1ce511ec 100644 --- a/class/ItemHandler.php +++ b/class/ItemHandler.php @@ -224,15 +224,14 @@ public function &getObjects(\CriteriaElement $criteria = null, $idKey = 'none', /** * count items matching a condition * - * @param \CriteriaElement|null $criteria {@link CriteriaElement} - * to match + * @param \CriteriaElement|null $criteria Criteria to match * @param string|null $notNullFields * * @return int count of items */ public function getCount(\CriteriaElement $criteria = null, $notNullFields = null) { - $notNullFields = $notNullFields ?? null; +// $notNullFields = $notNullFields ?? null; $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($this->helper->getDirname() . '_items'); if (null !== $criteria && ($criteria instanceof \Criteria || $criteria instanceof \CriteriaCompo)) { $whereClause = $criteria->renderWhere(); @@ -279,25 +278,30 @@ private function getItemsCriteria($categoryId = -1, $status = '', $notNullFields // return $ret; // } // } + $criteriaCategory = null; - if (isset($categoryId) && -1 != $categoryId) { + if (isset($categoryId) && -1 !== $categoryId) { $criteriaCategory = new \Criteria('categoryid', $categoryId); } + $criteriaStatus = new \CriteriaCompo(); if (!empty($status) && \is_array($status)) { foreach ($status as $v) { $criteriaStatus->add(new \Criteria('status', $v), 'OR'); } - } elseif (!empty($status) && -1 != $status) { + } elseif (!empty($status) && -1 !== $status) { $criteriaStatus->add(new \Criteria('status', $status), 'OR'); } + $criteria = new \CriteriaCompo(); if (null !== $criteriaCategory) { $criteria->add($criteriaCategory); } + if (null !== $criteriaPermissions) { $criteria->add($criteriaPermissions); } + if (null !== $criteriaStatus) { $criteria->add($criteriaStatus); } @@ -314,7 +318,7 @@ private function getItemsCriteria($categoryId = -1, $status = '', $notNullFields */ public function getItemsCount($categoryId = -1, $status = '', $notNullFields = null) { - $notNullFields = $notNullFields ?? null; +// $notNullFields = $notNullFields ?? null; $criteriaPermissions = null; if (!$this->publisherIsAdmin) { $criteriaPermissions = new \CriteriaCompo(); @@ -330,29 +334,6 @@ public function getItemsCount($categoryId = -1, $status = '', $notNullFields = n // $ret = []; $criteria = $this->getItemsCriteria($categoryId, $status, $notNullFields, $criteriaPermissions); - /* - if (isset($categoryId) && $categoryId != -1) { - $criteriaCategory = new \Criteria('categoryid', $categoryId); - } - $criteriaStatus = new \CriteriaCompo(); - if (!empty($status) && is_array($status)) { - foreach ($status as $v) { - $criteriaStatus->add(new \Criteria('status', $v), 'OR'); - } - } elseif (!empty($status) && $status != -1) { - $criteriaStatus->add(new \Criteria('status', $status), 'OR'); - } - $criteria = new \CriteriaCompo(); - if (!empty($criteriaCategory)) { - $criteria->add($criteriaCategory); - } - if (!empty($criteriaPermissions)) { - $criteria->add($criteriaPermissions); - } - if (!empty($criteriaStatus)) { - $criteria->add($criteriaStatus); - } - */ $ret = $this->getCount($criteria, $notNullFields); return $ret; diff --git a/class/VoteHandler.php b/class/VoteHandler.php index dca25afd..82a4177d 100644 --- a/class/VoteHandler.php +++ b/class/VoteHandler.php @@ -230,6 +230,188 @@ public function getItemRating($itemId = null, $source = null): array return $itemRating; } + /** + * Get Rating per item in the database + * @param null $itemObj + * @param int|null $source + * @return array + */ + public function getItemRating5($itemObj = null, $source = null): array + { + $itemId = $itemObj->itemid(); + $source = $source ?? 0; + $xoopsUser = $GLOBALS['xoopsUser']; + + $itemRating = []; + $itemRating['nb_vote'] = 0; + $uid = \is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; + $voted = false; + $ip = \getenv('REMOTE_ADDR'); + $currentRating = 0; + $count = 0; + + $max_units = 10; + $ratingbarsValue = $itemObj->votetype(); + $ratingArray = [Constants::RATING_5STARS, Constants::RATING_10STARS, Constants::RATING_10NUM]; + + if (in_array($ratingbarsValue, $ratingArray)) { + $rating_unitwidth = 25; + if (Constants::RATING_5STARS === $ratingbarsValue) { + $max_units = 5; + } + + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + $itemRating['nb_vote'] = $count; + + foreach ($voteObjs as $voteObj) { + $currentRating += $voteObj->getVar('rate'); + if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { + $voted = true; + $itemRating['id'] = $voteObj->getVar('ratingid'); + } + } + unset($criteria); + + $itemRating['avg_rate_value'] = 0; + if ($count > 0) { + $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2); + } + if (1 == $count) { + $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_1); + $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_1); + } else { + $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_X); + $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_X); + } + $text = \str_replace('%m', $max_units, $text); + $text = \str_replace('%t', $itemRating['nb_vote'], $text); + $shorttext = \str_replace('%t', $itemRating['nb_vote'], $shorttext); + $itemRating['text'] = $text; + $itemRating['shorttext'] = $shorttext; + $itemRating['size'] = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px'; + $itemRating['maxsize'] = ($max_units * $rating_unitwidth) . 'px'; + + $itemRating['ip'] = $ip; + $itemRating['uid'] = $uid; + $itemRating['voted'] = $voted; + // YouTube Liking ========================================== + } elseif (Constants::RATING_LIKES === $ratingbarsValue) { + // get count of "dislikes" + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + $criteria->add(new \Criteria('rate', 0, '<')); + + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + + foreach ($voteObjs as $voteObj) { + $currentRating += $voteObj->getVar('rate'); + if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { + $voted = true; + $itemRating['id'] = $voteObj->getVar('ratingid'); + } + } + unset($criteria); + $itemRating['dislikes'] = $count; + + // get count of "likes" + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + $criteria->add(new \Criteria('rate', 0, '>')); + + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + $currentRating = 0; + foreach ($voteObjs as $voteObj) { + $currentRating += $voteObj->getVar('rate'); + if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { + $voted = true; + $itemRating['id'] = $voteObj->getVar('ratingid'); + } + } + unset($criteria); + $itemRating['likes'] = $count; + + $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes']; + $itemRating['ip'] = $ip; + $itemRating['uid'] = $uid; + $itemRating['voted'] = $voted; + // Facebook Reactions ========================================== + } elseif (Constants::RATING_REACTION === $ratingbarsValue) { + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + $criteria->add(new \Criteria('rate', 1)); + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + $itemRating['likes'] = $count; + + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + $criteria->add(new \Criteria('rate', 2)); + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + $itemRating['love'] = $count; + + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + $criteria->add(new \Criteria('rate', 3)); + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + $itemRating['smile'] = $count; + + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + $criteria->add(new \Criteria('rate', 4)); + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + $itemRating['wow'] = $count; + + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + $criteria->add(new \Criteria('rate', 5)); + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + $itemRating['sad'] = $count; + + $criteria = new \CriteriaCompo(); + $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); + $criteria->add(new \Criteria(static::SOURCE, $source)); + $criteria->add(new \Criteria('rate', 6)); + $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); + $count = \count($voteObjs); + $itemRating['angry'] = $count; + + + $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['love'] + $itemRating['smile'] + $itemRating['wow'] + $itemRating['sad'] + $itemRating['angry']; + $itemRating['ip'] = $ip; + $itemRating['uid'] = $uid; + $itemRating['voted'] = $voted; + } else { + $itemRating['uid'] = $uid; + $itemRating['nb_vote'] = $count; + $itemRating['voted'] = $voted; + $itemRating['ip'] = $ip; + } + return $itemRating; + } + + + + + + /** * delete vote of given item * @param mixed $itemId diff --git a/config/paths.php b/config/paths.php index 78c8838e..9fbc71b3 100644 --- a/config/paths.php +++ b/config/paths.php @@ -4,8 +4,6 @@ /** @return object */ -//function getPaths() -//{ $moduleDirName = basename(dirname(__DIR__)); // $moduleDirNameUpper = mb_strtoupper($moduleDirName); @@ -20,4 +18,4 @@ 'uploadPathCategory' => XOOPS_UPLOAD_PATH . '/' . $moduleDirName . '/category', 'uploadPathScreenshots' => XOOPS_UPLOAD_PATH . '/' . $moduleDirName . '/screenshots', ]; -//} + diff --git a/include/search.inc.php b/include/search.inc.php index ace503fb..4cc6a7ae 100644 --- a/include/search.inc.php +++ b/include/search.inc.php @@ -47,7 +47,7 @@ function publisher_search($queryArray, $andor, $limit, $offset, $userid, $catego if (is_array($queryArray) && 0 === count($queryArray)) { $hightlightKey = ''; } else { - $keywords = implode('+', $queryArray); +// $keywords = implode('+', $queryArray); $hightlightKey = '&keywords=' . $keywords; } $itemHandler = $helper->getHandler('Item'); diff --git a/item.php b/item.php index 5078761c..3ca8e81e 100644 --- a/item.php +++ b/item.php @@ -36,7 +36,7 @@ $itemPageId = Request::getInt('page', -1, 'GET'); if (0 == $itemId) { - redirect_header('', 1, _MD_PUBLISHER_NOITEMSELECTED); +// redirect_header('', 1, _MD_PUBLISHER_NOITEMSELECTED); } $helper = Helper::getInstance(); @@ -47,7 +47,7 @@ // if the selected item was not found, exit if (null === $itemObj) { - redirect_header('', 1, _MD_PUBLISHER_NOITEMSELECTED); +// redirect_header('', 1, _MD_PUBLISHER_NOITEMSELECTED); } $GLOBALS['xoopsOption']['template_main'] = 'publisher_item.tpl'; @@ -288,15 +288,20 @@ 'replycomment_link' => PUBLISHER_URL . '/comment_reply.php?com_itemid=' . $com_itemid . '&com_order=' . $com_order . '&com_mode=' . $com_mode . $link_extra, ] ); - $xoopsTpl->_tpl_vars['commentsnav'] = str_replace("self.location.href='", "self.location.href='" . PUBLISHER_URL . '/', $xoopsTpl->_tpl_vars['commentsnav']); + $xoopsTpl->_tpl_vars['commentsnav'] = str_replace( + "self.location.href='", + "self.location.href='" . PUBLISHER_URL . '/', + $xoopsTpl->_tpl_vars['commentsnav']??'' + ); } // Original AJAX rating if ($helper->getConfig('perm_rating')) { $xoopsTpl->assign('rating_enabled', true); $item['ratingbar'] = Utility::ratingBar($itemId); - $xoTheme->addScript(PUBLISHER_URL . '/assets/js/behavior.js'); - $xoTheme->addScript(PUBLISHER_URL . '/assets/js/rating.js'); + + // $xoTheme->addScript(PUBLISHER_URL . '/assets/js/behavior.js'); + // $xoTheme->addScript(PUBLISHER_URL . '/assets/js/rating.js'); //} //=============== START VOTE RATING ====================================== @@ -305,57 +310,37 @@ $limit = Request::getInt('limit', $helper->getConfig('userpager')); $id = Request::getInt('itemid', 0, 'GET'); - $ratingbars = (int)$helper->getConfig('ratingbars'); - if ($ratingbars > 0) { + // $ratingbars = (int)$helper->getConfig('ratingbars'); //from Preferences + + $voteType = $itemObj->votetype(); + + if ($voteType > 0) { $GLOBALS['xoTheme']->addStylesheet(PUBLISHER_URL . '/assets/css/rating.css', null); - $GLOBALS['xoopsTpl']->assign('rating', $ratingbars); - $GLOBALS['xoopsTpl']->assign('rating_5stars', (Constants::RATING_5STARS === $ratingbars)); - $GLOBALS['xoopsTpl']->assign('rating_10stars', (Constants::RATING_10STARS === $ratingbars)); - $GLOBALS['xoopsTpl']->assign('rating_10num', (Constants::RATING_10NUM === $ratingbars)); - $GLOBALS['xoopsTpl']->assign('rating_likes', (Constants::RATING_LIKES === $ratingbars)); - $GLOBALS['xoopsTpl']->assign('rating_reaction', (Constants::RATING_REACTION === $ratingbars)); + $GLOBALS['xoopsTpl']->assign('rating', $voteType); + $GLOBALS['xoopsTpl']->assign('rating_5stars', (Constants::RATING_5STARS === $voteType)); + $GLOBALS['xoopsTpl']->assign('rating_10stars', (Constants::RATING_10STARS === $voteType)); + $GLOBALS['xoopsTpl']->assign('rating_10num', (Constants::RATING_10NUM === $voteType)); + $GLOBALS['xoopsTpl']->assign('rating_likes', (Constants::RATING_LIKES === $voteType)); + $GLOBALS['xoopsTpl']->assign('rating_reaction', (Constants::RATING_REACTION === $voteType)); $GLOBALS['xoopsTpl']->assign('itemid', 'itemid'); $GLOBALS['xoopsTpl']->assign('blog_icon_url_16', PUBLISHER_URL . '/' . $modPathIcon16); } - $crArticle = new \CriteriaCompo(); - if ($id > 0) { - $crArticle->add(new \Criteria('itemid', $id)); - } - /** @var ItemHandler $itemHandler */ /** @var VoteHandler $voteHandler */ - $itemHandler = $helper->getHandler('Item'); $voteHandler = $helper->getHandler('Vote'); - $articleCount = $itemHandler->getCount($crArticle); - $GLOBALS['xoopsTpl']->assign('articleCount', $articleCount); - $crArticle->setStart($start); - $crArticle->setLimit($limit); - $articleAll = $itemHandler->getAll($crArticle); - if ($articleCount > 0) { - $article = []; - // Get All Article - foreach (\array_keys($articleAll) as $i) { - $article[$i] = $articleAll[$i]->toArraySimple(); - $keywords[$i] = $articleAll[$i]->getVar('title'); - $rating = $voteHandler->getItemRating($articleAll[$i]->getVar('itemid'), Constants::TABLE_ARTICLE); - $article[$i]['rating'] = $rating; - - $item['rating'] = $rating; - } - // $GLOBALS['xoopsTpl']->assign('article', $article); - $xoopsTpl->assign('article', $article); - $xoopsTpl->assign('item2', $item); - $xoopsTpl->assign('rating', $rating); - unset($article); - } + $rating5 = $voteHandler->getItemRating5($itemObj, Constants::TABLE_ARTICLE); + $xoopsTpl->assign('rating', $rating5); + $item['rating'] = $rating5; + + // $GLOBALS['xoopsTpl']->assign('article', $article); + // $xoopsTpl->assign('article', $article); + $xoopsTpl->assign('item2', $item); + // $xoopsTpl->assign('rating', $rating); + // unset($article); + // } + - // Display Navigation - if ($articleCount > $limit) { - include_once XOOPS_ROOT_PATH . '/class/pagenav.php'; - $pagenav = new \XoopsPageNav($articleCount, $limit, $start, 'start', 'op=list&limit=' . $limit); - $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4)); - } $GLOBALS['xoopsTpl']->assign('type', $helper->getConfig('table_type')); $GLOBALS['xoopsTpl']->assign('divideby', $helper->getConfig('divideby')); $GLOBALS['xoopsTpl']->assign('numb_col', $helper->getConfig('numb_col')); diff --git a/language/english/modinfo.php b/language/english/modinfo.php index 062eadf8..b698d2d6 100644 --- a/language/english/modinfo.php +++ b/language/english/modinfo.php @@ -518,3 +518,7 @@ \define('_MI_BLOG_FACEBOOK_COMMENTS_DESC', 'Allow Facebook comments in the single page'); \define('_MI_BLOG_DISQUS_COMMENTS', 'Disqus comments'); \define('_MI_BLOG_DISQUS_COMMENTS_DESC', 'Allow Disqus comments in the single page'); + +//VOTING +\define('_MI_PUBLISHER_ALLOW_REPEAT_RATING', 'Allow Repeat-rating'); +\define('_MI_PUBLISHER_ALLOW_REPEAT_RATING_DSC', 'Should a user be able to rate repeatedly many times, or only once?'); diff --git a/sql/mysql.sql b/sql/mysql.sql index f255c5cf..31661e26 100644 --- a/sql/mysql.sql +++ b/sql/mysql.sql @@ -72,6 +72,7 @@ CREATE TABLE `publisher_items` ( `meta_description` TEXT NULL, `short_url` VARCHAR(255) NULL, `item_tag` TEXT NULL, + `votetype` TINYINT(1) NOT NULL DEFAULT '0', PRIMARY KEY (`itemid`), KEY categoryid (categoryid), KEY status (status) @@ -147,7 +148,7 @@ CREATE TABLE `publisher_rating` ( `date` INT(11) NOT NULL, `ip` VARCHAR(45) NOT NULL DEFAULT '', `source` TINYINT(2) NOT NULL DEFAULT '0', - `type` TINYINT(2) UNSIGNED NOT NULL DEFAULT '1', + `votetype` TINYINT(2) UNSIGNED NOT NULL DEFAULT '1', PRIMARY KEY (`ratingid`), KEY uid (uid), KEY ip (ip) @@ -155,56 +156,6 @@ CREATE TABLE `publisher_rating` ( ENGINE = MyISAM; -# CREATE TABLE `publisher_voting` ( -# `rate_id` INT(8) UNSIGNED NOT NULL AUTO_INCREMENT, -# `itemid` INT(8) NOT NULL DEFAULT '0', -# `source` INT(8) NOT NULL DEFAULT '0', -# `rate` INT(1) NOT NULL DEFAULT '0', -# `rate_uid` INT(8) NOT NULL DEFAULT '0', -# `rate_ip` VARCHAR(45) NOT NULL DEFAULT '', -# `rate_date` INT(8) NOT NULL DEFAULT '0', -# PRIMARY KEY (`rate_id`) -# ) -# ENGINE = MyISAM; - -# -------------------------------------------------------- - -# -# Table structure for table `publisher_liking` -# - -# CREATE TABLE `publisher_liking` ( -# `likingid` INT(11) NOT NULL AUTO_INCREMENT, -# `itemid` INT(11) NOT NULL, -# `uid` INT(11) NOT NULL, -# `like` INT(1) NOT NULL, -# `dislike` INT(1) NOT NULL, -# `date` INT(11) NOT NULL, -# `ip` VARCHAR(60) NOT NULL DEFAULT '', -# PRIMARY KEY (`likingid`), -# KEY uid (uid), -# KEY ip (ip) -# ) -# ENGINE = MyISAM; - -# -------------------------------------------------------- - -# -# Table structure for table `publisher_reactions` -# - -# CREATE TABLE `publisher_reactions` ( -# `reactionid` INT(11) NOT NULL AUTO_INCREMENT, -# `itemid` INT(11) NOT NULL, -# `uid` INT(11) NOT NULL, -# `reaction` INT(1) NOT NULL, -# `date` INT(11) NOT NULL, -# `ip` VARCHAR(60) NOT NULL DEFAULT '', -# PRIMARY KEY (`reactionid`), -# KEY uid (uid), -# KEY ip (ip) -# ) -# ENGINE = MyISAM; # -------------------------------------------------------- diff --git a/templates/publisher_item.tpl b/templates/publisher_item.tpl index 236113b1..21fb9d9f 100644 --- a/templates/publisher_item.tpl +++ b/templates/publisher_item.tpl @@ -108,7 +108,9 @@ <{if $displaylike}>
<{*
*}> + <{include file='db:publisher_vote.tpl'}> + <{*
*}> <{/if}> <{* ====== END VOTING =========*}> diff --git a/templates/publisher_vote.tpl b/templates/publisher_vote.tpl index fe05634e..23b3eabb 100644 --- a/templates/publisher_vote.tpl +++ b/templates/publisher_vote.tpl @@ -1,5 +1,5 @@ - <{if $rating_5stars}> + <{if $rating_5stars|default:0}>
@@ -26,7 +26,7 @@
<{/if}> - <{if $rating_10stars}> + <{if $rating_10stars|default:0}>
@@ -68,7 +68,7 @@
<{/if}> - <{if $rating_10num}> + <{if $rating_10num|default:0}>
@@ -89,7 +89,7 @@
<{/if}> - <{if $rating_likes}> + <{if $rating_likes|default:0}>
<{* *}> <{* <{$smarty.const._MA_BLOG_RATING_LIKE}>(<{$item.rating.likes}>)*}> @@ -97,13 +97,13 @@ - <{$item2.rating.dislikes}> + <{$item2.rating.dislikes|default:0}> - <{$item2.rating.likes}> + <{$item2.rating.likes|default:0}> @@ -130,27 +130,27 @@ - <{if $rating_reaction}> + <{if $rating_reaction|default:0}>
- <{$smarty.const._MA_BLOG_REACTION_LIKE}>(<{$item.rating.likes}>) + <{$smarty.const._MA_BLOG_REACTION_LIKE}>(<{$item.rating.likes|default:0}>) - <{$smarty.const._MA_BLOG_REACTION_LOVE}>(<{$item.rating.love}>) + <{$smarty.const._MA_BLOG_REACTION_LOVE}>(<{$item.rating.love|default:0}>) - <{$smarty.const._MA_BLOG_REACTION_HAHA}>(<{$item.rating.smile}>) + <{$smarty.const._MA_BLOG_REACTION_HAHA}>(<{$item.rating.smile|default:0}>) - <{$smarty.const._MA_BLOG_REACTION_WOW}>(<{$item.rating.wow}>) + <{$smarty.const._MA_BLOG_REACTION_WOW}>(<{$item.rating.wow|default:0}>) - <{$smarty.const._MA_BLOG_REACTION_SAD}>(<{$item.rating.sad}>) + <{$smarty.const._MA_BLOG_REACTION_SAD}>(<{$item.rating.sad|default:0}>) - - <{$smarty.const._MA_BLOG_REACTION_ANGRY}>(<{$item.rating.angry}>) + + <{$smarty.const._MA_BLOG_REACTION_ANGRY}>(<{$item.rating.angry|default:0}>)
<{/if}> diff --git a/vote.php b/vote.php index cca65bf9..bf695d07 100644 --- a/vote.php +++ b/vote.php @@ -14,7 +14,6 @@ /** * @copyright The XUUPS Project http://sourceforge.net/projects/xuups/ * @license http://www.fsf.org/copyleft/gpl.html GNU public license - * @since 1.0 * @author trabis */ @@ -30,8 +29,10 @@ /** @var VoteHandler $voteHandler */ require __DIR__ . '/header.php'; -$op = Request::getCmd('op', 'list'); -$source = Request::getInt('source', 0); +$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']; @@ -48,14 +49,12 @@ \redirect_header('index.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors())); } $rating = Request::getInt('rating', 0); - $itemId = 0; + $redir = Request::getString('HTTP_REFERER', '', 'SERVER'); if (Constants::TABLE_CATEGORY === $source) { - $itemId = Request::getInt('itemid', 0); $redir = 'category.php?op=show&itemid=' . $itemId; } if (Constants::TABLE_ARTICLE === $source) { - $itemId = Request::getInt('itemid', 0); $redir = 'item.php?op=show&itemid=' . $itemId; } @@ -73,7 +72,11 @@ } // Check rating value - switch ((int)$helper->getConfig('ratingbars')) { +// 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_BLOG_RATING_VOTE_BAD); @@ -101,13 +104,12 @@ } // Get existing rating - $itemRating = $voteHandler->getItemRating($itemId, $source); + $itemRating = $voteHandler->getItemRating5($articleObj, $source); // Set data rating - if ($itemRating['voted']) { - // If you want to allow revoting then deactivate next line + 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); - $voteObj = $voteHandler->get($itemRating['ratingid']); } else { $voteObj = $voteHandler->create(); } @@ -142,8 +144,8 @@ $fieldVote = '_vote'; $fieldVotes = '_votes'; $categoryObj = $categoryHandler->get($itemId); - $categoryObj->setVar('_vote', $avg_rate_value); - $categoryObj->setVar('_votes', $voteCount); + $categoryObj->setVar('rating', $avg_rate_value); + $categoryObj->setVar('votes', $voteCount); if ($categoryHandler->insert($categoryObj)) { \redirect_header($redir, 2, _MA_BLOG_RATING_VOTE_THANKS); } else { @@ -152,18 +154,7 @@ unset($categoryObj); } if (Constants::TABLE_ARTICLE === $source) { - $tableName = 'article'; - $fieldVote = '_vote'; - $fieldVotes = '_votes'; - $articleObj = $articleHandler->get($itemId); - $articleObj->setVar('_vote', $avg_rate_value); - $articleObj->setVar('_votes', $voteCount); - if ($articleHandler->insert($articleObj)) { \redirect_header($redir, 2, _MA_BLOG_RATING_VOTE_THANKS); - } else { - \redirect_header('item.php', 3, _MA_BLOG_RATING_ERROR1); - } - unset($articleObj); } \redirect_header('index.php', 2, _MA_BLOG_RATING_VOTE_THANKS); diff --git a/xoops_version.php b/xoops_version.php index 06471131..be6df046 100644 --- a/xoops_version.php +++ b/xoops_version.php @@ -981,6 +981,16 @@ 'category' => 'item', ]; +$modversion['config'][] = [ + 'name' => 'repeat_rating', + 'title' => '_MI_PUBLISHER_ALLOW_REPEAT_RATING', + 'description' => '_MI_PUBLISHER_ALLOW_REPEAT_RATING_DSC', + 'formtype' => 'yesno', + 'valuetype' => 'int', + 'default' => 0, + 'category' => 'item', +]; + // Get groups /** @var \XoopsMemberHandler $memberHandler */ $memberHandler = \xoops_getHandler('member');