<?php
session_start();
?>
<?
	include("user.php");
	include("login.php");
	include("class.php");
	include("topic.php");
	@include("article.php");
//	session_start();
	
	if (!$_SESSION['user'])
		header("Location: index.php");
	
	if(isset($_GET['approve'])) {
		approveArticle($_GET['article'], $_GET['approve']);
		header("Location: editarticles.php?id=".$_GET['id']."&class=".$_GET['class']);
	}
	
	if (isset($_GET['duplicate'])) {
		setIsDuplicate($_GET['article'], $_GET['duplicate']);
		header("Location: editarticles.php?id=".$_GET['id']."&class=".$_GET['class']);
	}
	
	if (isset($_GET['ref']))
		$_SESSION['referer'] = $_GET['ref'];
	else if (isset($_SESSION['referer']))
		unset($_SESSION['referer']);
	
	if (isset($_POST['articleDelete'])) {
		if (confirmPassword($_SESSION['user'], $_POST['deletepass']))
			removeArticle($_POST['articleDelete']);
		header("Location: editarticles.php?id=".$_GET['id']."&class=".$_GET['class']);
	}
	
?>

<!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>Edit Articles</title>
<script>
<!--
	document.onmousemove = getMouseLoc;
	var mouseX = 0;
	var mouseY = 0;
	
	function changeClass(classid) {
		oldclass = document.getElementById('selClass').value;
		document.getElementById('roster'+oldclass).style.display = 'none';
		document.getElementById('roster'+classid).style.display = 'block';
		document.getElementById('selClass').value = classid;
		newuser = document.getElementById('users'+classid).options[0].value;
		document.getElementById('selUser').value = newuser;
	}
	
	function changeUser(user) {
		document.getElementById('selUser').value = user;
	}
	
	function viewUser() {
		user = document.getElementById('selUser').value;
		classid = document.getElementById('selClass').value;
		window.location = 'editarticles.php?id='+user+'&class='+classid;
	}
	
	function goBack(target) {
		if (target == 1)
			window.location = 'editarticles.php';
		else
			window.location = 'index.php';
	}
	
	function editArticle(artid) {
		window.location = 'viewarticle.php?id='+artid+'&return=<? echo $_GET['id']; ?>.<? echo $_GET['class'] ?>';
	}
	
	function deleteArticle(artid, show) {
		if (show) {
			div = document.getElementById('confirmDelete');
			div.style.display = 'block';
			div.style.top = mouseY+'px';
			div.style.left = (mouseX+10)+'px';
			document.getElementById('articleDelete').value = artid;
			document.getElementById('yesdelete').disabled = false;
		}
		else {
			document.getElementById('confirmDelete').style.display = 'none';
			document.getElementById('articleDelete').value = 0;
			document.getElementById('yesdelete').disabled = true;
		}
	}
	
	function approve(artid, approve) {
		window.location = 'editarticles.php?id=<? echo $_GET['id']; ?>&class=<? echo $_GET['class']; ?>&approve='+approve+'&article='+artid;
	}
	
	function getMouseLoc(e) {
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			mouseX = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			mouseY = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
	}
// -->
</script>
</head>

