<?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'");
$artistid = $_GET['id'];
$artist = @mysqli_fetch_array(mysqli_query($con, "select * from quiz where id=$artistid limit 0,1"));
$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'];
   $size = getimagesize($tmp_name);
   $ratio = $size[0]/$size[1];
   $width = $size[0];
   $height = $size[1];
	if ($width > 500)
	{
		$width = 500;
		$height = $height * 500 / $size[0];
	}
   $ext = pathinfo($file_name, PATHINFO_EXTENSION);
   $file_name = "".time().".".$ext."";
   $path= "../img/".$file_name."";

	if (move_uploaded_file($tmp_name, $path))
	{
    require_once 'SimpleImage.php';
    $image = new SimpleImage();
    $image->load($path);
    $image->resize($width,$height);
    $image->save($path);
	}
		$img = $file_name;
	}
	else
	{
	$img = "";   
	}
   //IMG UPLOAD
	$question = base64_encode($_POST['name']);
	$qorder = $_POST['qorder'];
	$an1 = base64_encode($_POST['an1']);
	$sc1 = $_POST['sc1'];
	$an2 = base64_encode($_POST['an2']);
	$sc2 = $_POST['sc2'];
	$an3 = base64_encode($_POST['an3']);
	$sc3 = $_POST['sc3'];
	$checkold = mysqli_query($con, "select * from questions WHERE `question` = '$question' AND `quizid` = '$artistid' limit 1");
	$olddata = @mysqli_fetch_array($checkold);
	$exists = @mysqli_num_rows($checkold);

	if ($exists == 1)
	{
		$msg="السؤال [ <a href='question_edit.php?id=".$olddata['id']."'><b>".$question."</b></a> ] موجود مسبقا.";
	}
	else{
	mysqli_query($con, "insert into questions (question,quizid,qorder,an1,sc1,an2,sc2,an3,sc3,img) values ('$question','$artistid','$qorder','$an1','$sc1','$an2','$sc2','$an3','$sc3','$img')");
	$msg="السؤال [ <font color='green'><b>".base64_decode($question)."</b></font> ] تم اضافته بنجاح";
   }
   }
?>
<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="question_add.php?id=<?=$artistid?>" 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=<?=$artistid?>"><b><?=base64_decode($artist['title'])?></b></a></td>
                </tr>
                <tr>
                  <td height="26">اسم السؤال</td>
				<td><input type="text" name="name" size="50" value="" required></td>
                </tr>
<?
		$suggestorder = mysqli_fetch_row(mysqli_query($con, "SELECT count(*) FROM questions where quizid=$artistid"));
		$suggestorder = $suggestorder[0] +1;
?>
                <tr>
                  <td height="26">ترتيب السؤال</td>
				<td><input type="text" style="text-align: center" name="qorder" size="1" value="<?=$suggestorder?>" required></td>
                </tr>
                <tr>
                  <td height="26">الاجابة #1</td>
				<td><input type="text" name="an1" size="50" value="" required>
				<br>النقط:<input type="radio" name="sc1" value="1" checked="checked">1<input type="radio" name="sc1" value="2">2<input type="radio" name="sc1" value="3">3</td>
                </tr>
                <tr>
                  <td height="26">الاجابة #2</td>
				<td><input type="text" name="an2" size="50" value="" required>
				<br>النقط:<input type="radio" name="sc2" value="1">1<input type="radio" name="sc2" value="2" checked="checked">2<input type="radio" name="sc2" value="3">3</td>
                </tr>
                <tr>
                  <td height="26">الاجابة #3</td>
				<td><input type="text" name="an3" size="50" value="" required>
				<br>النقط:<input type="radio" name="sc3" value="1">1<input type="radio" name="sc3" value="2">2<input type="radio" name="sc3" value="3" checked="checked">3</td>
                </tr>
				<tr>
                <tr>
                  <td height="26">صورة يتم عرضها تحت الاختبار (اختياري)</td>
                  <td>
				  <input name="img" type="file" id="img" size="50" />
				  </td>
                </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");?>
