Rating.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Model;
  7. use Magento\Framework\DataObject\IdentityInterface;
  8. /**
  9. * Rating model
  10. *
  11. * @api
  12. * @method array getRatingCodes()
  13. * @method \Magento\Review\Model\Rating setRatingCodes(array $value)
  14. * @method array getStores()
  15. * @method \Magento\Review\Model\Rating setStores(array $value)
  16. * @method string getRatingCode()
  17. *
  18. * @author Magento Core Team <core@magentocommerce.com>
  19. * @since 100.0.2
  20. */
  21. class Rating extends \Magento\Framework\Model\AbstractModel implements IdentityInterface
  22. {
  23. /**
  24. * rating entity codes
  25. */
  26. const ENTITY_PRODUCT_CODE = 'product';
  27. const ENTITY_PRODUCT_REVIEW_CODE = 'product_review';
  28. const ENTITY_REVIEW_CODE = 'review';
  29. /**
  30. * @var \Magento\Review\Model\Rating\OptionFactory
  31. */
  32. protected $_ratingOptionFactory;
  33. /**
  34. * @var \Magento\Review\Model\ResourceModel\Rating\Option\CollectionFactory
  35. */
  36. protected $_ratingCollectionF;
  37. /**
  38. * @param \Magento\Framework\Model\Context $context
  39. * @param \Magento\Framework\Registry $registry
  40. * @param \Magento\Review\Model\Rating\OptionFactory $ratingOptionFactory
  41. * @param \Magento\Review\Model\ResourceModel\Rating\Option\CollectionFactory $ratingCollectionF
  42. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  43. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Framework\Model\Context $context,
  48. \Magento\Framework\Registry $registry,
  49. \Magento\Review\Model\Rating\OptionFactory $ratingOptionFactory,
  50. \Magento\Review\Model\ResourceModel\Rating\Option\CollectionFactory $ratingCollectionF,
  51. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  52. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  53. array $data = []
  54. ) {
  55. $this->_ratingOptionFactory = $ratingOptionFactory;
  56. $this->_ratingCollectionF = $ratingCollectionF;
  57. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  58. }
  59. /**
  60. * Define resource model
  61. *
  62. * @return void
  63. */
  64. protected function _construct()
  65. {
  66. $this->_init(\Magento\Review\Model\ResourceModel\Rating::class);
  67. }
  68. /**
  69. * @param int $optionId
  70. * @param int $entityPkValue
  71. * @return $this
  72. */
  73. public function addOptionVote($optionId, $entityPkValue)
  74. {
  75. $this->_ratingOptionFactory->create()->setOptionId(
  76. $optionId
  77. )->setRatingId(
  78. $this->getId()
  79. )->setReviewId(
  80. $this->getReviewId()
  81. )->setEntityPkValue(
  82. $entityPkValue
  83. )->addVote();
  84. return $this;
  85. }
  86. /**
  87. * @param int $optionId
  88. * @return $this
  89. */
  90. public function updateOptionVote($optionId)
  91. {
  92. $this->_ratingOptionFactory->create()->setOptionId(
  93. $optionId
  94. )->setVoteId(
  95. $this->getVoteId()
  96. )->setReviewId(
  97. $this->getReviewId()
  98. )->setDoUpdate(
  99. 1
  100. )->addVote();
  101. return $this;
  102. }
  103. /**
  104. * retrieve rating options
  105. *
  106. * @return array
  107. */
  108. public function getOptions()
  109. {
  110. $options = $this->getData('options');
  111. if ($options) {
  112. return $options;
  113. } elseif ($this->getId()) {
  114. return $this->_ratingCollectionF->create()->addRatingFilter(
  115. $this->getId()
  116. )->setPositionOrder()->load()->getItems();
  117. }
  118. return [];
  119. }
  120. /**
  121. * Get rating collection object
  122. *
  123. * @param int $entityPkValue
  124. * @param bool $onlyForCurrentStore
  125. * @return \Magento\Framework\Data\Collection\AbstractDb
  126. */
  127. public function getEntitySummary($entityPkValue, $onlyForCurrentStore = true)
  128. {
  129. $this->setEntityPkValue($entityPkValue);
  130. return $this->_getResource()->getEntitySummary($this, $onlyForCurrentStore);
  131. }
  132. /**
  133. * @param int $reviewId
  134. * @param bool $onlyForCurrentStore
  135. * @return array
  136. */
  137. public function getReviewSummary($reviewId, $onlyForCurrentStore = true)
  138. {
  139. $this->setReviewId($reviewId);
  140. return $this->_getResource()->getReviewSummary($this, $onlyForCurrentStore);
  141. }
  142. /**
  143. * Get rating entity type id by code
  144. *
  145. * @param string $entityCode
  146. * @return int
  147. */
  148. public function getEntityIdByCode($entityCode)
  149. {
  150. return $this->getResource()->getEntityIdByCode($entityCode);
  151. }
  152. /**
  153. * Return unique ID(s) for each object in system
  154. *
  155. * @return array
  156. */
  157. public function getIdentities()
  158. {
  159. // clear cache for all reviews
  160. return [Review::CACHE_TAG];
  161. }
  162. }