BxFdbModule.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. $iOwnerId = (int)$aEvent['owner_id'];
  125. $aAnswer = $this->_oDb->getAnswers(array('type' => 'id_for_profile', 'id' => $aEvent['object_id'], 'profile_id' => $iOwnerId));
  126. if(empty($aAnswer) || !is_array($aAnswer) || ($this->_oConfig->isModeNio() && (int)$aAnswer[$CNF['FIELD_ANS_IMPORTANT']] == 0))
  127. return false;
  128. $aQuestion = $this->_oDb->getQuestions(array('type' => 'id', 'id' => $aAnswer['question_id']));
  129. if(empty($aQuestion) || !is_array($aQuestion))
  130. return false;
  131. if(($aQuestion[$CNF['FIELD_STATUS_ADMIN']] != 'active'))
  132. return false;
  133. //--- Title & Description
  134. $sTitle = !empty($aAnswer[$CNF['FIELD_ANS2USR_TEXT']]) ? $aAnswer[$CNF['FIELD_ANS2USR_TEXT']] : '';
  135. $bCache = true;
  136. $aContent = $this->_getContentForTimelineAnswer($aEvent, $aQuestion, $aAnswer, $aBrowseParams);
  137. if(isset($aContent['_cache'])) {
  138. $bCache = (bool)$aContent['_cache'];
  139. unset($aContent['_cache']);
  140. }
  141. return array(
  142. '_cache' => $bCache,
  143. 'owner_id' => $iOwnerId,
  144. 'object_owner_id' => $iOwnerId,
  145. 'icon' => !empty($CNF['ICON']) ? $CNF['ICON'] : '',
  146. 'sample' => isset($CNF['T']['txt_sample_answer_single_with_article']) ? $CNF['T']['txt_sample_answer_single_with_article'] : $CNF['T']['txt_sample_single'],
  147. 'sample_wo_article' => $CNF['T']['txt_sample_answer_single'],
  148. 'sample_action' => isset($CNF['T']['txt_sample_answer_action']) ? $CNF['T']['txt_sample_answer_action'] : '',
  149. 'url' => BX_DOL_URL_ROOT,
  150. 'content' => $aContent, //a string to display or array to parse default template before displaying.
  151. 'allowed_view' => array('module' => $this->getName(), 'method' => 'get_timeline_answer_allowed_view'),
  152. 'date' => $aAnswer[$CNF['FIELD_ADDED']],
  153. 'title' => $sTitle, //may be empty.
  154. 'description' => '' //may be empty.
  155. );
  156. }
  157. public function serviceGetTimelineAnswerAllowedView($aEvent)
  158. {
  159. return CHECK_ACTION_RESULT_ALLOWED;
  160. }
  161. /**
  162. * Integration: Notifications.
  163. */
  164. public function serviceGetNotificationsData()
  165. {
  166. $sModule = $this->getName();
  167. $aResult = array(
  168. 'handlers' => array(
  169. 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' => ''),
  170. array('group' => $sModule . '_question', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited_question'),
  171. array('group' => $sModule . '_question', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted_question'),
  172. 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' => ''),
  173. array('group' => $sModule . '_answer', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted_answer_notif'),
  174. ),
  175. 'settings' => array(
  176. array('group' => 'content', 'unit' => $sModule, 'action' => 'added_question', 'types' => array('follow_member')),
  177. array('group' => 'content', 'unit' => $sModule, 'action' => 'added_answer_notif', 'types' => array('follow_member'))
  178. ),
  179. 'alerts' => array(
  180. array('unit' => $sModule, 'action' => 'added_question'),
  181. array('unit' => $sModule, 'action' => 'edited_question'),
  182. array('unit' => $sModule, 'action' => 'deleted_question'),
  183. array('unit' => $sModule, 'action' => 'added_answer_notif'),
  184. array('unit' => $sModule, 'action' => 'deleted_answer_notif')
  185. )
  186. );
  187. return $aResult;
  188. }
  189. public function serviceGetNotificationsQuestion($aEvent)
  190. {
  191. $CNF = &$this->_oConfig->CNF;
  192. $aQuestion = $this->_oDb->getQuestions(array('type' => 'id', 'id' => $aEvent['object_id']));
  193. if(empty($aQuestion) || !is_array($aQuestion) || $aQuestion[$CNF['FIELD_STATUS_ADMIN']] != 'active')
  194. return array();
  195. $sEntryUrl = BX_DOL_URL_ROOT;
  196. $sEntryCaption = strmaxtextlen($aQuestion[$CNF['FIELD_TEXT']], 20, '...');
  197. return array(
  198. 'entry_sample' => $CNF['T']['txt_sample_question_single'],
  199. 'entry_url' => $sEntryUrl,
  200. 'entry_caption' => $sEntryCaption,
  201. 'entry_author' => $aQuestion[$CNF['FIELD_AUTHOR']]
  202. );
  203. }
  204. public function serviceGetNotificationsAnswer($aEvent)
  205. {
  206. $CNF = &$this->_oConfig->CNF;
  207. $iOwnerId = (int)$aEvent['owner_id'];
  208. $oOwnerProfile = BxDolProfile::getInstance($iOwnerId);
  209. if(!$oOwnerProfile)
  210. return array();
  211. $aQuestion = $this->_oDb->getQuestions(array('type' => 'id', 'id' => $aEvent['object_id']));
  212. if(empty($aQuestion) || !is_array($aQuestion) || $aQuestion[$CNF['FIELD_STATUS_ADMIN']] != 'active')
  213. return array();
  214. $aAnswer = $this->_oDb->getAnswers(array('type' => 'id_for_profile', 'id' => $aEvent['subobject_id'], 'profile_id' => $iOwnerId));
  215. if(empty($aAnswer) || !is_array($aAnswer))
  216. return array();
  217. if($this->_oConfig->isModeNio() && (int)$aAnswer[$CNF['FIELD_ANS_IMPORTANT']] == 0)
  218. return array();
  219. $sEntryUrl = BX_DOL_URL_ROOT;
  220. $sEntryCaption = strmaxtextlen($aQuestion[$CNF['FIELD_TEXT']], 20, '...');
  221. return array(
  222. 'entry_sample' => $CNF['T']['txt_sample_question_single'],
  223. 'entry_url' => $sEntryUrl,
  224. 'entry_caption' => $sEntryCaption,
  225. 'entry_author' => $aQuestion[$CNF['FIELD_AUTHOR']],
  226. 'subentry_sample' => $CNF['T']['txt_sample_answer_single'],
  227. 'subentry_url' => $oOwnerProfile->getUrl(),
  228. );
  229. }
  230. /**
  231. * Privacy methods.
  232. */
  233. public function serviceCheckAllowedWithContentForProfile($sAction, $iContentId, $iProfileId, $isPerformAction = false)
  234. {
  235. return CHECK_ACTION_RESULT_ALLOWED;
  236. }
  237. /**
  238. * Event handlers.
  239. */
  240. public function onAddQuestion($iId)
  241. {
  242. bx_alert($this->getName(), 'added_question', $iId);
  243. }
  244. public function onEditQuestion($iId)
  245. {
  246. bx_alert($this->getName(), 'edited_question', $iId);
  247. }
  248. public function onDeleteQuestion($aQuestion)
  249. {
  250. $CNF = &$this->_oConfig->CNF;
  251. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  252. $oLanguage->deleteLanguageString($aQuestion[$CNF['FIELD_TEXT']]);
  253. $aAnswers = $this->_oDb->getAnswers(array('type' => 'question_id', 'question_id' => $aQuestion[$CNF['FIELD_ID']]));
  254. if(!empty($aAnswers) && is_array($aAnswers))
  255. foreach($aAnswers as $aAnswer) {
  256. $oLanguage->deleteLanguageString($aAnswer[$CNF['FIELD_ANS_TITLE']]);
  257. $this->_oDb->deleteAnswer(array('id' => $aAnswer[$CNF['FIELD_ANS_ID']]));
  258. $this->_oDb->deleteAnswer2User(array('answer_id' => $aAnswer[$CNF['FIELD_ANS_ID']]));
  259. }
  260. bx_alert($this->getName(), 'deleted_question', $aQuestion[$CNF['FIELD_ID']], false, array(
  261. 'question' => $aQuestion,
  262. 'answers' => $aAnswers,
  263. ));
  264. }
  265. public function onAddAnswer($iQuestionId, $iAnswerId, $iProfileId = false, $aParams = array())
  266. {
  267. $CNF = &$this->_oConfig->CNF;
  268. $sModule = $this->getName();
  269. $aAnswer = $this->_oDb->getAnswers(array('type' => 'id', 'id' => $iAnswerId));
  270. bx_alert($sModule, 'added_answer', $iAnswerId, $iProfileId, array_merge(array(
  271. 'object_author_id' => $iProfileId,
  272. 'question_id' => $iQuestionId,
  273. 'important' => $aAnswer['important'],
  274. 'data' => $aAnswer['data'],
  275. 'votes' => $aAnswer['votes']
  276. ), $aParams));
  277. bx_alert($sModule, 'added_answer_notif', $iQuestionId, $iProfileId, array_merge(array(
  278. 'object_author_id' => $iProfileId,
  279. 'subobject_id' => $iAnswerId,
  280. 'important' => $aAnswer['important'],
  281. 'data' => $aAnswer['data'],
  282. 'votes' => $aAnswer['votes']
  283. ), $aParams));
  284. }
  285. public function onDeleteAnswer($iQuestionId, $iAnswerId, $iProfileId = false, $aParams = array())
  286. {
  287. $CNF = &$this->_oConfig->CNF;
  288. $sModule = $this->getName();
  289. $aAnswer = $this->_oDb->getAnswers(array('type' => 'id', 'id' => $iAnswerId));
  290. bx_alert($sModule, 'deleted_answer', $iAnswerId, $iProfileId, array_merge(array(
  291. 'question_id' => $iQuestionId,
  292. 'important' => $aAnswer['important'],
  293. 'data' => $aAnswer['data'],
  294. 'votes' => $aAnswer['votes']
  295. ), $aParams));
  296. bx_alert($sModule, 'deleted_answer_notif', $iQuestionId, $iProfileId, array_merge(array(
  297. 'subobject_id' => $iAnswerId,
  298. 'important' => $aAnswer['important'],
  299. 'data' => $aAnswer['data'],
  300. 'votes' => $aAnswer['votes']
  301. ), $aParams));
  302. }
  303. /**
  304. * Internal methods
  305. */
  306. protected function _getContentForTimelineQuestion($aEvent, $aQuestion, $aBrowseParams = array())
  307. {
  308. $CNF = &$this->_oConfig->CNF;
  309. $sUrl = BX_DOL_URL_ROOT;
  310. //--- Title
  311. $sTitle = strmaxtextlen(_t($aQuestion[$CNF['FIELD_TEXT']]), 20, '...');
  312. return array(
  313. 'sample' => isset($CNF['T']['txt_sample_question_single_with_article']) ? $CNF['T']['txt_sample_question_single_with_article'] : $CNF['T']['txt_sample_question_single'],
  314. 'sample_wo_article' => $CNF['T']['txt_sample_question_single'],
  315. 'sample_action' => isset($CNF['T']['txt_sample_question_action']) ? $CNF['T']['txt_sample_question_action'] : '',
  316. 'url' => $sUrl,
  317. 'title' => $sTitle,
  318. 'text' => '',
  319. 'images' => array(),
  320. 'images_attach' => array(),
  321. 'videos' => array(),
  322. 'videos_attach' => array(),
  323. 'files' => array(),
  324. 'files_attach' => array()
  325. );
  326. }
  327. protected function _getContentForTimelineAnswer($aEvent, $aQuestion, $aAnswer, $aBrowseParams = array())
  328. {
  329. $CNF = &$this->_oConfig->CNF;
  330. $oOwnerProfile = BxDolProfile::getInstance((int)$aEvent['owner_id']);
  331. $sUrl = BX_DOL_URL_ROOT;
  332. if($oOwnerProfile)
  333. $sUrl = $oOwnerProfile->getUrl();
  334. //--- Title
  335. $sTitle = strmaxtextlen(_t($aQuestion[$CNF['FIELD_TEXT']]), 20, '...');
  336. $sTextKey = '_bx_feedback_txt_answer_format' . (empty($aAnswer[$CNF['FIELD_ANS2USR_TEXT']]) ? '_wo_comment' : '');
  337. $sText = _t($sTextKey, _t($aAnswer[$CNF['FIELD_ANS_TITLE']]), $aAnswer[$CNF['FIELD_ANS2USR_TEXT']]);
  338. return array(
  339. 'sample' => isset($CNF['T']['txt_sample_answer_single_with_article']) ? $CNF['T']['txt_sample_answer_single_with_article'] : $CNF['T']['txt_sample_answer_single'],
  340. 'sample_wo_article' => $CNF['T']['txt_sample_answer_single'],
  341. 'sample_action' => isset($CNF['T']['txt_sample_answer_action']) ? $CNF['T']['txt_sample_answer_action'] : '',
  342. 'url' => $sUrl,
  343. 'title' => $sTitle,
  344. 'text' => $sText,
  345. 'images' => array(),
  346. 'images_attach' => array(),
  347. 'videos' => array(),
  348. 'videos_attach' => array(),
  349. 'files' => array(),
  350. 'files_attach' => array()
  351. );
  352. }
  353. }
  354. /** @} */