BxBaseModTextVotePollAnswers.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 BaseText Base classes for text modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModTextVotePollAnswers extends BxTemplVoteLikes
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_aObjectInfo;
  16. protected $_aPollInfo;
  17. protected $_bContentInfo;
  18. protected $_aContentInfo;
  19. protected $_bHiddenResults;
  20. protected $_bAnonymousVoting;
  21. protected $_sTmplNameElementBlock;
  22. function __construct($sSystem, $iId, $iInit = 1)
  23. {
  24. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  25. parent::__construct($sSystem, $iId, $iInit, $this->_oModule->_oTemplate);
  26. $CNF = &$this->_oModule->_oConfig->CNF;
  27. $this->_aElementDefaults = array_merge($this->_aElementDefaults, array(
  28. 'show_do_vote_label' => true,
  29. 'show_counter' => false
  30. ));
  31. $this->_aObjectInfo = $this->_oModule->_oDb->getPollAnswers(array('type' => 'id', 'id' => $iId));
  32. $this->_aPollInfo = $this->_oModule->_oDb->getPolls(array('type' => 'answer_id', 'answer_id' => $iId));
  33. $this->_aContentInfo = [];
  34. if(!empty($this->_aPollInfo[$CNF['FIELD_POLL_CONTENT_ID']]))
  35. $this->_aContentInfo = $this->_oModule->_oDb->getContentInfoById($this->_aPollInfo[$CNF['FIELD_POLL_CONTENT_ID']]);
  36. $this->_bContentInfo = !empty($this->_aContentInfo) && is_array($this->_aContentInfo);
  37. $this->_bHiddenResults = $CNF['PARAM_POLL_HIDDEN_RESULTS'];
  38. $this->_bAnonymousVoting = $CNF['PARAM_POLL_ANONYMOUS_VOTING'];
  39. $this->_sTmplNameElementBlock = 'poll_answer_ve_block.html';
  40. }
  41. public function getJsClick($iValue = 0)
  42. {
  43. $CNF = &$this->_oModule->_oConfig->CNF;
  44. $sJsObjectVote = $this->getJsObjectName();
  45. $sJsObjectPoll = $this->_oModule->_oConfig->getJsObject('poll');
  46. return $sJsObjectVote . '.vote(this, ' . $this->getValue() . ', function(oLink, oData) {' . $sJsObjectPoll . '.onPollAnswerVote(oLink, oData, ' . $this->_aPollInfo[$CNF['FIELD_POLL_ID']] . ');})';
  47. }
  48. public function getCounter($aParams = array())
  49. {
  50. $CNF = &$this->_oModule->_oConfig->CNF;
  51. $bShowInBrackets = !isset($aParams['show_counter_in_brackets']) || $aParams['show_counter_in_brackets'] == true;
  52. $iObjectId = $this->getId();
  53. $iAuthorId = $this->_getAuthorId();
  54. if($this->_bHiddenResults)
  55. if(!$this->isPerformed($iObjectId, $iAuthorId))
  56. return '';
  57. $sResult = parent::getCounter($aParams);
  58. if($bShowInBrackets)
  59. $sResult = '(' . $sResult . ')';
  60. return $sResult;
  61. }
  62. public function getObjectAuthorId($iObjectId = 0)
  63. {
  64. if(empty($this->_aSystem['trigger_field_author']))
  65. return 0;
  66. $aPoll = $this->_oModule->_oDb->getPolls(array('type' => 'answer_id', 'answer_id' => $iObjectId ? $iObjectId : $this->getId()));
  67. if(empty($aPoll) || !is_array($aPoll))
  68. return 0;
  69. return $aPoll[$this->_aSystem['trigger_field_author']];
  70. }
  71. public function isPerformed($iObjectId, $iAuthorId, $iAuthorIp = 0)
  72. {
  73. $CNF = &$this->_oModule->_oConfig->CNF;
  74. return $this->_oModule->isPollPerformed((int)$this->_aPollInfo[$CNF['FIELD_POLL_ID']], $iAuthorId, $iAuthorIp);
  75. }
  76. /**
  77. * Permissions functions
  78. */
  79. public function isAllowedVote($isPerformAction = false)
  80. {
  81. if($this->_bContentInfo && $this->_oModule->checkAllowedView($this->_aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  82. return false;
  83. return parent::isAllowedVote($isPerformAction);
  84. }
  85. public function isAllowedVoteViewVoters($isPerformAction = false)
  86. {
  87. if($this->_bAnonymousVoting)
  88. return false;
  89. return parent::isAllowedVoteViewVoters($isPerformAction);
  90. }
  91. /**
  92. * Internal functions
  93. */
  94. protected function _isAllowedVoteByObject($aObject)
  95. {
  96. /**
  97. * If a poll wasn't attached to any content yet,
  98. * then we cannot check content's visibility.
  99. */
  100. if(!$this->_bContentInfo)
  101. return true;
  102. return parent::_isAllowedVoteByObject($this->_aContentInfo);
  103. }
  104. protected function _getIconDo($bVoted)
  105. {
  106. return $bVoted ? 'far dot-circle' : 'far circle';
  107. }
  108. protected function _getTitleDo($bVoted)
  109. {
  110. return bx_process_output($this->_aObjectInfo['title']);
  111. }
  112. protected function _getTitleDoBy($aParams = array())
  113. {
  114. $CNF = &$this->_oModule->_oConfig->CNF;
  115. return _t($CNF['T']['txt_poll_answer_vote_do_by']);
  116. }
  117. protected function _getCounterLabel($iCount, $aParams = array())
  118. {
  119. $CNF = &$this->_oModule->_oConfig->CNF;
  120. return _t($CNF['T']['txt_poll_answer_vote_counter'], $iCount);
  121. }
  122. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  123. {
  124. return !isset($aParams['show_do_vote']) || $aParams['show_do_vote'] == true;
  125. }
  126. protected function _getTmplContentElementBlock()
  127. {
  128. return $this->_oTemplate->getHtml($this->_sTmplNameElementBlock);
  129. }
  130. }
  131. /** @} */