<?php
/**
 * class show_controller
 *
 * Description for class show_controller
 *
 * @author:
*/
namespace area\presentation\controller;

use core\presentation;
use core\response;
use core\url;
use area\presentation\form\result_form;
use area\presentation\form\reaction_form;

class show_controller extends _controller {
	private $p = null;
	private $urlSave = "";

	public function __construct() {
		global $__routing;

		parent::__construct();
		
		$this->p = new presentation(isset($__routing[1]["id"]) ? $__routing[1]["id"] : "");
		if(!$this->p->exists()) {
			$this->viewError("404");
		}

		$this->urlSave = url::root()."p/{$this->p->presentationId}/zapisz/{step}";
	}

	public function index_action() {
		$this->setVariable("quizId", $this->p->presentationId);
		$this->setVariable("quizTempo", $this->p->presentationTempo);
		$this->setVariable("userId", $this->userId);
		return $this->view();
	}

	public function slide_action($vars) {
		$slideId = isset($vars["sid"]) ? $vars["sid"] : "";

		$show = $this->p->showSlide($slideId, $this->userId);
		if($show === false) {
			$this->viewError("404");
		}
		
		$this->setVariable("urlSave", str_replace("{step}", $slideId, $this->urlSave));
		$this->setVariable("countSlideQuiz", $this->p->countSlideQuiz());
		$this->setVariable("slideInfo", $show["slide"]);
		return $this->view($show["html"], null, "layoutEmpty");
	}

	public function reaction_action($vars) {
		global $__database;

		$slideId = isset($vars["sid"]) ? $vars["sid"] : "";
		
		$form = new reaction_form($this->p, $slideId, $this->userId);
		if($form->valid($this->userId)) {
			if($form->save()) {
				response::json(array("status" => true));
			}
		}

		response::json(array("status" => false));
	}

	public function save_action($vars) {
		global $__database;
		
		$step = isset($vars["step"]) ? $vars["step"] : "";
		if($step == "") {
			$this->viewError("404");
		}
		
		if(strlen($step) == 1 && $step == 0) {
			if(!isset($_POST["__action"])) {
				$this->viewError("404");
			}

			$name = isset($_POST["name"]) ? trim(stripslashes($_POST["name"])) : "";

			try {
				$query = $__database->prepare("UPDATE `results_users` SET `name`=:name WHERE `user_id`=:user_id");
				$query->bindParam(":name", $name);
				$query->bindParam(":user_id", $this->userId);
				if($query->execute()) {
					$slide = $this->p->slideGetStep(0);
					if($slide === false) {
						response::json(array("status" => false));
					}
					
					$_SESSION["authPresentation"][$this->p->presentationId]["userName"] = $name;
					response::json(array("status" => true, "slide" => $slide["board_id"]));
				}
			} catch(PDOException $error) {
				throw new databaseException($error->getMessage(), $error->getCode());
			}

			response::json(array("status" => false));
		}

		$form = new result_form($this->p, $step);
		if($form->valid($this->userId)) {
			response::json(array("status" => true));
		}

		response::json(array("status" => false));
	}

	public function quizResults_action($vars) {
		$slideId = isset($vars["sid"]) ? $vars["sid"] : "";
		if($slideId == "") {
			$this->viewError("404");
		}

		$this->p->slideId = $slideId;
		$this->p->loadSlideData();

		$slide = $this->p->slideGet();
		
		if($slide["type"] != "question.008_Quiz") {
			$this->viewError("404");
		}

		$slideQuizStatistics = $this->p->slideQuiz($this->userId);
		if($slideQuizStatistics === false) {
			$this->viewError("404");
		}

		$listRank = $slide["results"]["list"];

		$indexRank = array_search($this->userId, array_column($listRank, "user_id"));
		if($indexRank !== false) {
			$this->setVariable("rankExists", true);
			$this->setVariable("rankPerson", $listRank[$indexRank]);
		} else {
			$this->setVariable("rankExists", false);
		}
		
		$this->setVariable("statistics", $slideQuizStatistics);
		return $this->view(null, null, "layoutEmpty");
	}
}
?>