database.lib.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. <?php // $Id: database.lib.php 18799 2009-03-04 22:50:50Z juliomontoya $
  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. //Gradebook
  47. define('TABLE_MAIN_GRADEBOOK_CATEGORY', 'gradebook_category');
  48. define('TABLE_MAIN_GRADEBOOK_EVALUATION', 'gradebook_evaluation');
  49. define('TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG', 'gradebook_linkeval_log');
  50. define('TABLE_MAIN_GRADEBOOK_RESULT', 'gradebook_result');
  51. define('TABLE_MAIN_GRADEBOOK_RESULT_LOG', 'gradebook_result_log');
  52. define('TABLE_MAIN_GRADEBOOK_LINK', 'gradebook_link');
  53. define('TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY','gradebook_score_display');
  54. //Profiling
  55. define('TABLE_MAIN_USER_FIELD', 'user_field');
  56. define('TABLE_MAIN_USER_FIELD_OPTIONS', 'user_field_options');
  57. define('TABLE_MAIN_USER_FIELD_VALUES', 'user_field_values');
  58. //Search engine
  59. define('TABLE_MAIN_SPECIFIC_FIELD', 'specific_field');
  60. define('TABLE_MAIN_SPECIFIC_FIELD_VALUES', 'specific_field_values');
  61. define('TABLE_MAIN_SEARCH_ENGINE_REF', 'search_engine_ref');
  62. //Access URLS
  63. define('TABLE_MAIN_ACCESS_URL', 'access_url');
  64. define('TABLE_MAIN_ACCESS_URL_REL_USER', 'access_url_rel_user');
  65. define('TABLE_MAIN_ACCESS_URL_REL_COURSE', 'access_url_rel_course');
  66. define('TABLE_MAIN_ACCESS_URL_REL_SESSION', 'access_url_rel_session');
  67. //Global calendar
  68. define('TABLE_MAIN_SYSTEM_CALENDAR', 'sys_calendar');
  69. //Reservation System
  70. define('TABLE_MAIN_RESERVATION_ITEM', 'reservation_item');
  71. define('TABLE_MAIN_RESERVATION_RESERVATION', 'reservation_main');
  72. define('TABLE_MAIN_RESERVATION_SUBSCRIBTION', 'reservation_subscription');
  73. define('TABLE_MAIN_RESERVATION_CATEGORY', 'reservation_category');
  74. define('TABLE_MAIN_RESERVATION_ITEM_RIGHTS', 'reservation_item_rights');
  75. //Social networking
  76. define('TABLE_MAIN_USER_FRIEND','user_friend');
  77. define('TABLE_MAIN_USER_FRIEND_RELATION_TYPE','user_friend_relation_type');
  78. //Web services
  79. define('TABLE_MAIN_USER_API_KEY','user_api_key');
  80. define('TABLE_MAIN_COURSE_FIELD', 'course_field');
  81. define('TABLE_MAIN_COURSE_FIELD_VALUES', 'course_field_values');
  82. define('TABLE_MAIN_SESSION_FIELD', 'session_field');
  83. define('TABLE_MAIN_SESSION_FIELD_VALUES', 'session_field_values');
  84. //Message
  85. define('TABLE_MAIN_MESSAGE','message');
  86. //statistic database tables
  87. define('TABLE_STATISTIC_TRACK_E_LASTACCESS', 'track_e_lastaccess');
  88. define('TABLE_STATISTIC_TRACK_E_ACCESS', 'track_e_access');
  89. define('TABLE_STATISTIC_TRACK_E_LOGIN', 'track_e_login');
  90. define('TABLE_STATISTIC_TRACK_E_DOWNLOADS', 'track_e_downloads');
  91. define('TABLE_STATISTIC_TRACK_E_LINKS', 'track_e_links');
  92. define('TABLE_STATISTIC_TRACK_E_ONLINE', 'track_e_online');
  93. define('TABLE_STATISTIC_TRACK_E_HOTPOTATOES', 'track_e_hotpotatoes');
  94. define('TABLE_STATISTIC_TRACK_E_COURSE_ACCESS', 'track_e_course_access');
  95. define('TABLE_STATISTIC_TRACK_E_EXERCICES', 'track_e_exercices');
  96. define('TABLE_STATISTIC_TRACK_E_ATTEMPT', 'track_e_attempt');
  97. define('TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING', 'track_e_attempt_recording');
  98. define('TABLE_STATISTIC_TRACK_E_DEFAULT', 'track_e_default');
  99. define('TABLE_STATISTIC_TRACK_E_UPLOADS','track_e_uploads');
  100. define('TABLE_STATISTIC_TRACK_E_HOTSPOT','track_e_hotspot');
  101. //scorm database tables
  102. define('TABLE_SCORM_MAIN', 'scorm_main');
  103. define('TABLE_SCORM_SCO_DATA', 'scorm_sco_data');
  104. //course tables
  105. define('TABLE_AGENDA', 'calendar_event');
  106. define('TABLE_AGENDA_REPEAT', 'calendar_event_repeat');
  107. define('TABLE_AGENDA_REPEAT_NOT', 'calendar_event_repeat_not');
  108. define('TABLE_AGENDA_ATTACHMENT','calendar_event_attachment');
  109. define('TABLE_ANNOUNCEMENT', 'announcement');
  110. define('TABLE_CHAT_CONNECTED', 'chat_connected'); // @todo: probably no longer in use !!!
  111. define('TABLE_COURSE_DESCRIPTION', 'course_description');
  112. define('TABLE_DOCUMENT', 'document');
  113. define('TABLE_ITEM_PROPERTY', 'item_property');
  114. define('TABLE_LINK', 'link');
  115. define('TABLE_LINK_CATEGORY', 'link_category');
  116. define('TABLE_TOOL_LIST', 'tool');
  117. define('TABLE_TOOL_INTRO', 'tool_intro');
  118. define('TABLE_SCORMDOC', 'scormdocument');
  119. define('TABLE_STUDENT_PUBLICATION', 'student_publication');
  120. define('TABLE_STUDENT_PUBLICATION_ASSIGNMENT', 'student_publication_assignment');
  121. define('CHAT_CONNECTED_TABLE', 'chat_connected');
  122. //course forum tables
  123. define('TABLE_FORUM_CATEGORY','forum_category');
  124. define('TABLE_FORUM','forum_forum');
  125. define('TABLE_FORUM_THREAD','forum_thread');
  126. define('TABLE_FORUM_POST','forum_post');
  127. define('TABLE_FORUM_ATTACHMENT','forum_attachment');
  128. define('TABLE_FORUM_MAIL_QUEUE','forum_mailcue');
  129. define('TABLE_FORUM_THREAD_QUALIFY','forum_thread_qualify');
  130. define('TABLE_FORUM_THREAD_QUALIFY_LOG','forum_thread_qualify_log');
  131. //course group tables
  132. define('TABLE_GROUP', 'group_info');
  133. define('TABLE_GROUP_USER', 'group_rel_user');
  134. define('TABLE_GROUP_TUTOR', 'group_rel_tutor');
  135. define('TABLE_GROUP_CATEGORY', 'group_category');
  136. //course dropbox tables
  137. define('TABLE_DROPBOX_CATEGORY','dropbox_category');
  138. define('TABLE_DROPBOX_FEEDBACK','dropbox_feedback');
  139. define('TABLE_DROPBOX_POST','dropbox_post');
  140. define('TABLE_DROPBOX_FILE','dropbox_file');
  141. define('TABLE_DROPBOX_PERSON','dropbox_person');
  142. //course quiz tables
  143. define('TABLE_QUIZ_QUESTION', 'quiz_question');
  144. define('TABLE_QUIZ_TEST', 'quiz');
  145. define('TABLE_QUIZ_ANSWER', 'quiz_answer');
  146. define('TABLE_QUIZ_TEST_QUESTION', 'quiz_rel_question');
  147. //linked resource table
  148. define('TABLE_LINKED_RESOURCES', 'resource');
  149. //new scorm tables
  150. define('TABLE_LP_MAIN', 'lp');
  151. define('TABLE_LP_ITEM', 'lp_item');
  152. define('TABLE_LP_VIEW', 'lp_view');
  153. define('TABLE_LP_ITEM_VIEW', 'lp_item_view');
  154. define('TABLE_LP_IV_INTERACTION', 'lp_iv_interaction'); // IV = Item View
  155. define('TABLE_LP_IV_OBJECTIVE', 'lp_iv_objective'); // IV = Item View
  156. // Smartblogs (Kevin Van Den Haute::kevin@develop-it.be)
  157. // permission tables
  158. define('TABLE_PERMISSION_USER', 'permission_user');
  159. define('TABLE_PERMISSION_TASK', 'permission_task');
  160. define('TABLE_PERMISSION_GROUP', 'permission_group');
  161. // roles tables
  162. define('TABLE_ROLE', 'role');
  163. define('TABLE_ROLE_PERMISSION', 'role_permissions');
  164. define('TABLE_ROLE_USER', 'role_user');
  165. define('TABLE_ROLE_GROUP', 'role_group');
  166. // blogs tables
  167. define('TABLE_BLOGS', 'blog');
  168. define('TABLE_BLOGS_POSTS', 'blog_post');
  169. define('TABLE_BLOGS_COMMENTS', 'blog_comment');
  170. define('TABLE_BLOGS_REL_USER', 'blog_rel_user');
  171. define('TABLE_BLOGS_TASKS', 'blog_task');
  172. define('TABLE_BLOGS_TASKS_REL_USER', 'blog_task_rel_user');
  173. define('TABLE_BLOGS_RATING', 'blog_rating');
  174. define('TABLE_BLOGS_ATTACHMENT', 'blog_attachment');
  175. define('TABLE_BLOGS_TASKS_PERMISSIONS', 'permission_task');
  176. //end of Smartblogs
  177. // user information tables
  178. define('TABLE_USER_INFO', 'userinfo_def');
  179. define('TABLE_USER_INFO_CONTENT', 'userinfo_content');
  180. // course settings table
  181. define('TABLE_COURSE_SETTING', 'course_setting');
  182. // course online tables
  183. define('TABLE_ONLINE_LINK', 'online_link');
  184. define('TABLE_ONLINE_CONNECTED', 'online_connected');
  185. // dokeos_user database
  186. define('TABLE_PERSONAL_AGENDA', 'personal_agenda');
  187. define('TABLE_PERSONAL_AGENDA_REPEAT', 'personal_agenda_repeat');
  188. define('TABLE_PERSONAL_AGENDA_REPEAT_NOT', 'personal_agenda_repeat_not');
  189. define('TABLE_USER_COURSE_CATEGORY', 'user_course_category');
  190. //Survey
  191. // @todo: are these MAIN tables or course tables ?
  192. define('TABLE_MAIN_SURVEY', 'survey');
  193. define('TABLE_MAIN_GROUP', 'survey_group');
  194. define('TABLE_MAIN_SURVEYQUESTION', 'questions');
  195. // SURVEY
  196. define('TABLE_SURVEY', 'survey');
  197. define('TABLE_SURVEY_QUESTION', 'survey_question');
  198. define('TABLE_SURVEY_QUESTION_OPTION', 'survey_question_option');
  199. define('TABLE_SURVEY_INVITATION', 'survey_invitation');
  200. define('TABLE_SURVEY_ANSWER', 'survey_answer');
  201. define('TABLE_SURVEY_QUESTION_GROUP','survey_group');
  202. // wiki tables
  203. define('TABLE_WIKI', 'wiki');
  204. define('TABLE_WIKI_CONF', 'wiki_conf');
  205. define('TABLE_WIKI_DISCUSS', 'wiki_discuss');
  206. define('TABLE_WIKI_MAILCUE', 'wiki_mailcue');
  207. // GLOSSARY
  208. define('TABLE_GLOSSARY', 'glossary');
  209. // GLOSSARY
  210. define('TABLE_NOTEBOOK', 'notebook');
  211. // MESSAGE
  212. define('TABLE_MESSAGE', 'message');
  213. /*
  214. ==============================================================================
  215. DATABASE CLASS
  216. the class and its functions
  217. ==============================================================================
  218. */
  219. /**
  220. * @package dokeos.library
  221. */
  222. class Database
  223. {
  224. /*
  225. -----------------------------------------------------------------------------
  226. Accessor Functions
  227. Usually, you won't need these directly but instead
  228. rely on of the get_xxx_table functions.
  229. -----------------------------------------------------------------------------
  230. */
  231. /**
  232. * Returns the name of the main Dokeos database.
  233. */
  234. function get_main_database()
  235. {
  236. global $_configuration;
  237. return $_configuration['main_database'];
  238. }
  239. /**
  240. * Returns the name of the Dokeos statistics database.
  241. */
  242. function get_statistic_database()
  243. {
  244. global $_configuration;
  245. return $_configuration['statistics_database'];
  246. }
  247. /**
  248. * Returns the name of the Dokeos SCORM database.
  249. */
  250. function get_scorm_database()
  251. {
  252. global $_configuration;
  253. return $_configuration['scorm_database'];
  254. }
  255. /**
  256. * Returns the name of the database where all the personal stuff of the user is stored
  257. */
  258. function get_user_personal_database()
  259. {
  260. global $_configuration;
  261. return $_configuration['user_personal_database'];
  262. }
  263. /**
  264. * Returns the name of the main Dokeos database.
  265. */
  266. function get_current_course_database()
  267. {
  268. $course_info = api_get_course_info();
  269. return $course_info["dbName"];
  270. }
  271. /**
  272. * Returns the glued name of the current course database.
  273. */
  274. function get_current_course_glued_database()
  275. {
  276. $course_info = api_get_course_info();
  277. return $course_info["dbNameGlu"];
  278. }
  279. /**
  280. * The glue is the string needed between database and table.
  281. * The trick is: in multiple databases, this is a period (with backticks)
  282. * In single database, this can be e.g. an underscore so we just fake
  283. * there are multiple databases and the code can be written independent
  284. * of the single / multiple database setting.
  285. */
  286. function get_database_glue()
  287. {
  288. global $_configuration;
  289. return $_configuration['db_glue'];
  290. }
  291. /**
  292. * Returns the database prefix.
  293. * All created COURSE databases are prefixed with this string.
  294. *
  295. * TIP: this can be convenient e.g. if you have multiple Dokeos installations
  296. * on the same physical server.
  297. */
  298. function get_database_name_prefix()
  299. {
  300. global $_configuration;
  301. return $_configuration['db_prefix'];
  302. }
  303. /**
  304. * Returns the course table prefix for single database.
  305. * Not certain exactly when this is used.
  306. * Do research.
  307. * It's used in local.inc.php.
  308. */
  309. function get_course_table_prefix()
  310. {
  311. global $_configuration;
  312. return $_configuration['table_prefix'];
  313. }
  314. /*
  315. -----------------------------------------------------------------------------
  316. Table Name functions
  317. use these functions to get a table name for queries,
  318. instead of constructing them yourself.
  319. Backticks automatically surround the result,
  320. e.g. `COURSE_NAME`.`link`
  321. so the queries can look cleaner.
  322. Example:
  323. $table = Database::get_course_table(TABLE_DOCUMENT);
  324. $sql_query = "SELECT * FROM $table WHERE $condition";
  325. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  326. $result = mysql_fetch_array($sql_result);
  327. -----------------------------------------------------------------------------
  328. */
  329. /**
  330. * A more generic function than the other get_main_xxx_table functions,
  331. * this one can return the correct complete name of any table of the main database of which you pass
  332. * the short name as a parameter.
  333. * Please define table names as constants in this library and use them
  334. * instead of directly using magic words in your tool code.
  335. *
  336. * @param string $short_table_name, the name of the table
  337. */
  338. function get_main_table($short_table_name)
  339. {
  340. $database = Database::get_main_database();
  341. return Database::format_table_name($database, $short_table_name);
  342. }
  343. /**
  344. * A more generic function than the older get_course_xxx_table functions,
  345. * this one can return the correct complete name of any course table of which you pass
  346. * the short name as a parameter.
  347. * Please define table names as constants in this library and use them
  348. * instead of directly using magic words in your tool code.
  349. *
  350. * @param string $short_table_name, the name of the table
  351. * @param string $database_name, optional, name of the course database
  352. * - if you don't specify this, you work on the current course.
  353. */
  354. function get_course_table($short_table_name, $database_name = '')
  355. {
  356. $database_name_with_glue = Database::fix_database_parameter($database_name);
  357. return Database::format_glued_course_table_name($database_name_with_glue, $short_table_name);
  358. }
  359. /**
  360. * Get a complete course table name from a course code
  361. *
  362. * @param string $course_code
  363. * @param string $table the name of the table
  364. */
  365. function get_course_table_from_code($course_code, $table) {
  366. $ret = NULL;
  367. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  368. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  369. $sql = "SELECT $course_table.db_name, $course_cat_table.code
  370. FROM $course_table
  371. LEFT JOIN $course_cat_table
  372. ON $course_table.category_code = $course_cat_table.code
  373. WHERE $course_table.code = '$course_code'
  374. LIMIT 1";
  375. $res = api_sql_query($sql, __FILE__, __LINE__);
  376. $result = Database::fetch_array($res);
  377. $ret = sprintf ("%s.%s", $result[0], $table);
  378. return $ret;
  379. }
  380. /**
  381. * This generic function returns the correct and complete name of any statistic table
  382. * of which you pass the short name as a parameter.
  383. * Please define table names as constants in this library and use them
  384. * instead of directly using magic words in your tool code.
  385. *
  386. * @param string $short_table_name, the name of the table
  387. */
  388. function get_statistic_table($short_table_name)
  389. {
  390. $database = Database::get_statistic_database();
  391. return Database::format_table_name($database, $short_table_name);
  392. }
  393. /**
  394. * This generic function returns the correct and complete name of any scorm
  395. * table of which you pass the short name as a parameter. Please define
  396. * table names as constants in this library and use them instead of directly
  397. * using magic words in your tool code.
  398. *
  399. * @param string $short_table_name, the name of the table
  400. */
  401. function get_scorm_table($short_table_name)
  402. {
  403. $database = Database::get_scorm_database();
  404. return Database::format_table_name($database, $short_table_name);
  405. }
  406. /**
  407. * This generic function returns the correct and complete name of any scorm
  408. * table of which you pass the short name as a parameter. Please define
  409. * table names as constants in this library and use them instead of directly
  410. * using magic words in your tool code.
  411. *
  412. * @param string $short_table_name, the name of the table
  413. */
  414. function get_user_personal_table($short_table_name)
  415. {
  416. $database = Database::get_user_personal_database();
  417. return Database::format_table_name($database, $short_table_name);
  418. }
  419. /**
  420. * Returns the isocode corresponding to the language directory given.
  421. * Returned codes are according to the following standards (in order of preference):
  422. * - ISO 639-1 : Alpha-2 code (two-letters code - en, fr, es, ...)
  423. * - RFC 4646 : five-letter code based on the ISO 639 two-letter language codes
  424. * and the ISO 3166 two-letter territory codes (pt-BR, ...)
  425. * - ISO 639-2 : Alpha-3 code (three-letters code - ast, fur, ...)
  426. * @return string The isocode
  427. */
  428. function get_language_isocode($lang_folder)
  429. {
  430. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  431. $sql_query = "SELECT isocode FROM $table WHERE dokeos_folder = '$lang_folder'";
  432. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  433. $result = mysql_fetch_array($sql_result);
  434. $result = $result['isocode'];
  435. return $result;
  436. }
  437. /*
  438. -----------------------------------------------------------------------------
  439. Query Functions
  440. these execute a query and return the result(s).
  441. -----------------------------------------------------------------------------
  442. */
  443. /**
  444. * @return a list (array) of all courses.
  445. * @todo shouldn't this be in the course.lib.php script?
  446. */
  447. function get_course_list()
  448. {
  449. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  450. $sql_query = "SELECT * FROM $table";
  451. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  452. $result = api_store_result($sql_result);
  453. return $result;
  454. }
  455. /**
  456. * Returns an array with all database fields for the specified course.
  457. *
  458. * @param the real (system) code of the course (ID from inside the main course table)
  459. * @todo shouldn't this be in the course.lib.php script?
  460. */
  461. function get_course_info($course_code)
  462. {
  463. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  464. $sql_query = "SELECT * FROM $table WHERE `code` = '$course_code'";
  465. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  466. $result = mysql_fetch_array($sql_result);
  467. $result = Database::generate_abstract_course_field_names($result);
  468. if ($result===false) {
  469. $result['db_name']='';
  470. }
  471. return $result;
  472. }
  473. /**
  474. * @param $user_id (integer): the id of the user
  475. * @return $user_info (array): user_id, lastname, firstname, username, email, ...
  476. * @author Patrick Cool <patrick.cool@UGent.be>, expanded to get info for any user
  477. * @author Roan Embrechts, first version + converted to Database API
  478. * @version 30 September 2004
  479. * @desc find all the information about a specified user. Without parameter this is the current user.
  480. * @todo shouldn't this be in the user.lib.php script?
  481. */
  482. function get_user_info_from_id($user_id = '')
  483. {
  484. $table = Database::get_main_table(TABLE_MAIN_USER);
  485. if ($user_id == '')
  486. {
  487. return $GLOBALS["_user"];
  488. }
  489. else
  490. {
  491. $sql_query = "SELECT * FROM $table WHERE `user_id` = '$user_id'";
  492. $result = api_sql_query($sql_query, __FILE__, __LINE__);
  493. $result_array = mysql_fetch_array($result);
  494. $result_array = Database::generate_abstract_user_field_names($result_array);
  495. return $result_array;
  496. }
  497. }
  498. /**
  499. * This creates an abstraction layer between database field names
  500. * and field names expected in code.
  501. *
  502. * This helps when changing database names.
  503. * It's also useful now to get rid of the 'franglais'.
  504. *
  505. * @todo add more array entries to abstract course info from field names
  506. * @author Roan Embrechts
  507. *
  508. * @todo what's the use of this function. I think this is better removed.
  509. * There should be consistency in the variable names and the use throughout the scripts
  510. * for the database name we should consistently use or db_name or database (db_name probably being the better one)
  511. */
  512. function generate_abstract_course_field_names($result_array) {
  513. $visual_code = isset($result_array["visual_code"]) ? $result_array["visual_code"] : null;
  514. $code = isset($result_array["code"]) ? $result_array["code"] : null;
  515. $title = isset($result_array['title']) ? $result_array['title'] : null;
  516. $db_name = isset($result_array["db_name"]) ? $result_array["db_name"] : null;
  517. $category_code= isset($result_array["category_code"]) ? $result_array["category_code"] : null;
  518. $result_array["official_code"] = $visual_code;
  519. $result_array["visual_code"] = $visual_code;
  520. $result_array["real_code"] = $code;
  521. $result_array["system_code"] = $code;
  522. $result_array["title"] = $title;
  523. $result_array["database"] = $db_name;
  524. $result_array["faculty"] = $category_code;
  525. //$result_array["directory"] = $result_array["directory"];
  526. /*
  527. still to do: (info taken from local.inc.php)
  528. $_course['id' ] = $cData['cours_id' ]; //auto-assigned integer
  529. $_course['name' ] = $cData['title' ];
  530. $_course['official_code'] = $cData['visual_code' ]; // use in echo
  531. $_course['sysCode' ] = $cData['code' ]; // use as key in db
  532. $_course['path' ] = $cData['directory' ]; // use as key in path
  533. $_course['dbName' ] = $cData['db_name' ]; // use as key in db list
  534. $_course['dbNameGlu' ] = $_configuration['table_prefix'] . $cData['dbName'] . $_configuration['db_glue']; // use in all queries
  535. $_course['titular' ] = $cData['tutor_name' ];
  536. $_course['language' ] = $cData['course_language' ];
  537. $_course['extLink' ]['url' ] = $cData['department_url' ];
  538. $_course['extLink' ]['name'] = $cData['department_name'];
  539. $_course['categoryCode'] = $cData['faCode' ];
  540. $_course['categoryName'] = $cData['faName' ];
  541. $_course['visibility' ] = (bool) ($cData['visibility'] == 2 || $cData['visibility'] == 3);
  542. $_course['registrationAllowed'] = (bool) ($cData['visibility'] == 1 || $cData['visibility'] == 2);
  543. */
  544. return $result_array;
  545. }
  546. /**
  547. * This creates an abstraction layer between database field names
  548. * and field names expected in code.
  549. *
  550. * This helps when changing database names.
  551. * It's also useful now to get rid of the 'franglais'.
  552. *
  553. * @todo add more array entries to abstract user info from field names
  554. * @author Roan Embrechts
  555. * @author Patrick Cool
  556. *
  557. * @todo what's the use of this function. I think this is better removed.
  558. * There should be consistency in the variable names and the use throughout the scripts
  559. */
  560. function generate_abstract_user_field_names($result_array)
  561. {
  562. $result_array['firstName'] = $result_array['firstname'];
  563. $result_array['lastName'] = $result_array['lastname'];
  564. $result_array['mail'] = $result_array['email'];
  565. #$result_array['picture_uri'] = $result_array['picture_uri'];
  566. #$result_array ['user_id' ] = $result_array['user_id' ];
  567. return $result_array;
  568. }
  569. /*
  570. -----------------------------------------------------------------------------
  571. Private Functions
  572. You should not access these from outside the class
  573. No effort is made to keep the names / results the same.
  574. -----------------------------------------------------------------------------
  575. */
  576. /**
  577. * Glues a course database.
  578. * glue format from local.inc.php.
  579. */
  580. function glue_course_database_name($database_name)
  581. {
  582. $prefix = Database::get_course_table_prefix();
  583. $glue = Database::get_database_glue();
  584. $database_name_with_glue = $prefix.$database_name.$glue;
  585. return $database_name_with_glue;
  586. }
  587. /**
  588. * @param string $database_name, can be empty to use current course db
  589. *
  590. * @return the glued parameter if it is not empty,
  591. * or the current course database (glued) if the parameter is empty.
  592. */
  593. function fix_database_parameter($database_name)
  594. {
  595. if ($database_name == '')
  596. {
  597. $course_info = api_get_course_info();
  598. $database_name_with_glue = $course_info["dbNameGlu"];
  599. }
  600. else
  601. {
  602. $database_name_with_glue = Database::glue_course_database_name($database_name);
  603. }
  604. return $database_name_with_glue;
  605. }
  606. /**
  607. * Structures a course database and table name to ready them
  608. * for querying. The course database parameter is considered glued:
  609. * e.g. COURSE001`.`
  610. */
  611. function format_glued_course_table_name($database_name_with_glue, $table)
  612. {
  613. //$course_info = api_get_course_info();
  614. return "`".$database_name_with_glue.$table."`";
  615. }
  616. /**
  617. * Structures a database and table name to ready them
  618. * for querying. The database parameter is considered not glued,
  619. * just plain e.g. COURSE001
  620. */
  621. function format_table_name($database, $table)
  622. {
  623. return "`".$database."`.`".$table."`";
  624. }
  625. /**
  626. * Count the number of rows in a table
  627. * @param string $table The table of which the rows should be counted
  628. * @return int The number of rows in the given table.
  629. */
  630. function count_rows($table)
  631. {
  632. $sql = "SELECT COUNT(*) AS n FROM $table";
  633. $res = api_sql_query($sql, __FILE__, __LINE__);
  634. $obj = mysql_fetch_object($res);
  635. return $obj->n;
  636. }
  637. /**
  638. * Gets the ID of the last item inserted into the database
  639. *
  640. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  641. * @return integer The last ID as returned by the DB function
  642. * @comment This should be updated to use ADODB at some point
  643. */
  644. function get_last_insert_id()
  645. {
  646. return mysql_insert_id();
  647. }
  648. /**
  649. * Escapes a string to insert into the database as text
  650. * @param string The string to escape
  651. * @return string The escaped string
  652. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  653. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  654. */
  655. function escape_string($string)
  656. {
  657. if (get_magic_quotes_gpc())
  658. {
  659. $string = stripslashes($string);
  660. }
  661. return mysql_real_escape_string($string);
  662. }
  663. /**
  664. * Gets the array from a SQL result (as returned by api_sql_query) - help achieving database independence
  665. * @param resource The result from a call to sql_query (e.g. api_sql_query)
  666. * @param string Optional: "ASSOC","NUM" or "BOTH", as the constant used in mysql_fetch_array.
  667. * @return array Array of results as returned by php
  668. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  669. */
  670. function fetch_array($res, $option = 'BOTH')
  671. {
  672. if ($option != 'BOTH')
  673. {
  674. if ($option == 'ASSOC')
  675. {
  676. return mysql_fetch_array($res, MYSQL_ASSOC);
  677. }
  678. elseif ($option == 'NUM')
  679. {
  680. return mysql_fetch_array($res, MYSQL_NUM);
  681. }
  682. }
  683. else
  684. {
  685. return mysql_fetch_array($res);
  686. }
  687. }
  688. /**
  689. * Gets the next row of the result of the SQL query (as returned by api_sql_query) in an object form
  690. * @param resource The result from a call to sql_query (e.g. api_sql_query)
  691. * @param string Optional class name to instanciate
  692. * @param array Optional array of parameters
  693. * @return object Object of class StdClass or the required class, containing the query result row
  694. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  695. */
  696. function fetch_object($res,$class=null,$params=null)
  697. {
  698. if(!empty($class))
  699. {
  700. if(is_array($params))
  701. {
  702. return mysql_fetch_object($res,$class,$params);
  703. }
  704. return mysql_fetch_object($res,$class);
  705. }
  706. return mysql_fetch_object($res);
  707. }
  708. /**
  709. * Gets the array from a SQL result (as returned by api_sql_query) - help achieving database independence
  710. * @param resource The result from a call to sql_query (e.g. api_sql_query)
  711. * @return array Array of results as returned by php (mysql_fetch_row)
  712. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  713. */
  714. function fetch_row($res)
  715. {
  716. return mysql_fetch_row($res);
  717. }
  718. /**
  719. * Gets the number of rows from the last query result - help achieving database independence
  720. * @param resource The result
  721. * @return integer The number of rows contained in this result
  722. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  723. **/
  724. function num_rows($res)
  725. {
  726. return mysql_num_rows($res);
  727. }
  728. function get_course_chat_connected_table($database_name = '')
  729. {
  730. $database_name_with_glue = Database::fix_database_parameter($database_name);
  731. return Database::format_glued_course_table_name($database_name_with_glue, CHAT_CONNECTED_TABLE);
  732. }
  733. /**
  734. * Acts as the relative *_result() function of most DB drivers and fetches a
  735. * specific line and a field
  736. * @param resource The database resource to get data from
  737. * @param integer The row number
  738. * @param string Optional field name or number
  739. * @result mixed One cell of the result, or FALSE on error
  740. */
  741. function result($resource,$row,$field='')
  742. {
  743. if (mysql_num_rows($resource) > 0) {
  744. if(!empty($field))
  745. {
  746. return mysql_result($resource,$row,$field);
  747. }
  748. else
  749. {
  750. return mysql_result($resource,$row);
  751. }
  752. }
  753. }
  754. /**
  755. * Recovers the last ID of any insert query executed over this SQL connection
  756. * @return integer Unique ID of the latest inserted row
  757. */
  758. function insert_id()
  759. {
  760. return mysql_insert_id();
  761. }
  762. /**
  763. * Returns the number of affected rows
  764. * @param resource Optional database resource
  765. */
  766. function affected_rows($r=null)
  767. {
  768. if(isset($r))
  769. {
  770. return mysql_affected_rows($r);
  771. }
  772. else
  773. {
  774. return mysql_affected_rows();
  775. }
  776. }
  777. function query($sql,$file='',$line=0)
  778. {
  779. return api_sql_query($sql,$file,$line);
  780. }
  781. function error()
  782. {
  783. return mysql_error();
  784. }
  785. /**
  786. * Return course code from one given gradebook category's id
  787. * @param int Category ID
  788. * @return string Course code
  789. * @todo move this function in a gradebook-related library
  790. */
  791. function get_course_by_category ($category_id) {
  792. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  793. $sql = 'SELECT course_code FROM '.$tbl_grade_categories.' WHERE id='.$category_id;
  794. $res=api_sql_query($sql, __FILE__, __LINE__);
  795. $option=Database::fetch_array($res,'ASSOC');
  796. if ($option) {
  797. return $option['course_code'];
  798. } else {
  799. return false;
  800. }
  801. }
  802. }
  803. //end class Database