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

class result_002_ImageChoice 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
				);
		}

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

		$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;
			}
		}
		
		$return["results"] = $result;

		return $return;
	}
}
?>