BxReputationModule.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 serviceGetSafeServices()
  41. {
  42. return array_merge(parent::serviceGetSafeServices(), [
  43. 'GetBlockActions' => '',
  44. 'GetBlockLevels' => '',
  45. 'GetBlockLeaderboard' => '',
  46. 'GetBlockSummary' => '',
  47. ]);
  48. }
  49. public function serviceAssignPoints($iProfileId, $iPoints, $iContextId = 0)
  50. {
  51. return $this->assignPoints($iProfileId, $iContextId, $iPoints);
  52. }
  53. public function serviceGetLevels($iProfileId)
  54. {
  55. return $this->_oDb->getLevels([
  56. 'sample' => 'profile_id',
  57. 'profile_id' => $iProfileId
  58. ]);
  59. }
  60. public function serviceGetBlockActions($iStart = 0, $iLimit = 0)
  61. {
  62. if(($iGetStart = bx_get('start')) !== false)
  63. $iStart = (int)$iGetStart;
  64. if(($iGetPerPage = bx_get('per_page')) !== false)
  65. $iLimit = (int)$iGetPerPage;
  66. $mixedResult = $this->_oTemplate->getBlockActions($iStart, $iLimit);
  67. return !$this->_bIsApi ? $mixedResult : [
  68. bx_api_get_block('reputation_actions', $mixedResult)
  69. ];
  70. }
  71. public function serviceGetBlockLevels()
  72. {
  73. $mixedResult = $this->_oTemplate->getBlockLevels();
  74. return !$this->_bIsApi ? $mixedResult : [
  75. bx_api_get_block('reputation_levels', $mixedResult)
  76. ];
  77. }
  78. public function serviceGetBlockSummary($iProfileId = 0, $iContextId = 0)
  79. {
  80. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  81. $iProfileId = $iLoggedId;
  82. if(!$iProfileId)
  83. return false;
  84. $mixedResult = $this->_oTemplate->getBlockSummary($iProfileId, $iContextId);
  85. return !$this->_bIsApi ? $mixedResult : [
  86. bx_api_get_block('reputation_summary', $mixedResult)
  87. ];
  88. }
  89. public function serviceGetBlockHistory($iProfileId = 0, $iContextId = 0, $iStart = 0, $iLimit = 0)
  90. {
  91. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  92. $iProfileId = $iLoggedId;
  93. if(!$iProfileId)
  94. return false;
  95. if(($iGetStart = bx_get('start')) !== false)
  96. $iStart = (int)$iGetStart;
  97. if(($iGetPerPage = bx_get('per_page')) !== false)
  98. $iLimit = (int)$iGetPerPage;
  99. return $this->_oTemplate->getBlockHistory($iProfileId, $iContextId, $iStart, $iLimit);
  100. }
  101. public function serviceGetBlockLeaderboard($iContextId = 0, $iDays = 0)
  102. {
  103. $mixedResult = $this->_oTemplate->getBlockLeaderboard($iContextId, $iDays);
  104. return !$this->_bIsApi ? $mixedResult : [
  105. bx_api_get_block('reputation_leaderboard', $mixedResult)
  106. ];
  107. }
  108. public function serviceGetBlockWidget($iProfileId = 0, $iContextId = 0, $aParams = [])
  109. {
  110. $iLoggedId = 0;
  111. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  112. $iProfileId = $iLoggedId;
  113. if(!$iProfileId)
  114. return false;
  115. if(!$this->_bIsApi)
  116. return false;
  117. $sModule = $this->getName();
  118. $sLangKey = '_bx_reputation_page_block_title_';
  119. $sParamProfile = '&params[]=' . $iProfileId;
  120. $sParamContext = '&params[]=' . $iContextId;
  121. $aTabs = [
  122. 'leaderboard_week' => [
  123. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=7',
  124. 'title' => _t($sLangKey . 'leaderboard_week'),
  125. ],
  126. 'leaderboard_month' => [
  127. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=30',
  128. 'title' => _t($sLangKey . 'leaderboard_month')
  129. ],
  130. 'leaderboard_all_time' => [
  131. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '',
  132. 'title' => _t($sLangKey . 'leaderboard_all_time')
  133. ],
  134. 'summary' => [
  135. 'url' => '/api.php?r=' . $sModule . '/get_block_summary' . $sParamProfile . $sParamContext,
  136. 'title' => _t($sLangKey . 'summary' . ($iProfileId == $iLoggedId ? '_own' : '')),
  137. ]
  138. ];
  139. if(($sKey = 'tabs_order') && !empty($aParams[$sKey]) && is_array($aParams[$sKey]))
  140. $aTabs = array_merge(array_flip($aParams[$sKey]), $aTabs);
  141. $sTabSelected = ($sKey = 'tab_selected') && !empty($aParams[$sKey]) && isset($aTabs[$aParams[$sKey]]) ? $aParams[$sKey] : array_key_first($aTabs);
  142. $aTabs[$sTabSelected] = array_merge($aTabs[$sTabSelected], [
  143. 'selected' => true,
  144. 'data' => $this->{'_getTabData' . bx_gen_method_name($sTabSelected)}((int)$iProfileId, (int)$iContextId),
  145. ]);
  146. return [
  147. bx_api_get_block('reputation_widget', [
  148. 'tabs'=> array_values($aTabs)
  149. ])
  150. ];
  151. }
  152. private function _getTabDataLeaderboardWeek($iProfileId, $iContextId)
  153. {
  154. return $this->_oTemplate->getBlockLeaderboard($iContextId, 7);
  155. }
  156. private function _getTabDataLeaderboardMonth($iProfileId, $iContextId)
  157. {
  158. return $this->_oTemplate->getBlockLeaderboard($iContextId, 30);
  159. }
  160. private function _getTabDataLeaderboardAllTime($iProfileId, $iContextId)
  161. {
  162. return $this->_oTemplate->getBlockLeaderboard($iContextId);
  163. }
  164. private function _getTabDataSummary($iProfileId, $iContextId)
  165. {
  166. return $this->_oTemplate->getBlockSummary($iProfileId, $iContextId);
  167. }
  168. /**
  169. * COMMON METHODS
  170. */
  171. public function assignPoints($iProfileId, $iContextId, $iPoints)
  172. {
  173. if(!$this->_oDb->insertProfile($iProfileId, $iContextId, $iPoints))
  174. return false;
  175. $this->assignLevels($iProfileId, $iContextId);
  176. return true;
  177. }
  178. public function assignLevels($iProfileId, $iContextId)
  179. {
  180. $iProfilePoints = $this->_oDb->getProfilePoints($iProfileId, $iContextId);
  181. $aLevels = $this->_oDb->getLevels([
  182. 'sample' => 'points',
  183. 'points' => $iProfilePoints
  184. ]);
  185. $bMultilevel = $this->_oConfig->isMultilevel();
  186. if($bMultilevel)
  187. $this->_oDb->deleteProfilesLevelsByPoints($iProfileId, $iContextId, $iProfilePoints);
  188. foreach($aLevels as $aLevel) {
  189. $iLevelId = (int)$aLevel['id'];
  190. if($this->hasLevel($iProfileId, $iContextId, $iLevelId))
  191. continue;
  192. if(!$bMultilevel)
  193. $this->_oDb->deleteProfilesLevels([
  194. 'sample' => 'profile_id',
  195. 'profile_id' => $iProfileId,
  196. 'context_id' => $iContextId
  197. ]);
  198. $this->_oDb->insertProfilesLevels([
  199. 'profile_id' => $iProfileId,
  200. 'context_id' => $iContextId,
  201. 'level_id' => $iLevelId
  202. ]);
  203. }
  204. }
  205. public function hasLevel($iProfileId, $iContextId, $iLevelId)
  206. {
  207. $aProfileLevels = $this->_oDb->getLevels([
  208. 'sample' => 'profile_id',
  209. 'profile_id' => $iProfileId,
  210. 'context_id' => $iContextId
  211. ]);
  212. foreach($aProfileLevels as $aProfileLevel)
  213. if($aProfileLevel['id'] == $iLevelId)
  214. return true;
  215. return false;
  216. }
  217. public function getUnitTitle($sUnit)
  218. {
  219. if(($sKey = '_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  220. return $_sKey;
  221. if(($sKey = '_bx_reputation_txt_au_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  222. return $_sKey;
  223. if(($iPosition = strpos($sUnit, '_fans')) !== false)
  224. return _t('_bx_reputation_txt_au_module_fans', _t('_' . substr($sUnit, 0, $iPosition)));
  225. return $sUnit;
  226. }
  227. public function getActionTitle($sAction)
  228. {
  229. return _t('_bx_reputation_txt_aa_' . $sAction);
  230. }
  231. }
  232. /** @} */