<?php
	ob_start();
session_start();
if($_SESSION['admin']=="")
{
	header("location: login.php");
}
$madeby = $_SESSION['admin'];
$pageTitle = "اضف نتيجة جديده";
include("../var.php");
include("header.php");
	$msg="";
$con = mysqli_connect($DBHOST,$DBUSER,$DBPASS,$DBNAME) or die("Some error occurred during connection " . mysqli_error($con));
mysqli_query($con, "SET CHARACTER SET 'utf8'");
$songid = $_GET['id'];
$posted = "";
if(isset($_POST['submit'])){ $posted = $_POST['submit']; }
if($posted !== "")
   {
   //IMG UPLOAD
	$file_name = "";
	if(isset($_FILES['img']['name'])){ $file_name = $_FILES['img']['name']; }
   if($file_name != "")
	{
   $tmp_name = $_FILES['img']['tmp_name'];
   $ext = pathinfo($file_name, PATHINFO_EXTENSION);
   $file_name = "".time().".".$ext."";
   $path= "../img/".$file_name."";
	
	if (move_uploaded_file($tmp_name, $path))
	{
	}
		$img = $file_name;
	}
	else
	{
	$img = "logo.png"; 
	}
   //IMG UPLOAD

	$title = base64_encode($_POST['name']);
	$desc = base64_encode($_POST['desc']);
	$score = $_POST['score'];
	mysqli_query($con, "insert into result (`title`,`desc`,`score`,`img`,`quizid`) values ('$title','$desc','$score','$img','$songid')");
	$msg="تم اضافة الاجابة بنجاح";
   }

$quiz = @mysqli_fetch_array(mysqli_query($con, "select * from quiz where id=$songid limit 0,1"));
?>
<table width="100%" border="0" cellspacing="3" cellpadding="3">
	<tr>
	  <td valign="top">
	  <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC">
        <tr> 
            <td  valign="top" bgcolor="#FFFFFF">
			 <table width="800" border="1" style="border-collapse:collapse" cellspacing="2" cellpadding="6" align="center">
              <form method="post" action="result_add.php?id=<?=$songid?>" enctype="multipart/form-data">
                <tr>
                  <td colspan="2" bgcolor="f2f2f2" align="center"><strong><?=$pageTitle?></strong></td>
                </tr>
				<? if($msg!="") {?>
				 <tr>
                  <td colspan="2"  align="center"><font color="#FF0000"><?=$msg?></font></td>
                </tr>
				<? }?>
                <tr>
                  <td height="26">اسم الاختبار</td>
				  <td><a href="quiz_view.php?id=<?=$quiz['id'];?>"><b><?=base64_decode($quiz['title'])?></b></a>
				  </td>
                </tr>
                <tr>
				<td height="26">اسم النتيجة</td>
				<td><input type="text" name="name" size="50" style="text-align: left" value="" required></td>
                </tr>
                <tr>
                  <td height="26">وصف نتيجة الاختبار</td>
				<td>
				<textarea name="desc" style="text-align: left" value="" rows="9" cols="40"></textarea></td>
                </tr>
                <tr>
<?
		$suggestscore = mysqli_fetch_row(mysqli_query($con, "SELECT count(*) FROM result where quizid=$songid"));
		$suggestscore = $suggestscore[0] +1;
?>
                  <td height="26">نقط النتيجة</td>
				<td><input style="text-align: center" type="text" name="score" size="1" value="<?=$suggestscore?>" required> </td>
                </tr>
                <tr>
                  <td height="26">الصورة</td>
                  <td>
				  <input name="img" type="file" id="img" size="50" />
				  </td>
                </tr>
				<tr>
                  <td height="26">حالة الاختبار</td>
                  <td>
				  <?
			$color = $quiz['live'];
if ($color == 1) { echo "<img title='الاختبار نشط' src='img/online.png'/>"; } else{ echo "<img title='الاختبار غير نشط' src='img/offline.png'/>"; } ?>
				  </td>
                </tr>
				<tr>
                <input type='hidden' name='id' value='<?=$id?>' size='20'></tr>
				<tr>
				  <td colspan="2" align="center" bgcolor="#CCCCCC"><input type="submit" name="submit" value="اضف النتيجة"></td>
				</tr>
              </form>
            </table>
        </tr>
      </table>
	  </td>
</tr>
</table>
<?	include("footer.php");?>
