BxBaseScoreServices.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System services related to Score.
  11. */
  12. class BxBaseScoreServices extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. /**
  19. * @page service Service Calls
  20. * @section bx_system_general System Services
  21. * @subsection bx_system_general-score Score
  22. * @subsubsection bx_system_general-do do
  23. *
  24. * @code bx_srv('system', 'do', [[...]], 'TemplScoreServices'); @endcode
  25. * @code {{~system:do:TemplScoreServices[[...]]~}} @endcode
  26. *
  27. * Performs Do (Up/Down) action
  28. * @param $aParams an array with necessary parameters
  29. *
  30. * @see BxBaseScoreServices::serviceDo
  31. */
  32. /**
  33. * @ref bx_system_general-do "do"
  34. * @api @ref bx_system_general-do "do"
  35. */
  36. public function serviceDo($aParams)
  37. {
  38. if(is_string($aParams))
  39. $aParams = json_decode($aParams, true);
  40. if(!$aParams['s'] || !$aParams['o'] || !$aParams['a'])
  41. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  42. $oScore = BxDolScore::getObjectInstance($aParams['s'], $aParams['o']);
  43. if(!$oScore || !$oScore->isEnabled())
  44. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  45. $aResult = $oScore->vote(['type' => $aParams['a']]);
  46. if((int)$aResult['code'] != 0)
  47. return $aResult;
  48. return [
  49. 'is_voted' => $aResult['voted'],
  50. 'is_disabled' => $aResult['disabled'],
  51. $aParams['a'] => [
  52. 'icon' => $aResult['label_icon'],
  53. 'title' => $aResult['label_title'],
  54. ],
  55. 'counter' => $oScore->getVote()
  56. ];
  57. }
  58. /**
  59. * @page service Service Calls
  60. * @section bx_system_general System Services
  61. * @subsection bx_system_general-score Score
  62. * @subsubsection bx_system_general-get_performed_by get_performed_by
  63. *
  64. * @code bx_srv('system', 'get_performed_by', [[...]], 'TemplScoreServices'); @endcode
  65. * @code {{~system:get_performed_by:TemplScoreServices[[...]]~}} @endcode
  66. *
  67. * Gets a list of PerformedBy users.
  68. * @param $aParams an array with necessary parameters
  69. *
  70. * @see BxBaseScoreServices::serviceGetPerformedBy
  71. */
  72. /**
  73. * @ref bx_system_general-get_performed_by "get_performed_by"
  74. * @api @ref bx_system_general-get_performed_by "get_performed_by"
  75. */
  76. public function serviceGetPerformedBy($aParams)
  77. {
  78. if(is_string($aParams))
  79. $aParams = json_decode($aParams, true);
  80. if(!$aParams['s'] || !$aParams['o'])
  81. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  82. $oScore = BxDolScore::getObjectInstance($aParams['s'], $aParams['o']);
  83. if(!$oScore || !$oScore->isEnabled())
  84. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  85. $aValues = $oScore->getQueryObject()->getPerformedBy($oScore->getId());
  86. $aTmplUsers = [];
  87. foreach($aValues as $aValue)
  88. $aTmplUsers[] = [
  89. 'author_data' => BxDolProfile::getData($aValue['id']),
  90. 'vote_type' => $aValue['vote_type'],
  91. 'vote_date' => $aValue['vote_date']
  92. ];
  93. return [
  94. 'performed_by' => $aTmplUsers
  95. ];
  96. }
  97. }
  98. /** @} */