<?php

class suivi
{
	// Pour l'utilisateur
	function select_patient($id_user, $selected)
	{
		$data = "<select name=\"id_pat\" id=\"id_pat\" size=\"1\">\n";
		$data .= "<option value=\"\">-- Choisir --</option>\n";

		$q = "SELECT p.* FROM patient AS p LEFT JOIN suivi AS s ON (s.id_pat = p.id_pat && s.id_user = '" . $id_user . "') WHERE s.id_pat IS NULL ORDER BY p.pat_nom ASC, p.pat_prenom ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
				{
					$pat_nom = substr($n["pat_nom"], 0, 3);
					$pat_prenom = substr($n["pat_prenom"], 0, 1);

					$data .= "<option value=\"" . $n["id_pat"] . "\"" . ($n["id_pat"] == $selected ? " selected" : "") . ">" . $pat_nom . " " . $pat_prenom . " - " . format_date($n["pat_datenaiss"]) . "</option>\n";
				}
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		$data .= "</select>";

		return $data;
	}

	function select_suivi_util($id_user, $selected)
	{
		$data = "<select name=\"id_pat\" id=\"id_pat\" size=\"1\">\n";
		$data .= "<option value=\"\">-- Choisir --</option>\n";

		$q = "SELECT * FROM suivi AS s LEFT JOIN patient AS p ON (p.id_pat = s.id_pat) WHERE s.id_user = '" . $id_user . "' ORDER BY p.pat_nom ASC, p.pat_prenom ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
					$data .= "<option value=\"" . $n["id_pat"] . "\"" . ($n["id_pat"] == $selected ? " selected" : "") . ">" . $n["pat_nom"] . " " . $n["pat_prenom"] .  "</option>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		$data .= "</select>";

		return $data;
	}

	function list_suivi_util($id_user)
	{
		$data = array();

		$q = "SELECT * FROM suivi AS s LEFT JOIN patient AS p ON (p.id_pat = s.id_pat) WHERE s.id_user = '" . $id_user . "' ORDER BY p.pat_nom ASC, p.pat_prenom ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
					$data[] = $n;
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return $data;
	}

	function list_suivi_admin()
	{
		$data = array();

		$q = "SELECT * FROM suivi AS s LEFT JOIN patient AS p ON (p.id_pat = s.id_pat) GROUP BY p.id_pat ORDER BY p.pat_nom ASC, p.pat_prenom ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
					$data[] = $n;
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return $data;
	}

	// Pour le patient
	function select_utilisateur($id_pat, $selected)
	{
		$data = "<select name=\"id_user\" id=\"id_user\" size=\"1\">\n";
		$data .= "<option value=\"\">-- Choisir --</option>\n";

		$q = "SELECT u.* FROM utilisateur AS u LEFT JOIN suivi AS s ON (s.id_user = u.id_user && s.id_pat = '" . $id_pat . "') WHERE s.id_user IS NULL ORDER BY u.user_nom ASC, u.user_prenom ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
					$data .= "<option value=\"" . $n["id_user"] . "\"" . ($n["id_user"] == $selected ? " selected" : "") . ">" . $n["user_nom"] . " " . $n["user_prenom"] . "</option>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		$data .= "</select>";

		return $data;
	}

	function list_suivi_pat($id_pat)
	{
		$data = array();

		$q = "SELECT * FROM suivi AS s LEFT JOIN utilisateur AS u ON (u.id_user = s.id_user) WHERE s.id_pat = '" . $id_pat . "' ORDER BY u.user_nom ASC, u.user_prenom ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
					$data[] = $n;
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return $data;
	}

	// Fonctions communes
	function edit_suivi($id_pat, $id_user)
	{
		$q = "INSERT INTO suivi (id_pat, id_user) VALUES ('" . $id_pat . "', '" . $id_user . "')";
		$r = mysql_query($q);

		if ($r)
			return true;
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return false;
	}

	function del_suivi($id_pat, $id_user)
	{
		$q = "DELETE FROM suivi WHERE id_pat = '" . $id_pat . "' && id_user = '" . $id_user . "'";
		$r = mysql_query($q);

		if ($r)
		{
			$q = "OPTIMIZE TABLE suivi";
			$r = mysql_query($q);

			return true;
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return false;
	}

	function check_suivi($id_pat, $id_user)
	{
		$q = "SELECT * FROM suivi WHERE id_pat = '" . $id_pat . "' && id_user = '" . $id_user . "'";
		$r = mysql_query($q);

		if ($r)
		{
			if (mysql_num_rows($r))
				return true;
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return false;
	}
}

?>