database.lib.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php // $Id: database.lib.php 16432 2008-10-03 12:05:39Z herodoto $
  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. // wiki tables
  170. define('TABLE_WIKI', 'wiki');
  171. define('TABLE_WIKI_CONF', 'wiki_conf');
  172. define('TABLE_WIKI_DISCUSS', 'wiki_discuss');
  173. define('TABLE_WIKI_MAILCUE', 'wiki_mailcue');
  174. /*
  175. ==============================================================================
  176. DATABASE CLASS
  177. the class and its functions
  178. ==============================================================================
  179. */
  180. /**
  181. * @package dokeos.library
  182. */
  183. class Database
  184. {
  185. /*
  186. -----------------------------------------------------------------------------
  187. Accessor Functions
  188. Usually, you won't need these directly but instead
  189. rely on of the get_xxx_table functions.
  190. -----------------------------------------------------------------------------
  191. */
  192. /**
  193. * Returns the name of the main Dokeos database.
  194. */
  195. function get_main_database()
  196. {
  197. global $_configuration;
  198. return $_configuration['main_database'];
  199. }
  200. /**
  201. * Returns the name of the Dokeos statistics database.
  202. */
  203. function get_statistic_database()
  204. {
  205. global $_configuration;
  206. return $_configuration['statistics_database'];
  207. }
  208. /**
  209. * Returns the name of the Dokeos SCORM database.
  210. */
  211. function get_scorm_database()
  212. {
  213. global $_configuration;
  214. return $_configuration['scorm_database'];
  215. }
  216. /**
  217. * Returns the name of the database where all the personal stuff of the user is stored
  218. */
  219. function get_user_personal_database()
  220. {
  221. global $_configuration;
  222. return $_configuration['user_personal_database'];
  223. }
  224. /**
  225. * Returns the name of the main Dokeos database.
  226. */
  227. function get_current_course_database()
  228. {
  229. $course_info = api_get_course_info();
  230. return $course_info["dbName"];
  231. }
  232. /**
  233. * Returns the glued name of the current course database.
  234. */
  235. function get_current_course_glued_database()
  236. {
  237. $course_info = api_get_course_info();
  238. return $course_info["dbNameGlu"];
  239. }
  240. /**
  241. * The glue is the string needed between database and table.
  242. * The trick is: in multiple databases, this is a period (with backticks)
  243. * In single database, this can be e.g. an underscore so we just fake
  244. * there are multiple databases and the code can be written independent
  245. * of the single / multiple database setting.
  246. */
  247. function get_database_glue()
  248. {
  249. global $_configuration;
  250. return $_configuration['db_glue'];
  251. }
  252. /**
  253. * Returns the database prefix.
  254. * All created COURSE databases are prefixed with this string.
  255. *
  256. * TIP: this can be convenient e.g. if you have multiple Dokeos installations
  257. * on the same physical server.
  258. */
  259. function get_database_name_prefix()
  260. {
  261. global $_configuration;
  262. return $_configuration['db_prefix'];
  263. }
  264. /**
  265. * Returns the course table prefix for single database.
  266. * Not certain exactly when this is used.
  267. * Do research.
  268. * It's used in local.inc.php.
  269. */
  270. function get_course_table_prefix()
  271. {
  272. global $_configuration;
  273. return $_configuration['table_prefix'];
  274. }
  275. /*
  276. -----------------------------------------------------------------------------
  277. Table Name functions
  278. use these functions to get a table name for queries,
  279. instead of constructing them yourself.
  280. Backticks automatically surround the result,
  281. e.g. `COURSE_NAME`.`link`
  282. so the queries can look cleaner.
  283. Example:
  284. $table = Database::get_course_table(TABLE_DOCUMENT);
  285. $sql_query = "SELECT * FROM $table WHERE $condition";
  286. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  287. $result = mysql_fetch_array($sql_result);
  288. -----------------------------------------------------------------------------
  289. */
  290. /**
  291. * A more generic function than the other get_main_xxx_table functions,
  292. * this one can return the correct complete name of any table of the main database of which you pass
  293. * the short name as a parameter.
  294. * Please define table names as constants in this library and use them
  295. * instead of directly using magic words in your tool code.
  296. *
  297. * @param string $short_table_name, the name of the table
  298. */
  299. function get_main_table($short_table_name)
  300. {
  301. $database = Database::get_main_database();
  302. return Database::format_table_name($database, $short_table_name);
  303. }
  304. /**
  305. * A more generic function than the older get_course_xxx_table functions,
  306. * this one can return the correct complete name of any course table of which you pass
  307. * the short name as a parameter.
  308. * Please define table names as constants in this library and use them
  309. * instead of directly using magic words in your tool code.
  310. *
  311. * @param string $short_table_name, the name of the table
  312. * @param string $database_name, optional, name of the course database
  313. * - if you don't specify this, you work on the current course.
  314. */
  315. function get_course_table($short_table_name, $database_name = '')
  316. {
  317. $database_name_with_glue = Database::fix_database_parameter($database_name);
  318. return Database::format_glued_course_table_name($database_name_with_glue, $short_table_name);
  319. }
  320. /**
  321. * This generic function returns the correct and complete name of any statistic table
  322. * of which you pass the short name as a parameter.
  323. * Please define table names as constants in this library and use them
  324. * instead of directly using magic words in your tool code.
  325. *
  326. * @param string $short_table_name, the name of the table
  327. */
  328. function get_statistic_table($short_table_name)
  329. {
  330. $database = Database::get_statistic_database();
  331. return Database::format_table_name($database, $short_table_name);
  332. }
  333. /**
  334. * This generic function returns the correct and complete name of any scorm
  335. * table of which you pass the short name as a parameter. Please define
  336. * table names as constants in this library and use them instead of directly
  337. * using magic words in your tool code.
  338. *
  339. * @param string $short_table_name, the name of the table
  340. */
  341. function get_scorm_table($short_table_name)
  342. {
  343. $database = Database::get_scorm_database();
  344. return Database::format_table_name($database, $short_table_name);
  345. }
  346. /**
  347. * This generic function returns the correct and complete name of any scorm
  348. * table of which you pass the short name as a parameter. Please define
  349. * table names as constants in this library and use them instead of directly
  350. * using magic words in your tool code.
  351. *
  352. * @param string $short_table_name, the name of the table
  353. */
  354. function get_user_personal_table($short_table_name)
  355. {
  356. $database = Database::get_user_personal_database();
  357. return Database::format_table_name($database, $short_table_name);
  358. }
  359. /**
  360. * Returns the isocode corresponding to the language directory given.
  361. */
  362. function get_language_isocode($lang_folder)
  363. {
  364. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  365. $sql_query = "SELECT isocode FROM $table WHERE dokeos_folder = '$lang_folder'";
  366. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  367. $result = mysql_fetch_array($sql_result);
  368. $result = $result['isocode'];
  369. return $result;
  370. }
  371. /*
  372. -----------------------------------------------------------------------------
  373. Query Functions
  374. these execute a query and return the result(s).
  375. -----------------------------------------------------------------------------
  376. */
  377. /**
  378. * @return a list (array) of all courses.
  379. * @todo shouldn't this be in the course.lib.php script?
  380. */
  381. function get_course_list()
  382. {
  383. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  384. $sql_query = "SELECT * FROM $table";
  385. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  386. $result = api_store_result($sql_result);
  387. return $result;
  388. }
  389. /**
  390. * Returns an array with all database fields for the specified course.
  391. *
  392. * @param the real (system) code of the course (ID from inside the main course table)
  393. * @todo shouldn't this be in the course.lib.php script?
  394. */
  395. function get_course_info($course_code)
  396. {
  397. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  398. $sql_query = "SELECT * FROM $table WHERE `code` = '$course_code'";
  399. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  400. $result = mysql_fetch_array($sql_result);
  401. $result = Database::generate_abstract_course_field_names($result);
  402. return $result;
  403. }
  404. /**
  405. * @param $user_id (integer): the id of the user
  406. * @return $user_info (array): user_id, lastname, firstname, username, email, ...
  407. * @author Patrick Cool <patrick.cool@UGent.be>, expanded to get info for any user
  408. * @author Roan Embrechts, first version + converted to Database API
  409. * @version 30 September 2004
  410. * @desc find all the information about a specified user. Without parameter this is the current user.
  411. * @todo shouldn't this be in the user.lib.php script?
  412. */
  413. function get_user_info_from_id($user_id = '')
  414. {
  415. $table = Database::get_main_table(TABLE_MAIN_USER);
  416. if ($user_id == '')
  417. {
  418. return $GLOBALS["_user"];
  419. }
  420. else
  421. {
  422. $sql_query = "SELECT * FROM $table WHERE `user_id` = '$user_id'";
  423. $result = api_sql_query($sql_query, __FILE__, __LINE__);
  424. $result_array = mysql_fetch_array($result);
  425. $result_array = Database::generate_abstract_user_field_names($result_array);
  426. return $result_array;
  427. }
  428. }
  429. /**
  430. * This creates an abstraction layer between database field names
  431. * and field names expected in code.
  432. *
  433. * This helps when changing database names.
  434. * It's also useful now to get rid of the 'franglais'.
  435. *
  436. * @todo add more array entries to abstract course info from field names
  437. * @author Roan Embrechts
  438. *
  439. * @todo what's the use of this function. I think this is better removed.
  440. * There should be consistency in the variable names and the use throughout the scripts
  441. * for the database name we should consistently use or db_name or database (db_name probably being the better one)
  442. */
  443. function generate_abstract_course_field_names($result_array)
  444. {
  445. $result_array["official_code"] = $result_array["visual_code"];
  446. $result_array["visual_code"] = $result_array["visual_code"];
  447. $result_array["real_code"] = $result_array["code"];
  448. $result_array["system_code"] = $result_array["code"];
  449. $result_array["title"] = $result_array['title'];
  450. $result_array["database"] = $result_array["db_name"];
  451. $result_array["faculty"] = $result_array["category_code"];
  452. //$result_array["directory"] = $result_array["directory"];
  453. /*
  454. still to do: (info taken from local.inc.php)
  455. $_course['id' ] = $cData['cours_id' ]; //auto-assigned integer
  456. $_course['name' ] = $cData['title' ];
  457. $_course['official_code'] = $cData['visual_code' ]; // use in echo
  458. $_course['sysCode' ] = $cData['code' ]; // use as key in db
  459. $_course['path' ] = $cData['directory' ]; // use as key in path
  460. $_course['dbName' ] = $cData['db_name' ]; // use as key in db list
  461. $_course['dbNameGlu' ] = $_configuration['table_prefix'] . $cData['dbName'] . $_configuration['db_glue']; // use in all queries
  462. $_course['titular' ] = $cData['tutor_name' ];
  463. $_course['language' ] = $cData['course_language' ];
  464. $_course['extLink' ]['url' ] = $cData['department_url' ];
  465. $_course['extLink' ]['name'] = $cData['department_name'];
  466. $_course['categoryCode'] = $cData['faCode' ];
  467. $_course['categoryName'] = $cData['faName' ];
  468. $_course['visibility' ] = (bool) ($cData['visibility'] == 2 || $cData['visibility'] == 3);
  469. $_course['registrationAllowed'] = (bool) ($cData['visibility'] == 1 || $cData['visibility'] == 2);
  470. */
  471. return $result_array;
  472. }
  473. /**
  474. * This creates an abstraction layer between database field names
  475. * and field names expected in code.
  476. *
  477. * This helps when changing database names.
  478. * It's also useful now to get rid of the 'franglais'.
  479. *
  480. * @todo add more array entries to abstract user info from field names
  481. * @author Roan Embrechts
  482. * @author Patrick Cool
  483. *
  484. * @todo what's the use of this function. I think this is better removed.
  485. * There should be consistency in the variable names and the use throughout the scripts
  486. */
  487. function generate_abstract_user_field_names($result_array)
  488. {
  489. $result_array['firstName'] = $result_array['firstname'];
  490. $result_array['lastName'] = $result_array['lastname'];
  491. $result_array['mail'] = $result_array['email'];
  492. #$result_array['picture_uri'] = $result_array['picture_uri'];
  493. #$result_array ['user_id' ] = $result_array['user_id' ];
  494. return $result_array;
  495. }
  496. /*
  497. -----------------------------------------------------------------------------
  498. Private Functions
  499. You should not access these from outside the class
  500. No effort is made to keep the names / results the same.
  501. -----------------------------------------------------------------------------
  502. */
  503. /**
  504. * Glues a course database.
  505. * glue format from local.inc.php.
  506. */
  507. function glue_course_database_name($database_name)
  508. {
  509. $prefix = Database::get_course_table_prefix();
  510. $glue = Database::get_database_glue();
  511. $database_name_with_glue = $prefix.$database_name.$glue;
  512. return $database_name_with_glue;
  513. }
  514. /**
  515. * @param string $database_name, can be empty to use current course db
  516. *
  517. * @return the glued parameter if it is not empty,
  518. * or the current course database (glued) if the parameter is empty.
  519. */
  520. function fix_database_parameter($database_name)
  521. {
  522. if ($database_name == '')
  523. {
  524. $course_info = api_get_course_info();
  525. $database_name_with_glue = $course_info["dbNameGlu"];
  526. }
  527. else
  528. {
  529. $database_name_with_glue = Database::glue_course_database_name($database_name);
  530. }
  531. return $database_name_with_glue;
  532. }
  533. /**
  534. * Structures a course database and table name to ready them
  535. * for querying. The course database parameter is considered glued:
  536. * e.g. COURSE001`.`
  537. */
  538. function format_glued_course_table_name($database_name_with_glue, $table)
  539. {
  540. //$course_info = api_get_course_info();
  541. return "`".$database_name_with_glue.$table."`";
  542. }
  543. /**
  544. * Structures a database and table name to ready them
  545. * for querying. The database parameter is considered not glued,
  546. * just plain e.g. COURSE001
  547. */
  548. function format_table_name($database, $table)
  549. {
  550. return "`".$database."`.`".$table."`";
  551. }
  552. /**
  553. * Count the number of rows in a table
  554. * @param string $table The table of which the rows should be counted
  555. * @return int The number of rows in the given table.
  556. */
  557. function count_rows($table)
  558. {
  559. $sql = "SELECT COUNT(*) AS n FROM $table";
  560. $res = api_sql_query($sql, __FILE__, __LINE__);
  561. $obj = mysql_fetch_object($res);
  562. return $obj->n;
  563. }
  564. /**
  565. * Gets the ID of the last item inserted into the database
  566. *
  567. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  568. * @return integer The last ID as returned by the DB function
  569. * @comment This should be updated to use ADODB at some point
  570. */
  571. function get_last_insert_id()
  572. {
  573. return mysql_insert_id();
  574. }
  575. /**
  576. * Escapes a string to insert into the database as text
  577. * @param string The string to escape
  578. * @return string The escaped string
  579. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  580. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  581. */
  582. function escape_string($string)
  583. {
  584. if (get_magic_quotes_gpc())
  585. {
  586. $string = stripslashes($string);
  587. }
  588. return mysql_real_escape_string($string);
  589. }
  590. /**
  591. * Gets the array from a SQL result (as returned by api_sql_query) - help achieving database independence
  592. * @param resource The result from a call to sql_query (e.g. api_sql_query)
  593. * @param string Optional: "ASSOC","NUM" or "BOTH", as the constant used in mysql_fetch_array.
  594. * @return array Array of results as returned by php
  595. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  596. */
  597. function fetch_array($res, $option = 'BOTH')
  598. {
  599. if ($option != 'BOTH')
  600. {
  601. if ($option == 'ASSOC')
  602. {
  603. return mysql_fetch_array($res, MYSQL_ASSOC);
  604. }
  605. elseif ($option == 'NUM')
  606. {
  607. return mysql_fetch_array($res, MYSQL_NUM);
  608. }
  609. }
  610. else
  611. {
  612. return mysql_fetch_array($res);
  613. }
  614. }
  615. /**
  616. * Gets the next row of the result of the SQL query (as returned by api_sql_query) in an object form
  617. * @param resource The result from a call to sql_query (e.g. api_sql_query)
  618. * @param string Optional class name to instanciate
  619. * @param array Optional array of parameters
  620. * @return object Object of class StdClass or the required class, containing the query result row
  621. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  622. */
  623. function fetch_object($res,$class=null,$params=null)
  624. {
  625. if(!empty($class))
  626. {
  627. if(is_array($params))
  628. {
  629. return mysql_fetch_object($res,$class,$params);
  630. }
  631. return mysql_fetch_object($res,$class);
  632. }
  633. return mysql_fetch_object($res);
  634. }
  635. /**
  636. * Gets the array from a SQL result (as returned by api_sql_query) - help achieving database independence
  637. * @param resource The result from a call to sql_query (e.g. api_sql_query)
  638. * @return array Array of results as returned by php (mysql_fetch_row)
  639. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  640. */
  641. function fetch_row($res)
  642. {
  643. return mysql_fetch_row($res);
  644. }
  645. /**
  646. * Gets the number of rows from the last query result - help achieving database independence
  647. * @param resource The result
  648. * @return integer The number of rows contained in this result
  649. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  650. **/
  651. function num_rows($res)
  652. {
  653. return mysql_num_rows($res);
  654. }
  655. function get_course_chat_connected_table($database_name = '')
  656. {
  657. $database_name_with_glue = Database::fix_database_parameter($database_name);
  658. return Database::format_glued_course_table_name($database_name_with_glue, CHAT_CONNECTED_TABLE);
  659. }
  660. /**
  661. * Acts as the relative *_result() function of most DB drivers and fetches a
  662. * specific line and a field
  663. * @param resource The database resource to get data from
  664. * @param integer The row number
  665. * @param string Optional field name or number
  666. * @result mixed One cell of the result, or FALSE on error
  667. */
  668. function result($resource,$row,$field='')
  669. {
  670. if(!empty($field))
  671. {
  672. return mysql_result($resource,$row,$field);
  673. }
  674. else
  675. {
  676. return mysql_result($resource,$row);
  677. }
  678. }
  679. /**
  680. * Recovers the last ID of any insert query executed over this SQL connection
  681. * @return integer Unique ID of the latest inserted row
  682. */
  683. function insert_id()
  684. {
  685. return mysql_insert_id();
  686. }
  687. /**
  688. * Returns the number of affected rows
  689. * @param resource Optional database resource
  690. */
  691. function affected_rows($r=null)
  692. {
  693. if(isset($r))
  694. {
  695. return mysql_affected_rows($r);
  696. }
  697. else
  698. {
  699. return mysql_affected_rows();
  700. }
  701. }
  702. function query($sql,$file='',$line=0)
  703. {
  704. return api_sql_query($sql,$file,$line);
  705. }
  706. function error()
  707. {
  708. return mysql_error();
  709. }
  710. }
  711. //end class Database
  712. ?>