database.lib.php 26 KB

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