<?session_start();
	include("class.php");
	include("topic.php");
	include("logic.php");
	
	
	if (!$_SESSION['user'])
		header("Location: index.php");
		
	if ($_SESSION['admin'] >= 0)
		$classes = getActiveClasses(true, $_SESSION['user']);
		
	if ($_POST['create']) {
		$classid = $_POST['selClass'];
		if ($_POST['createPos'] != '')
			$pos = $_POST['createPos'];
		else {
			$positions = getPositions($_POST['selTopic']);
			foreach ($positions as $p) {
				if ($p['ID'] === $_POST['selPosition'])
					$pos = $p['Position'];
			}
		}
		
		if (!get_magic_quotes_gpc())
			$pos = addslashes($pos);
		$lastid = addLogicBase($_SESSION['user'], $classid, $pos);
		header("Location: logicedit.php?id=$lastid");
	}
	
	if ($_GET['remove']) {
		$base = getLogicBaseById($_GET['remove']);
		if ($base['UserID'] === $_SESSION['user'] || $_SESSION['admin'] != 0)
			removeLogicBase($_GET['remove']);
		header("Location: logichome.php");
	}
		
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Arguments</title>

<script language='javascript'>
<!--
	function changeClass(classid) {
		oldclass = document.getElementById('selClass').value;
		oldtopic = document.getElementById('selTopic').value;
		oldposition = document.getElementById('selPosition').value;
		document.getElementById('nopositions').style.display = 'none';
		document.getElementById('topics'+oldclass+'div').style.display = 'none';
		document.getElementById('positions'+oldtopic+'div').style.display = 'none';
		document.getElementById('topics'+classid+'div').style.display = 'block';
		document.getElementById('selClass').value = classid;
		topic = document.getElementById('topics'+classid);
		topicid = topic.options[topic.selectedIndex].value;
		document.getElementById('positions'+topicid+'div').style.display = 'block'; 
		document.getElementById('selTopic').value = topicid;
		var positionid = null;
		var i = 1;
		while ((position = document.getElementById('positions'+topicid+'_'+i)) != null) {
			if (position.checked) {
				positionid = position.value;
				break;
			}
			else
				i++;
		}
		if (!positionid) {
			document.getElementById('nopositions').style.display = 'block';
			positionid = -1;
		}
		document.getElementById('selPosition').value = positionid;
	}
	
	function changeTopic(topicid) {
		oldtopic = document.getElementById('selTopic').value;
		oldposition = document.getElementById('selPosition').value;
		document.getElementById('nopositions').style.display = 'none';
		document.getElementById('positions'+oldtopic+'div').style.display = 'none';
		document.getElementById('positions'+topicid+'div').style.display = 'block'; 
		document.getElementById('selTopic').value = topicid;
		var positionid = null;
		var i = 1;
		while ((position = document.getElementById('positions'+topicid+'_'+i)) != null) {
			if (position.checked) {
				positionid = position.value;
				break;
			}
			else
				i++;
		}
		if (!positionid) {
			document.getElementById('nopositions').style.display = 'block';
			positionid = -1;
		}
		document.getElementById('selPosition').value = positionid;
	}
	
	function changePosition(posid) {
		document.getElementById('selPosition').value = posid;
	}
-->
</script>

</head>

