'') : $result; } /** * @param $user_id (integer): the id of the user * @return $user_info (array): user_id, lastname, firstname, username, email, ... * @author Patrick Cool , 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? */ public static function get_user_info_from_id($user_id = '') { if (empty($user_id)) { return $GLOBALS['_user']; } $table = self::get_main_table(TABLE_MAIN_USER); $user_id = self::escape_string($user_id); return self::generate_abstract_user_field_names( self::fetch_array(self::query("SELECT * FROM $table WHERE user_id = '$user_id'", __FILE__, __LINE__))); } /** * 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) */ public static 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 */ public static 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. */ public static function glue_course_database_name($database_name) { return self::get_course_table_prefix().$database_name.self::get_database_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. */ public static function fix_database_parameter($database_name) { if (empty($database_name)) { $course_info = api_get_course_info(); return $course_info['dbNameGlu']; } return self::glue_course_database_name($database_name); } /** * Structures a course database and table name to ready them * for querying. The course database parameter is considered glued: * e.g. COURSE001`.` */ public static 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 */ public static 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. */ public static function count_rows($table) { $obj = self::fetch_object(self::query("SELECT COUNT(*) AS n FROM $table", __FILE__, __LINE__)); return $obj->n; } /** * 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 */ public static function escape_string($string) { return get_magic_quotes_gpc() ? mysql_real_escape_string(stripslashes($string)) : mysql_real_escape_string($string); } /** * Gets the array from a SQL result (as returned by Database::query) - help achieving database independence * @param resource The result from a call to sql_query (e.g. Database::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 */ public static function fetch_array($res, $option = 'BOTH') { return $option == 'ASSOC' ? mysql_fetch_array($res, MYSQL_ASSOC) : ($option == 'NUM' ? mysql_fetch_array($res, MYSQL_NUM) : mysql_fetch_array($res)); } /** * Gets the next row of the result of the SQL query (as returned by Database::query) in an object form * @param resource The result from a call to sql_query (e.g. Database::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 */ public static function fetch_object($res, $class = null, $params = null) { return !empty($class) ? (is_array($params) ? mysql_fetch_object($res, $class, $params) : mysql_fetch_object($res,$class)) : mysql_fetch_object($res); } /** * Gets the array from a SQL result (as returned by Database::query) - help achieving database independence * @param resource The result from a call to sql_query (e.g. Database::query) * @return array Array of results as returned by php (mysql_fetch_row) * @author Yannick Warnier */ public static 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 **/ public static function num_rows($res) { return mysql_num_rows($res); } public static function get_course_chat_connected_table($database_name = '') { return self::format_glued_course_table_name(self::fix_database_parameter($database_name), 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 */ public static function result($resource, $row, $field = '') { return mysql_num_rows($resource) > 0 ? (!empty($field) ? mysql_result($resource, $row, $field) : mysql_result($resource, $row)) : null; } /** * Gets the ID of the last item inserted into the database * * @return integer The last ID as returned by the DB function * @comment This should be updated to use ADODB at some point */ public static function insert_id() { return mysql_insert_id(); } /** * Returns the number of affected rows * @param resource Optional database resource */ public static function affected_rows($r = null) { return !is_null($r) ? mysql_affected_rows($r) : mysql_affected_rows(); } /** * This function returns a resource * Documentation has been added by Arthur Portugal * An adaptation for reliable showing error messages has been done by Ivan Tcholakov, 2009 * @author Olivier Brouckaert * @param string $query - SQL query * @param string $file - optional, the file path and name of the error (__FILE__) * @param string $line - optional, the line of the error (__LINE__) * @return resource - the return value of the query */ public static function query($query, $file = '', $line = 0) { if (!($result = @mysql_query($query)) && $line && api_get_setting('server_type') != 'production') { $security_ok = class_exists('Security') && class_exists('HTMLPurifier'); $error = self::error(); $info = '
';
			$info .= 'DATABASE ERROR :
'; $info .= $security_ok ? Security::remove_XSS($error) : api_htmlentities($error, ENT_QUOTES); $info .= '
'; $info .= 'QUERY :
'; $info .= $security_ok ? Security::remove_XSS($query) : api_htmlentities($query, ENT_QUOTES); $info .= '
'; $info .= 'FILE :
'; $info .= ($file == '' ? ' unknown ' : $file); $info .= '
'; $info .= 'LINE :
'; $info .= ($line == 0 ? ' unknown ' : $line); $info .= '
'; echo $info; } return $result; } public static 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 */ public static function get_course_by_category($category_id) { $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'); return $info ? $info['course_code'] : false; } /** * Stores a query result into an array. * * @author Olivier Brouckaert * @param resource $result - the return value of the query * @return array - the value returned by the query */ public static function store_result($result) { $array = array(); if ($result !== false) { // For isolation from database engine's behaviour. while ($row = self::fetch_array($result)) { $array[] = $row; } } return $array; } /* ============================================================================== DEPRECATED METHODS ============================================================================== */ /** * @deprecated Use api_get_language_isocode($language) instead. */ public static function get_language_isocode($language) { return api_get_language_isocode($language); } /** * @deprecated Use Database::insert_id() instead. */ public static function get_last_insert_id() { return mysql_insert_id(); } } //end class Database