<? 	include("header.php");
	if (isset($_GET['id'])) {
		$user = getUser('ID', $_GET['id']);
		if ($_GET['class'])
			$class = getClass($_GET['class']);
		if ($_GET['class'] == 0) {
			echo("Select the article you want to edit.  ");
			if ($_SESSION['admin'] == 0)
				echo("You may not edit any articles that have been approved.");
			echo("<br><br>");
		}
		else {
			echo("Select the article you want to edit, approve, or delete for the user ".
				 $user['FirstName']." ".$user['LastName'].".<br>");
			echo("These articles were submitted under topics for the class ".$class['CourseName'].".<br><br>");
		}
		
		$articles = getArticles($_GET['id'], $_GET['class'], false); 
?>

<table border=1 cellpadding='6px'>
	<tr>
		<th>Article</th>
		<th>Author</th>
		<th>Web Address</th>
		<th>Date Submitted</th>
		<th>Approved</th>
		<? if ($_SESSION['admin'] != 0) echo("<th>Duplicate</th>"); ?>
	</tr>
<?
	foreach($articles as $a) {
		echo("<tr>");
		echo("<td>".$a['Title']."</td>");
		echo("<td>".$a['Author']."</td>");
		if ($a['URL'] && $a['URL'] != '') {
			if (substr($a['URL'], 0, 7) != 'http://')
				$a['URL'] = 'http://'.$a['URL'];
			echo("<td><a href='".$a['URL']."' target=_blank >");
			if (strlen($a['URL']) > 20)
				$a['URL'] = substr($a['URL'], 0, 20)."...";
			echo($a['URL']."</a></td>");
		}
		else
			echo("<td>No address provided.</td>");
		echo("<td>".$a['DateEntered']."</td>");
		if ($a['Approved'] == 1) {
			if ($_SESSION['admin'] != 0)
				echo("<td align='center'><a href='javascript:approve(".$a['ID'].", 0)'>Yes</a></td>");
			else
				echo("<td align='center'>Yes</td>");
		}
		else {
			if ($_SESSION['admin'] != 0)
				echo("<td bgcolor='#FFB3B3' align='center'><a href='javascript:approve(".$a['ID'].", 1)'>No</a><br>");
			else
				echo("<td align='center'>No<br>");
			echo($a['UnapproveReason']."</td>");
		}
		if ($_SESSION['admin'] != 0) {
			if ($a['Duplicate'] != 1)
				echo("<td align='center'>No</td>");
			else
				echo("<td bgcolor='#FFB3B3' align='center'>".
					 "<a href='editarticles.php?id=".$_GET['id']."&class=".$_GET['class']."&duplicate=0&article=".$a['ID']."'>Yes</a></td>");
		}
		echo("<td align='center'><input type='button' value='Edit' ");
		if ($a['Approved'] == 0 || $_SESSION['admin'] != 0)
			echo("onclick='editArticle(".$a['ID'].")'></td>");
		else
			echo("DISABLED></td>");
		if ($_SESSION['admin'] != 0) {
			?>
			<td><input type='button' value='Delete' 
					onclick='deleteArticle(<? echo $a['ID']; ?>, true)' />
			</td>
			<? 
		}
		echo("</tr>");
	}
?>
</table>
	
<?
echo("<br><br>");
if (isset($_SESSION['referer'])) {
	if ($_SESSION['referer'] == 1)
		echo("<input type='button' value='Class Roster' onclick='window.location=\"editclass.php?id=".$_GET['class']."\"'>");
	else if ($_SESSION['referer'] == 2)
		echo("<input type='button' value='Progress Report' onclick='window.location=\"".$_SESSION['referer']."\"'>");
}
else {
	if ($_SESSION['admin'] != 0)
		echo("<input type='button' value='Select a User' onclick='goBack(1)' /> ");
	echo("<input type='button' value='Home' onclick='goBack(0)' />");
}
	
	} else {
		if ($_SESSION['admin'] == 0)
			header("Location: editarticles.php?id=".$_SESSION['user']."&class=0");
		if ($_SESSION['admin'] < 0)
			$classes = getActiveClasses(true, $_SESSION['admin']==-2?null:$_SESSION['user']);
		else if ($_SESSION['admin'] == 1)
			$classes = getStudentAdminClasses($_SESSION['user']);
		$_SESSION['ignoreRef'] = 1;
		if (count($classes) > 0) {
			echo("Please select a class and specify which student's articles to view.<br />");
			$roster = getRoster_Inst($classes[0]['ID']);
			echo("<input type='hidden' id='selClass' value='".$classes[0]['ID']."'>");
			echo("<input type='hidden' id='selUser' value='".$roster[0]['ID']."'>");
			echo("<select id='classes' style='position:relative; left:15px' ".
				 "onchange='changeClass(this.options[this.selectedIndex].value)'>");
			foreach($classes as $c)
				echo("<option value='".$c['ID']."'>".$c['CourseName']."</option>");
			
			echo("</select><br><br>");
			
			$first = true;
			foreach($classes as $c) {
				$roster = getRoster_Inst($c['ID']);
				echo("<div id='roster".$c['ID']."' style='position:relative; left:15px; ");
				if ($first) {
					echo("display: block'>");
					$first = false;
				}
				else
					echo("display: none'>");
				if (count($roster) > 0) {
					echo("<select id='users".$c['ID']."' onchange='changeUser(this.options[this.selectedIndex].value)'>");
					foreach($roster as $user)
						echo("<option value='".$user['ID']."'>".$user['FirstName']." ".$user['LastName']."</option>");
					echo("</select><br><br><input type='button' value='View' onclick='viewUser()'>");
				}
				else
					echo("There are no students in this class.");
				echo("</div>");
			}
		}
		else
			echo("There are no classes available for you to view.");
		echo("<br><input type='button' value='Home' onclick='goBack(0)' />");
	} ?>
	
<div id='confirmDelete' style='position:absolute; z-index:3; display:none; background-color:#FFFFCC;'>
<table border='1'>
<tr><td>
	<form id='deleteconfirm' name='deleteconfirm' submit='editarticles.php?id=<? echo $_GET['id']; ?>&class=<? echo $_GET['class']; ?>' method='POST'>
		<table cellpadding='2px'>
			<input type='hidden' id='articleDelete' name='articleDelete' />
			<tr>
				<td colspan='2'><b>Confirm password.</b></td>
			</tr>
			<tr>
				<td align='right' style='font-weight:bold'>Password:</td>
				<td align='left'><input type='password' id='deletepass' name='deletepass' /></td>
			</tr>
			<tr>
				<td></td>
				<td><input type='submit' id='yesdelete' name='yesdelete' value='Confirm' disabled="disabled" />
				    <input type='reset' value='Cancel' onclick='deleteArticle(0, false)' /></td>
			</tr>
		</table>
	</form>
</td></tr>
</table>
</div>

<? include("footer.php"); ?>
</html>
