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

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

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

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

		$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"];
		}

		$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"];
		}

		$rank = 1;

		$query = $__database->prepare("SELECT `reply`, COUNT(`result_id`) AS `quantity` FROM `results` WHERE `board_id`=:id GROUP BY `reply` ORDER BY COUNT(`result_id`) DESC");
		$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;
				$result[$row["reply"]]["rank"] = $rank;
				$rank++;
			}
		}
		
		$return["results"] = $result;

		return $return;
	}
}
?>