database.lib.php 29 KB

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