database.lib.php 31 KB

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