BxFdbModule.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 Feedback Feedback
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxFdbModule extends BxBaseModGeneralModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. public function actionAnswer()
  18. {
  19. $iProfileId = bx_get_logged_profile_id();
  20. if(!$iProfileId)
  21. return echoJson(array('code' => 1, 'msg' => _t('_bx_feedback_err_login_required')));
  22. $iQuestionId = (int)bx_get('question_id');
  23. $iAnswerId = (int)bx_get('answer_id');
  24. if(empty($iQuestionId) || empty($iAnswerId))
  25. return echoJson(array('code' => 2, 'msg' => _t('_bx_feedback_err_wrong_data')));
  26. if(($iAnswerIdOld = $this->_oDb->isAnswer($iQuestionId, $iProfileId)) !== false)
  27. if($this->_oDb->undoAnswer($iAnswerIdOld, $iProfileId))
  28. $this->onDeleteAnswer($iQuestionId, $iAnswerIdOld, $iProfileId);
  29. $sText = bx_process_input(bx_get('text'));
  30. if(!$this->_oDb->doAnswer($iAnswerId, $iProfileId, $sText))
  31. return echoJson(array('code' => 3, 'msg' => _t('_bx_feedback_err_cannot_perform')));
  32. $this->onAddAnswer($iQuestionId, $iAnswerId, $iProfileId, array(
  33. 'text' => $sText
  34. ));
  35. return echoJson(array(
  36. 'code' => 0
  37. ));
  38. }
  39. public function serviceGetBlockQuestion()
  40. {
  41. $iProfileId = bx_get_logged_profile_id();
  42. if(!$iProfileId)
  43. return '';
  44. $aQuestion = $this->_oDb->getQuestions(array('type' => 'actual'));
  45. if(empty($aQuestion) || !is_array($aQuestion))
  46. return '';
  47. $aAnswers = $this->_oDb->getAnswers(array('type' => 'question_id_for_profile', 'question_id' => $aQuestion['id'], 'profile_id' => $iProfileId));
  48. if(empty($aAnswers) || !is_array($aAnswers))
  49. return '';
  50. return $this->_oTemplate->getBlockQuestion($aQuestion, $aAnswers);
  51. }
  52. /**
  53. * Integration methods.
  54. *
  55. * Integration: Timeline.
  56. */
  57. public function serviceGetTimelineData()
  58. {
  59. $sModule = $this->getName();
  60. return array(
  61. 'handlers' => array(
  62. array('group' => $sModule . '_question', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'added_question', 'module_name' => $sModule, 'module_method' => 'get_timeline_question', 'module_class' => 'Module', 'groupable' => 0, 'group_by' => ''),
  63. array('group' => $sModule . '_question', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited_question'),
  64. array('group' => $sModule . '_question', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted_question'),
  65. array('group' => $sModule . '_answer', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'added_answer', 'module_name' => $sModule, 'module_method' => 'get_timeline_answer', 'module_class' => 'Module', 'groupable' => 0, 'group_by' => ''),
  66. array('group' => $sModule . '_answer', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted_answer')
  67. ),
  68. 'alerts' => array(
  69. array('unit' => $sModule, 'action' => 'added_question'),
  70. array('unit' => $sModule, 'action' => 'edited_question'),
  71. array('unit' => $sModule, 'action' => 'deleted_question'),
  72. array('unit' => $sModule, 'action' => 'added_answer'),
  73. array('unit' => $sModule, 'action' => 'deleted_answer'),
  74. )
  75. );
  76. }
  77. public function serviceGetTimelineQuestion($aEvent, $aBrowseParams = array())
  78. {
  79. $aQuestion = $this->_oDb->getQuestions(array('type' => 'id', 'id' => $aEvent['object_id']));
  80. if(empty($aQuestion) || !is_array($aQuestion))
  81. return false;
  82. $CNF = &$this->_oConfig->CNF;
  83. if(($aQuestion[$CNF['FIELD_STATUS_ADMIN']] != 'active'))
  84. return false;
  85. $iUserId = $this->getUserId();
  86. $iAuthorId = (int)$aQuestion[$CNF['FIELD_AUTHOR']];
  87. $iAuthorIdAbs = abs($iAuthorId);
  88. if($iAuthorId < 0 && ((is_numeric($aEvent['owner_id']) && $iAuthorIdAbs == (int)$aEvent['owner_id']) || (is_array($aEvent['owner_id']) && in_array($iAuthorIdAbs, $aEvent['owner_id']))) && $iAuthorIdAbs != $iUserId)
  89. return false;
  90. //--- Title & Description
  91. $sTitle = !empty($aQuestion[$CNF['FIELD_TEXT']]) ? $aQuestion[$CNF['FIELD_TEXT']] : '';
  92. $iOwnerId = $iAuthorIdAbs;
  93. if(isset($aEvent['object_privacy_view']) && (int)$aEvent['object_privacy_view'] < 0)
  94. $iOwnerId = abs($aEvent['object_privacy_view']);
  95. $bCache = true;
  96. $aContent = $this->_getContentForTimelineQuestion($aEvent, $aQuestion, $aBrowseParams);
  97. if(isset($aContent['_cache'])) {
  98. $bCache = (bool)$aContent['_cache'];
  99. unset($aContent['_cache']);
  100. }
  101. return array(
  102. '_cache' => $bCache,
  103. 'owner_id' => $iOwnerId,
  104. 'object_owner_id' => $iOwnerId,
  105. 'icon' => !empty($CNF['ICON']) ? $CNF['ICON'] : '',
  106. 'sample' => isset($CNF['T']['txt_sample_question_single_with_article']) ? $CNF['T']['txt_sample_question_single_with_article'] : $CNF['T']['txt_sample_single'],
  107. 'sample_wo_article' => $CNF['T']['txt_sample_question_single'],
  108. 'sample_action' => isset($CNF['T']['txt_sample_question_action']) ? $CNF['T']['txt_sample_question_action'] : '',
  109. 'url' => BX_DOL_URL_ROOT,
  110. 'content' => $aContent, //a string to display or array to parse default template before displaying.
  111. 'allowed_view' => array('module' => $this->getName(), 'method' => 'get_timeline_question_allowed_view'),
  112. 'date' => $aQuestion[$CNF['FIELD_ADDED']],
  113. 'title' => $sTitle, //may be empty.
  114. 'description' => '' //may be empty.
  115. );
  116. }
  117. public function serviceGetTimelineQuestionAllowedView($aEvent)
  118. {
  119. return CHECK_ACTION_RESULT_ALLOWED;
  120. }
  121. public function serviceGetTimelineAnswer($aEvent, $aBrowseParams = array())
  122. {
  123. $CNF = &$this->_oConfig->CNF;
  124. $bModeAni = $this->_oConfig->isModeAni();
  125. $iOwnerId = (int)$aEvent['owner_id'];
  126. $aAnswer = $this->_oDb->getAnswers(array('type' => 'id_for_profile', 'id' => $aEvent['object_id'], 'profile_id' => $iOwnerId));
  127. if(empty($aAnswer) || !is_array($aAnswer) || ($bModeAni && (int)$aAnswer[$CNF['FIELD_ANS_IMPORTANT']] == 0))
  128. return false;
  129. $aQuestion = $this->_oDb->getQuestions(array('type' => 'id', 'id' => $aAnswer['question_id']));
  130. if(empty($aQuestion) || !is_array($aQuestion))
  131. return false;
  132. if(($aQuestion[$CNF['FIELD_STATUS_ADMIN']] != 'active'))
  133. return false;
  134. //--- Title & Description
  135. $sTitle = !empty($aAnswer[$CNF['FIELD_TEXT']]) ? $aAnswer[$CNF['FIELD_TEXT']] : '';
  136. $bCache = true;
  137. $aContent = $this->_getContentForTimelineAnswer($aEvent, $aQuestion, $aAnswer, $aBrowseParams);
  138. if(isset($aContent['_cache'])) {
  139. $bCache = (bool)$aContent['_cache'];
  140. unset($aContent['_cache']);
  141. }
  142. return array(
  143. '_cache' => $bCache,
  144. 'owner_id' => $iOwnerId,
  145. 'object_owner_id' => $iOwnerId,
  146. 'icon' => !empty($CNF['ICON']) ? $CNF['ICON'] : '',
  147. 'sample' => isset($CNF['T']['txt_sample_answer_single_with_article']) ? $CNF['T']['txt_sample_answer_single_with_article'] : $CNF['T']['txt_sample_single'],
  148. 'sample_wo_article' => $CNF['T']['txt_sample_answer_single'],
  149. 'sample_action' => isset($CNF['T']['txt_sample_answer_action']) ? $CNF['T']['txt_sample_answer_action'] : '',
  150. 'url' => BX_DOL_URL_ROOT,
  151. 'content' => $aContent, //a string to display or array to parse default template before displaying.
  152. 'allowed_view' => array('module' => $this->getName(), 'method' => 'get_timeline_answer_allowed_view'),
  153. 'date' => $aAnswer[$CNF['FIELD_ADDED']],
  154. 'title' => $sTitle, //may be empty.
  155. 'description' => '' //may be empty.
  156. );
  157. }
  158. public function serviceGetTimelineAnswerAllowedView($aEvent)
  159. {
  160. return CHECK_ACTION_RESULT_ALLOWED;
  161. }
  162. /**
  163. * Integration: Notifications.
  164. */
  165. public function serviceGetNotificationsData()
  166. {
  167. $sModule = $this->getName();
  168. $aResult = array(
  169. 'handlers' => array(
  170. array('group' => $sModule . '_question', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'added_question', 'module_name' => $sModule, 'module_method' => 'get_notifications_question', 'module_class' => 'Module', 'module_event_privacy' => ''),
  171. array('group' => $sModule . '_question', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited_question'),
  172. array('group' => $sModule . '_question', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted_question'),
  173. array('group' => $sModule . '_answer', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'added_answer_notif', 'module_name' => $sModule, 'module_method' => 'get_notifications_answer', 'module_class' => 'Module', 'module_event_privacy' => ''),
  174. array('group' => $sModule . '_answer', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted_answer_notif'),
  175. ),
  176. 'settings' => array(
  177. array('group' => 'content', 'unit' => $sModule, 'action' => 'added_question', 'types' => array('follow_member')),
  178. array('group' => 'content', 'unit' => $sModule, 'action' => 'added_answer_notif', 'types' => array('follow_member'))
  179. ),
  180. 'alerts' => array(
  181. array('unit' => $sModule, 'action' => 'added_question'),
  182. array('unit' => $sModule, 'action' => 'edited_question'),
  183. array('unit' => $sModule, 'action' => 'deleted_question'),
  184. array('unit' => $sModule, 'action' => 'added_answer_notif'),
  185. array('unit' => $sModule, 'action' => 'deleted_answer_notif')
  186. )
  187. );
  188. return $aResult;
  189. }
  190. public function serviceGetNotificationsQuestion($aEvent)
  191. {
  192. $CNF = &$this->_oConfig->CNF;
  193. $aQuestion = $this->_oDb->getQuestions(array('type' => 'id', 'id' => $aEvent['object_id']));
  194. if(empty($aQuestion) || !is_array($aQuestion) || $aQuestion[$CNF['FIELD_STATUS_ADMIN']] != 'active')
  195. return array();
  196. $sEntryUrl = BX_DOL_URL_ROOT;
  197. $sEntryCaption = strmaxtextlen($aQuestion[$CNF['FIELD_TEXT']], 20, '...');
  198. return array(
  199. 'entry_sample' => $CNF['T']['txt_sample_question_single'],
  200. 'entry_url' => $sEntryUrl,
  201. 'entry_caption' => $sEntryCaption,
  202. 'entry_author' => $aQuestion[$CNF['FIELD_AUTHOR']]
  203. );
  204. }
  205. public function serviceGetNotificationsAnswer($aEvent)
  206. {
  207. $CNF = &$this->_oConfig->CNF;
  208. $iOwnerId = (int)$aEvent['owner_id'];
  209. $oOwnerProfile = BxDolProfile::getInstance($iOwnerId);
  210. if(!$oOwnerProfile)
  211. return array();
  212. $aQuestion = $this->_oDb->getQuestions(array('type' => 'id', 'id' => $aEvent['object_id']));
  213. if(empty($aQuestion) || !is_array($aQuestion) || $aQuestion[$CNF['FIELD_STATUS_ADMIN']] != 'active')
  214. return array();
  215. $aAnswer = $this->_oDb->getAnswers(array('type' => 'id_for_profile', 'id' => $aEvent['subobject_id'], 'profile_id' => $iOwnerId));
  216. if(empty($aAnswer) || !is_array($aAnswer))
  217. return array();
  218. $sEntryUrl = BX_DOL_URL_ROOT;
  219. $sEntryCaption = strmaxtextlen($aQuestion[$CNF['FIELD_TEXT']], 20, '...');
  220. return array(
  221. 'entry_sample' => $CNF['T']['txt_sample_question_single'],
  222. 'entry_url' => $sEntryUrl,
  223. 'entry_caption' => $sEntryCaption,
  224. 'entry_author' => $aQuestion[$CNF['FIELD_AUTHOR']],
  225. 'subentry_sample' => $CNF['T']['txt_sample_answer_single'],
  226. 'subentry_url' => $oOwnerProfile->getUrl(),
  227. );
  228. }
  229. /**
  230. * Privacy methods.
  231. */
  232. public function serviceCheckAllowedWithContentForProfile($sAction, $iContentId, $iProfileId, $isPerformAction = false)
  233. {
  234. return CHECK_ACTION_RESULT_ALLOWED;
  235. }
  236. /**
  237. * Event handlers.
  238. */
  239. public function onAddQuestion($iId)
  240. {
  241. bx_alert($this->getName(), 'added_question', $iId);
  242. }
  243. public function onEditQuestion($iId)
  244. {
  245. bx_alert($this->getName(), 'edited_question', $iId);
  246. }
  247. public function onDeleteQuestion($iId)
  248. {
  249. $this->_oDb->deleteAnswer(array('question_id' => $iId));
  250. bx_alert($this->getName(), 'deleted_question', $iId);
  251. }
  252. public function onAddAnswer($iQuestionId, $iAnswerId, $iProfileId = false, $aParams = array())
  253. {
  254. $CNF = &$this->_oConfig->CNF;
  255. $sModule = $this->getName();
  256. $bModeAni = $this->_oConfig->isModeAni();
  257. $aAnswer = $this->_oDb->getAnswers(array('type' => 'id', 'id' => $iAnswerId));
  258. if(!$bModeAni || (int)$aAnswer[$CNF['FIELD_ANS_IMPORTANT']] > 0) {
  259. bx_alert($sModule, 'added_answer', $iAnswerId, $iProfileId, array_merge(array(
  260. 'object_author_id' => $iProfileId,
  261. 'question_id' => $iQuestionId,
  262. 'data' => $aAnswer['data'],
  263. 'votes' => $aAnswer['votes']
  264. ), $aParams));
  265. bx_alert($sModule, 'added_answer_notif', $iQuestionId, $iProfileId, array_merge(array(
  266. 'object_author_id' => $iProfileId,
  267. 'subobject_id' => $iAnswerId,
  268. 'data' => $aAnswer['data'],
  269. 'votes' => $aAnswer['votes']
  270. ), $aParams));
  271. }
  272. }
  273. public function onDeleteAnswer($iQuestionId, $iAnswerId, $iProfileId = false, $aParams = array())
  274. {
  275. $CNF = &$this->_oConfig->CNF;
  276. $sModule = $this->getName();
  277. $bModeAni = $this->_oConfig->isModeAni();
  278. $aAnswer = $this->_oDb->getAnswers(array('type' => 'id', 'id' => $iAnswerId));
  279. if(!$bModeAni || (int)$aAnswer[$CNF['FIELD_ANS_IMPORTANT']] > 0) {
  280. bx_alert($sModule, 'deleted_answer', $iAnswerId, $iProfileId, array_merge(array('question_id' => $iQuestionId), $aParams));
  281. bx_alert($sModule, 'deleted_answer_notif', $iQuestionId, $iProfileId, array_merge(array('subobject_id' => $iAnswerId), $aParams));
  282. }
  283. }
  284. /**
  285. * Internal methods
  286. */
  287. protected function _getContentForTimelineQuestion($aEvent, $aQuestion, $aBrowseParams = array())
  288. {
  289. $CNF = &$this->_oConfig->CNF;
  290. $sUrl = BX_DOL_URL_ROOT;
  291. //--- Title
  292. $sTitle = strmaxtextlen($aQuestion[$CNF['FIELD_TEXT']], 20, '...');
  293. return array(
  294. 'sample' => isset($CNF['T']['txt_sample_question_single_with_article']) ? $CNF['T']['txt_sample_question_single_with_article'] : $CNF['T']['txt_sample_question_single'],
  295. 'sample_wo_article' => $CNF['T']['txt_sample_question_single'],
  296. 'sample_action' => isset($CNF['T']['txt_sample_question_action']) ? $CNF['T']['txt_sample_question_action'] : '',
  297. 'url' => $sUrl,
  298. 'title' => $sTitle,
  299. 'text' => '',
  300. 'images' => array(),
  301. 'images_attach' => array(),
  302. 'videos' => array(),
  303. 'videos_attach' => array(),
  304. 'files' => array(),
  305. 'files_attach' => array()
  306. );
  307. }
  308. protected function _getContentForTimelineAnswer($aEvent, $aQuestion, $aAnswer, $aBrowseParams = array())
  309. {
  310. $CNF = &$this->_oConfig->CNF;
  311. $oOwnerProfile = BxDolProfile::getInstance((int)$aEvent['owner_id']);
  312. $sUrl = BX_DOL_URL_ROOT;
  313. if($oOwnerProfile)
  314. $sUrl = $oOwnerProfile->getUrl();
  315. //--- Title
  316. $sTitle = strmaxtextlen($aQuestion[$CNF['FIELD_TEXT']], 20, '...');
  317. $sTextKey = '_bx_feedback_txt_answer_format' . (empty($aAnswer[$CNF['FIELD_TEXT']]) ? '_wo_comment' : '');
  318. $sText = _t($sTextKey, $aAnswer[$CNF['FIELD_ANS_TITLE']], $aAnswer[$CNF['FIELD_TEXT']]);
  319. return array(
  320. 'sample' => isset($CNF['T']['txt_sample_answer_single_with_article']) ? $CNF['T']['txt_sample_answer_single_with_article'] : $CNF['T']['txt_sample_answer_single'],
  321. 'sample_wo_article' => $CNF['T']['txt_sample_answer_single'],
  322. 'sample_action' => isset($CNF['T']['txt_sample_answer_action']) ? $CNF['T']['txt_sample_answer_action'] : '',
  323. 'url' => $sUrl,
  324. 'title' => $sTitle,
  325. 'text' => $sText,
  326. 'images' => array(),
  327. 'images_attach' => array(),
  328. 'videos' => array(),
  329. 'videos_attach' => array(),
  330. 'files' => array(),
  331. 'files_attach' => array()
  332. );
  333. }
  334. }
  335. /** @} */