<?php

function loadUserValues() {
	$_SESSION["final"] = Tournament::getFinalTournament();

	$_SESSION["user"] = $_SESSION["team"] = $_SESSION["tournament"] = null;
	unset($_SESSION["user"]);
	unset($_SESSION["role"]);
	unset($_SESSION["team"]);
	unset($_SESSION["tournament"]);

	if (isset($_SESSION["user_id"])) {
		$user = $_SESSION["user"] = User::fromId($_SESSION["user_id"]);
		$_SESSION["role"] = $user->getRole();

		if ($user->getTeamId() !== null) {
			$team = $_SESSION["team"] = Team::fromId($user->getTeamId());
			$_SESSION["tournament"] = Tournament::fromId($team->getTournamentId());
		}

		if (isset($_GET["be-admin"])) {
			quitTeam();
			$user->setRole(Role::ADMIN);
			exit();
		}

		if (isset($_GET["be-organizer"])) {
			quitTeam();
			$user->setRole(Role::ORGANIZER);
			exit();
		}

		if (isset($_GET["be-participant"])) {
			quitTeam();
			$user->setRole(Role::PARTICIPANT);
			exit();
		}

		if (isset($_GET["be-encadrant"])) {
			quitTeam();
			$user->setRole(Role::ENCADRANT);
			exit();
		}
	}
}

function echoDate($date = NULL, $with_time = false) {
	if ($date == NULL)
		$date = date("yyyy-mm-dd");

	return strftime("%d %B %G" . ($with_time ? " %H:%M" : ""), strtotime($date));
}

function quitTeam() {
	global $DB, $URL_BASE;

	header("Location: $URL_BASE");

	/** @var User $user */
	$user = $_SESSION["user"];
	$user_id = $user->getId();
	$role = $user->getRole();

	if ($role == Role::ADMIN || $role == Role::ORGANIZER)
		return;

	for ($i = 1; $i <= ($role == Role::ENCADRANT ? 6 : 2); ++$i)
		/** @noinspection SqlResolve */
		$DB->exec("UPDATE `teams` SET `" . strtolower(Role::getName($role)) . "_$i` = NULL WHERE `" . strtolower(Role::getName($role)) . "_$i` = $user_id;");
	$user->setTeamId(null);
	$DB->exec("UPDATE `teams` SET `encadrant_1` = `encadrant_2`, `encadrant_2` = NULL WHERE `encadrant_1` IS NULL;");
	for ($i = 1; $i <= 5; ++$i) {
		/** @noinspection SqlResolve */
		$DB->exec("UPDATE `teams` SET `participant_$i` = `participant_" . strval($i + 1) . "`, `participant_" . strval($i + 1) . "` = NULL WHERE `participant_$i` IS NULL;");
	}

	$req = $DB->query("SELECT `file_id` FROM `documents` WHERE `user` = $user_id;");
	while (($data = $req->fetch()) !== false)
		unlink("$URL_BASE/files/" . $data["file_id"]);
	$DB->exec("DELETE FROM `documents` WHERE `user` = $user_id;");

	if ($DB->exec("DELETE FROM `teams` WHERE `encadrant_1` IS NULL AND `participant_1` IS NULL;") > 0) {
		$team_id = $user->getTeamId();
		$req = $DB->query("SELECT `file_id` FROM `solutions` WHERE `team` = $team_id;");
		while (($data = $req->fetch()) !== false)
			unlink("$URL_BASE/files/" . $data["file_id"]);
		$DB->exec("DELETE FROM `solutions` WHERE `team` = $team_id;");

		$req = $DB->query("SELECT `file_id` FROM `syntheses` WHERE `team` = $team_id;");
		while (($data = $req->fetch()) !== false)
			unlink("$URL_BASE/files/" . $data["file_id"]);
		$DB->exec("DELETE FROM `syntheses` WHERE `team` = $team_id;");
	}

	$_SESSION["team"] = null;
	unset($_SESSION["team"]);
}