Skip to content

Commit

Permalink
updating rating
Browse files Browse the repository at this point in the history
  • Loading branch information
mambax7 committed Jan 1, 2021
1 parent f48cf90 commit 5c20d18
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 170 deletions.
2 changes: 1 addition & 1 deletion class/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
39 changes: 10 additions & 29 deletions class/ItemHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
Expand All @@ -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;
Expand Down
182 changes: 182 additions & 0 deletions class/VoteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions config/paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

/** @return object */

//function getPaths()
//{
$moduleDirName = basename(dirname(__DIR__));
// $moduleDirNameUpper = mb_strtoupper($moduleDirName);

Expand All @@ -20,4 +18,4 @@
'uploadPathCategory' => XOOPS_UPLOAD_PATH . '/' . $moduleDirName . '/category',
'uploadPathScreenshots' => XOOPS_UPLOAD_PATH . '/' . $moduleDirName . '/screenshots',
];
//}

2 changes: 1 addition & 1 deletion include/search.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '&amp;keywords=' . $keywords;
}
$itemHandler = $helper->getHandler('Item');
Expand Down
Loading

0 comments on commit 5c20d18

Please sign in to comment.