database.lib.php 32 KB

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