<?
include("header.php");
if ($_SESSION['admin'] >= 0) {
?>
<h3>Add New Argument Set</h3>
<div style='position:relative; left:20px'>
To begin, select the proper class for which this argument will be submitted under.<br />
You may then select an existing position from this class or create your own.<br />
(Entering your own position takes precedence over selecting a pre-existing one.)<br /> 
When finished, click the 'Create Argument' button.<br /><br />
<form id='add_args' name='add_args' method='POST'>
<input type='hidden' name='create' value='1' />
<div style='position:relative; left:20px'>
<table>
	<tr>
		<td align='right' style='font-weight:bold'>Select Class:</td>
		<td align='left' colspan=2>
		<select id='classlist' name='classlist' 
				onchange='changeClass(this.options[this.selectedIndex].value)'>
		<?
			foreach($classes as $c) {
				if (count(getTopics($c['ID'])) > 0)
					echo("<option value='".$c['ID']."'>".$c['CourseName']."</option>");
			}		
		?>
		</select>
		</td>
	</tr>
</table>
</div>
<div style='position:relative; left:40px'>
<table>
	<tr>
		<td align='right' style='font-weight:bold'>Select Topic:</td>
		<td align='left' colspan=2>
		<?
			$first = true;
			foreach($classes as $c) {
				if (count(getTopics($c['ID'])) > 0) {
					$topics = getTopics($c['ID']);
					echo("<div id='topics".$c['ID']."div' style='display:");
					if ($first) {
						echo("block'>");
						$first = false;
						$selclass = $c['ID'];
						$seltopic = $topics[0]['ID'];
					}
					else
						echo("none'>");
					echo("<select id='topics".$c['ID']."' name='topics".$c['ID']."' ");
					echo("onchange='changeTopic(this.options[this.selectedIndex].value)'>");
					$first2 = true;
					foreach($topics as $t) {
						if (count(getPositions($t['ID'])) > 0) {
							echo("<option value='".$t['ID']."'");
							if ($first2) {
								echo("SELECTED");
								$first2 = false;
							}
							echo(">".$t['Topic']."</option>");
						}
					}
					echo("</select></div>");
				}
			}
			echo("<input type='hidden' id='selClass' name='selClass' value='$selclass' />");
			echo("<input type='hidden' id='selTopic' name='selTopic' value='$seltopic'>");
		?>
		</td>
	</tr>
	<tr>
		<td colspan=2 style='font-weight:bold'>Select Position:</td>
	</tr>
</table>
<table>
	<tr>
		<td colspan=4>
		<span style='color:#FF3300; position:relative; left:20px; display:none' id='nopositions'>
			There are no valid positions currently assigned to this topic.  Please select a different topic.</span>
		<?
			$first = true;
			foreach($classes as $c) {
				$topics = getTopics($c['ID']);
				foreach($topics as $t) {
					$positions = getPositions($t['ID']);
					echo("<div id='positions".$t['ID']."div' style='position:relative; left:20px; display:");
					if ($first) {
						echo("block'>");
						$first = false;
						$selposition = $positions[0]['ID'];
					}
					else
						echo("none'>");
					echo("<table><tr>");
					$first2 = true;
					$j = 1;
					for($i = 0; $i < count($positions); $i++) {
						if ($i % 3 === 0)
							echo("</tr><tr>");
						echo("<td style='width: 16em' valign='top'><input type='radio' name='positions".$t['ID']."' value='".$positions[$i]['ID'].
							 "' onclick='changePosition(this.value)' id='positions".$t['ID']."_".$j."' ");
						if ($first2) {
							echo("CHECKED");
							$first2 = false;
						}
						echo(" />".$positions[$i]['Position']." &nbsp; &nbsp;</td>");
						$j++;
					}
					echo("</tr></table></div>");
				}
			}
			echo("<input type='hidden' id='selPosition' name='selPosition' value=$selposition />");
		?>
		</td>
	</tr>
</table>
<br /><br />
<table>
	<tr>
		<td align='right' style='font-weight:bold'>Create Position:</td>
		<td><input type='text' name='createPos' id='createPos' /></td>
	</tr>
	<tr>
		<td align='right'><br /><input type='submit' value='Create Argument' /></td>
	</tr>
</table>
</div>
</form>
</div>
<br />
<h3>Edit Existing Argument Sets</h3>
<div style='position:relative; left:20px;'>
<table>
<?
	$existing = getLogicBaseByUser($_SESSION['user']);
	if (count($existing) > 0) {
		foreach($existing as $e) {
			echo("<tr><td>".$e['BasePosition']."</td><td align='right'>&nbsp; <a href='logicedit.php?id=".$e['ID']."'".
				 " style='font-variant:small-caps; font-size:14px'>Edit</a> - <a href='logichome.php?remove=".$e['ID'].
				 "' style='font-variant:small-caps; font-size:14px'>Remove</a></td></tr>");
		}
	}
	else
		echo("<tr><td>You currently have no existing argument sets.  Please add one using the form above.</td></tr>");
?>
</table>
</div>
<br />
<? 
}
if ($_SESSION['admin'] != 0) {
	if ($_SESSION['admin'] < 0)
		$classes = getActiveClasses(true, $_SESSION['admin']==-2?null:$_SESSION['user']);
	else if ($_SESSION['admin'] == 1)
		$classes = getStudentAdminClasses($_SESSION['user']);
?>
<h3>View Student Argument Sets</h3>
<div style='position:relative; left:20px;'>
<?
	foreach($classes as $c) {
		echo("<b>".$c['CourseName']."</b>");
		$students = getRoster_Inst($c['ID']);
		?> <div style='position:relative; left:20px'> <?
		$exists = false;
		foreach($students as $s) {
			$temp = getLogicBaseByUser($s['ID']);
			$args = array();
			foreach($temp as $t) {
				if ($t['ClassID'] === $c['ID'])
					array_push($args, $t);
			}
			if (count($args) > 0) {
				$exists = true;;
				echo("<u>".$s['FirstName']." ".$s['LastName']."</u><br>");
				?> <div style='position:relative; left:20px'><table> <?
				foreach($args as $a) {			
					echo("<tr><td>".$a['BasePosition']."</td><td align='right'>&nbsp; <a href='logicedit.php?id=".$a['ID']."'".
						 " style='font-variant:small-caps; font-size:14px'>Edit</a> - <a href='logichome.php?remove=".$a['ID'].
						 "' style='font-variant:small-caps; font-size:14px'>Remove</a></td></tr>");
				}
				?> </table></div> <?
			}
		}
		if (!$exists)
			echo("There are no existing argument sets to display for this class.");
		echo("</div><br><br>");
	}
?>
</div>
<?
} 
include("footer.php");
?>
</html>