BxFdbDb.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. /*
  12. * Module database queries
  13. */
  14. class BxFdbDb extends BxBaseModGeneralDb
  15. {
  16. function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function getQuestions($aParams = array())
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  24. $sWhereClause = $sOrderByClause = $sLimitClause = "";
  25. $sSelectClause = "`tq`.*";
  26. switch($aParams['type']) {
  27. case 'id':
  28. $aMethod['name'] = 'getRow';
  29. $aMethod['params'][1] = array(
  30. 'id' => $aParams['id']
  31. );
  32. $sWhereClause .= " AND `tq`.`id`=:id";
  33. break;
  34. case 'actual':
  35. $aMethod['name'] = 'getRow';
  36. $aMethod['params'][1] = array(
  37. 'status' => 'active'
  38. );
  39. $sWhereClause .= " AND `tq`.`status_admin`=:status";
  40. $sOrderByClause = "`tq`.`added` DESC";
  41. break;
  42. case 'all':
  43. break;
  44. }
  45. if(!empty($aParams['order_by']))
  46. $sOrderByClause = '`' . $aParams['order_by'] . '` ' . (isset($aParams['order_way']) ? strtoupper($aParams['order_way']) : 'ASC');
  47. $sOrderByClause = !empty($sOrderByClause) ? " ORDER BY " . $sOrderByClause : $sOrderByClause;
  48. $sLimitClause = !empty($sLimitClause) ? " LIMIT " . $sLimitClause : $sLimitClause;
  49. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_QUESTIONS'] . "` AS `tq` WHERE 1 " . $sWhereClause . $sOrderByClause;
  50. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  51. }
  52. public function insertQuestion($aParamsSet)
  53. {
  54. $CNF = &$this->_oConfig->CNF;
  55. if(empty($aParamsSet))
  56. return false;
  57. $sSql = "INSERT INTO `" . $CNF['TABLE_QUESTIONS'] . "` SET " . $this->arrayToSQL($aParamsSet);
  58. return $this->query($sSql);
  59. }
  60. public function updateQuestion($aParamsSet, $aParamsWhere)
  61. {
  62. $CNF = &$this->_oConfig->CNF;
  63. if(empty($aParamsSet) || empty($aParamsWhere))
  64. return false;
  65. $sSql = "UPDATE `" . $CNF['TABLE_QUESTIONS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  66. return $this->query($sSql);
  67. }
  68. public function deleteQuestion($aParams)
  69. {
  70. $CNF = &$this->_oConfig->CNF;
  71. $sSql = "DELETE FROM `" . $CNF['TABLE_QUESTIONS'] . "` WHERE " . $this->arrayToSQL($aParams, " AND ");
  72. return $this->query($sSql);
  73. }
  74. public function getAnswers($aParams = array())
  75. {
  76. $CNF = &$this->_oConfig->CNF;
  77. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  78. $sSelectClause = "`ta`.*";
  79. $sJoinClause = $sWhereClause = $sOrderByClause = "";
  80. switch($aParams['type']) {
  81. case 'id':
  82. $aMethod['name'] = 'getRow';
  83. $aMethod['params'][1] = array(
  84. 'id' => $aParams['id']
  85. );
  86. $sWhereClause .= " AND `ta`.`id`=:id";
  87. break;
  88. case 'id_for_profile':
  89. $aMethod['name'] = 'getRow';
  90. $aMethod['params'][1] = array(
  91. 'id' => $aParams['id'],
  92. 'profile_id' => $aParams['profile_id'],
  93. );
  94. $sSelectClause .= ", `tau`.`text` AS `text`, `tau`.`added` AS `added`";
  95. $sJoinClause = " INNER JOIN `" . $CNF['TABLE_ANSWERS2USERS'] . "` AS `tau` ON `ta`.`id`=`tau`.`answer_id` AND `tau`.`profile_id`=:profile_id";
  96. $sWhereClause .= " AND `ta`.`id`=:id";
  97. break;
  98. case 'ids':
  99. $aMethod['name'] = 'getAllWithKey';
  100. $aMethod['params'][1] = 'id';
  101. $sWhereClause .= " AND `ta`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  102. break;
  103. case 'question_id_pairs':
  104. $aMethod['name'] = 'getPairs';
  105. $aMethod['params'][1] = 'id';
  106. $aMethod['params'][2] = 'title';
  107. $aMethod['params'][3] = array(
  108. 'question_id' => $aParams['question_id']
  109. );
  110. $sWhereClause .= " AND `ta`.`question_id`=:question_id";
  111. break;
  112. case 'question_id':
  113. $aMethod['params'][1] = array(
  114. 'question_id' => $aParams['question_id']
  115. );
  116. $sWhereClause .= " AND `ta`.`question_id`=:question_id";
  117. break;
  118. case 'question_id_for_profile':
  119. $aMethod['params'][1] = array(
  120. 'question_id' => $aParams['question_id'],
  121. 'profile_id' => $aParams['profile_id'],
  122. );
  123. $sSelectClause .= ", `tau`.`added` AS `checked`";
  124. $sJoinClause = " LEFT JOIN `" . $CNF['TABLE_ANSWERS2USERS'] . "` AS `tau` ON `ta`.`id`=`tau`.`answer_id` AND `tau`.`profile_id`=:profile_id";
  125. $sWhereClause .= " AND `ta`.`question_id`=:question_id";
  126. break;
  127. case 'question_id_max_order':
  128. $aMethod['name'] = 'getOne';
  129. $aMethod['params'][1] = array(
  130. 'question_id' => $aParams['question_id']
  131. );
  132. $sSelectClause = "MAX(`ta`.`order`)";
  133. $sWhereClause .= " AND `ta`.`question_id`=:question_id";
  134. break;
  135. case 'answered':
  136. $aMethod['name'] = 'getOne';
  137. $aMethod['params'][1] = array(
  138. 'question_id' => $aParams['question_id'],
  139. 'profile_id' => $aParams['profile_id'],
  140. );
  141. $sSelectClause = "`tau`.`answer_id`";
  142. $sJoinClause = " INNER JOIN `" . $CNF['TABLE_ANSWERS2USERS'] . "` AS `tau` ON `ta`.`id`=`tau`.`answer_id` AND `tau`.`profile_id`=:profile_id";
  143. $sWhereClause .= " AND `ta`.`question_id`=:question_id";
  144. break;
  145. case 'all':
  146. break;
  147. }
  148. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? '`' . $aParams['order_by'] . '`' : '`ta`.`order`');
  149. $sOrderByClause .= " " . (isset($aParams['order_way']) ? strtoupper($aParams['order_way']) : 'ASC');
  150. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ANSWERS'] . "` AS `ta`" . $sJoinClause . " WHERE 1 " . $sWhereClause . $sOrderByClause;
  151. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  152. }
  153. public function insertAnswer($aParamsSet)
  154. {
  155. $CNF = &$this->_oConfig->CNF;
  156. if(empty($aParamsSet))
  157. return false;
  158. $sSql = "INSERT INTO `" . $CNF['TABLE_ANSWERS'] . "` SET " . $this->arrayToSQL($aParamsSet);
  159. return $this->query($sSql);
  160. }
  161. public function updateAnswer($aParamsSet, $aParamsWhere)
  162. {
  163. $CNF = &$this->_oConfig->CNF;
  164. if(empty($aParamsSet) || empty($aParamsWhere))
  165. return false;
  166. $sSql = "UPDATE `" . $CNF['TABLE_ANSWERS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  167. return $this->query($sSql);
  168. }
  169. public function deleteAnswer($aParams)
  170. {
  171. $CNF = &$this->_oConfig->CNF;
  172. $sSql = "DELETE FROM `" . $CNF['TABLE_ANSWERS'] . "` WHERE " . $this->arrayToSQL($aParams, " AND ");
  173. return $this->query($sSql);
  174. }
  175. public function deleteAnswerById($mixedId)
  176. {
  177. $CNF = &$this->_oConfig->CNF;
  178. if(!is_array($mixedId))
  179. $mixedId = array($mixedId);
  180. $sSql = "DELETE FROM `" . $CNF['TABLE_ANSWERS'] . "` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")";
  181. return $this->query($sSql);
  182. }
  183. public function insertAnswer2User($aParamsSet)
  184. {
  185. $CNF = &$this->_oConfig->CNF;
  186. if(empty($aParamsSet))
  187. return false;
  188. $sSql = "INSERT INTO `" . $CNF['TABLE_ANSWERS2USERS'] . "` SET " . $this->arrayToSQL($aParamsSet);
  189. return $this->query($sSql);
  190. }
  191. public function deleteAnswer2User($aParams)
  192. {
  193. $CNF = &$this->_oConfig->CNF;
  194. $sSql = "DELETE FROM `" . $CNF['TABLE_ANSWERS2USERS'] . "` WHERE " . $this->arrayToSQL($aParams, " AND ");
  195. return $this->query($sSql);
  196. }
  197. public function isAnswer($iQuestionId, $iProfileId)
  198. {
  199. $iAnswer = (int)$this->getAnswers(array(
  200. 'type' => 'answered',
  201. 'question_id' => $iQuestionId,
  202. 'profile_id' => $iProfileId
  203. ));
  204. return $iAnswer > 0 ? $iAnswer : false;
  205. }
  206. public function doAnswer($iAnswerId, $iProfileId, $sText = '')
  207. {
  208. $CNF = &$this->_oConfig->CNF;
  209. $bResult = (int)$this->insertAnswer2User(array(
  210. 'answer_id' => $iAnswerId,
  211. 'profile_id' => $iProfileId,
  212. 'text' => $sText,
  213. 'added' => time()
  214. )) > 0;
  215. if($bResult)
  216. $this->query("UPDATE `" . $CNF['TABLE_ANSWERS'] . "` SET `votes`=`votes`+1 WHERE `id`=:id", array('id' => $iAnswerId));
  217. return $bResult;
  218. }
  219. public function undoAnswer($iAnswerId, $iProfileId)
  220. {
  221. $CNF = &$this->_oConfig->CNF;
  222. $bResult = (int)$this->deleteAnswer2User(array(
  223. 'answer_id' => $iAnswerId,
  224. 'profile_id' => $iProfileId
  225. )) > 0;
  226. if($bResult)
  227. $this->query("UPDATE `" . $CNF['TABLE_ANSWERS'] . "` SET `votes`=`votes`-1 WHERE `id`=:id", array('id' => $iAnswerId));
  228. return $bResult;
  229. }
  230. }
  231. /** @} */