, expanded to get info for any user * @author Roan Embrechts, first version + converted to Database API * @version 30 September 2004 * @desc find all the information about a specified user. Without parameter this is the current user. * @todo shouldn't this be in the user.lib.php script? */ function get_user_info_from_id($user_id = '') { $table = Database::get_main_table(TABLE_MAIN_USER); if ($user_id == '') { return $GLOBALS["_user"]; } else { $sql_query = "SELECT * FROM $table WHERE `user_id` = '$user_id'"; $result = api_sql_query($sql_query, __FILE__, __LINE__); $result_array = mysql_fetch_array($result); $result_array = Database::generate_abstract_user_field_names($result_array); return $result_array; } } /** * This creates an abstraction layer between database field names * and field names expected in code. * * This helps when changing database names. * It's also useful now to get rid of the 'franglais'. * * @todo add more array entries to abstract course info from field names * @author Roan Embrechts * * @todo what's the use of this function. I think this is better removed. * There should be consistency in the variable names and the use throughout the scripts * for the database name we should consistently use or db_name or database (db_name probably being the better one) */ function generate_abstract_course_field_names($result_array) { $visual_code = isset($result_array["visual_code"]) ? $result_array["visual_code"] : null; $code = isset($result_array["code"]) ? $result_array["code"] : null; $title = isset($result_array['title']) ? $result_array['title'] : null; $db_name = isset($result_array["db_name"]) ? $result_array["db_name"] : null; $category_code= isset($result_array["category_code"]) ? $result_array["category_code"] : null; $result_array["official_code"] = $visual_code; $result_array["visual_code"] = $visual_code; $result_array["real_code"] = $code; $result_array["system_code"] = $code; $result_array["title"] = $title; $result_array["database"] = $db_name; $result_array["faculty"] = $category_code; //$result_array["directory"] = $result_array["directory"]; /* still to do: (info taken from local.inc.php) $_course['id' ] = $cData['cours_id' ]; //auto-assigned integer $_course['name' ] = $cData['title' ]; $_course['official_code'] = $cData['visual_code' ]; // use in echo $_course['sysCode' ] = $cData['code' ]; // use as key in db $_course['path' ] = $cData['directory' ]; // use as key in path $_course['dbName' ] = $cData['db_name' ]; // use as key in db list $_course['dbNameGlu' ] = $_configuration['table_prefix'] . $cData['dbName'] . $_configuration['db_glue']; // use in all queries $_course['titular' ] = $cData['tutor_name' ]; $_course['language' ] = $cData['course_language' ]; $_course['extLink' ]['url' ] = $cData['department_url' ]; $_course['extLink' ]['name'] = $cData['department_name']; $_course['categoryCode'] = $cData['faCode' ]; $_course['categoryName'] = $cData['faName' ]; $_course['visibility' ] = (bool) ($cData['visibility'] == 2 || $cData['visibility'] == 3); $_course['registrationAllowed'] = (bool) ($cData['visibility'] == 1 || $cData['visibility'] == 2); */ return $result_array; } /** * This creates an abstraction layer between database field names * and field names expected in code. * * This helps when changing database names. * It's also useful now to get rid of the 'franglais'. * * @todo add more array entries to abstract user info from field names * @author Roan Embrechts * @author Patrick Cool * * @todo what's the use of this function. I think this is better removed. * There should be consistency in the variable names and the use throughout the scripts */ function generate_abstract_user_field_names($result_array) { $result_array['firstName'] = $result_array['firstname']; $result_array['lastName'] = $result_array['lastname']; $result_array['mail'] = $result_array['email']; #$result_array['picture_uri'] = $result_array['picture_uri']; #$result_array ['user_id' ] = $result_array['user_id' ]; return $result_array; } /* ----------------------------------------------------------------------------- Private Functions You should not access these from outside the class No effort is made to keep the names / results the same. ----------------------------------------------------------------------------- */ /** * Glues a course database. * glue format from local.inc.php. */ function glue_course_database_name($database_name) { $prefix = Database::get_course_table_prefix(); $glue = Database::get_database_glue(); $database_name_with_glue = $prefix.$database_name.$glue; return $database_name_with_glue; } /** * @param string $database_name, can be empty to use current course db * * @return the glued parameter if it is not empty, * or the current course database (glued) if the parameter is empty. */ function fix_database_parameter($database_name) { if ($database_name == '') { $course_info = api_get_course_info(); $database_name_with_glue = $course_info["dbNameGlu"]; } else { $database_name_with_glue = Database::glue_course_database_name($database_name); } return $database_name_with_glue; } /** * Structures a course database and table name to ready them * for querying. The course database parameter is considered glued: * e.g. COURSE001`.` */ function format_glued_course_table_name($database_name_with_glue, $table) { //$course_info = api_get_course_info(); return "`".$database_name_with_glue.$table."`"; } /** * Structures a database and table name to ready them * for querying. The database parameter is considered not glued, * just plain e.g. COURSE001 */ function format_table_name($database, $table) { return "`".$database."`.`".$table."`"; } /** * Count the number of rows in a table * @param string $table The table of which the rows should be counted * @return int The number of rows in the given table. */ function count_rows($table) { $sql = "SELECT COUNT(*) AS n FROM $table"; $res = api_sql_query($sql, __FILE__, __LINE__); $obj = mysql_fetch_object($res); return $obj->n; } /** * Gets the ID of the last item inserted into the database * * @author Yannick Warnier * @return integer The last ID as returned by the DB function * @comment This should be updated to use ADODB at some point */ function get_last_insert_id() { return mysql_insert_id(); } /** * Escapes a string to insert into the database as text * @param string The string to escape * @return string The escaped string * @author Yannick Warnier * @author Patrick Cool , Ghent University */ function escape_string($string) { if (get_magic_quotes_gpc()) { $string = stripslashes($string); } return mysql_real_escape_string($string); } /** * Gets the array from a SQL result (as returned by api_sql_query) - help achieving database independence * @param resource The result from a call to sql_query (e.g. api_sql_query) * @param string Optional: "ASSOC","NUM" or "BOTH", as the constant used in mysql_fetch_array. * @return array Array of results as returned by php * @author Yannick Warnier */ function fetch_array($res, $option = 'BOTH') { if ($option != 'BOTH') { if ($option == 'ASSOC') { return mysql_fetch_array($res, MYSQL_ASSOC); } elseif ($option == 'NUM') { return mysql_fetch_array($res, MYSQL_NUM); } } else { return mysql_fetch_array($res); } } /** * Gets the next row of the result of the SQL query (as returned by api_sql_query) in an object form * @param resource The result from a call to sql_query (e.g. api_sql_query) * @param string Optional class name to instanciate * @param array Optional array of parameters * @return object Object of class StdClass or the required class, containing the query result row * @author Yannick Warnier */ function fetch_object($res,$class=null,$params=null) { if(!empty($class)) { if(is_array($params)) { return mysql_fetch_object($res,$class,$params); } return mysql_fetch_object($res,$class); } return mysql_fetch_object($res); } /** * Gets the array from a SQL result (as returned by api_sql_query) - help achieving database independence * @param resource The result from a call to sql_query (e.g. api_sql_query) * @return array Array of results as returned by php (mysql_fetch_row) * @author Yannick Warnier */ function fetch_row($res) { return mysql_fetch_row($res); } /** * Gets the number of rows from the last query result - help achieving database independence * @param resource The result * @return integer The number of rows contained in this result * @author Yannick Warnier **/ function num_rows($res) { return mysql_num_rows($res); } function get_course_chat_connected_table($database_name = '') { $database_name_with_glue = Database::fix_database_parameter($database_name); return Database::format_glued_course_table_name($database_name_with_glue, CHAT_CONNECTED_TABLE); } /** * Acts as the relative *_result() function of most DB drivers and fetches a * specific line and a field * @param resource The database resource to get data from * @param integer The row number * @param string Optional field name or number * @result mixed One cell of the result, or FALSE on error */ function result($resource,$row,$field='') { if (mysql_num_rows($resource) > 0) { if(!empty($field)) { return mysql_result($resource,$row,$field); } else { return mysql_result($resource,$row); } } } /** * Recovers the last ID of any insert query executed over this SQL connection * @return integer Unique ID of the latest inserted row */ function insert_id() { return mysql_insert_id(); } /** * Returns the number of affected rows * @param resource Optional database resource */ function affected_rows($r=null) { if(isset($r)) { return mysql_affected_rows($r); } else { return mysql_affected_rows(); } } function query($sql,$file='',$line=0) { return api_sql_query($sql,$file,$line); } function error() { return mysql_error(); } /** * Return course code from one given gradebook category's id * @param int Category ID * @return string Course code * @todo move this function in a gradebook-related library */ function get_course_by_category ($category_id) { $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); $sql = 'SELECT course_code FROM '.$tbl_grade_categories.' WHERE id='.$category_id; $res=api_sql_query($sql, __FILE__, __LINE__); $option=Database::fetch_array($res,'ASSOC'); if ($option) { return $option['course_code']; } else { return false; } } } //end class Database