BxDolCmtsReviewsQuery.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * @see BxDolCmtsReviews
  11. */
  12. class BxDolCmtsReviewsQuery extends BxDolCmtsQuery
  13. {
  14. public function __construct(&$oMain)
  15. {
  16. parent::__construct($oMain);
  17. }
  18. public function isReviewed($iId, $iAuthorId)
  19. {
  20. $aReviews = $this->getCommentsBy(array('type' => 'author_id', 'author_id' => $iAuthorId, 'object_id' => $iId));
  21. return !empty($aReviews) && is_array($aReviews);
  22. }
  23. public function getReviewAuthorId($iId, $mixedCmt)
  24. {
  25. if(!is_array($mixedCmt))
  26. $mixedCmt = $this->getCommentSimple($iId, (int)$mixedCmt);
  27. if((int)$mixedCmt['cmt_parent_id'] == 0)
  28. return (int)$mixedCmt['cmt_author_id'];
  29. return (int)$this->getReviewAuthorId($iId, $mixedCmt['cmt_parent_id']);
  30. }
  31. public function getReviewsStats ($iId, $iCmtVParentId = -1, $iAuthorId = 0, $sFilter = '')
  32. {
  33. $aBindings = array(
  34. 'cmt_parent_id' => 0,
  35. 'cmt_object_id' => $iId
  36. );
  37. $sWhereClause = " AND `{$this->_sTable}`.`cmt_parent_id` = :cmt_parent_id AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  38. if((int)$iCmtVParentId >= 0) {
  39. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  40. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  41. }
  42. $sJoinClause = '';
  43. switch($sFilter) {
  44. case BX_CMT_FILTER_FRIENDS:
  45. case BX_CMT_FILTER_SUBSCRIPTIONS:
  46. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  47. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  48. $sJoinClause .= ' ' . $aQueryParts['join'];
  49. break;
  50. case BX_CMT_FILTER_OTHERS:
  51. $aBindings['cmt_author_id'] = $iAuthorId;
  52. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` <> :cmt_author_id";
  53. break;
  54. }
  55. return $this->getRow("SELECT COUNT(*) as `count`, SUM(`cmt_mood`)/COUNT(*) AS `avg` FROM `{$this->_sTable}` $sJoinClause WHERE 1 " . $sWhereClause, $aBindings);
  56. }
  57. public function updateTriggerTableAvg($iId, $fAvg)
  58. {
  59. $sField = $this->_sTriggerFieldComments . '_avg';
  60. return $this->query("UPDATE `{$this->_sTriggerTable}` SET `{$sField}` = :avg WHERE `{$this->_sTriggerFieldId}` = :id LIMIT 1", array(
  61. 'avg' => $fAvg,
  62. 'id' => $iId
  63. ));
  64. }
  65. }
  66. /** @} */