BxReputationModule.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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', $mixedResult)
  78. ];
  79. }
  80. public function serviceGetBlockHistory($iProfileId = 0, $iContextId = 0, $iStart = 0, $iLimit = 0)
  81. {
  82. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  83. $iProfileId = $iLoggedId;
  84. if(!$iProfileId)
  85. return false;
  86. if(($iGetStart = bx_get('start')) !== false)
  87. $iStart = (int)$iGetStart;
  88. if(($iGetPerPage = bx_get('per_page')) !== false)
  89. $iLimit = (int)$iGetPerPage;
  90. return $this->_oTemplate->getBlockHistory($iProfileId, $iContextId, $iStart, $iLimit);
  91. }
  92. public function serviceGetBlockLeaderboard($iContextId = 0, $iDays = 0)
  93. {
  94. return $this->_oTemplate->getBlockLeaderboard($iContextId, $iDays);
  95. }
  96. public function serviceGetBlockWidget($iProfileId = 0, $iContextId = 0)
  97. {
  98. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  99. $iProfileId = $iLoggedId;
  100. if(!$iProfileId)
  101. return false;
  102. if(!$this->_bIsApi)
  103. return false;
  104. $sModule = $this->getName();
  105. $sLangKey = '_bx_reputation_page_block_title_';
  106. $sParamProfile = '&params[]=' . $iProfileId;
  107. $sParamContext = '&params[]=' . $iContextId;
  108. return [
  109. bx_api_get_block('reputation_widget', [
  110. 'tabs'=> [[
  111. 'url' => '/api.php?r=' . $sModule . '/get_block_summary' . $sParamProfile . $sParamContext,
  112. 'title' => _t($sLangKey . 'summary'),
  113. 'data' => $this->_oTemplate->getBlockSummary($iProfileId, (int)$iContextId),
  114. 'selected' => true,
  115. ], [
  116. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=7',
  117. 'title' => _t($sLangKey . 'leaderboard_week')
  118. ], [
  119. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=30',
  120. 'title' => _t($sLangKey . 'leaderboard_month')
  121. ], [
  122. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '',
  123. 'title' => _t($sLangKey . 'leaderboard_all_time')
  124. ]]
  125. ])
  126. ];
  127. }
  128. /**
  129. * COMMON METHODS
  130. */
  131. public function assignPoints($iProfileId, $iContextId, $iPoints)
  132. {
  133. if(!$this->_oDb->insertProfile($iProfileId, $iContextId, $iPoints))
  134. return false;
  135. $this->assignLevels($iProfileId, $iContextId);
  136. return true;
  137. }
  138. public function assignLevels($iProfileId, $iContextId)
  139. {
  140. $iProfilePoints = $this->_oDb->getProfilePoints($iProfileId, $iContextId);
  141. $aLevels = $this->_oDb->getLevels([
  142. 'sample' => 'points',
  143. 'points' => $iProfilePoints
  144. ]);
  145. $bMultilevel = $this->_oConfig->isMultilevel();
  146. if($bMultilevel)
  147. $this->_oDb->deleteProfilesLevelsByPoints($iProfileId, $iContextId, $iProfilePoints);
  148. foreach($aLevels as $aLevel) {
  149. $iLevelId = (int)$aLevel['id'];
  150. if($this->hasLevel($iProfileId, $iContextId, $iLevelId))
  151. continue;
  152. if(!$bMultilevel)
  153. $this->_oDb->deleteProfilesLevels([
  154. 'sample' => 'profile_id',
  155. 'profile_id' => $iProfileId,
  156. 'context_id' => $iContextId
  157. ]);
  158. $this->_oDb->insertProfilesLevels([
  159. 'profile_id' => $iProfileId,
  160. 'context_id' => $iContextId,
  161. 'level_id' => $iLevelId
  162. ]);
  163. }
  164. }
  165. public function hasLevel($iProfileId, $iContextId, $iLevelId)
  166. {
  167. $aProfileLevels = $this->_oDb->getLevels([
  168. 'sample' => 'profile_id',
  169. 'profile_id' => $iProfileId,
  170. 'context_id' => $iContextId
  171. ]);
  172. foreach($aProfileLevels as $aProfileLevel)
  173. if($aProfileLevel['id'] == $iLevelId)
  174. return true;
  175. return false;
  176. }
  177. public function getUnitTitle($sUnit)
  178. {
  179. if(($sKey = '_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  180. return $_sKey;
  181. if(($sKey = '_bx_reputation_txt_au_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  182. return $_sKey;
  183. if(($iPosition = strpos($sUnit, '_fans')) !== false)
  184. return _t('_bx_reputation_txt_au_module_fans', _t('_' . substr($sUnit, 0, $iPosition)));
  185. return $sUnit;
  186. }
  187. public function getActionTitle($sAction)
  188. {
  189. return _t('_bx_reputation_txt_aa_' . $sAction);
  190. }
  191. }
  192. /** @} */