<?php
/**
 * class result_008_Quiz
 *
 * Description for class result_008_Quiz
 *
 * @author:
*/
use core\slideResult;

class result_008_Quiz extends slideResult {
	public function result() {
		global $__database;

		$return = array(
			"users" => 0,
			"results" => array()
		);
		$result = array(
			"__slide__" => array(
				"list" => array()
			)
		);
		
		if(count($this->options) == 0) {
			$return["results"] = $result;
			return $return;
		}

		$query = $__database->prepare("SELECT COUNT(DISTINCT `user_id`) AS `users` FROM `results` WHERE `board_id`=:id");
		$query->bindParam(":id", $this->id);
		$query->execute();
		if($row = $query->fetch()) {
			$return["users"] = $row["users"];
		}

		foreach($this->options as $option) {
			$result[$option["id"]] = array(
				"value" => 0,
				"percent" => 0
			);
		}

		$quantity = 0;

		$query = $__database->prepare("SELECT COUNT(`result_id`) AS `quantity` FROM `results` WHERE `board_id`=:id");
		$query->bindParam(":id", $this->id);
		$query->execute();
		if($row = $query->fetch()) {
			$quantity = $row["quantity"];
		}

		$query = $__database->prepare("SELECT `reply`, COUNT(`result_id`) AS `quantity` FROM `results` WHERE `board_id`=:id GROUP BY `reply`");
		$query->bindParam(":id", $this->id);
		$query->execute();
		while($row = $query->fetch()) {
			if(isset($result[$row["reply"]])) {
				$result[$row["reply"]]["value"] = $row["quantity"];
				$result[$row["reply"]]["percent"] = ($row["quantity"] / $quantity) * 100;
			}
		}

		$list = array();
		$maxPoints = -1;

		$boards = $this->listBoads();
		
		$boardsQuiz = array();
		if(count($boards) > 0) {
			foreach($boards as $board) {
				if($board["type"] == $this->type) {
					$boardsQuiz[] = $board["board_id"];

					if($board["board_id"] == $this->id) {
						break;
					}
				}
			}
		}

		$rankIdx = 1;
		$currentRank = 1;
		$currentPoints = -1;

		$query = $__database->prepare("SELECT r.user_id, SUM(r.points) AS `points`, r.time, u.name FROM `results` AS `r` INNER JOIN `results_users` AS `u` ON r.user_id=u.user_id WHERE r.board_id IN ('".(implode("', '", $boardsQuiz))."') GROUP BY r.user_id ORDER BY SUM(r.points) DESC");
		$query->execute();
		while($row = $query->fetch()) {
			if($maxPoints == -1) {
				$maxPoints = $row["points"];
			}

			if($currentPoints != $row["points"]) {
				$currentRank = $rankIdx;
				$currentPoints = $row["points"];
			}

			$list[] = array(
				"user_id" => $row["user_id"],
				"name" => $row["name"],
				"points" => $row["points"],
				"percent" => $row["points"] > 0 ? (($row["points"] / $maxPoints) * 100) : 0,
				"rank" => $currentRank,
				"time" => $row["time"]
			);

			$rankIdx++;
		}
		
		$result["__slide__"]["list"] = $list;
		$return["results"] = $result;

		return $return;
	}

	private function listBoads() {
		global $__database;

		$list = array();

		$query = $__database->prepare("SELECT `board_id`, `type` FROM `boards` WHERE `presentation_id`=:presentation_id ORDER BY `order` ASC");
		$query->bindParam(":presentation_id", $this->presentationId);
		$query->execute();
		while($row = $query->fetch()) {
			$list[] = $row;
		}

		return $list;
	}
}
?>