database.lib.php 31 KB

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