database.lib.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2005 Dokeos S.A.
  6. Copyright (c) Roan Embrechts, Vrije Universiteit Brussel
  7. Copyright (c) Patrick Cool, Ghent University
  8. Copyright (c) Yannick Warnier, Dokeos S.A.
  9. Copyright (c) Bart Mollet, Hogeschool Gent
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  18. Mail: info@dokeos.com
  19. ==============================================================================
  20. */
  21. /**
  22. ==============================================================================
  23. * This is the main database library for Dokeos.
  24. * Include/require it in your code to use its functionality.
  25. * Because this library contains all the basic database calls, it could be
  26. * replaced by another library for say, PostgreSQL, to actually use Dokeos
  27. * with another database (this is not ready yet because a lot of code still
  28. * uses the MySQL database functions extensively).
  29. *
  30. * @package dokeos.library
  31. * @todo the table constants have all to start with TABLE_
  32. * This is because of the analogy with the tool constants TOOL_
  33. ==============================================================================
  34. */
  35. /*
  36. ==============================================================================
  37. CONSTANTS
  38. ==============================================================================
  39. */
  40. //main database tables
  41. define('TABLE_MAIN_COURSE', 'course');
  42. define('TABLE_MAIN_USER', 'user');
  43. define('TABLE_MAIN_CLASS', 'class');
  44. define('TABLE_MAIN_ADMIN', 'admin');
  45. define('TABLE_MAIN_COURSE_CLASS', 'course_rel_class');
  46. define('TABLE_MAIN_COURSE_USER', 'course_rel_user');
  47. define('TABLE_MAIN_CLASS_USER', 'class_user');
  48. define('TABLE_MAIN_CATEGORY', 'course_category');
  49. define('TABLE_MAIN_COURSE_MODULE', 'course_module');
  50. define('TABLE_MAIN_SYSTEM_ANNOUNCEMENTS', 'sys_announcement');
  51. define('TABLE_MAIN_LANGUAGE', 'language');
  52. define('TABLE_MAIN_SETTINGS_OPTIONS', 'settings_options');
  53. define('TABLE_MAIN_SETTINGS_CURRENT', 'settings_current');
  54. define('TABLE_MAIN_SESSION', 'session');
  55. define('TABLE_MAIN_SESSION_COURSE', 'session_rel_course');
  56. define('TABLE_MAIN_SESSION_USER', 'session_rel_user');
  57. define('TABLE_MAIN_SESSION_CLASS', 'session_rel_class');
  58. define('TABLE_MAIN_SESSION_COURSE_USER', 'session_rel_course_rel_user');
  59. define('TABLE_MAIN_SHARED_SURVEY', 'shared_survey');
  60. define('TABLE_MAIN_SHARED_SURVEY_QUESTION', 'shared_survey_question');
  61. define('TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION', 'shared_survey_question_option');
  62. //statistic database tables
  63. define('TABLE_STATISTIC_TRACK_E_LASTACCESS', 'track_e_lastaccess');
  64. define('TABLE_STATISTIC_TRACK_E_ACCESS', 'track_e_access');
  65. define('TABLE_STATISTIC_TRACK_E_LOGIN', 'track_e_login');
  66. define('TABLE_STATISTIC_TRACK_E_DOWNLOADS', 'track_e_downloads');
  67. define('TABLE_STATISTIC_TRACK_E_LINKS', 'track_e_links');
  68. define('TABLE_STATISTIC_TRACK_E_ONLINE', 'track_e_online');
  69. define('TABLE_STATISTIC_TRACK_E_HOTPOTATOES', 'track_e_hotpotatoes');
  70. define('TABLE_STATISTIC_TRACK_E_COURSE_ACCESS', 'track_e_course_access');
  71. define('TABLE_STATISTIC_TRACK_E_EXERCICES', 'track_e_exercices');
  72. define('TABLE_STATISTIC_TRACK_E_ATTEMPT', 'track_e_attempt');
  73. define('TABLE_STATISTIC_TRACK_E_DEFAULT', 'track_e_default');
  74. define('TABLE_STATISTIC_TRACK_E_UPLOADS','track_e_uploads');
  75. //scorm database tables
  76. define('TABLE_SCORM_MAIN', 'scorm_main');
  77. define('TABLE_SCORM_SCO_DATA', 'scorm_sco_data');
  78. //course tables
  79. define('TABLE_AGENDA', 'calendar_event');
  80. define('TABLE_ANNOUNCEMENT', 'announcement');
  81. define('TABLE_CHAT_CONNECTED', 'chat_connected'); // @todo: probably no longer in use !!!
  82. define('TABLE_COURSE_DESCRIPTION', 'course_description');
  83. define('TABLE_DOCUMENT', 'document');
  84. define('TABLE_ITEM_PROPERTY', 'item_property');
  85. define('TABLE_LINK', 'link');
  86. define('TABLE_LINK_CATEGORY', 'link_category');
  87. define('TABLE_TOOL_LIST', 'tool');
  88. define('TABLE_TOOL_INTRO', 'tool_intro');
  89. define('TABLE_SCORMDOC', 'scormdocument');
  90. define('TABLE_STUDENT_PUBLICATION', 'student_publication');
  91. define('CHAT_CONNECTED_TABLE', 'chat_connected');
  92. //course forum tables
  93. define('TABLE_FORUM_CATEGORY','forum_category');
  94. define('TABLE_FORUM','forum_forum');
  95. define('TABLE_FORUM_THREAD','forum_thread');
  96. define('TABLE_FORUM_POST','forum_post');
  97. //course group tables
  98. define('TABLE_GROUP', 'group_info');
  99. define('TABLE_GROUP_USER', 'group_rel_user');
  100. define('TABLE_GROUP_TUTOR', 'group_rel_tutor');
  101. define('TABLE_GROUP_CATEGORY', 'group_category');
  102. //course dropbox tables
  103. define('TABLE_DROPBOX_CATEGORY','dropbox_category');
  104. define('TABLE_DROPBOX_FEEDBACK','dropbox_feedback');
  105. define('TABLE_DROPBOX_POST','dropbox_post');
  106. define('TABLE_DROPBOX_FILE','dropbox_file');
  107. define('TABLE_DROPBOX_PERSON','dropbox_person');
  108. //course quiz tables
  109. define('TABLE_QUIZ_QUESTION', 'quiz_question');
  110. define('TABLE_QUIZ_TEST', 'quiz');
  111. define('TABLE_QUIZ_ANSWER', 'quiz_answer');
  112. define('TABLE_QUIZ_TEST_QUESTION', 'quiz_rel_question');
  113. //linked resource table
  114. define('TABLE_LINKED_RESOURCES', 'resource');
  115. //learnpath tables
  116. define('TABLE_LEARNPATH_MAIN', 'learnpath_main');
  117. define('TABLE_LEARNPATH_CHAPTER', 'learnpath_chapter');
  118. define('TABLE_LEARNPATH_ITEM', 'learnpath_item');
  119. define('TABLE_LEARNPATH_USER', 'learnpath_user');
  120. //new scorm tables
  121. define('TABLE_LP_MAIN', 'lp');
  122. define('TABLE_LP_ITEM', 'lp_item');
  123. define('TABLE_LP_VIEW', 'lp_view');
  124. define('TABLE_LP_ITEM_VIEW', 'lp_item_view');
  125. define('TABLE_LP_IV_INTERACTION', 'lp_iv_interaction'); // IV = Item View
  126. // Smartblogs (Kevin Van Den Haute::kevin@develop-it.be)
  127. // permission tables
  128. define('TABLE_PERMISSION_USER', 'permission_user');
  129. define('TABLE_PERMISSION_TASK', 'permission_task');
  130. define('TABLE_PERMISSION_GROUP', 'permission_group');
  131. // roles tables
  132. define('TABLE_ROLE', 'role');
  133. define('TABLE_ROLE_PERMISSION', 'role_permissions');
  134. define('TABLE_ROLE_USER', 'role_user');
  135. define('TABLE_ROLE_GROUP', 'role_group');
  136. // blogs tables
  137. define('TABLE_BLOGS', 'blog');
  138. define('TABLE_BLOGS_POSTS', 'blog_post');
  139. define('TABLE_BLOGS_COMMENTS', 'blog_comment');
  140. define('TABLE_BLOGS_REL_USER', 'blog_rel_user');
  141. define('TABLE_BLOGS_TASKS', 'blog_task');
  142. define('TABLE_BLOGS_TASKS_REL_USER', 'blog_task_rel_user');
  143. define('TABLE_BLOGS_RATING', 'blog_rating');
  144. define('TABLE_BLOGS_TASKS_PERMISSIONS', 'permission_task');
  145. //end of Smartblogs
  146. // user information tables
  147. define('TABLE_USER_INFO', 'userinfo_def');
  148. define('TABLE_USER_INFO_CONTENT', 'userinfo_content');
  149. // course settings table
  150. define('TABLE_COURSE_SETTING', 'course_setting');
  151. // course online tables
  152. define('TABLE_ONLINE_LINK', 'online_link');
  153. define('TABLE_ONLINE_CONNECTED', 'online_connected');
  154. // dokeos_user database
  155. define('TABLE_PERSONAL_AGENDA', 'personal_agenda');
  156. define('TABLE_USER_COURSE_CATEGORY', 'user_course_category');
  157. //Survey
  158. // @todo: are these MAIN tables or course tables ?
  159. define('TABLE_MAIN_SURVEY', 'survey');
  160. define('TABLE_MAIN_GROUP', 'survey_group');
  161. define('TABLE_MAIN_SURVEYQUESTION', 'questions');
  162. // SURVEY
  163. define('TABLE_SURVEY', 'survey');
  164. define('TABLE_SURVEY_QUESTION', 'survey_question');
  165. define('TABLE_SURVEY_QUESTION_OPTION', 'survey_question_option');
  166. define('TABLE_SURVEY_INVITATION', 'survey_invitation');
  167. define('TABLE_SURVEY_ANSWER', 'survey_answer');
  168. /*
  169. ==============================================================================
  170. DATABASE CLASS
  171. the class and its functions
  172. ==============================================================================
  173. */
  174. /**
  175. * @package dokeos.library
  176. */
  177. class Database
  178. {
  179. /*
  180. -----------------------------------------------------------------------------
  181. Accessor Functions
  182. Usually, you won't need these directly but instead
  183. rely on of the get_xxx_table functions.
  184. -----------------------------------------------------------------------------
  185. */
  186. /**
  187. * Returns the name of the main Dokeos database.
  188. */
  189. function get_main_database()
  190. {
  191. global $_configuration;
  192. return $_configuration['main_database'];
  193. }
  194. /**
  195. * Returns the name of the Dokeos statistics database.
  196. */
  197. function get_statistic_database()
  198. {
  199. global $_configuration;
  200. return $_configuration['statistics_database'];
  201. }
  202. /**
  203. * Returns the name of the Dokeos SCORM database.
  204. */
  205. function get_scorm_database()
  206. {
  207. global $_configuration;
  208. return $_configuration['scorm_database'];
  209. }
  210. /**
  211. * Returns the name of the database where all the personal stuff of the user is stored
  212. */
  213. function get_user_personal_database()
  214. {
  215. global $_configuration;
  216. return $_configuration['user_personal_database'];
  217. }
  218. /**
  219. * Returns the name of the main Dokeos database.
  220. */
  221. function get_current_course_database()
  222. {
  223. $course_info = api_get_course_info();
  224. return $course_info["dbName"];
  225. }
  226. /**
  227. * Returns the glued name of the current course database.
  228. */
  229. function get_current_course_glued_database()
  230. {
  231. $course_info = api_get_course_info();
  232. return $course_info["dbNameGlu"];
  233. }
  234. /**
  235. * The glue is the string needed between database and table.
  236. * The trick is: in multiple databases, this is a period (with backticks)
  237. * In single database, this can be e.g. an underscore so we just fake
  238. * there are multiple databases and the code can be written independent
  239. * of the single / multiple database setting.
  240. */
  241. function get_database_glue()
  242. {
  243. global $_configuration;
  244. return $_configuration['db_glue'];
  245. }
  246. /**
  247. * Returns the database prefix.
  248. * All created COURSE databases are prefixed with this string.
  249. *
  250. * TIP: this can be convenient e.g. if you have multiple Dokeos installations
  251. * on the same physical server.
  252. */
  253. function get_database_name_prefix()
  254. {
  255. global $_configuration;
  256. return $_configuration['db_prefix'];
  257. }
  258. /**
  259. * Returns the course table prefix for single database.
  260. * Not certain exactly when this is used.
  261. * Do research.
  262. * It's used in local.inc.php.
  263. */
  264. function get_course_table_prefix()
  265. {
  266. global $_configuration;
  267. return $_configuration['table_prefix'];
  268. }
  269. /*
  270. -----------------------------------------------------------------------------
  271. Table Name functions
  272. use these functions to get a table name for queries,
  273. instead of constructing them yourself.
  274. Backticks automatically surround the result,
  275. e.g. `COURSE_NAME`.`link`
  276. so the queries can look cleaner.
  277. Example:
  278. $table = Database::get_course_table(TABLE_DOCUMENT);
  279. $sql_query = "SELECT * FROM $table WHERE $condition";
  280. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  281. $result = mysql_fetch_array($sql_result);
  282. -----------------------------------------------------------------------------
  283. */
  284. /**
  285. * A more generic function than the other get_main_xxx_table functions,
  286. * this one can return the correct complete name of any table of the main database of which you pass
  287. * the short name as a parameter.
  288. * Please define table names as constants in this library and use them
  289. * instead of directly using magic words in your tool code.
  290. *
  291. * @param string $short_table_name, the name of the table
  292. */
  293. function get_main_table($short_table_name)
  294. {
  295. $database = Database::get_main_database();
  296. return Database::format_table_name($database, $short_table_name);
  297. }
  298. /**
  299. * A more generic function than the older get_course_xxx_table functions,
  300. * this one can return the correct complete name of any course table of which you pass
  301. * the short name as a parameter.
  302. * Please define table names as constants in this library and use them
  303. * instead of directly using magic words in your tool code.
  304. *
  305. * @param string $short_table_name, the name of the table
  306. * @param string $database_name, optional, name of the course database
  307. * - if you don't specify this, you work on the current course.
  308. */
  309. function get_course_table($short_table_name, $database_name = '')
  310. {
  311. $database_name_with_glue = Database::fix_database_parameter($database_name);
  312. return Database::format_glued_course_table_name($database_name_with_glue, $short_table_name);
  313. }
  314. /**
  315. * This generic function returns the correct and complete name of any statistic table
  316. * of which you pass the short name as a parameter.
  317. * Please define table names as constants in this library and use them
  318. * instead of directly using magic words in your tool code.
  319. *
  320. * @param string $short_table_name, the name of the table
  321. */
  322. function get_statistic_table($short_table_name)
  323. {
  324. $database = Database::get_statistic_database();
  325. return Database::format_table_name($database, $short_table_name);
  326. }
  327. /**
  328. * This generic function returns the correct and complete name of any scorm
  329. * table of which you pass the short name as a parameter. Please define
  330. * table names as constants in this library and use them instead of directly
  331. * using magic words in your tool code.
  332. *
  333. * @param string $short_table_name, the name of the table
  334. */
  335. function get_scorm_table($short_table_name)
  336. {
  337. $database = Database::get_scorm_database();
  338. return Database::format_table_name($database, $short_table_name);
  339. }
  340. /**
  341. * This generic function returns the correct and complete name of any scorm
  342. * table of which you pass the short name as a parameter. Please define
  343. * table names as constants in this library and use them instead of directly
  344. * using magic words in your tool code.
  345. *
  346. * @param string $short_table_name, the name of the table
  347. */
  348. function get_user_personal_table($short_table_name)
  349. {
  350. $database = Database::get_user_personal_database();
  351. return Database::format_table_name($database, $short_table_name);
  352. }
  353. /**
  354. * Returns the isocode corresponding to the language directory given.
  355. */
  356. function get_language_isocode($lang_folder)
  357. {
  358. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  359. $sql_query = "SELECT isocode FROM $table WHERE dokeos_folder = '$lang_folder'";
  360. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  361. $result = mysql_fetch_array($sql_result);
  362. $result = $result['isocode'];
  363. return $result;
  364. }
  365. /*
  366. -----------------------------------------------------------------------------
  367. Query Functions
  368. these execute a query and return the result(s).
  369. -----------------------------------------------------------------------------
  370. */
  371. /**
  372. * @return a list (array) of all courses.
  373. * @todo shouldn't this be in the course.lib.php script?
  374. */
  375. function get_course_list()
  376. {
  377. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  378. $sql_query = "SELECT * FROM $table";
  379. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  380. $result = api_store_result($sql_result);
  381. return $result;
  382. }
  383. /**
  384. * Returns an array with all database fields for the specified course.
  385. *
  386. * @param the real (system) code of the course (key of the main course table)
  387. * @todo shouldn't this be in the course.lib.php script?
  388. */
  389. function get_course_info($course_code)
  390. {
  391. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  392. $sql_query = "SELECT * FROM $table WHERE `code` = '$course_code'";
  393. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  394. $result = mysql_fetch_array($sql_result);
  395. $result = Database::generate_abstract_course_field_names($result);
  396. return $result;
  397. }
  398. /**
  399. * @param $user_id (integer): the id of the user
  400. * @return $user_info (array): user_id, lastname, firstname, username, email, ...
  401. * @author Patrick Cool <patrick.cool@UGent.be>, expanded to get info for any user
  402. * @author Roan Embrechts, first version + converted to Database API
  403. * @version 30 September 2004
  404. * @desc find all the information about a specified user. Without parameter this is the current user.
  405. * @todo shouldn't this be in the user.lib.php script?
  406. */
  407. function get_user_info_from_id($user_id = '')
  408. {
  409. $table = Database::get_main_table(TABLE_MAIN_USER);
  410. if ($user_id == '')
  411. {
  412. return $GLOBALS["_user"];
  413. }
  414. else
  415. {
  416. $sql_query = "SELECT * FROM $table WHERE `user_id` = '$user_id'";
  417. $result = api_sql_query($sql_query, __FILE__, __LINE__);
  418. $result_array = mysql_fetch_array($result);
  419. $result_array = Database::generate_abstract_user_field_names($result_array);
  420. return $result_array;
  421. }
  422. }
  423. /**
  424. * This creates an abstraction layer between database field names
  425. * and field names expected in code.
  426. *
  427. * This helps when changing database names.
  428. * It's also useful now to get rid of the 'franglais'.
  429. *
  430. * @todo add more array entries to abstract course info from field names
  431. * @author Roan Embrechts
  432. *
  433. * @todo what's the use of this function. I think this is better removed.
  434. * There should be consistency in the variable names and the use throughout the scripts
  435. * for the database name we should consistently use or db_name or database (db_name probably being the better one)
  436. */
  437. function generate_abstract_course_field_names($result_array)
  438. {
  439. $result_array["official_code"] = $result_array["visual_code"];
  440. $result_array["visual_code"] = $result_array["visual_code"];
  441. $result_array["real_code"] = $result_array["code"];
  442. $result_array["system_code"] = $result_array["code"];
  443. $result_array["title"] = $result_array['title'];
  444. $result_array["database"] = $result_array["db_name"];
  445. $result_array["faculty"] = $result_array["category_code"];
  446. //$result_array["directory"] = $result_array["directory"];
  447. /*
  448. still to do: (info taken from local.inc.php)
  449. $_course['id' ] = $cData['cours_id' ]; //auto-assigned integer
  450. $_course['name' ] = $cData['title' ];
  451. $_course['official_code'] = $cData['visual_code' ]; // use in echo
  452. $_course['sysCode' ] = $cData['code' ]; // use as key in db
  453. $_course['path' ] = $cData['directory' ]; // use as key in path
  454. $_course['dbName' ] = $cData['db_name' ]; // use as key in db list
  455. $_course['dbNameGlu' ] = $_configuration['table_prefix'] . $cData['dbName'] . $_configuration['db_glue']; // use in all queries
  456. $_course['titular' ] = $cData['tutor_name' ];
  457. $_course['language' ] = $cData['course_language' ];
  458. $_course['extLink' ]['url' ] = $cData['department_url' ];
  459. $_course['extLink' ]['name'] = $cData['department_name'];
  460. $_course['categoryCode'] = $cData['faCode' ];
  461. $_course['categoryName'] = $cData['faName' ];
  462. $_course['visibility' ] = (bool) ($cData['visibility'] == 2 || $cData['visibility'] == 3);
  463. $_course['registrationAllowed'] = (bool) ($cData['visibility'] == 1 || $cData['visibility'] == 2);
  464. */
  465. return $result_array;
  466. }
  467. /**
  468. * This creates an abstraction layer between database field names
  469. * and field names expected in code.
  470. *
  471. * This helps when changing database names.
  472. * It's also useful now to get rid of the 'franglais'.
  473. *
  474. * @todo add more array entries to abstract user info from field names
  475. * @author Roan Embrechts
  476. * @author Patrick Cool
  477. *
  478. * @todo what's the use of this function. I think this is better removed.
  479. * There should be consistency in the variable names and the use throughout the scripts
  480. */
  481. function generate_abstract_user_field_names($result_array)
  482. {
  483. $result_array['firstName'] = $result_array['firstname'];
  484. $result_array['lastName'] = $result_array['lastname'];
  485. $result_array['mail'] = $result_array['email'];
  486. #$result_array['picture_uri'] = $result_array['picture_uri'];
  487. #$result_array ['user_id' ] = $result_array['user_id' ];
  488. return $result_array;
  489. }
  490. /*
  491. -----------------------------------------------------------------------------
  492. Private Functions
  493. You should not access these from outside the class
  494. No effort is made to keep the names / results the same.
  495. -----------------------------------------------------------------------------
  496. */
  497. /**
  498. * Glues a course database.
  499. * glue format from local.inc.php.
  500. */
  501. function glue_course_database_name($database_name)
  502. {
  503. $prefix = Database::get_course_table_prefix();
  504. $glue = Database::get_database_glue();
  505. $database_name_with_glue = $prefix.$database_name.$glue;
  506. return $database_name_with_glue;
  507. }
  508. /**
  509. * @param string $database_name, can be empty to use current course db
  510. *
  511. * @return the glued parameter if it is not empty,
  512. * or the current course database (glued) if the parameter is empty.
  513. */
  514. function fix_database_parameter($database_name)
  515. {
  516. if ($database_name == '')
  517. {
  518. $course_info = api_get_course_info();
  519. $database_name_with_glue = $course_info["dbNameGlu"];
  520. }
  521. else
  522. {
  523. $database_name_with_glue = Database::glue_course_database_name($database_name);
  524. }
  525. return $database_name_with_glue;
  526. }
  527. /**
  528. * Structures a course database and table name to ready them
  529. * for querying. The course database parameter is considered glued:
  530. * e.g. COURSE001`.`
  531. */
  532. function format_glued_course_table_name($database_name_with_glue, $table)
  533. {
  534. $course_info = api_get_course_info();
  535. return "`".$database_name_with_glue.$table."`";
  536. }
  537. /**
  538. * Structures a database and table name to ready them
  539. * for querying. The database parameter is considered not glued,
  540. * just plain e.g. COURSE001
  541. */
  542. function format_table_name($database, $table)
  543. {
  544. return "`".$database."`.`".$table."`";
  545. }
  546. /**
  547. * Count the number of rows in a table
  548. * @param string $table The table of which the rows should be counted
  549. * @return int The number of rows in the given table.
  550. */
  551. function count_rows($table)
  552. {
  553. $sql = "SELECT COUNT(*) AS n FROM $table";
  554. $res = api_sql_query($sql, __FILE__, __LINE__);
  555. $obj = mysql_fetch_object($res);
  556. return $obj->n;
  557. }
  558. /**
  559. * Gets the ID of the last item inserted into the database
  560. *
  561. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  562. * @return integer The last ID as returned by the DB function
  563. * @comment This should be updated to use ADODB at some point
  564. */
  565. function get_last_insert_id()
  566. {
  567. return mysql_insert_id();
  568. }
  569. /**
  570. * Escapes a string to insert into the database as text
  571. * @param string The string to escape
  572. * @return string The escaped string
  573. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  574. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  575. */
  576. function escape_string($string)
  577. {
  578. if (get_magic_quotes_gpc())
  579. {
  580. $string = stripslashes($string);
  581. }
  582. return mysql_real_escape_string($string);
  583. }
  584. /**
  585. * Gets the array from a SQL result (as returned by api_sql_query) - help achieving database independence
  586. * @param resource The result from a call to sql_query (e.g. api_sql_query)
  587. * @param string Optional: "ASSOC","NUM" or "BOTH", as the constant used in mysql_fetch_array.
  588. * @return array Array of results as returned by php
  589. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  590. */
  591. function fetch_array($res, $option = 'BOTH')
  592. {
  593. if ($option != 'BOTH')
  594. {
  595. if ($option == 'ASSOC')
  596. {
  597. return mysql_fetch_array($res, MYSQL_ASSOC);
  598. }
  599. elseif ($option == 'NUM')
  600. {
  601. return mysql_fetch_array($res, MYSQL_NUM);
  602. }
  603. }
  604. else
  605. {
  606. return mysql_fetch_array($res);
  607. }
  608. }
  609. /**
  610. * Gets the next row of the result of the SQL query (as returned by api_sql_query) in an object form
  611. * @param resource The result from a call to sql_query (e.g. api_sql_query)
  612. * @param string Optional class name to instanciate
  613. * @param array Optional array of parameters
  614. * @return object Object of class StdClass or the required class, containing the query result row
  615. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  616. */
  617. function fetch_object($res,$class=null,$params=null)
  618. {
  619. if(!empty($class))
  620. {
  621. if(is_array($params))
  622. {
  623. return mysql_fetch_object($res,$class,$params);
  624. }
  625. return mysql_fetch_object($res,$class);
  626. }
  627. return mysql_fetch_object($res);
  628. }
  629. /**
  630. * Gets the number of rows from the last query result - help achieving database independence
  631. * @param resource The result
  632. * @return integer The number of rows contained in this result
  633. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  634. **/
  635. function num_rows($res)
  636. {
  637. return mysql_num_rows($res);
  638. }
  639. function get_course_chat_connected_table($database_name = '')
  640. {
  641. $database_name_with_glue = Database::fix_database_parameter($database_name);
  642. return Database::format_glued_course_table_name($database_name_with_glue, CHAT_CONNECTED_TABLE);
  643. }
  644. }
  645. //end class Database
  646. ?>