BxFdbModule.php 18 KB

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