BxReputationModule.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 Reputation Reputation
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxReputationModule extends BxBaseModNotificationsModule
  12. {
  13. public function __construct($aModule)
  14. {
  15. parent::__construct($aModule);
  16. $this->_oConfig->init($this->_oDb);
  17. }
  18. /**
  19. * ACTION METHODS
  20. */
  21. public function actionCheckName()
  22. {
  23. $CNF = &$this->_oConfig->CNF;
  24. $sName = bx_process_input(bx_get('name'));
  25. if(empty($sName))
  26. return echoJson([]);
  27. $sResult = '';
  28. if(($iId = (int)bx_get('id')) != 0) {
  29. $aLevel = $this->_oDb->getLevels(['sample' => 'id', 'id' => $iId]);
  30. if(strcmp($sName, $aLevel[$CNF['FIELD_LEVEL_NAME']]) == 0)
  31. $sResult = $sName;
  32. }
  33. echoJson([
  34. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getLevelName($sName)
  35. ]);
  36. }
  37. /**
  38. * SERVICE METHODS
  39. */
  40. public function serviceAssignPoints($iProfileId, $iPoints, $iContextId = 0)
  41. {
  42. return $this->assignPoints($iProfileId, $iContextId, $iPoints);
  43. }
  44. public function serviceGetLevels($iProfileId)
  45. {
  46. return $this->_oDb->getLevels([
  47. 'sample' => 'profile_id',
  48. 'profile_id' => $iProfileId
  49. ]);
  50. }
  51. public function serviceGetBlockActions($iStart = 0, $iLimit = 0)
  52. {
  53. if(($iGetStart = bx_get('start')) !== false)
  54. $iStart = (int)$iGetStart;
  55. if(($iGetPerPage = bx_get('per_page')) !== false)
  56. $iLimit = (int)$iGetPerPage;
  57. $mixedResult = $this->_oTemplate->getBlockActions($iStart, $iLimit);
  58. return !$this->_bIsApi ? $mixedResult : [
  59. bx_api_get_block('reputation_actions', $mixedResult)
  60. ];
  61. }
  62. public function serviceGetBlockLevels()
  63. {
  64. $mixedResult = $this->_oTemplate->getBlockLevels();
  65. return !$this->_bIsApi ? $mixedResult : [
  66. bx_api_get_block('reputation_levels', $mixedResult)
  67. ];
  68. }
  69. public function serviceGetBlockSummary($iProfileId = 0, $iContextId = 0)
  70. {
  71. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  72. $iProfileId = $iLoggedId;
  73. if(!$iProfileId)
  74. return false;
  75. $mixedResult = $this->_oTemplate->getBlockSummary($iProfileId, $iContextId);
  76. return !$this->_bIsApi ? $mixedResult : [
  77. bx_api_get_block('reputation_summary', array_merge($mixedResult, [
  78. 'actions_list' => $this->_oTemplate->getBlockActions(),
  79. 'levels_list' => $this->_oTemplate->getBlockLevels()
  80. ]))
  81. ];
  82. }
  83. public function serviceGetBlockHistory($iProfileId = 0, $iContextId = 0, $iStart = 0, $iLimit = 0)
  84. {
  85. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  86. $iProfileId = $iLoggedId;
  87. if(!$iProfileId)
  88. return false;
  89. if(($iGetStart = bx_get('start')) !== false)
  90. $iStart = (int)$iGetStart;
  91. if(($iGetPerPage = bx_get('per_page')) !== false)
  92. $iLimit = (int)$iGetPerPage;
  93. return $this->_oTemplate->getBlockHistory($iProfileId, $iContextId, $iStart, $iLimit);
  94. }
  95. public function serviceGetBlockLeaderboard($iContextId = 0, $iDays = 0)
  96. {
  97. return $this->_oTemplate->getBlockLeaderboard($iContextId, $iDays);
  98. }
  99. public function serviceGetBlockWidget($iProfileId = 0, $iContextId = 0)
  100. {
  101. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  102. $iProfileId = $iLoggedId;
  103. if(!$iProfileId)
  104. return false;
  105. if(!$this->_bIsApi)
  106. return false;
  107. $sModule = $this->getName();
  108. $sLangKey = '_bx_reputation_page_block_title_';
  109. $sParamProfile = '&params[]=' . $iProfileId;
  110. $sParamContext = ($iContextId = (int)$iContextId) ? '&params[]=' . $iContextId : '';
  111. return [
  112. bx_api_get_block('reputation_widget', [
  113. 'tabs'=> [
  114. ['url' => '/api.php?r=' . $sModule . '/get_block_summary' . $sParamProfile . $sParamContext, 'title' => _t($sLangKey . 'summary')],
  115. ['url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=7', 'title' => _t($sLangKey . 'leaderboard_week')],
  116. ['url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=30', 'title' => _t($sLangKey . 'leaderboard_month')],
  117. ['url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '', 'title' => _t($sLangKey . 'leaderboard_all_time')]
  118. ]
  119. ])
  120. ];
  121. }
  122. /**
  123. * COMMON METHODS
  124. */
  125. public function assignPoints($iProfileId, $iContextId, $iPoints)
  126. {
  127. if(!$this->_oDb->insertProfile($iProfileId, $iContextId, $iPoints))
  128. return false;
  129. $this->assignLevels($iProfileId, $iContextId);
  130. return true;
  131. }
  132. public function assignLevels($iProfileId, $iContextId)
  133. {
  134. $iProfilePoints = $this->_oDb->getProfilePoints($iProfileId, $iContextId);
  135. $aLevels = $this->_oDb->getLevels([
  136. 'sample' => 'points',
  137. 'points' => $iProfilePoints
  138. ]);
  139. $bMultilevel = $this->_oConfig->isMultilevel();
  140. if($bMultilevel)
  141. $this->_oDb->deleteProfilesLevelsByPoints($iProfileId, $iContextId, $iProfilePoints);
  142. foreach($aLevels as $aLevel) {
  143. $iLevelId = (int)$aLevel['id'];
  144. if($this->hasLevel($iProfileId, $iContextId, $iLevelId))
  145. continue;
  146. if(!$bMultilevel)
  147. $this->_oDb->deleteProfilesLevels([
  148. 'sample' => 'profile_id',
  149. 'profile_id' => $iProfileId,
  150. 'context_id' => $iContextId
  151. ]);
  152. $this->_oDb->insertProfilesLevels([
  153. 'profile_id' => $iProfileId,
  154. 'context_id' => $iContextId,
  155. 'level_id' => $iLevelId
  156. ]);
  157. }
  158. }
  159. public function hasLevel($iProfileId, $iContextId, $iLevelId)
  160. {
  161. $aProfileLevels = $this->_oDb->getLevels([
  162. 'sample' => 'profile_id',
  163. 'profile_id' => $iProfileId,
  164. 'context_id' => $iContextId
  165. ]);
  166. foreach($aProfileLevels as $aProfileLevel)
  167. if($aProfileLevel['id'] == $iLevelId)
  168. return true;
  169. return false;
  170. }
  171. public function getUnitTitle($sUnit)
  172. {
  173. if(($sKey = '_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  174. return $_sKey;
  175. if(($sKey = '_bx_reputation_txt_au_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  176. return $_sKey;
  177. if(($iPosition = strpos($sUnit, '_fans')) !== false)
  178. return _t('_bx_reputation_txt_au_module_fans', _t('_' . substr($sUnit, 0, $iPosition)));
  179. return $sUnit;
  180. }
  181. public function getActionTitle($sAction)
  182. {
  183. return _t('_bx_reputation_txt_aa_' . $sAction);
  184. }
  185. }
  186. /** @} */