BxReputationModule.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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)
  41. {
  42. return $this->assignPoints($iProfileId, $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)
  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);
  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, $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, $iStart, $iLimit);
  94. }
  95. public function serviceGetBlockLeaderboard($iDays = 0)
  96. {
  97. return $this->_oTemplate->getBlockLeaderboard($iDays);
  98. }
  99. /**
  100. * COMMON METHODS
  101. */
  102. public function assignPoints($iProfileId, $iPoints)
  103. {
  104. if(!$this->_oDb->insertProfile($iProfileId, $iPoints))
  105. return false;
  106. $iProfilePoints = $this->_oDb->getProfilePoints($iProfileId);
  107. $aLevels = $this->_oDb->getLevels([
  108. 'sample' => 'points',
  109. 'points' => $iProfilePoints
  110. ]);
  111. $bMultilevel = $this->_oConfig->isMultilevel();
  112. if($bMultilevel)
  113. $this->_oDb->deleteProfilesLevelsByPoints($iProfileId, $iProfilePoints);
  114. foreach($aLevels as $aLevel) {
  115. $iLevelId = (int)$aLevel['id'];
  116. if($this->hasLevel($iProfileId, $iLevelId))
  117. continue;
  118. if(!$bMultilevel)
  119. $this->_oDb->deleteProfilesLevels(['sample' => 'profile_id', 'profile_id' => $iProfileId]);
  120. $this->_oDb->insertProfilesLevels(['profile_id' => $iProfileId, 'level_id' => $iLevelId]);
  121. }
  122. return true;
  123. }
  124. public function hasLevel($iProfileId, $iLevelId)
  125. {
  126. $aProfileLevels = $this->_oDb->getLevels([
  127. 'sample' => 'profile_id',
  128. 'profile_id' => $iProfileId
  129. ]);
  130. foreach($aProfileLevels as $aProfileLevel)
  131. if($aProfileLevel['id'] == $iLevelId)
  132. return true;
  133. return false;
  134. }
  135. public function getUnitTitle($sUnit)
  136. {
  137. if(($sKey = '_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  138. return $_sKey;
  139. if(($sKey = '_bx_reputation_txt_au_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  140. return $_sKey;
  141. if(($iPosition = strpos($sUnit, '_fans')) !== false)
  142. return _t('_bx_reputation_txt_au_module_fans', _t('_' . substr($sUnit, 0, $iPosition)));
  143. return $sUnit;
  144. }
  145. public function getActionTitle($sAction)
  146. {
  147. return _t('_bx_reputation_txt_aa_' . $sAction);
  148. }
  149. }
  150. /** @} */