BxReputationModule.php 4.5 KB

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