<?php
	require("settings.php");
	require_once("helper.php");

	session_start();
	html_start("Tasks");

	if(!session_is_registered("id") || $_SESSION["id"] == "") {
		echo "Not logged in";
		login_form("index.php");
		die();
	}

	echo "<H1>Task Managment</H1>\n";

	$link = db_connect();
	$todo = $_REQUEST["todo"];

	if($todo == "add") {
		echo "<H3>Add task</H3>\n";
		echo "<FORM action=\"tasks.php\" method=\"post\">\n";
		echo "<INPUT type=\"hidden\" name=\"todo\" value=\"reallyadd\">\n";
		echo "<LABLE for=\"title\">Title:</LABEL><BR>\n";
		echo "<INPUT type=\"text\" id=\"title\" name=\"title\" size=\"80\" maxlength=\"256\"><BR>\n";
		echo "<LABLE for=\"text\">Text:</LABEL><BR>\n";
		echo "<TEXTAREA id=\"text\" name=\"text\" cols=\"80\" rows=\"10\"></TEXTAREA><BR>\n";
		echo "<INPUT type=\"submit\" value=\"Save\">\n";
		echo "</FORM>\n";
	} else if($todo == "reallyadd") {
		$title = mysql_real_escape_string(htmlspecialchars($_REQUEST["title"]), $link);
		$text = mysql_real_escape_string(htmlspecialchars($_REQUEST["text"]), $link);

		$query = "INSERT INTO tasks SET title=\"$title\", text=\"$text\"";
		$result = mysql_query($query, $link) or die("Can't insert task: ".mysql_error($link));
		$id = mysql_insert_id($link);
		if($id != 0) {
			echo "Tasks saved successfully";
		} else {
			echo "Saving failed";
		}

		echo "<BR><BR><A href=\"tasks.php\">View Tasks</A>\n";
	} else if($todo == "rm") {
		$tid = intval($_REQUEST["tid"]);
		$query = "SELECT title FROM tasks WHERE tid = $tid";
		$result = mysql_query($query, $link) or die("Can't fetch task: ".mysql_error($link));
		$num = mysql_num_rows($result);
		if($num < 1) {
			echo "Can't find task: ".mysql_error($link);
			echo "<BR><BR><A href=\"tasks.php\">View Tasks</A>\n";
		} else if($num > 1) {
			echo "Taskid not unique: ".mysql_error($link);
			echo "<BR><BR><A href=\"tasks.php\">View Tasks</A>\n";
		} else {
			$line = mysql_fetch_array($result);
			echo "Do you really want to delete the task {$line["title"]}?<BR>\n";
			echo "<FORM action=\"tasks.php\" method=\"post\">\n";
			echo "<INPUT type=\"hidden\" name=\"tid\" value=\"$tid\">\n";
			echo "<INPUT type=\"hidden\" name=\"todo\" value=\"reallyrm\">\n";
			echo "<INPUT type=\"submit\" value=\"Yes\">\n";
			echo "</FROM>\n";
			echo "<A href=\"tasks.php\"><BUTTON type=\"button\">No</BUTTON></A>\n";
		}
	} else if($todo == "reallyrm") {
		$tid = intval($_REQUEST["tid"]);
		$query = "DELETE FROM tasks WHERE tid = $tid";
		$result = mysql_query($query, $link) or die("Can't delete task: ".mysql_error($link));
		$num = mysql_affected_rows($link);
		if($num != 1) {
			echo "Deleting failed<BR>\n";
		} else {
			echo "Finished deleting<BR>\n";
		}
		echo "<BR><BR><A href=\"tasks.php\">View Tasks</A>\n";
	} else {
		$query = "SELECT tid, title FROM tasks ORDER BY tid";
		$result = mysql_query($query, $link) or die("Can't fetch tasks: ".mysql_error($link));
		echo "<UL class=\"task\">\n";
		while($task = mysql_fetch_array($result)) {
			echo "<LI><A href=\"assignment.php?tid={$task["tid"]}\">{$task["title"]}</A> ";
			if(check_capa($_SESSION["id"], "taskedit", $link)) {
				echo "<A href=\"tasks.php?todo=rm&tid={$task["tid"]}\"><IMG src=\"icons/report_delete.png\" alt=\"delete\"></A>";
			}
			echo "</LI>\n";
		}
		echo "</UL>\n";
		if(check_capa($_SESSION["id"], "taskedit", $link)) {
			echo "<A href=\"tasks.php?todo=add\"><IMG src=\"icons/report_add.png\" alt=\"add\">Add task</A>";
		}
	}
	mysql_close($link);

	echo "<DIV class=\"footer\">\n";
	echo "<A href=\"index.php\"><IMG src=\"icons/house.png\" alt=\"home\">Home</A>\n";
	echo "<A href=\"logout.php\"><IMG src=\"icons/door_in.png\" alt=\"logout\">Logout</A>\n";
	echo "<DIV>\n";
?>
 </BODY>
</HTML>
