⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.lib.php

📁 完美的在线教育系统
💻 PHP
📖 第 1 页 / 共 2 页
字号:
	*	Returns the isocode corresponding to the language directory given.	*/	function get_language_isocode($lang_folder)	{		$table = Database::get_main_table(TABLE_MAIN_LANGUAGE);		$sql_query = "SELECT isocode FROM $table WHERE dokeos_folder = '$lang_folder'";		$sql_result = api_sql_query($sql_query, __FILE__, __LINE__);		$result = mysql_fetch_array($sql_result);		$result = $result['isocode'];		return $result;	}	/*	-----------------------------------------------------------------------------		Query Functions		these execute a query and return the result(s).	-----------------------------------------------------------------------------	*/	/**	*	@return a list (array) of all courses.	* 	@todo shouldn't this be in the course.lib.php script?	*/	function get_course_list()	{		$table = Database::get_main_table(TABLE_MAIN_COURSE);		$sql_query = "SELECT * FROM $table";		$sql_result = api_sql_query($sql_query, __FILE__, __LINE__);		$result = api_store_result($sql_result);		return $result;	}	/**	*	Returns an array with all database fields for the specified course.	*	*	@param the real (system) code of the course (ID from inside the main course table)	* 	@todo shouldn't this be in the course.lib.php script?	*/	function get_course_info($course_code)	{		$table = Database::get_main_table(TABLE_MAIN_COURSE);		$sql_query = "SELECT * FROM $table WHERE `code` = '$course_code'";		$sql_result = api_sql_query($sql_query, __FILE__, __LINE__);		$result = mysql_fetch_array($sql_result);		$result = Database::generate_abstract_course_field_names($result);		return $result;	}	/**	*	@param $user_id (integer): the id of the user	*	@return $user_info (array): user_id, lastname, firstname, username, email, ...	*	@author Patrick Cool <patrick.cool@UGent.be>, 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)	{		$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	*	* 	@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 <yannick.warnier@dokeos.com>	 * @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 <yannick.warnier@dokeos.com>	 * @author  Patrick Cool <patrick.cool@UGent.be>, 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 <yannick.warnier@dokeos.com>	 */	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 <yannick.warnier@dokeos.com>	 */	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 <yannick.warnier@dokeos.com>	 */	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 <yannick.warnier@dokeos.com>	 **/	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(!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();    }}//end class Database?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -