BxCoursesDb.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 Courses Courses
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Courses module database queries
  13. */
  14. class BxCoursesDb extends BxBaseModGroupsDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function getContentNodes($aParams)
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  24. $sSelectClause = "`tcn`.*";
  25. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  26. switch($aParams['sample']) {
  27. case 'id':
  28. $aMethod['name'] = 'getRow';
  29. $aMethod['params'][1] = [
  30. 'id' => $aParams['id']
  31. ];
  32. $sWhereClause = "AND `tcn`.`id`=:id";
  33. break;
  34. case 'id_full':
  35. $aMethod['name'] = 'getRow';
  36. $aMethod['params'][1] = [
  37. 'id' => $aParams['id']
  38. ];
  39. $sSelectClause .= ", `tcs`.`parent_id`, `tcs`.`level`, `tcs`.`order`, `tcs`.`cn_l2`, `tcs`.`cn_l3`";
  40. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_CNT_STRUCTURE'] . "` AS `tcs` ON `tcn`.`id`=`tcs`.`node_id`";
  41. $sWhereClause = "AND `tcn`.`id`=:id";
  42. break;
  43. }
  44. if(!empty($sOrderClause))
  45. $sOrderClause = "ORDER BY " . $sOrderClause;
  46. if(!empty($sLimitClause))
  47. $sLimitClause = "LIMIT " . $sLimitClause;
  48. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_CNT_NODES'] . "` AS `tcn` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  49. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  50. }
  51. public function updateContentNodes($aParamsSet, $aParamsWhere)
  52. {
  53. $CNF = &$this->_oConfig->CNF;
  54. if(empty($aParamsSet) || empty($aParamsWhere))
  55. return false;
  56. return $this->query("UPDATE `" . $CNF['TABLE_CNT_NODES'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  57. }
  58. public function deleteContentNodes($aParamsWhere)
  59. {
  60. $CNF = &$this->_oConfig->CNF;
  61. if(empty($aParamsWhere))
  62. return false;
  63. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_NODES'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  64. }
  65. public function deleteContentNodesWithTracks($iEntryId)
  66. {
  67. $CNF = &$this->_oConfig->CNF;
  68. return $this->query("DELETE FROM `tcn`, `tcnu` USING `" . $CNF['TABLE_CNT_NODES'] . "` AS `tcn` LEFT JOIN `" . $CNF['TABLE_CNT_NODES2USERS'] . "` AS `tcnu` ON `tcn`.`id`=`tcnu`.`node_id` WHERE `tcn`.`entry_id`=:entry_id", [
  69. 'entry_id' => $iEntryId
  70. ]);
  71. }
  72. public function insertContentNodes2Users($aParamsSet)
  73. {
  74. $CNF = &$this->_oConfig->CNF;
  75. if(empty($aParamsSet))
  76. return false;
  77. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_CNT_NODES2USERS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `date`=UNIX_TIMESTAMP()") > 0 ? (int)$this->lastId() : false;
  78. }
  79. public function deleteContentNodes2Users($aParamsWhere)
  80. {
  81. $CNF = &$this->_oConfig->CNF;
  82. if(empty($aParamsWhere))
  83. return false;
  84. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_NODES2USERS'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  85. }
  86. public function getContentStructure($aParams)
  87. {
  88. $CNF = &$this->_oConfig->CNF;
  89. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  90. $sSelectClause = "`tcs`.*";
  91. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  92. switch($aParams['sample']) {
  93. case 'id':
  94. $aMethod['name'] = 'getRow';
  95. $aMethod['params'][1] = [
  96. 'id' => $aParams['id']
  97. ];
  98. $sWhereClause = "AND `tcs`.`id`=:id";
  99. break;
  100. case 'node_id':
  101. $aMethod['name'] = 'getRow';
  102. $aMethod['params'][1] = [
  103. 'node_id' => $aParams['node_id']
  104. ];
  105. $sWhereClause = "AND `tcs`.`node_id`=:node_id";
  106. break;
  107. case 'entry_id':
  108. $aMethod['params'][1] = [
  109. 'entry_id' => $aParams['entry_id']
  110. ];
  111. $sWhereClause = "AND `tcs`.`entry_id`=:entry_id";
  112. if(isset($aParams['level'])) {
  113. $aMethod['params'][1]['level'] = $aParams['level'];
  114. $sWhereClause .= " AND `tcs`.`level`=:level";
  115. }
  116. break;
  117. case 'entry_id_full':
  118. $aMethod['params'][1] = [
  119. 'entry_id' => $aParams['entry_id']
  120. ];
  121. $sSelectClause .= ", `tcn`.`title`, `tcn`.`counters`";
  122. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_CNT_NODES'] . "` AS `tcn` ON `tcs`.`node_id`=`tcn`.`id`";
  123. $sWhereClause = "AND `tcs`.`entry_id`=:entry_id";
  124. if(isset($aParams['parent_id'])) {
  125. $aMethod['params'][1]['parent_id'] = $aParams['parent_id'];
  126. $sWhereClause .= " AND `tcs`.`parent_id`=:parent_id";
  127. }
  128. if(isset($aParams['level'])) {
  129. $aMethod['params'][1]['level'] = $aParams['level'];
  130. $sWhereClause .= " AND `tcs`.`level`=:level";
  131. }
  132. if(isset($aParams['status'])) {
  133. $aMethod['params'][1]['status'] = $aParams['status'];
  134. $sWhereClause .= " AND `tcn`.`status`=:status";
  135. }
  136. if(isset($aParams['start'], $aParams['per_page']) && (int)$aParams['per_page'] != 0)
  137. $sLimitClause = $aParams['start'] . ', ' . $aParams['per_page'];
  138. $sOrderClause = "`tcs`.`order` ASC";
  139. break;
  140. case 'entry_id_counters':
  141. $aMethod['name'] = 'getRow';
  142. $aMethod['params'][1] = [
  143. 'entry_id' => $aParams['entry_id']
  144. ];
  145. $sSelectClause = "COUNT(`tcs`.`id`) AS `cn_l1`, SUM(`tcs`.`cn_l2`) AS `cn_l2`, SUM(`tcs`.`cn_l3`) AS `cn_l3`";
  146. $sWhereClause = "AND `tcs`.`entry_id`=:entry_id AND `tcs`.`level`='1'";
  147. $sGroupClause = "`tcs`.`entry_id`";
  148. break;
  149. case 'parent_id':
  150. $aMethod['params'][1] = [
  151. 'parent_id' => $aParams['parent_id']
  152. ];
  153. $sWhereClause = "AND `tcs`.`parent_id`=:parent_id";
  154. break;
  155. case 'user_passed':
  156. $aMethod['name'] = 'getRow';
  157. $aMethod['params'][1] = [
  158. 'profile_id' => $aParams['profile_id'],
  159. 'node_id' => $aParams['node_id']
  160. ];
  161. $sSelectClause = "`tcnu`.*";
  162. $sJoinClause = "INNER JOIN `" . $CNF['TABLE_CNT_NODES2USERS'] . "` AS `tcnu` ON `tcs`.`node_id`=`tcnu`.`node_id` AND `tcnu`.`node_id`=:node_id AND `tcnu`.`profile_id`=:profile_id";
  163. break;
  164. case 'user_track':
  165. $aMethod['params'][1] = [
  166. 'profile_id' => $aParams['profile_id'],
  167. 'entry_id' => $aParams['entry_id'],
  168. 'node_id' => $aParams['node_id']
  169. ];
  170. $sJoinClause = "INNER JOIN `" . $CNF['TABLE_CNT_NODES2USERS'] . "` AS `tcnu` ON `tcs`.`node_id`=`tcnu`.`node_id` AND `tcnu`.`profile_id`=:profile_id";
  171. $sWhereClause = "AND `tcs`.`entry_id`=:entry_id AND `tcs`.`parent_id`=:node_id";
  172. break;
  173. }
  174. if(!empty($sGroupClause))
  175. $sGroupClause = "GROUP BY " . $sGroupClause;
  176. if(!empty($sOrderClause))
  177. $sOrderClause = "ORDER BY " . $sOrderClause;
  178. if(!empty($sLimitClause))
  179. $sLimitClause = "LIMIT " . $sLimitClause;
  180. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_CNT_STRUCTURE'] . "` AS `tcs` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  181. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  182. }
  183. public function getContentStructureOrderMax($iEntryId, $iParentId)
  184. {
  185. $CNF = &$this->_oConfig->CNF;
  186. return (int)$this->getOne("SELECT IFNULL(MAX(`order`), 0) FROM `" . $CNF['TABLE_CNT_STRUCTURE'] . "` WHERE `entry_id`=:entry_id AND `parent_id`=:parent_id", [
  187. 'entry_id' => $iEntryId,
  188. 'parent_id' => $iParentId
  189. ]);
  190. }
  191. public function insertContentStructureNode($aParamsSet)
  192. {
  193. $CNF = &$this->_oConfig->CNF;
  194. if(empty($aParamsSet))
  195. return false;
  196. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_CNT_STRUCTURE'] . "` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  197. }
  198. public function updateContentStructureNode($aParamsSet, $aParamsWhere)
  199. {
  200. $CNF = &$this->_oConfig->CNF;
  201. if(empty($aParamsSet) || empty($aParamsWhere))
  202. return false;
  203. return $this->query("UPDATE `" . $CNF['TABLE_CNT_STRUCTURE'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  204. }
  205. public function updateContentStructureCounters($iParentId, $iLevel, $iAdd)
  206. {
  207. $aParent = $this->getContentStructure(['sample' => 'node_id', 'node_id' => $iParentId]);
  208. if(empty($aParent) || !is_array($aParent))
  209. return;
  210. $sKeyLevel = 'cn_l' . $iLevel;
  211. $this->updateContentStructureNode([$sKeyLevel => $aParent[$sKeyLevel] + $iAdd], ['id' => $aParent['id']]);
  212. if(!empty($aParent['parent_id']))
  213. $this->updateContentStructureCounters($aParent['parent_id'], $iLevel, $iAdd);
  214. }
  215. public function deleteContentStructureNode($aParamsWhere)
  216. {
  217. $CNF = &$this->_oConfig->CNF;
  218. if(empty($aParamsWhere))
  219. return false;
  220. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_STRUCTURE'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  221. }
  222. public function getContentData($aParams)
  223. {
  224. $CNF = &$this->_oConfig->CNF;
  225. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  226. $sSelectClause = "`tcd`.*";
  227. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  228. switch($aParams['sample']) {
  229. case 'id':
  230. $aMethod['name'] = 'getRow';
  231. $aMethod['params'][1] = [
  232. 'id' => $aParams['id']
  233. ];
  234. $sWhereClause = "AND `tcd`.`id`=:id";
  235. break;
  236. case 'content':
  237. $aMethod['name'] = 'getRow';
  238. $aMethod['params'][1] = [
  239. 'content_type' => $aParams['content_type'],
  240. 'content_id' => $aParams['content_id']
  241. ];
  242. $sWhereClause = "AND `tcd`.`content_type`=:content_type AND `tcd`.`content_id`=:content_id";
  243. break;
  244. case 'entry_id':
  245. $aMethod['params'][1] = [
  246. 'entry_id' => $aParams['entry_id']
  247. ];
  248. $sWhereClause = "AND `tcd`.`entry_id`=:entry_id";
  249. break;
  250. case 'node_id':
  251. $aMethod['params'][1] = [
  252. 'node_id' => $aParams['node_id']
  253. ];
  254. $sWhereClause = "AND `tcd`.`node_id`=:node_id";
  255. $sOrderClause = "`tcd`.`order` ASC";
  256. break;
  257. case 'entry_node_ids':
  258. $aMethod['params'][1] = [
  259. 'entry_id' => $aParams['entry_id'],
  260. 'node_id' => $aParams['node_id']
  261. ];
  262. $sWhereClause = "AND `tcd`.`entry_id`=:entry_id AND `tcd`.`node_id`=:node_id";
  263. if(isset($aParams['usage'])) {
  264. $aMethod['params'][1]['usage'] = $aParams['usage'];
  265. $sWhereClause .= " AND `tcd`.`usage`=:usage";
  266. }
  267. $sOrderClause = "`tcd`.`order` ASC";
  268. break;
  269. case 'siblings':
  270. $aMethod['name'] = 'getAllWithKey';
  271. $aMethod['params'][1] = 'order';
  272. $aMethod['params'][2] = [
  273. 'entry_id' => $aParams['entry_id'],
  274. 'node_id' => $aParams['node_id'],
  275. 'usage' => $aParams['usage'],
  276. 'order' => $aParams['order']
  277. ];
  278. $sWhereClause = "AND `tcd`.`entry_id`=:entry_id AND `tcd`.`node_id`=:node_id AND `tcd`.`usage`=:usage AND (`tcd`.`order`=(:order - 1) OR `tcd`.`order`=(:order + 1))";
  279. $sOrderClause = "`tcd`.`order` ASC";
  280. break;
  281. case 'user_passed':
  282. $aMethod['name'] = 'getRow';
  283. $aMethod['params'][1] = [
  284. 'profile_id' => $aParams['profile_id'],
  285. 'data_id' => $aParams['data_id']
  286. ];
  287. $sSelectClause = "`tcdu`.*";
  288. $sJoinClause = "INNER JOIN `" . $CNF['TABLE_CNT_DATA2USERS'] . "` AS `tcdu` ON `tcd`.`id`=`tcdu`.`data_id` AND `tcdu`.`data_id`=:data_id AND `tcdu`.`profile_id`=:profile_id";
  289. break;
  290. case 'user_track':
  291. $aMethod['params'][1] = [
  292. 'profile_id' => $aParams['profile_id'],
  293. 'entry_id' => $aParams['entry_id'],
  294. 'node_id' => $aParams['node_id']
  295. ];
  296. $sJoinClause = "INNER JOIN `" . $CNF['TABLE_CNT_DATA2USERS'] . "` AS `tcdu` ON `tcd`.`id`=`tcdu`.`data_id` AND `tcdu`.`profile_id`=:profile_id";
  297. $sWhereClause = "AND `tcd`.`entry_id`=:entry_id AND `tcd`.`node_id`=:node_id AND `tcd`.`usage`='" . BX_COURSES_CND_USAGE_ST . "'";
  298. break;
  299. }
  300. if(!empty($sOrderClause))
  301. $sOrderClause = "ORDER BY " . $sOrderClause;
  302. if(!empty($sLimitClause))
  303. $sLimitClause = "LIMIT " . $sLimitClause;
  304. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_CNT_DATA'] . "` AS `tcd` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  305. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  306. }
  307. public function getContentDataOrderMax($iEntryId, $iNodeId)
  308. {
  309. $CNF = &$this->_oConfig->CNF;
  310. return (int)$this->getOne("SELECT IFNULL(MAX(`order`), 0) FROM `" . $CNF['TABLE_CNT_DATA'] . "` WHERE `entry_id`=:entry_id AND `node_id`=:node_id", [
  311. 'entry_id' => $iEntryId,
  312. 'node_id' => $iNodeId
  313. ]);
  314. }
  315. public function insertContentData($aParamsSet)
  316. {
  317. $CNF = &$this->_oConfig->CNF;
  318. if(empty($aParamsSet))
  319. return false;
  320. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_CNT_DATA'] . "` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  321. }
  322. public function deleteContentData($aParamsWhere)
  323. {
  324. $CNF = &$this->_oConfig->CNF;
  325. if(empty($aParamsWhere))
  326. return false;
  327. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_DATA'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  328. }
  329. public function deleteContentDataWithTracks($iEntryId)
  330. {
  331. $CNF = &$this->_oConfig->CNF;
  332. return $this->query("DELETE FROM `tcd`, `tcdu` USING `" . $CNF['TABLE_CNT_DATA'] . "` AS `tcd` LEFT JOIN `" . $CNF['TABLE_CNT_DATA2USERS'] . "` AS `tcdu` ON `tcd`.`id`=`tcdu`.`data_id` WHERE `tcd`.`entry_id`=:entry_id", [
  333. 'entry_id' => $iEntryId
  334. ]);
  335. }
  336. public function insertContentData2Users($aParamsSet)
  337. {
  338. $CNF = &$this->_oConfig->CNF;
  339. if(empty($aParamsSet))
  340. return false;
  341. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_CNT_DATA2USERS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `date`=UNIX_TIMESTAMP()") > 0 ? (int)$this->lastId() : false;
  342. }
  343. public function deleteContentData2Users($aParamsWhere)
  344. {
  345. $CNF = &$this->_oConfig->CNF;
  346. if(empty($aParamsWhere))
  347. return false;
  348. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_DATA2USERS'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  349. }
  350. }
  351. /** @} */