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

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

		$query = $__database->prepare("SELECT `option_id`, SUM(`reply`) AS `reply_sum`, 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"]]["value"] = $row["reply_sum"];
				$result[$row["option_id"]]["percent"] = round($row["reply_sum"] / $row["quantity"]);
			}
		}
		
		$return["results"] = $result;

		return $return;
	}
}
?>