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

class result_006_2By2Matrix 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("x" => 0, "y" => 0, "x_percent" => 0, "y_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"];
		}

		$query = $__database->prepare("SELECT `option_id`, SUM(`reply`) AS `reply_x`, SUM(`reply2`) AS `reply_y`, COUNT(`result_id`) AS `quantity` FROM `results` WHERE `board_id`=:id GROUP BY `option_id`");
		$query->bindParam(":id", $this->id);
		$query->execute();
		while($row = $query->fetch()) {
			if(isset($result[$row["option_id"]])) {
				$result[$row["option_id"]]["x"] = $row["reply_x"] / $row["quantity"];
				$result[$row["option_id"]]["y"] = $row["reply_y"] / $row["quantity"];
				$result[$row["option_id"]]["x_percent"] = ($result[$row["option_id"]]["x"] / $this->config["max"]) * 100;
				$result[$row["option_id"]]["y_percent"] = ($result[$row["option_id"]]["y"] / $this->config["max"]) * 100;
			}
		}
		
		$return["results"] = $result;

		return $return;
	}
}
?>