, 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. */ 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 */ function generate_abstract_course_field_names($result_array) { $result_array["official_code"] = $result_array["visual_code"]; $result_array["visual_code"] = $result_array["visual_code"]; $result_array["real_code"] = $result_array["code"]; $result_array["system_code"] = $result_array["code"]; $result_array["title"] = $result_array['title']; $result_array["database"] = $result_array["db_name"]; $result_array["faculty"] = $result_array["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 */ 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(); } /** * 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 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); } /** * Returns the name of the tool table of a course. * If no database parameter is present, the function works on the current course. */ function get_course_tool_list_table($database_name = '') { $database_name_with_glue = Database::fix_database_parameter($database_name); return Database::format_glued_course_table_name($database_name_with_glue, TOOL_LIST_TABLE); } } //end class Database ?>