1
0

BxBaseModGroupsDb.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 BaseGroups Base classes for groups modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Groups module database queries
  13. */
  14. class BxBaseModGroupsDb extends BxBaseModProfileDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function updateAuthorById ($iContentId, $iProfileId)
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_AUTHOR'] . "` = :author WHERE `" . $CNF['FIELD_ID'] . "` = :id";
  24. return $this->query($sQuery, array(
  25. 'id' => $iContentId,
  26. 'author' => $iProfileId,
  27. ));
  28. }
  29. public function toAdmins ($iGroupProfileId, $mixedFansIds)
  30. {
  31. if (is_array($mixedFansIds)) {
  32. foreach ($mixedFansIds as $iFanId)
  33. $this->toAdmins ($iGroupProfileId, $iFanId);
  34. return true;
  35. }
  36. $iFanId = (int)$mixedFansIds;
  37. $sQuery = $this->prepare("INSERT IGNORE INTO `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET `group_profile_id` = ?, `fan_id` = ?, `role` = ?", $iGroupProfileId, $iFanId, BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR);
  38. if (!$this->res($sQuery))
  39. return false;
  40. $oModule = BxDolModule::getInstance($this->_oConfig->getName());
  41. if ($oModule && method_exists($oModule, 'onFanAddedToAdmins'))
  42. $oModule->onFanAddedToAdmins($iGroupProfileId, $iFanId);
  43. $oModule->doAudit($iGroupProfileId, $iFanId, '_sys_audit_action_group_to_admins');
  44. return true;
  45. }
  46. public function fromAdmins ($iGroupProfileId, $mixedFansIds)
  47. {
  48. if (is_array($mixedFansIds)) {
  49. foreach ($mixedFansIds as $iFanId)
  50. $this->fromAdmins ($iGroupProfileId, $iFanId);
  51. return true;
  52. }
  53. $iFanId = (int)$mixedFansIds;
  54. $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ? AND `fan_id` = ?", $iGroupProfileId, $iFanId);
  55. if (!$this->res($sQuery))
  56. return false;
  57. $oModule = BxDolModule::getInstance($this->_oConfig->getName());
  58. if ($oModule && method_exists($oModule, 'onFanRemovedFromAdmins'))
  59. $oModule->onFanRemovedFromAdmins($iGroupProfileId, $iFanId);
  60. $oModule->doAudit($iGroupProfileId, $iFanId, '_sys_audit_action_group_from_admins');
  61. return true;
  62. }
  63. public function deleteAdminsByGroupId ($iGroupProfileId, $iProfileId = 0)
  64. {
  65. if ($iProfileId != 0)
  66. $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ? AND `fan_id` = ?", $iGroupProfileId, $iProfileId);
  67. else
  68. $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ?", $iGroupProfileId);
  69. return $this->res($sQuery);
  70. }
  71. public function deleteAdminsByProfileId ($iProfileId)
  72. {
  73. $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `fan_id` = ?", $iProfileId);
  74. return $this->res($sQuery);
  75. }
  76. public function isAdmin ($iGroupProfileId, $iFanId, $aDataEntry = array())
  77. {
  78. $CNF = &$this->_oConfig->CNF;
  79. if(isset($aDataEntry[$CNF['FIELD_AUTHOR']]) && $iFanId == $aDataEntry[$CNF['FIELD_AUTHOR']])
  80. return true;
  81. if(!isset($CNF['TABLE_ADMINS']))
  82. return false;
  83. $aBindings = array(
  84. 'group_profile_id' => $iGroupProfileId,
  85. 'fan_id' => $iFanId,
  86. 'role' => BX_BASE_MOD_GROUPS_ROLE_COMMON
  87. );
  88. $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id AND `role` <> :role";
  89. return $this->getOne("SELECT `id` FROM `" . $CNF['TABLE_ADMINS'] . "` WHERE 1" . $sWhereClause, $aBindings) ? true : false;
  90. }
  91. public function getAdmin ($iGroupProfileId, $iProfileId, $bRoleOnly = false)
  92. {
  93. $CNF = &$this->_oConfig->CNF;
  94. $sMethod = 'getRow';
  95. $sSelectClause = '*';
  96. if($bRoleOnly) {
  97. $sMethod = 'getOne';
  98. $sSelectClause = '`role`';
  99. }
  100. $aBindings = array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId, 'role' => BX_BASE_MOD_GROUPS_ROLE_COMMON);
  101. $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id AND `role` <> :role";
  102. return $this->$sMethod("SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ADMINS'] . "` WHERE 1" . $sWhereClause . " LIMIT 1", $aBindings);
  103. }
  104. public function getAdmins ($iGroupProfileId, $iStart = 0, $iLimit = 0)
  105. {
  106. $CNF = &$this->_oConfig->CNF;
  107. $aBindings = array('group_profile_id' => $iGroupProfileId, 'role' => BX_BASE_MOD_GROUPS_ROLE_COMMON);
  108. $sWhereClause = " AND `group_profile_id` = :group_profile_id AND `role` <> :role";
  109. $sLimitClause = "";
  110. if($iLimit > 0)
  111. $sLimitClause = " LIMIT " . (int)$iStart . ", " . (int)$iLimit;
  112. return $this->getColumn("SELECT `fan_id` FROM `" . $CNF['TABLE_ADMINS'] . "` INNER JOIN `sys_profiles` as `p` ON (`p`.`id` = `fan_id` AND `p`.`status` = 'active') WHERE 1" . $sWhereClause . $sLimitClause, $aBindings);
  113. }
  114. public function getRole($iGroupProfileId, $iProfileId)
  115. {
  116. return (int)$this->getRoles(array('type' => 'by_gf_id', 'group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId, 'role_only' => true));
  117. }
  118. public function getRoles($aParams)
  119. {
  120. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  121. $sSelectClause = "`ta`.*";
  122. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  123. switch($aParams['type']) {
  124. case 'by_id':
  125. $aMethod['name'] = 'getRow';
  126. $aMethod['params'][1] = array(
  127. 'id' => $aParams['value']
  128. );
  129. $sWhereClause = "AND `ta`.`id`=:id";
  130. break;
  131. case 'by_gf_id':
  132. $aMethod['name'] = 'getRow';
  133. $aMethod['params'][1] = array(
  134. 'group_profile_id' => $aParams['group_profile_id'],
  135. 'fan_id' => $aParams['fan_id'],
  136. );
  137. $sWhereClause = "AND `group_profile_id` = :group_profile_id AND `fan_id` = :fan_id";
  138. $sLimitClause = 1;
  139. if(!empty($aParams['role_only'])) {
  140. $aMethod['name'] = 'getOne';
  141. $sSelectClause = "`ta`.`role`";
  142. }
  143. break;
  144. case 'fan_pids_by_group_pid':
  145. $aMethod['name'] = 'getColumn';
  146. $aMethod['params'][1] = array(
  147. 'group_profile_id' => $aParams['group_profile_id'],
  148. );
  149. $sSelectClause = "`ta`.`fan_id`";
  150. $sWhereClause = "AND `group_profile_id` = :group_profile_id";
  151. if(!empty($aParams['role'])) {
  152. $aMethod['params'][1]['role'] = $aParams['role'];
  153. $sWhereClause .= " AND `ta`.`role` = :role";
  154. }
  155. if(isset($aParams['start'], $aParams['limit']))
  156. $sLimitClause = $aParams['start'] . ', ' . $aParams['limit'];
  157. break;
  158. case 'group_pids_by_fan_id':
  159. $aMethod['name'] = 'getColumn';
  160. $aMethod['params'][1] = [
  161. 'fan_id' => $aParams['fan_id'],
  162. ];
  163. $sSelectClause = "`ta`.`group_profile_id`";
  164. $sWhereClause = "AND `ta`.`fan_id` = :fan_id";
  165. if(!empty($aParams['role'])) {
  166. $aMethod['params'][1]['role'] = $aParams['role'];
  167. $sWhereClause .= " AND `ta`.`role` = :role";
  168. }
  169. break;
  170. case 'expired':
  171. $sWhereClause .= "AND `ta`.`added` < UNIX_TIMESTAMP() AND `ta`.`expired` <> 0 AND `ta`.`expired` < UNIX_TIMESTAMP()";
  172. break;
  173. }
  174. if(!empty($sOrderClause))
  175. $sOrderClause = "ORDER BY " . $sOrderClause;
  176. if(!empty($sLimitClause))
  177. $sLimitClause = "LIMIT " . $sLimitClause;
  178. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` AS `ta` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  179. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  180. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  181. }
  182. public function updateRoles($aSet, $aWhere)
  183. {
  184. $sWhereClause = 1;
  185. if(!empty($aWhere))
  186. $sWhereClause = $this->arrayToSQL($aWhere, ' AND ');
  187. return (int)$this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . $sWhereClause) > 0;
  188. }
  189. public function deleteRoles($aWhere)
  190. {
  191. if(empty($aWhere))
  192. return false;
  193. return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
  194. }
  195. public function setRole ($iGroupProfileId, $iProfileId, $mixedRole, $mixedPeriod = false, $sOrder = '')
  196. {
  197. $CNF = &$this->_oConfig->CNF;
  198. if($this->_oConfig->isMultiRoles() && is_array($mixedRole)) {
  199. if(count($mixedRole) != 1 || current($mixedRole) != 0) {
  200. $iProfileRole = 0;
  201. foreach($mixedRole as $iRole)
  202. $iProfileRole = $iProfileRole | pow(2, ($iRole - 1));
  203. $mixedRole = $iProfileRole;
  204. }
  205. else
  206. $mixedRole = 0;
  207. }
  208. $aBindings = array(
  209. 'group_profile_id' => $iGroupProfileId,
  210. 'fan_id' => $iProfileId,
  211. 'role' => $mixedRole,
  212. 'added' => time()
  213. );
  214. $sSetClause = "`group_profile_id` = :group_profile_id, `fan_id` = :fan_id, `role` = :role, `added` = :added";
  215. if(!empty($mixedPeriod)) {
  216. if(is_numeric($mixedPeriod))
  217. $mixedPeriod = array('period' => (int)$mixedPeriod, 'period_unit' => BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY);
  218. $aBindings['period'] = (int)$mixedPeriod['period'];
  219. $sSetExpired = "";
  220. switch($mixedPeriod['period_unit']) {
  221. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_DAY:
  222. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK:
  223. if($mixedPeriod['period_unit'] == BX_BASE_MOD_GROUPS_PERIOD_UNIT_WEEK)
  224. $aBindings['period'] *= 7;
  225. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period DAY)";
  226. break;
  227. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_MONTH:
  228. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period MONTH)";
  229. break;
  230. case BX_BASE_MOD_GROUPS_PERIOD_UNIT_YEAR:
  231. $sSetExpired = "DATE_ADD(FROM_UNIXTIME(:added), INTERVAL :period YEAR)";
  232. break;
  233. }
  234. if(!empty($sSetExpired) && !empty($mixedPeriod['period_reserve'])) {
  235. $aBindings['reserve'] = (int)$mixedPeriod['period_reserve'];
  236. $sSetExpired = "DATE_ADD(" . $sSetExpired . ", INTERVAL :reserve DAY)";
  237. }
  238. if(!empty($sSetExpired))
  239. $sSetClause .= ", `expired` = UNIX_TIMESTAMP(" . $sSetExpired . ")";
  240. }
  241. if(!empty($sOrder)) {
  242. $aBindings['order'] = $sOrder;
  243. $sSetClause .= ", `order` = :order";
  244. }
  245. return !$this->query("REPLACE INTO `" . $CNF['TABLE_ADMINS'] . "` SET " . $sSetClause, $aBindings) ? false : true;
  246. }
  247. public function unsetRole($iGroupProfileId, $iProfileId)
  248. {
  249. return $this->deleteRoles(array('group_profile_id' => $iGroupProfileId, 'fan_id' => $iProfileId));
  250. }
  251. public function getPrices($aParams)
  252. {
  253. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  254. $sSelectClause = "`tp`.*";
  255. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  256. if(!isset($aParams['order']) || empty($aParams['order']))
  257. $sOrderClause = "ORDER BY `tp`.`Order` ASC";
  258. switch($aParams['type']) {
  259. case 'by_id':
  260. $aMethod['name'] = 'getRow';
  261. $aMethod['params'][1] = array(
  262. 'id' => $aParams['value']
  263. );
  264. $sWhereClause .= "AND `tp`.`id`=:id";
  265. break;
  266. case 'by_name':
  267. $aMethod['name'] = 'getRow';
  268. $aMethod['params'][1] = array(
  269. 'id' => $aParams['value']
  270. );
  271. $sWhereClause .= "AND `tp`.`name`=:name";
  272. break;
  273. case 'by_profile_id':
  274. $aMethod['params'][1] = array(
  275. 'profile_id' => $aParams['profile_id']
  276. );
  277. $sWhereClause .= "AND `tp`.`profile_id`=:profile_id";
  278. break;
  279. case 'by_prpp':
  280. $aMethod['name'] = 'getRow';
  281. $aMethod['params'][1] = array(
  282. 'profile_id' => $aParams['profile_id'],
  283. 'role_id' => $aParams['role_id'],
  284. 'period' => $aParams['period'],
  285. 'period_unit' => $aParams['period_unit'],
  286. );
  287. $sWhereClause .= "AND `tp`.`profile_id`=:profile_id AND `tp`.`role_id`=:role_id AND `tp`.`period`=:period AND `tp`.`period_unit`=:period_unit";
  288. break;
  289. }
  290. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` AS `tp` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  291. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  292. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  293. }
  294. public function getPriceOrderMax($iRoleId)
  295. {
  296. return (int)$this->getOne("SELECT MAX(`order`) FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE `role_id`=:role_id", array(
  297. 'role_id' => $iRoleId
  298. ));
  299. }
  300. public function deletePrices($aWhere)
  301. {
  302. return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE " . $this->arrayToSQL($aWhere, " AND ")) > 0;
  303. }
  304. public function insertInvite($sKey, $sGroupProfileId, $iAuthorProfileId, $iInvitedProfileId)
  305. {
  306. $aBindings = array(
  307. 'key' => $sKey,
  308. 'group_profile_id' => $sGroupProfileId,
  309. 'author_profile_id' => $iAuthorProfileId,
  310. 'invited_profile_id' => $iInvitedProfileId,
  311. 'added' => time()
  312. );
  313. $CNF = $this->_oConfig->CNF;
  314. $this->query("INSERT `" . $CNF["TABLE_INVITES"] . "` (`key`, `group_profile_id`, `author_profile_id`, `invited_profile_id`, `added`) VALUES (:key, :group_profile_id, :author_profile_id, :invited_profile_id, :added)", $aBindings);
  315. return (int)$this->lastId();
  316. }
  317. public function getInviteByKey($sKey, $iGroupProfileId)
  318. {
  319. $CNF = &$this->_oConfig->CNF;
  320. return $this->getRow("SELECT * FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", [
  321. 'key' => $sKey,
  322. 'group_profile_id' => $iGroupProfileId
  323. ]);
  324. }
  325. public function getInviteByInvited($iInvitedProfileId, $iGroupProfileId)
  326. {
  327. $CNF = &$this->_oConfig->CNF;
  328. return $this->getRow("SELECT * FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `invited_profile_id` = :invited_profile_id AND group_profile_id = :group_profile_id", [
  329. 'invited_profile_id' => $iInvitedProfileId,
  330. 'group_profile_id' => $iGroupProfileId
  331. ]);
  332. }
  333. public function isInviteByInvited($iInvitedProfileId, $iGroupProfileId)
  334. {
  335. $aInvite = $this->getInviteByInvited($iInvitedProfileId, $iGroupProfileId);
  336. return !empty($aInvite) && is_array($aInvite);
  337. }
  338. public function updateInviteByKey($sKey, $iGroupProfileId, $sColumn, $sValue)
  339. {
  340. $aBindings = array(
  341. 'key' => $sKey,
  342. 'value' => $sValue,
  343. 'group_profile_id' => $iGroupProfileId
  344. );
  345. $CNF = $this->_oConfig->CNF;
  346. return $this->query("UPDATE `" . $CNF["TABLE_INVITES"] . "` SET `" . $sColumn . "` = :value WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  347. }
  348. public function deleteInviteByKey($sKey, $iGroupProfileId)
  349. {
  350. $aBindings = array(
  351. 'key' => $sKey,
  352. 'group_profile_id' => $iGroupProfileId
  353. );
  354. $CNF = $this->_oConfig->CNF;
  355. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `key` = :key AND group_profile_id = :group_profile_id", $aBindings);
  356. }
  357. public function deleteInvite($iId)
  358. {
  359. $aBindings = array(
  360. 'id' => $iId
  361. );
  362. $CNF = $this->_oConfig->CNF;
  363. return $this->query("DELETE FROM `" . $CNF["TABLE_INVITES"] . "` WHERE `id` = :id", $aBindings);
  364. }
  365. public function getQuestions($aParams = [])
  366. {
  367. $CNF = &$this->_oConfig->CNF;
  368. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  369. $sSelectClause = "`tq`.*";
  370. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  371. switch($aParams['sample']) {
  372. case 'id':
  373. $aMethod['name'] = 'getRow';
  374. $aMethod['params'][1] = [
  375. 'id' => $aParams['id']
  376. ];
  377. $sWhereClause = " AND `tq`.`id`=:id";
  378. break;
  379. case 'content_id':
  380. $aMethod['params'][1] = [
  381. 'content_id' => $aParams['content_id']
  382. ];
  383. $sWhereClause = " AND `tq`.`content_id`=:content_id";
  384. $sOrderClause = "`tq`.`order` ASC";
  385. break;
  386. case 'content_pid':
  387. $aMethod['params'][1] = [
  388. 'module' => $this->_oConfig->getName(),
  389. 'content_pid' => $aParams['content_pid']
  390. ];
  391. $sJoinClause = "INNER JOIN `sys_profiles` AS `tp` ON `tq`.`content_id`=`tp`.`content_id` AND `tp`.`type`=:module";
  392. $sWhereClause = " AND `tp`.`id`=:content_pid";
  393. $sOrderClause = "`tq`.`order` ASC";
  394. break;
  395. case 'answers':
  396. $aMethod['params'][1] = [
  397. 'content_id' => $aParams['content_id'],
  398. 'profile_id' => $aParams['profile_id']
  399. ];
  400. $sSelectClause .= ", `ta`.`answer`";
  401. $sJoinClause = "INNER JOIN `" . $CNF['TABLE_ANSWERS'] . "` AS `ta` ON `tq`.`id`=`ta`.`question_id` AND `ta`.`profile_id`=:profile_id";
  402. $sWhereClause = " AND `tq`.`content_id`=:content_id";
  403. break;
  404. }
  405. if(!empty($sOrderClause))
  406. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  407. if(!empty($sLimitClause))
  408. $sLimitClause = 'LIMIT ' . $sLimitClause;
  409. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_QUESTIONS'] . "` AS `tq` " . $sJoinClause . " WHERE 1" . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  410. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  411. }
  412. public function hasQuestions($iContentId)
  413. {
  414. $aQuestions = $this->getQuestions([
  415. 'sample' => 'content_id',
  416. 'content_id' => $iContentId
  417. ]);
  418. return !empty($aQuestions) && is_array($aQuestions);
  419. }
  420. public function areQuestionsAnswered($iContentId, $iProfileId)
  421. {
  422. $aQuestions = $this->getQuestions([
  423. 'sample' => 'answers',
  424. 'content_id' => $iContentId,
  425. 'profile_id' => $iProfileId
  426. ]);
  427. return !empty($aQuestions) && is_array($aQuestions);
  428. }
  429. public function getQuestionOrderMax($iContentId)
  430. {
  431. $CNF = &$this->_oConfig->CNF;
  432. return (int)$this->getOne("SELECT MAX(`order`) FROM `" . $CNF['TABLE_QUESTIONS'] . "` WHERE `content_id`=:content_id", [
  433. 'content_id' => $iContentId
  434. ]);
  435. }
  436. public function insertAnswer($iQuestionId, $iProfileId, $sAnswer)
  437. {
  438. $CNF = &$this->_oConfig->CNF;
  439. $sSetClause = $this->arrayToSQL([
  440. 'question_id' => $iQuestionId,
  441. 'profile_id' => $iProfileId,
  442. 'answer' => $sAnswer
  443. ]);
  444. return $this->query("INSERT INTO `" . $CNF['TABLE_ANSWERS'] . "` SET " . $sSetClause . ", `added`=UNIX_TIMESTAMP() ON DUPLICATE KEY UPDATE `answer`=:answer, `added`=UNIX_TIMESTAMP()", [
  445. 'answer' => $sAnswer
  446. ]) !== false ? (int)$this->lastId() : false;
  447. }
  448. public function deleteAnswersProfileId($iContentId, $iProfileId)
  449. {
  450. $CNF = &$this->_oConfig->CNF;
  451. return $this->query("DELETE FROM `ta` USING `" . $CNF['TABLE_QUESTIONS'] . "` AS `tq` LEFT JOIN `" . $CNF['TABLE_ANSWERS'] . "` AS `ta` ON `tq`.`id`=`ta`.`question_id` WHERE `tq`.`content_id`=:content_id AND `ta`.`profile_id`=:profile_id", [
  452. 'content_id' => $iContentId,
  453. 'profile_id' => $iProfileId
  454. ]) !== false;
  455. }
  456. public function deleteQuestionnaires($iContentId)
  457. {
  458. $CNF = &$this->_oConfig->CNF;
  459. $this->query("DELETE FROM `tq`, `ta` USING `" . $CNF['TABLE_QUESTIONS'] . "` AS `tq` LEFT JOIN `" . $CNF['TABLE_ANSWERS'] . "` AS `ta` ON `tq`.`id`=`ta`.`question_id` WHERE `tq`.`content_id`=:content_id", [
  460. 'content_id' => $iContentId
  461. ]) !== false;
  462. }
  463. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  464. {
  465. $CNF = &$this->_oConfig->CNF;
  466. if(!empty($CNF['FIELD_STATUS']))
  467. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_STATUS'] . "`='active'";
  468. if(!empty($CNF['FIELD_STATUS_ADMIN']))
  469. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_STATUS_ADMIN'] . "`='active'";
  470. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  471. }
  472. }
  473. /** @} */