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

class result extends slideResult_form {
	public function valid($sendData) {
		if(!$this->optionExist($sendData)) {
			return false;
		}

		return true;
	}

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

		try {
			$correct = false;
			
			$key = array_search($sendData, array_column($this->slide["options"], "option_id"));
			if(isset($this->slide["options"][$key]) && $this->slide["options"][$key]["correct"] == "1") {
				$correct = true;
			}
			
			$maxTime = $this->slide["config"]["time"] * 1000;
			$points = round(1000 - (1000 * ($time / $maxTime)));
			if($points < 0 || !$correct) {
				$points = 0;
			}

			$query = $__database->prepare("INSERT INTO `results` (`result_id`, `presentation_id`, `board_id`, `user_id`, `reply`, `time`, `points`) VALUES (UUID(), :presentation_id, :board_id, :user_id, :reply, :time, :points)");
			$query->bindParam(":presentation_id", $presentationId);
			$query->bindParam(":board_id", $slideId);
			$query->bindParam(":user_id", $userId);
			$query->bindParam(":reply", $sendData);
			$query->bindParam(":time", $time);
			$query->bindParam(":points", $points);
			$query->execute();
			return true;
		} catch(PDOException $error) {

		}

		return false;
	}
}
?>