<?php
	ob_start();
session_start();
if($_SESSION['admin']=="")
{
	header("location: login.php");
}
$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'");
$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
	$title = base64_encode($_POST['name']);
	$result = base64_encode($_POST['result']);
	$igot = base64_encode($_POST['igot']);
	$lang = $_POST['lang'];
	$html = base64_encode($_POST['html']);
	$desc = base64_encode($_POST['desc']);
	
	$checkold = mysqli_query($con, "select * from quiz WHERE `title` = '$title' limit 1");
	$olddata = @mysqli_fetch_array($checkold);
	$exists = @mysqli_num_rows($checkold);

	if ($exists == 1)
	{
		$msg="There is already a quiz named: <a href='quiz_view.php?id=".$olddata['id']."'><b>".$title."</b></a> ";
	}else{
	mysqli_query($con, "INSERT into `quiz` (`title`,`desc`,`result`,`img`,`html`,`lang`,`igot`) values ('$title','$desc','$result','$img','$html','$lang','$igot')");
	$nd = @mysqli_fetch_array(mysqli_query($con, "select * from quiz ORDER by id DESC limit 0,1"));
	$nid = $nd['id'];
	$msg="The Quiz [ <a href='quiz_view.php?id=".$nid."'><b>".base64_decode($title)."</b></a> ] تمت الاضافة ";
	}
   }
?>
<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="" enctype="multipart/form-data">
                <tr>
                  <td colspan="2" bgcolor="f2f2f2" align="center"><strong>اضف اختبار جديد</strong></td>
                </tr>
				<? if($msg!="") {?>
				 <tr>
                  <td colspan="2"  align="center"><font color="#FF0000"><?=$msg?></font></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"  rows="9" cols="40"></textarea></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><input type="text" name="result" size="50" style="text-align: left" value="" required></td>
                </tr>
                <tr>
                  <td height="26">اسم الحصول على نتيجة الاختبار (مثال : لقد حصلت على</td>
				<td><input type="text" name="igot" size="50" style="text-align: left" value="" required></td>
                </tr>
                <tr>
                  <td height="26">اللغة</td>
				<td>
				  			<select name="lang">
<?
$langsql = "SELECT * FROM `lang` order by id ASC";
$langres = mysqli_query($con, $langsql);
			while($languages = mysqli_fetch_array($langres))
			{
			echo '<option value="'.$languages['id'].'">'.$languages['lang'].'</option>';
			}
?>
							</select>
				</td>
                </tr>
                <tr>
                  <td height="26"> Html مخصص<br>(اختياري)</td>
				<td><textarea name="html" style="text-align: left" rows="9" cols="40"></textarea><br>
				يتم عرضه بعد انهاء الاختبار (اختياري) - اتركه فارغ لعرض ملف <b>ads_234x250.php</b><br>
				<font color="red" size="2">يمكنك اضافة اكواد الاعلانات هنا</font></td>
                </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");?>
