<?php

class bilan
{
	var $listPatID;
	var $listConsuInitID;
	var $listConsuFinalID;

	var $listPropID;

	var $listLink;

	function bilan($listPat, $listConsuInit, $listConsuFinal)
	{
		$this->listPatID = array();
		$this->listConsuInitID = array();
		$this->listConsuFinalID = array();

		$this->listPropID = array();

		$this->listLink = array();

		$this->listPatID = $listPat;
		$this->listConsuInitID = $listConsuInit;
		$this->listConsuFinalID = $listConsuFinal;


		$c = count($this->listConsuInitID);
		for ($i = 0; $i < $c; $i++)
			$this->listLink[$this->listConsuInitID[$i]] = $this->listConsuFinalID[$i];

// 		printVar($this->listLink);
	}

	function getSexe()
	{
		$q = "SELECT COUNT(*) FROM consultation";
		$total = queryOneField($q);

		// M
		$q = "SELECT COUNT(*) FROM patient WHERE pat_sexe = 1";
		$totalM = queryOneField($q);

		// F
		$q = "SELECT COUNT(*) FROM patient WHERE pat_sexe = 2";
		$totalF = queryOneField($q);

		echo "<tr>";
		echo "<td>Sexe</td>";

		echo "<td>Masculin: " . $totalM . "</td>";
		echo "<td>Féminin: " . $totalF . "</td>";
		echo "</tr>";
	}

	function getAge()
	{
		$q = "SELECT AVG(" . $this->year . " - YEAR(pat_datenaiss)) FROM patient";
		$ageMoyen = queryOneField($q);

		echo "<tr>";
		echo "<td>Moyenne d'age</td>";
		echo "<td colspan=\"2\">" . round($ageMoyen) . "</td>";
		echo "</tr>";
	}

	function getFicheContact()
	{
		$q = "SELECT COUNT(*) FROM consultation WHERE consu_type = 1";
		$totalFicheContact = queryOneField($q);

		echo "<tr>";
		echo "<td>Nombre de fiches de contact</td>";
		echo "<td colspan=\"2\">" . $totalFicheContact . "</td>";
		echo "</tr>";
	}

	function getMulti($title, $array, $table, $field, $opt = "")
	{
		if (!empty($opt))
			$opt = " && " . $opt;

		$q = "SELECT id_consult, " . $field . ", COUNT(" . $field . ") as total FROM " . $table . " WHERE id_consult IN (" . implode(", ", $this->listConsuInitID) . ")" . $opt ." GROUP BY " . $field . " ASC";
		$datas = queryList($q);

		echo "<tr>";
		echo "<td colspan=\"3\"><span class=\"bold\">" . $title . "</span></td>";
		echo "</tr>";

		foreach ($datas as $data)
		{
			$label = isset($array[$data[$field]]) ? $array[$data[$field]] : "-";

			echo "<tr>";
			echo "<td><ul><li>" . $label . "</li></ul></td>";
			echo "<td>" . $data["total"] . "</td>";
// 			$q = "SELECT id_consult, " . $field . ", COUNT(" . $field . ") as total FROM " . $table . " WHERE id_consult = '" . $this->listLink[$data["id_consult"]] . "'" . $opt ." GROUP BY " . $field . " ASC";
			$q = "SELECT COUNT(" . $field . ") as total FROM " . $table . " WHERE id_consult IN (" . implode(", ", $this->listConsuFinalID) . ") && " . $field . " = '" . $data[$field] . "'" . $opt;
 			$dataFinal = queryOne($q);
 			echo "<td>" . $dataFinal["total"] . "</td>";
			echo "</tr>";
		}
	}

	function getMultiTableDebut($title, $array, $field, $opt = "", $table = "debut")
	{
		if (!empty($opt))
			$opt = " && " . $opt . " ";

		$q = "SELECT " . $field . ", COUNT(" . $field . ") as total FROM " . $table . " WHERE id_pat IN (" . implode(", ", $this->listPatID) . ")" . $opt . " GROUP BY " . $field . " ASC";
		$datas = queryList($q);

		echo "<tr>";
		echo "<td colspan=\"3\"><span class=\"bold\">" . $title . "</span></td>";
		echo "</tr>";

		foreach ($datas as $data)
		{
			$label = isset($array[$data[$field]]) ? $array[$data[$field]] : "-";
			echo "<tr>";
			echo "<td><ul><li>" . $label . "</li></ul></td>";
			echo "<td colspan=\"2\">" . $data["total"] . "</td>";
			echo "</tr>";
		}
	}

	function getMoyenne($title, $table, $field, $round = 0, $opt = "")
	{
		if (!empty($opt))
			$opt = " WHERE 1 && " . $opt . " ";
		else
			$opt = " WHERE 1";

		$q = "SELECT AVG(" . $field . ") as total FROM " . $table . $opt . " && id_consult IN (" . implode(", ", $this->listConsuInitID) . ")";
		$total = queryOneField($q);

		echo "<tr>";
		echo "<td>" . $title . "</td>";
		echo "<td>" . round($total, $round) . "</td>";
		$q = "SELECT AVG(" . $field . ") as total FROM " . $table . $opt . " && id_consult IN (" . implode(", ", $this->listConsuFinalID) . ")";
		$total = queryOneField($q);
		echo "<td>" . round($total, $round) . "</td>";
		echo "</tr>";
	}

	function getSingle($title, $table, $field, $opt = "")
	{
		if (!empty($opt))
			$opt = "&& " . $opt . " ";

		if ($field == "*")
			$groupBY = "";
		else
			$groupBY = "GROUP BY " . $field . " ASC";

		$q = "SELECT COUNT(" . $field . ") as total FROM " . $table . " WHERE id_consult IN (" . implode(", ", $this->listConsuInitID) . ") " . $opt . $groupBY;
		$total = queryOneField($q);

		echo "<tr>";
		echo "<td>" . $title . "</td>";
		echo "<td>" . ($total ? $total : 0). "</td>";

		$q = "SELECT COUNT(" . $field . ") as total FROM " . $table . " WHERE id_consult IN (" . implode(", ", $this->listConsuFinalID) . ") " . $opt . $groupBY;
		$total = queryOneField($q);

		echo "<td>" . ($total ? $total : 0). "</td>";

		echo "</tr>";
	}

	function titre($title)
	{
		echo "<tr>";
		echo "<td colspan=\"3\"><span class=\"bold\">" . $title . "</span></td>";
		echo "</tr>";
	}

}

?>