database.lib.php 25 KB

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