<?php
/**
 * class result
 *
 * Description for class result
 *
 * @author:
*/
use form\slideResult_form;

class result extends slideResult_form {
	public function valid($sendData) {
		$valid = true;
		
		$valueMin = $this->config["min"];
		$valueMax = $this->config["max"];

		foreach($this->optionsKeys as $optionId) {
			if(!isset($sendData[$optionId]) || !isset($sendData[$optionId]["x"]) || !isset($sendData[$optionId]["y"])) {
				$valid = false;
				continue;
			}

			$yValue = $sendData[$optionId]["y"];
			$xValue = $sendData[$optionId]["x"];

			if($xValue == "-1" || $yValue == "-1") {
				continue;
			}

			if($valueMin > $yValue || $valueMax < $yValue || $valueMin > $xValue || $valueMax < $xValue) {
				$valid = false;
			}
		}
		
		return $valid;
	}

	public function save($presentationId, $slideId, $userId, $sendData, $time) {
		global $__database;

		try {
			foreach($this->optionsKeys as $optionId) {
				$xValue = $sendData[$optionId]["x"];
				$yValue = $sendData[$optionId]["y"];

				if($xValue == "-1" || $yValue == "-1") {
					continue;
				}

				$query = $__database->prepare("INSERT INTO `results` (`result_id`, `presentation_id`, `board_id`, `option_id`, `user_id`, `reply`, `reply2`, `time`) VALUES (UUID(), :presentation_id, :board_id, :option_id, :user_id, :reply, :reply2, :time)");
				$query->bindParam(":presentation_id", $presentationId);
				$query->bindParam(":board_id", $slideId);
				$query->bindParam(":option_id", $optionId);
				$query->bindParam(":user_id", $userId);
				$query->bindParam(":reply", $xValue);
				$query->bindParam(":reply2", $yValue);
				$query->bindParam(":time", $time);
				$query->execute();
			}

			return true;
		} catch(PDOException $error) {

		}

		return false;
	}
}
?>