BxReputationModule.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. return $this->_oTemplate->getBlockActions($iStart, $iLimit);
  58. }
  59. public function serviceGetBlockLevels()
  60. {
  61. return $this->_oTemplate->getBlockLevels();
  62. }
  63. public function serviceGetBlockSummary($iProfileId = 0)
  64. {
  65. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  66. $iProfileId = $iLoggedId;
  67. if(!$iProfileId)
  68. return false;
  69. return $this->_oTemplate->getBlockSummary($iProfileId);
  70. }
  71. public function serviceGetBlockHistory($iProfileId = 0, $iStart = 0, $iLimit = 0)
  72. {
  73. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  74. $iProfileId = $iLoggedId;
  75. if(!$iProfileId)
  76. return false;
  77. if(($iGetStart = bx_get('start')) !== false)
  78. $iStart = (int)$iGetStart;
  79. if(($iGetPerPage = bx_get('per_page')) !== false)
  80. $iLimit = (int)$iGetPerPage;
  81. return $this->_oTemplate->getBlockHistory($iProfileId, $iStart, $iLimit);
  82. }
  83. public function serviceGetBlockLeaderboard($iDays = 0)
  84. {
  85. return $this->_oTemplate->getBlockLeaderboard($iDays);
  86. }
  87. /**
  88. * COMMON METHODS
  89. */
  90. public function assignPoints($iProfileId, $iPoints)
  91. {
  92. if(!$this->_oDb->insertProfile($iProfileId, $iPoints))
  93. return false;
  94. $iProfilePoints = $this->_oDb->getProfilePoints($iProfileId);
  95. $aLevels = $this->_oDb->getLevels([
  96. 'sample' => 'points',
  97. 'points' => $iProfilePoints
  98. ]);
  99. $bMultilevel = $this->_oConfig->isMultilevel();
  100. if($bMultilevel)
  101. $this->_oDb->deleteProfilesLevelsByPoints($iProfileId, $iProfilePoints);
  102. foreach($aLevels as $aLevel) {
  103. $iLevelId = (int)$aLevel['id'];
  104. if($this->hasLevel($iProfileId, $iLevelId))
  105. continue;
  106. if(!$bMultilevel)
  107. $this->_oDb->deleteProfilesLevels(['sample' => 'profile_id', 'profile_id' => $iProfileId]);
  108. $this->_oDb->insertProfilesLevels(['profile_id' => $iProfileId, 'level_id' => $iLevelId]);
  109. }
  110. return true;
  111. }
  112. public function hasLevel($iProfileId, $iLevelId)
  113. {
  114. $aProfileLevels = $this->_oDb->getLevels([
  115. 'sample' => 'profile_id',
  116. 'profile_id' => $iProfileId
  117. ]);
  118. foreach($aProfileLevels as $aProfileLevel)
  119. if($aProfileLevel['id'] == $iLevelId)
  120. return true;
  121. return false;
  122. }
  123. public function getUnitTitle($sUnit)
  124. {
  125. if(($sKey = '_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  126. return $_sKey;
  127. if(($sKey = '_bx_reputation_txt_au_' . $sUnit) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) != 0)
  128. return $_sKey;
  129. if(($iPosition = strpos($sUnit, '_fans')) !== false)
  130. return _t('_bx_reputation_txt_au_module_fans', _t('_' . substr($sUnit, 0, $iPosition)));
  131. return $sUnit;
  132. }
  133. public function getActionTitle($sAction)
  134. {
  135. return _t('_bx_reputation_txt_aa_' . $sAction);
  136. }
  137. }
  138. /** @} */