BxReputationModule.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 serviceGetBlockSummary($iProfileId = 0)
  52. {
  53. if(!$iProfileId && ($iLoggedId = bx_get_logged_profile_id()))
  54. $iProfileId = $iLoggedId;
  55. if(!$iProfileId)
  56. return false;
  57. return $this->_oTemplate->getBlockSummary($iProfileId);
  58. }
  59. public function serviceGetBlockLeaderboard($iDays = 0)
  60. {
  61. return $this->_oTemplate->getBlockLeaderboard($iDays);
  62. }
  63. /**
  64. * COMMON METHODS
  65. */
  66. public function assignPoints($iProfileId, $iPoints)
  67. {
  68. if(!$this->_oDb->insertProfile($iProfileId, $iPoints))
  69. return false;
  70. $iProfilePoints = $this->_oDb->getProfilePoints($iProfileId);
  71. $aLevels = $this->_oDb->getLevels([
  72. 'sample' => 'points',
  73. 'points' => $iProfilePoints
  74. ]);
  75. $bMultilevel = $this->_oConfig->isMultilevel();
  76. if($bMultilevel)
  77. $this->_oDb->deleteProfilesLevelsByPoints($iProfileId, $iProfilePoints);
  78. foreach($aLevels as $aLevel) {
  79. $iLevelId = (int)$aLevel['id'];
  80. if($this->hasLevel($iProfileId, $iLevelId))
  81. continue;
  82. if(!$bMultilevel)
  83. $this->_oDb->deleteProfilesLevels(['sample' => 'profile_id', 'profile_id' => $iProfileId]);
  84. $this->_oDb->insertProfilesLevels(['profile_id' => $iProfileId, 'level_id' => $iLevelId]);
  85. }
  86. return true;
  87. }
  88. public function hasLevel($iProfileId, $iLevelId)
  89. {
  90. $aProfileLevels = $this->_oDb->getLevels([
  91. 'sample' => 'profile_id',
  92. 'profile_id' => $iProfileId
  93. ]);
  94. foreach($aProfileLevels as $aProfileLevel)
  95. if($aProfileLevel['id'] == $iLevelId)
  96. return true;
  97. return false;
  98. }
  99. }
  100. /** @} */