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

class result_001_MultipleChoice 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,
				"empty" => false
			);
		}
		
		$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"];
		}

		if($quantity > 0) {
			$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"] = round(($row["quantity"] / $quantity) * 100);
					$result[$row["reply"]]["empty"] = false;
				}
			}
		} else {
			if(isset($this->config["show_type"]) && ($this->config["show_type"] == "2" || $this->config["show_type"] == "3")) {
				foreach($this->options as $option) {
					$result[$option["id"]]["value"] = "999999999999999999999999";
					$result[$option["id"]]["empty"] = true;
				}
			}
		}
		
		$return["results"] = $result;

		return $return;
	}
}
?>