dokeos-library
[ class tree: dokeos-library ] [ index: dokeos-library ] [ all elements ]

Source for file database.lib.php

Documentation is available at database.lib.php

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

Documentation generated on Thu, 12 Jun 2008 13:17:56 -0500 by phpDocumentor 1.4.1