<?php
// $Id: EmailAvailable.php 10190 2007-07-06 00:23:20Z pcool $
/*
==============================================================================
	Dokeos - elearning and course management software
	
	Copyright (c) 2007 iilearn.org
	
	For a full list of contributors, see "credits.txt".
	The full license can be read in "license.txt".
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	See the GNU General Public License for more details.
	
	Mail: poopsoft@163.com
==============================================================================
*/
require_once ('HTML/QuickForm/Rule.php');
/**
 * QuickForm rule to check if a username is available
 */
class HTML_QuickForm_Rule_EmailAvailable extends HTML_QuickForm_Rule
{
	/**
	 * Function to check if a username is available
	 * @see HTML_QuickForm_Rule
	 * @param string $email Wanted email
	 * @param string $current_email 
	 * @return boolean True if email is available
	 */
	function validate($email,$current_email = null)
	{
		$user_table = Database::get_main_table(TABLE_MAIN_USER);
		$sql = "SELECT * FROM $user_table WHERE email = '$email'";
		if(!is_null($current_username))
		{
			$sql .= " AND email != '$current_email'";
		}
		$res = api_sql_query($sql,__FILE__,__LINE__);
		$number = mysql_num_rows($res);
		return $number == 0;
	}
}
?>