
<?php
session_start();
function is_answered($id, $name) {
	$answers = json_decode( file_get_contents( 'data/answers.json' ), true );
	foreach ( $answers as $answer ) {
		if ($answer["qid"] == $id && $answer["username"] == $name) {
			return True;
		}
	}
	return False;
}
?>
<html>
<head>
<title> The Learning Jar </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<meta content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name = "viewport" />
</head>
<body>
<div id=container>
	<div id=top-logo>
	<center>
	<img src=img/mypage_s.png>
	<a href=index.php>
	<img src=img/logo_s.png width=250>
	</a>
	</center>
	</div>
	<div id=user-bar align=right>
	<?php
		if ( $_SESSION["username"] == "" ) {
		?>
			<form id=login action=login.php method=post>
			<input type='text' name='username' style="font-size:10;margin-top:5px;"
			id='username' size=15 maxlength=20 />
			<input type='password' name='password' style="font-size:10;"
			id='password' size=15 maxlength=20 />
			<input type='submit' name='Submit' value='login' 
			style="font-size:10;"/>
			<a href=join.htm>join</a>
			</form>
		<?php
		} else {
		?>
			<a href=mypage.php>mypage</a> :
			<a href=logout.php>logout</a>
		<?php
		}
	?>
	</div>
	<div id=command-bar>
	subscribed tags:
	<?php
	$tags = explode(",",$_SESSION["tags"]);
	foreach($tags as $tag) {
		if ( $tag == "" ) continue;
		echo "<a href=taglist.php?r=$tag>$tag</a> ";
		echo "[<a href=addtag.php?r=$tag>x</a>] ";
	}
	?>
	<form id=addtag action=addtag.php method=post align=right>
	<input type='text' name='tag' style="font-size:10"
	id='tag' size=15 maxlength=15/>
	<input type='submit' name='submit' value='add tag'
	style="font-size:10;"/>
	</form>
	</div>
	<div id=content>
	<table width=100% style="font-size:11">
	<?php
		$questions = json_decode( file_get_contents( 'data/questions.json' ), true );
		$match = False;
		$mycount = 0;
		foreach($questions as $q) {
			$qtags = explode(",",$q["tags"]);
			$stags = explode(",", $_SESSION["tags"]);
			foreach($qtags as $qtag) {
				foreach($stags as $stag) {
					if ( $qtag == $stag ) {
						$match = True;
						break;
					}
				}
			}
			if ( $match == False ) { continue; }
			$mycount += 1;

			if ($mycount % 2 == 0 ) {
				echo "<tr bgcolor=#DDD>";
			} else {
				echo "<tr>";
			}
			echo "<td width=200px>".$q['title']."</td>";
			echo "<td width=70px>";
			foreach ($qtags as $qtag) {
				echo "".$qtag.", ";
			}
			echo "</td>";
			echo "<td width=40px>";
			echo "<a href=answer.php?id=".$q['id'].">";
			if ( is_answered($q['id'], $_SESSION["username"]) == True ) {
				echo "edit";
			} else {
				echo "new";
			}
			echo "</a></td>";

			echo "</tr>";
			echo "<tr bgcolor=#fff><td colspan=3>";
			echo "".$q["question"];
			echo "</td></tr>";

		}
		if ( $mycount == 0 ) {
			echo "<tr><td>No questions!</td></tr>";
		}
	?>
	</table>
	</div>
	<div id=footer>
		contact:
	</div>
</div>
</body>
</html>
