public static function createGuess($firstName, $lastName, $mail, $weight) { // Make sure the name is valid if (!AccountUtils::isValidName($firstName) || !AccountUtils::isValidName($firstName)) { throw new Exception('The name is invalid.'); } // Make sure the mail is valid if (!AccountUtils::isValidMail($mail)) { throw new Exception('The mail is invalid.'); } // TODO: Validate the weight! // Get the session ID $sessionId = getSessionKey(); // Determine the creation date time $dateTime = DateTime::now(); // Get the guess IP $ip = IpUtils::getClientIp(); // Prepare a query for the picture being added $statement = Database::getPDO()->prepare('INSERT INTO ' . static::getDatabaseTableName() . ' (guess_session_id, guess_first_name, guess_last_name, guess_mail, guess_weight, guess_datetime, guess_ip) ' . 'VALUES (:session_id, :first_name, :last_name, :mail, :weight, :guess_datetime, :ip)'); $statement->bindValue(':session_id', $sessionId, PDO::PARAM_STR); $statement->bindValue(':first_name', $firstName, PDO::PARAM_STR); $statement->bindValue(':last_name', $lastName, PDO::PARAM_STR); $statement->bindValue(':mail', $mail, PDO::PARAM_STR); $statement->bindValue(':weight', $weight, PDO::PARAM_STR); // TODO: Use the UTC/GMT timezone! $statement->bindValue(':guess_datetime', $dateTime->toString(), PDO::PARAM_STR); $statement->bindValue(':ip', $ip, PDO::PARAM_STR); // Execute the prepared query if (!$statement->execute()) { throw new Exception('Failed to query the database.'); } // Get and return the guess instance return new Guess(Database::getPDO()->lastInsertId()); }
/** * Set a registry value. If the value doesn't exist yet, it will be created. * * @param string $key The key. * @param string $value The value. * * @return RegistryValue The registry as object. * * @throws Exception Throws if an error occurred. */ public static function setValue($key, $value) { // Trim the key // TODO: Parse the registry key $key = trim($key); // Create this if it hasn't been set already if (!static::isValueWithKey($key)) { // Prepare a query for the being created $statement = Database::getPDO()->prepare('INSERT INTO ' . static::getDatabaseTableName() . ' (registry_key, registry_value, registry_modified_datetime) VALUES (:registry_key, :registry_value, :modified_datetime)'); $statement->bindValue(':registry_key', $key, PDO::PARAM_STR); $statement->bindValue(':registry_value', $value, PDO::PARAM_STR); // TODO: Use the UTC/GMT timezone! $statement->bindValue(':modified_datetime', DateTime::now()->toString(), PDO::PARAM_STR); // Execute the prepared query if (!$statement->execute()) { throw new Exception('Failed to query the database.'); } // Return the created registry as object return new RegistryValue(Database::getPDO()->lastInsertId()); } // Get the object $registryValue = static::getValue($key); // Set the registry value $registryValue->setValue($value); // Return the object return $registryValue; }