BxCoursesDb.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 insertContentNodes2Users($aParamsSet)
  59. {
  60. $CNF = &$this->_oConfig->CNF;
  61. if(empty($aParamsSet))
  62. return false;
  63. 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;
  64. }
  65. public function deleteContentNodes2Users($aParamsWhere)
  66. {
  67. $CNF = &$this->_oConfig->CNF;
  68. if(empty($aParamsWhere))
  69. return false;
  70. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_NODES2USERS'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  71. }
  72. public function getContentStructure($aParams)
  73. {
  74. $CNF = &$this->_oConfig->CNF;
  75. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  76. $sSelectClause = "`tcs`.*";
  77. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  78. switch($aParams['sample']) {
  79. case 'id':
  80. $aMethod['name'] = 'getRow';
  81. $aMethod['params'][1] = [
  82. 'id' => $aParams['id']
  83. ];
  84. $sWhereClause = "AND `tcs`.`id`=:id";
  85. break;
  86. case 'node_id':
  87. $aMethod['name'] = 'getRow';
  88. $aMethod['params'][1] = [
  89. 'node_id' => $aParams['node_id']
  90. ];
  91. $sWhereClause = "AND `tcs`.`node_id`=:node_id";
  92. break;
  93. case 'entry_id_full':
  94. $aMethod['params'][1] = [
  95. 'entry_id' => $aParams['entry_id']
  96. ];
  97. $sSelectClause .= ", `tcn`.`title`, `tcn`.`counters`";
  98. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_CNT_NODES'] . "` AS `tcn` ON `tcs`.`node_id`=`tcn`.`id`";
  99. $sWhereClause = "AND `tcs`.`entry_id`=:entry_id";
  100. if(isset($aParams['parent_id'])) {
  101. $aMethod['params'][1]['parent_id'] = $aParams['parent_id'];
  102. $sWhereClause .= " AND `tcs`.`parent_id`=:parent_id";
  103. }
  104. if(isset($aParams['level'])) {
  105. $aMethod['params'][1]['level'] = $aParams['level'];
  106. $sWhereClause .= " AND `tcs`.`level`=:level";
  107. }
  108. if(isset($aParams['status'])) {
  109. $aMethod['params'][1]['status'] = $aParams['status'];
  110. $sWhereClause .= " AND `tcn`.`status`=:status";
  111. }
  112. if(isset($aParams['start'], $aParams['per_page']) && (int)$aParams['per_page'] != 0)
  113. $sLimitClause = $aParams['start'] . ', ' . $aParams['per_page'];
  114. break;
  115. case 'parent_id':
  116. $aMethod['params'][1] = [
  117. 'parent_id' => $aParams['parent_id']
  118. ];
  119. $sWhereClause = "AND `tcs`.`parent_id`=:parent_id";
  120. break;
  121. case 'user_passed':
  122. $aMethod['name'] = 'getRow';
  123. $aMethod['params'][1] = [
  124. 'profile_id' => $aParams['profile_id'],
  125. 'node_id' => $aParams['node_id']
  126. ];
  127. $sSelectClause = "`tcnu`.*";
  128. $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";
  129. break;
  130. case 'user_track':
  131. $aMethod['params'][1] = [
  132. 'profile_id' => $aParams['profile_id'],
  133. 'entry_id' => $aParams['entry_id'],
  134. 'node_id' => $aParams['node_id']
  135. ];
  136. $sJoinClause = "INNER JOIN `" . $CNF['TABLE_CNT_NODES2USERS'] . "` AS `tcnu` ON `tcs`.`node_id`=`tcnu`.`node_id` AND `tcnu`.`profile_id`=:profile_id";
  137. $sWhereClause = "AND `tcs`.`entry_id`=:entry_id AND `tcs`.`parent_id`=:node_id";
  138. break;
  139. }
  140. if(!empty($sOrderClause))
  141. $sOrderClause = "ORDER BY " . $sOrderClause;
  142. if(!empty($sLimitClause))
  143. $sLimitClause = "LIMIT " . $sLimitClause;
  144. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_CNT_STRUCTURE'] . "` AS `tcs` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  145. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  146. }
  147. public function getContentStructureOrderMax($iEntryId, $iParentId)
  148. {
  149. $CNF = &$this->_oConfig->CNF;
  150. return (int)$this->getOne("SELECT IFNULL(MAX(`order`), 0) FROM `" . $CNF['TABLE_CNT_STRUCTURE'] . "` WHERE `entry_id`=:entry_id AND `parent_id`=:parent_id", [
  151. 'entry_id' => $iEntryId,
  152. 'parent_id' => $iParentId
  153. ]);
  154. }
  155. public function insertContentStructureNode($aParamsSet)
  156. {
  157. $CNF = &$this->_oConfig->CNF;
  158. if(empty($aParamsSet))
  159. return false;
  160. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_CNT_STRUCTURE'] . "` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  161. }
  162. public function updateContentStructureNode($aParamsSet, $aParamsWhere)
  163. {
  164. $CNF = &$this->_oConfig->CNF;
  165. if(empty($aParamsSet) || empty($aParamsWhere))
  166. return false;
  167. return $this->query("UPDATE `" . $CNF['TABLE_CNT_STRUCTURE'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  168. }
  169. public function updateContentStructureCounters($iParentId, $iLevel, $iAdd)
  170. {
  171. $aParent = $this->getContentStructure(['sample' => 'node_id', 'node_id' => $iParentId]);
  172. if(empty($aParent) || !is_array($aParent))
  173. return;
  174. $sKeyLevel = 'cn_l' . $iLevel;
  175. $this->updateContentStructureNode([$sKeyLevel => $aParent[$sKeyLevel] + $iAdd], ['id' => $aParent['id']]);
  176. if(!empty($aParent['parent_id']))
  177. $this->updateContentStructureCounters($aParent['parent_id'], $iLevel, $iAdd);
  178. }
  179. public function deleteContentStructureNode($aParamsWhere)
  180. {
  181. $CNF = &$this->_oConfig->CNF;
  182. if(empty($aParamsWhere))
  183. return false;
  184. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_STRUCTURE'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  185. }
  186. public function getContentData($aParams)
  187. {
  188. $CNF = &$this->_oConfig->CNF;
  189. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  190. $sSelectClause = "`tcd`.*";
  191. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  192. switch($aParams['sample']) {
  193. case 'id':
  194. $aMethod['name'] = 'getRow';
  195. $aMethod['params'][1] = [
  196. 'id' => $aParams['id']
  197. ];
  198. $sWhereClause = "AND `tcd`.`id`=:id";
  199. break;
  200. case 'content':
  201. $aMethod['name'] = 'getRow';
  202. $aMethod['params'][1] = [
  203. 'content_type' => $aParams['content_type'],
  204. 'content_id' => $aParams['content_id']
  205. ];
  206. $sWhereClause = "AND `tcd`.`content_type`=:content_type AND `tcd`.`content_id`=:content_id";
  207. break;
  208. case 'node_id':
  209. $aMethod['params'][1] = [
  210. 'node_id' => $aParams['node_id']
  211. ];
  212. $sWhereClause = "AND `tcd`.`node_id`=:node_id";
  213. $sOrderClause = "`tcd`.`order` ASC";
  214. break;
  215. case 'entry_node_ids':
  216. $aMethod['params'][1] = [
  217. 'entry_id' => $aParams['entry_id'],
  218. 'node_id' => $aParams['node_id']
  219. ];
  220. $sWhereClause = "AND `tcd`.`entry_id`=:entry_id AND `tcd`.`node_id`=:node_id";
  221. $sOrderClause = "`tcd`.`order` ASC";
  222. if(isset($aParams['usage'])) {
  223. $aMethod['params'][1]['usage'] = $aParams['usage'];
  224. $sWhereClause .= " AND `tcd`.`usage`=:usage";
  225. }
  226. break;
  227. case 'user_passed':
  228. $aMethod['name'] = 'getRow';
  229. $aMethod['params'][1] = [
  230. 'profile_id' => $aParams['profile_id'],
  231. 'data_id' => $aParams['data_id']
  232. ];
  233. $sSelectClause = "`tcdu`.*";
  234. $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";
  235. break;
  236. case 'user_track':
  237. $aMethod['params'][1] = [
  238. 'profile_id' => $aParams['profile_id'],
  239. 'entry_id' => $aParams['entry_id'],
  240. 'node_id' => $aParams['node_id']
  241. ];
  242. $sJoinClause = "INNER JOIN `" . $CNF['TABLE_CNT_DATA2USERS'] . "` AS `tcdu` ON `tcd`.`id`=`tcdu`.`data_id` AND `tcdu`.`profile_id`=:profile_id";
  243. $sWhereClause = "AND `tcd`.`entry_id`=:entry_id AND `tcd`.`node_id`=:node_id AND `tcd`.`usage`='" . BX_COURSES_CND_USAGE_ST . "'";
  244. break;
  245. }
  246. if(!empty($sOrderClause))
  247. $sOrderClause = "ORDER BY " . $sOrderClause;
  248. if(!empty($sLimitClause))
  249. $sLimitClause = "LIMIT " . $sLimitClause;
  250. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_CNT_DATA'] . "` AS `tcd` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  251. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  252. }
  253. public function getContentDataOrderMax($iEntryId, $iNodeId)
  254. {
  255. $CNF = &$this->_oConfig->CNF;
  256. return (int)$this->getOne("SELECT IFNULL(MAX(`order`), 0) FROM `" . $CNF['TABLE_CNT_DATA'] . "` WHERE `entry_id`=:entry_id AND `node_id`=:node_id", [
  257. 'entry_id' => $iEntryId,
  258. 'node_id' => $iNodeId
  259. ]);
  260. }
  261. public function insertContentData($aParamsSet)
  262. {
  263. $CNF = &$this->_oConfig->CNF;
  264. if(empty($aParamsSet))
  265. return false;
  266. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_CNT_DATA'] . "` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  267. }
  268. public function deleteContentData($aParamsWhere)
  269. {
  270. $CNF = &$this->_oConfig->CNF;
  271. if(empty($aParamsWhere))
  272. return false;
  273. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_DATA'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  274. }
  275. public function insertContentData2Users($aParamsSet)
  276. {
  277. $CNF = &$this->_oConfig->CNF;
  278. if(empty($aParamsSet))
  279. return false;
  280. 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;
  281. }
  282. public function deleteContentData2Users($aParamsWhere)
  283. {
  284. $CNF = &$this->_oConfig->CNF;
  285. if(empty($aParamsWhere))
  286. return false;
  287. return $this->query("DELETE FROM `" . $CNF['TABLE_CNT_DATA2USERS'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  288. }
  289. }
  290. /** @} */