BxReputationModule.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. public function actionGetLeaderboard()
  38. {
  39. $aParams = $this->_prepareParamsGet();
  40. return echoJson($this->serviceGetLeaderboard($aParams));
  41. }
  42. public function actionGetFiltersLeaderboard()
  43. {
  44. $aParams = $this->_prepareParamsGet();
  45. $sContent = $this->_oTemplate->getFiltersLeaderboard($aParams);
  46. if(empty($sContent))
  47. return echoJson([]);
  48. echoJson([
  49. 'code' => 0,
  50. 'popup' => [
  51. 'html' => $sContent,
  52. 'options' => [
  53. 'closeOnOuterClick' => true,
  54. 'removeOnClose' => false,
  55. ]
  56. ],
  57. ]);
  58. }
  59. /**
  60. * SERVICE METHODS
  61. */
  62. public function serviceGetSafeServices()
  63. {
  64. return array_merge(parent::serviceGetSafeServices(), [
  65. 'GetLeaderboard' => '',
  66. 'GetBlockActions' => '',
  67. 'GetBlockLevels' => '',
  68. 'GetBlockLeaderboard' => '',
  69. 'GetBlockSummary' => '',
  70. ]);
  71. }
  72. public function serviceAssignPoints($iProfileId, $iPoints, $iContextId = 0)
  73. {
  74. return $this->assignPoints($iProfileId, $iContextId, $iPoints);
  75. }
  76. public function serviceGetLevels($iProfileId)
  77. {
  78. return $this->_oDb->getLevels([
  79. 'sample' => 'profile_id',
  80. 'profile_id' => $iProfileId
  81. ]);
  82. }
  83. public function serviceGetLeaderboard($mixedParams)
  84. {
  85. if($this->_bIsApi && is_string($mixedParams))
  86. $mixedParams = $this->_prepareParamsGet(bx_api_get_browse_params($mixedParams));
  87. $aResult = $this->_oTemplate->getBlockLeaderboard($mixedParams);
  88. if($this->_bIsApi)
  89. return $aResult;
  90. return !empty($aResult['content']) ? [
  91. 'code' => 0,
  92. 'content' => $aResult['content'],
  93. ] : [];
  94. }
  95. public function serviceGetBlockActions($iStart = 0, $iLimit = 0)
  96. {
  97. if(($iGetStart = bx_get('start')) !== false)
  98. $iStart = (int)$iGetStart;
  99. if(($iGetPerPage = bx_get('per_page')) !== false)
  100. $iLimit = (int)$iGetPerPage;
  101. $mixedResult = $this->_oTemplate->getBlockActions($iStart, $iLimit);
  102. return !$this->_bIsApi ? $mixedResult : [
  103. bx_api_get_block('reputation_actions', $mixedResult)
  104. ];
  105. }
  106. public function serviceGetBlockLevels()
  107. {
  108. $mixedResult = $this->_oTemplate->getBlockLevels();
  109. return !$this->_bIsApi ? $mixedResult : [
  110. bx_api_get_block('reputation_levels', $mixedResult)
  111. ];
  112. }
  113. public function serviceGetBlockSummary($iProfileId = 0, $iContextId = 0)
  114. {
  115. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  116. $iProfileId = $iLoggedId;
  117. if(!$iProfileId)
  118. return false;
  119. $mixedResult = $this->_oTemplate->getBlockSummary($iProfileId, $iContextId);
  120. return !$this->_bIsApi ? $mixedResult : [
  121. bx_api_get_block('reputation_summary', $mixedResult)
  122. ];
  123. }
  124. public function serviceGetBlockHistory($iProfileId = 0, $iContextId = 0, $iStart = 0, $iLimit = 0)
  125. {
  126. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  127. $iProfileId = $iLoggedId;
  128. if(!$iProfileId)
  129. return false;
  130. if(($iGetStart = bx_get('start')) !== false)
  131. $iStart = (int)$iGetStart;
  132. if(($iGetPerPage = bx_get('per_page')) !== false)
  133. $iLimit = (int)$iGetPerPage;
  134. return $this->_oTemplate->getBlockHistory($iProfileId, $iContextId, $iStart, $iLimit);
  135. }
  136. public function serviceGetBlockLeaderboard($iContextId = 0, $iDays = 0, $bFilters = false)
  137. {
  138. $mixedResult = $this->_oTemplate->getBlockLeaderboard([
  139. 'context_id' => (int)$iContextId,
  140. 'days' => (int)$iDays,
  141. 'filters' => (bool)$bFilters
  142. ]);
  143. return !$this->_bIsApi ? $mixedResult : [
  144. bx_api_get_block('reputation_leaderboard', $mixedResult)
  145. ];
  146. }
  147. public function serviceGetBlockWidget($iProfileId = 0, $iContextId = 0, $aParams = [])
  148. {
  149. $iLoggedId = 0;
  150. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  151. $iProfileId = $iLoggedId;
  152. if(!$iProfileId)
  153. return false;
  154. if(!$this->_bIsApi)
  155. return false;
  156. $sModule = $this->getName();
  157. $sLangKey = '_bx_reputation_page_block_title_';
  158. $sParamProfile = '&params[]=' . $iProfileId;
  159. $sParamContext = '&params[]=' . $iContextId;
  160. $aTabs = [
  161. 'leaderboard_week' => [
  162. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=7',
  163. 'title' => _t($sLangKey . 'leaderboard_week'),
  164. ],
  165. 'leaderboard_month' => [
  166. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=30',
  167. 'title' => _t($sLangKey . 'leaderboard_month')
  168. ],
  169. 'leaderboard_all_time' => [
  170. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '',
  171. 'title' => _t($sLangKey . 'leaderboard_all_time')
  172. ],
  173. 'summary' => [
  174. 'url' => '/api.php?r=' . $sModule . '/get_block_summary' . $sParamProfile . $sParamContext,
  175. 'title' => _t($sLangKey . 'summary' . ($iProfileId == $iLoggedId ? '_own' : '')),
  176. ]
  177. ];
  178. if(($sKey = 'tabs_order') && !empty($aParams[$sKey]) && is_array($aParams[$sKey]))
  179. $aTabs = array_merge(array_flip($aParams[$sKey]), $aTabs);
  180. $sTabSelected = ($sKey = 'tab_selected') && !empty($aParams[$sKey]) && isset($aTabs[$aParams[$sKey]]) ? $aParams[$sKey] : array_key_first($aTabs);
  181. $aTabs[$sTabSelected] = array_merge($aTabs[$sTabSelected], [
  182. 'selected' => true,
  183. 'data' => $this->{'_getTabData' . bx_gen_method_name($sTabSelected)}((int)$iProfileId, (int)$iContextId),
  184. ]);
  185. return [
  186. bx_api_get_block('reputation_widget', [
  187. 'tabs'=> array_values($aTabs)
  188. ])
  189. ];
  190. }
  191. private function _getTabDataLeaderboardWeek($iProfileId, $iContextId)
  192. {
  193. return $this->_oTemplate->getBlockLeaderboard($iContextId, 7);
  194. }
  195. private function _getTabDataLeaderboardMonth($iProfileId, $iContextId)
  196. {
  197. return $this->_oTemplate->getBlockLeaderboard($iContextId, 30);
  198. }
  199. private function _getTabDataLeaderboardAllTime($iProfileId, $iContextId)
  200. {
  201. return $this->_oTemplate->getBlockLeaderboard($iContextId);
  202. }
  203. private function _getTabDataSummary($iProfileId, $iContextId)
  204. {
  205. return $this->_oTemplate->getBlockSummary($iProfileId, $iContextId);
  206. }
  207. /**
  208. * COMMON METHODS
  209. */
  210. public function assignPoints($iProfileId, $iContextId, $iPoints)
  211. {
  212. if(!$this->_oDb->insertProfile($iProfileId, $iContextId, $iPoints))
  213. return false;
  214. $this->assignLevels($iProfileId, $iContextId);
  215. return true;
  216. }
  217. public function assignLevels($iProfileId, $iContextId)
  218. {
  219. $bMultilevel = $this->_oConfig->isMultilevel();
  220. $iProfilePoints = $this->_oDb->getProfilePoints($iProfileId, $iContextId);
  221. $aLevels = $this->_oDb->getLevels([
  222. 'sample' => 'points',
  223. 'points' => $iProfilePoints
  224. ]);
  225. if($bMultilevel)
  226. $this->_oDb->deleteProfilesLevelsByPoints($iProfileId, $iContextId, $iProfilePoints);
  227. foreach($aLevels as $aLevel) {
  228. $iLevelId = (int)$aLevel['id'];
  229. if($this->hasLevel($iProfileId, $iContextId, $iLevelId))
  230. continue;
  231. if(!$bMultilevel)
  232. $this->_oDb->deleteProfilesLevels([
  233. 'sample' => 'profile_id',
  234. 'profile_id' => $iProfileId,
  235. 'context_id' => $iContextId
  236. ]);
  237. $this->_oDb->insertProfilesLevels($iProfileId, $iContextId, $iLevelId);
  238. }
  239. }
  240. public function hasLevel($iProfileId, $iContextId, $iLevelId)
  241. {
  242. $aProfileLevels = $this->_oDb->getLevels([
  243. 'sample' => 'profile_id',
  244. 'profile_id' => $iProfileId,
  245. 'context_id' => $iContextId
  246. ]);
  247. foreach($aProfileLevels as $aProfileLevel)
  248. if($aProfileLevel['id'] == $iLevelId)
  249. return true;
  250. return false;
  251. }
  252. public function getUnitTitle($sUnit)
  253. {
  254. if(($sKey = '_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  255. return $_sKey;
  256. if(($sKey = '_bx_reputation_txt_au_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  257. return $_sKey;
  258. if(($iPosition = strpos($sUnit, '_fans')) !== false)
  259. return _t('_bx_reputation_txt_au_module_fans', _t('_' . substr($sUnit, 0, $iPosition)));
  260. return $sUnit;
  261. }
  262. public function getActionTitle($sAction)
  263. {
  264. return _t('_bx_reputation_txt_aa_' . $sAction);
  265. }
  266. protected function _prepareParamsGet($mParams = false)
  267. {
  268. $aKeys = ['name', 'context_id', 'days', 'username', 'start', 'per_page'];
  269. $aParams = [];
  270. if(!empty($mParams) && is_array($mParams))
  271. foreach($aKeys as $sKey)
  272. $aParams[$sKey] = isset($mParams[$sKey]) ? $mParams[$sKey] : false;
  273. else
  274. foreach($aKeys as $sKey)
  275. $aParams[$sKey] = bx_get($sKey);
  276. $aParams['name'] = $aParams['name'] !== false ? bx_process_input($aParams['name']) : '';
  277. $aParams['context_id'] = $aParams['context_id'] !== false ? bx_process_input($aParams['context_id'], BX_DATA_INT) : 0;
  278. $aParams['days'] = $aParams['days'] !== false ? bx_process_input(str_replace('d-', '', $aParams['days']), BX_DATA_INT) : 0;
  279. $aParams['username'] = $aParams['username'] !== false ? bx_process_input($aParams['username']) : '';
  280. $aParams['start'] = $aParams['start'] !== false ? bx_process_input($aParams['start'], BX_DATA_INT) : 0;
  281. $aParams['per_page'] = $aParams['per_page'] !== false ? bx_process_input($aParams['per_page'], BX_DATA_INT) : 12;
  282. return array_merge($aParams, [
  283. 'dynamic_mode' => true,
  284. ]);
  285. }
  286. }
  287. /** @} */