BxDolCmtsReviewsQuery.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_object_id' => $iId
  35. );
  36. $sWhereClause = " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  37. if((int)$iCmtVParentId >= 0) {
  38. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  39. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  40. }
  41. $sJoinClause = '';
  42. switch($sFilter) {
  43. case BX_CMT_FILTER_FRIENDS:
  44. case BX_CMT_FILTER_SUBSCRIPTIONS:
  45. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  46. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  47. $sJoinClause .= ' ' . $aQueryParts['join'];
  48. break;
  49. case BX_CMT_FILTER_OTHERS:
  50. $aBindings['cmt_author_id'] = $iAuthorId;
  51. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` <> :cmt_author_id";
  52. break;
  53. }
  54. return $this->getRow("SELECT COUNT(*) as `count`, SUM(`cmt_mood`)/COUNT(*) AS `avg` FROM `{$this->_sTable}` $sJoinClause WHERE 1 " . $sWhereClause, $aBindings);
  55. }
  56. public function updateTriggerTableAvg($iId, $fAvg)
  57. {
  58. $sField = $this->_sTriggerFieldComments . '_avg';
  59. return $this->query("UPDATE `{$this->_sTriggerTable}` SET `{$sField}` = :avg WHERE `{$this->_sTriggerFieldId}` = :id LIMIT 1", array(
  60. 'avg' => $fAvg,
  61. 'id' => $iId
  62. ));
  63. }
  64. }
  65. /** @} */