BxReputationModule.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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, $bManage = false)
  137. {
  138. $mixedResult = $this->_oTemplate->getBlockLeaderboard([
  139. 'context_id' => (int)$iContextId,
  140. 'days' => (int)$iDays,
  141. 'filters' => (bool)$bFilters,
  142. 'manage' => (bool)$bManage
  143. ]);
  144. return !$this->_bIsApi ? $mixedResult : [
  145. bx_api_get_block('reputation_leaderboard', $mixedResult)
  146. ];
  147. }
  148. public function serviceGetBlockManage($iContextId = 0)
  149. {
  150. $CNF = &$this->_oConfig->CNF;
  151. if(!$iContextId && ($_iContextId = bx_get('context_id')) !== false)
  152. $iContextId = (int)$_iContextId;
  153. if(($mixedResult = $this->checkAllowedManage($iContextId)) !== CHECK_ACTION_RESULT_ALLOWED)
  154. return $this->_bIsApi ? [bx_api_get_msg($mixedResult)] : MsgBox($mixedResult);
  155. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_MANAGE_LEADERBOARD']);
  156. if(!$oGrid)
  157. return '';
  158. $oGrid->setContextId($iContextId);
  159. if($this->_bIsApi)
  160. return [
  161. bx_api_get_block('grid', $oGrid->getCodeAPI())
  162. ];
  163. return $oGrid->getCode();
  164. }
  165. public function serviceGetBlockWidget($iProfileId = 0, $iContextId = 0, $aParams = [])
  166. {
  167. $iLoggedId = 0;
  168. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  169. $iProfileId = $iLoggedId;
  170. if(!$iProfileId)
  171. return false;
  172. if(!$this->_bIsApi)
  173. return false;
  174. $sModule = $this->getName();
  175. $sLangKey = '_bx_reputation_page_block_title_';
  176. $sParamProfile = '&params[]=' . $iProfileId;
  177. $sParamContext = '&params[]=' . $iContextId;
  178. $aTabs = [
  179. 'leaderboard_week' => [
  180. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=7',
  181. 'title' => _t($sLangKey . 'leaderboard_week'),
  182. ],
  183. 'leaderboard_month' => [
  184. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '&params[]=30',
  185. 'title' => _t($sLangKey . 'leaderboard_month')
  186. ],
  187. 'leaderboard_all_time' => [
  188. 'url' => '/api.php?r=' . $sModule . '/get_block_leaderboard' . $sParamContext . '',
  189. 'title' => _t($sLangKey . 'leaderboard_all_time')
  190. ],
  191. 'summary' => [
  192. 'url' => '/api.php?r=' . $sModule . '/get_block_summary' . $sParamProfile . $sParamContext,
  193. 'title' => _t($sLangKey . 'summary' . ($iProfileId == $iLoggedId ? '_own' : '')),
  194. ]
  195. ];
  196. if(($sKey = 'tabs_order') && !empty($aParams[$sKey]) && is_array($aParams[$sKey]))
  197. $aTabs = array_merge(array_flip($aParams[$sKey]), $aTabs);
  198. $sTabSelected = ($sKey = 'tab_selected') && !empty($aParams[$sKey]) && isset($aTabs[$aParams[$sKey]]) ? $aParams[$sKey] : array_key_first($aTabs);
  199. $aTabs[$sTabSelected] = array_merge($aTabs[$sTabSelected], [
  200. 'selected' => true,
  201. 'data' => $this->{'_getTabData' . bx_gen_method_name($sTabSelected)}((int)$iProfileId, (int)$iContextId),
  202. ]);
  203. return [
  204. bx_api_get_block('reputation_widget', [
  205. 'tabs'=> array_values($aTabs)
  206. ])
  207. ];
  208. }
  209. private function _getTabDataLeaderboardWeek($iProfileId, $iContextId)
  210. {
  211. return $this->_oTemplate->getBlockLeaderboard([
  212. 'profile_id' => $iProfileId,
  213. 'context_id' => $iContextId,
  214. 'days' => 7
  215. ]);
  216. }
  217. private function _getTabDataLeaderboardMonth($iProfileId, $iContextId)
  218. {
  219. return $this->_oTemplate->getBlockLeaderboard([
  220. 'profile_id' => $iProfileId,
  221. 'context_id' => $iContextId,
  222. 'days' => 30
  223. ]);
  224. }
  225. private function _getTabDataLeaderboardAllTime($iProfileId, $iContextId)
  226. {
  227. return $this->_oTemplate->getBlockLeaderboard([
  228. 'profile_id' => $iProfileId,
  229. 'context_id' => $iContextId
  230. ]);
  231. }
  232. private function _getTabDataSummary($iProfileId, $iContextId)
  233. {
  234. return $this->_oTemplate->getBlockSummary($iProfileId, $iContextId);
  235. }
  236. /**
  237. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  238. */
  239. public function checkAllowedManage ($iContextId = 0, $isPerformAction = false)
  240. {
  241. // moderator have access
  242. if($this->_isModerator($isPerformAction))
  243. return CHECK_ACTION_RESULT_ALLOWED;
  244. // check for context's admins
  245. if($iContextId && ($oContext = BxDolProfile::getInstance($iContextId)) !== false) {
  246. $sModule = $oContext->getModule();
  247. $oModule = BxDolModule::getInstance($sModule);
  248. // check for context's extra roles with rights
  249. if (method_exists($oModule, 'isAllowedModuleActionByProfile')) {
  250. $bResult = $oModule->isAllowedModuleActionByProfile($oContext->getContentId(), $this->getName(), 'edit_any');
  251. if($bResult !== NULL)
  252. return $bResult;
  253. }
  254. $aEntity = [];
  255. if(($sMethod = 'get_all') && bx_is_srv($sModule, $sMethod))
  256. $aEntity = bx_srv($sModule, $sMethod, [['type' => 'id', 'id' => $oContext->getContentId()]]);
  257. // if allowed edit a group then allowed to edit anything inside its context
  258. if(!empty($aEntity) && $oModule->checkAllowedEdit($aEntity) === CHECK_ACTION_RESULT_ALLOWED)
  259. return CHECK_ACTION_RESULT_ALLOWED;
  260. }
  261. return _t('_sys_txt_access_denied');
  262. }
  263. /**
  264. * COMMON METHODS
  265. */
  266. public function assignPoints($iProfileId, $iContextId, $iPoints)
  267. {
  268. if(!$this->_oDb->insertProfile($iProfileId, $iContextId, $iPoints))
  269. return false;
  270. $this->assignLevels($iProfileId, $iContextId);
  271. return true;
  272. }
  273. public function assignLevels($iProfileId, $iContextId)
  274. {
  275. $bMultilevel = $this->_oConfig->isMultilevel();
  276. $iProfilePoints = $this->_oDb->getProfilePoints($iProfileId, $iContextId);
  277. $aLevels = $this->_oDb->getLevels([
  278. 'sample' => 'points',
  279. 'points' => $iProfilePoints
  280. ]);
  281. if($bMultilevel)
  282. $this->_oDb->deleteProfilesLevelsByPoints($iProfileId, $iContextId, $iProfilePoints);
  283. foreach($aLevels as $aLevel) {
  284. $iLevelId = (int)$aLevel['id'];
  285. if($this->hasLevel($iProfileId, $iContextId, $iLevelId))
  286. continue;
  287. if(!$bMultilevel)
  288. $this->_oDb->deleteProfilesLevels([
  289. 'sample' => 'profile_id',
  290. 'profile_id' => $iProfileId,
  291. 'context_id' => $iContextId
  292. ]);
  293. $this->_oDb->insertProfilesLevels($iProfileId, $iContextId, $iLevelId);
  294. }
  295. }
  296. public function hasLevel($iProfileId, $iContextId, $iLevelId)
  297. {
  298. $aProfileLevels = $this->_oDb->getLevels([
  299. 'sample' => 'profile_id',
  300. 'profile_id' => $iProfileId,
  301. 'context_id' => $iContextId
  302. ]);
  303. foreach($aProfileLevels as $aProfileLevel)
  304. if($aProfileLevel['id'] == $iLevelId)
  305. return true;
  306. return false;
  307. }
  308. public function getUnitTitle($sUnit)
  309. {
  310. if(($sKey = '_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  311. return $_sKey;
  312. if(($sKey = '_bx_reputation_txt_au_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  313. return $_sKey;
  314. if(($iPosition = strpos($sUnit, '_fans')) !== false)
  315. return _t('_bx_reputation_txt_au_module_fans', _t('_' . substr($sUnit, 0, $iPosition)));
  316. return $sUnit;
  317. }
  318. public function getActionTitle($sAction)
  319. {
  320. return _t('_bx_reputation_txt_aa_' . $sAction);
  321. }
  322. protected function _prepareParamsGet($mParams = false)
  323. {
  324. $aKeys = ['name', 'context_id', 'days', 'username', 'start', 'per_page'];
  325. $aParams = [];
  326. if(!empty($mParams) && is_array($mParams))
  327. foreach($aKeys as $sKey)
  328. $aParams[$sKey] = isset($mParams[$sKey]) ? $mParams[$sKey] : false;
  329. else
  330. foreach($aKeys as $sKey)
  331. $aParams[$sKey] = bx_get($sKey);
  332. $aParams['name'] = $aParams['name'] !== false ? bx_process_input($aParams['name']) : '';
  333. $aParams['context_id'] = $aParams['context_id'] !== false ? bx_process_input($aParams['context_id'], BX_DATA_INT) : 0;
  334. $aParams['days'] = $aParams['days'] !== false ? bx_process_input(str_replace('d-', '', $aParams['days']), BX_DATA_INT) : 0;
  335. $aParams['username'] = $aParams['username'] !== false ? bx_process_input($aParams['username']) : '';
  336. $aParams['start'] = $aParams['start'] !== false ? bx_process_input($aParams['start'], BX_DATA_INT) : 0;
  337. $aParams['per_page'] = $aParams['per_page'] !== false ? bx_process_input($aParams['per_page'], BX_DATA_INT) : 12;
  338. return array_merge($aParams, [
  339. 'dynamic_mode' => true,
  340. ]);
  341. }
  342. }
  343. /** @} */