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

class result extends slideResult_form {
	public function valid($sendData) {
		$valid = true;
		$points = 0;
		
		foreach($this->optionsKeys as $optionId) {
			if(!isset($sendData[$optionId]) || (empty($sendData[$optionId]) && $sendData[$optionId] != "0")) {
				$valid = false;
				continue;
			}

			$points = $points + (int)$sendData[$optionId];
		}

		if($points > 100) {
			$valid = false;
		}
		
		return $valid;
	}

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

		foreach($this->optionsKeys as $optionId) {
			$value = (int)$sendData[$optionId];

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

		return true;
	}
}
?>