<?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'");
$quizid = $_GET['id'];
$quiz = @mysqli_fetch_array(mysqli_query($con, "select * from quiz where id=$quizid 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;
		$img_sql = "`img` = '".$img."', ";
	}
	else
	{
	$img = $_POST['imgn']; 
	$img_sql = "";   
	}
   //IMG UPLOAD
	$html = base64_encode($_POST['html']);
	$desc = base64_encode($_POST['desc']);
	$title = base64_encode($_POST['name']);
	$result = base64_encode($_POST['result']);
	$igot = base64_encode($_POST['igot']);
	$lang = $_POST['lang'];
	$live = $_POST['live'];
	mysqli_query($con, "UPDATE `quiz` SET `title` = '$title', `desc` = '$desc', ".$img_sql."`result` = '$result',  `igot` = '$igot', `live` = '$live', `html` = '$html', `lang` = '$lang' WHERE `id` = '$quizid'");
	$msg="الاختبار [ <a href='quiz_view.php?id=".$quizid."'><b>".base64_decode($title)."</b></a> ] تم تحريره! ";
   }
$quiz = @mysqli_fetch_array(mysqli_query($con, "select * from quiz where id=$quizid limit 0,1"));
$live = $quiz['live'];
?>
<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 bgcolor="#FFFFFF" class="heading"><a href="quizlist.php" class="in_link"><center><<== الرجوع لقائمة الاختبارات</center></a></td>
		</tr>
	  <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><?=$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><input type="text" name="name" size="50" style="text-align: left" value="<?=base64_decode($quiz['title'])?>" required></td>
                </tr>
                <tr>
                  <td height="26">وصف الاختبار</td>
				<td><textarea name="desc" style="text-align: left" rows="9" cols="40"><?=base64_decode($quiz['desc'])?></textarea></td>
                </tr>
                <tr>
                  <td height="26">صورة الغلاف (اختياري)</td>
                  <td>
<?
if ($quiz['img'] != "")
{
?>
<img src="../img/<?=$quiz['img']?>" width="300"/><br>
<input name="imgn" type="hidden" value="<?=$quiz['img']?>"/>
<?
}else{
?>
<input name="imgn" type="hidden" value=""/>
<?
}
?>
				  <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="<?=base64_decode($quiz['result'])?>" required></td>
                </tr>
                <tr>
                  <td height="26">اسم الحصول على نتيجة الاختبار (مثال : لقد حصلت على)</td>
				<td><input type="text" name="igot" size="50" style="text-align: left" value="<?=base64_decode($quiz['igot'])?>" required></td>
                </tr>
                <tr>
                  <td height="26">كود Html مخصص<br>(اختياري)</td>
				<td><textarea name="html" style="text-align: left" rows="9" cols="40"><? echo base64_decode($quiz['html']);?></textarea><br>
				يتم عرضه بعد انهاء الاختبار (اختياري) - اتركه فارغ لعرض ملف <b>ads_234x250.php</b><br>
				<font color="red" size="2">يمكنك اضافة اكواد الاعلانات هنا</font></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))
			{
			$selected = "";
			if ($languages['id'] == $quiz['lang']) $selected = 'selected';
			echo '<option value="'.$languages['id'].'" '.$selected.'>'.$languages['lang'].'</option>';
			}
?>
							</select>
				  </td>
                </tr>
                <tr>
                  <td height="26">الحالة</td>
                  <td>
				  			<select name="live">
								<option value='0' style="background-color: #E60000;">غير مفعل</option>
								<option value='1' <?php if($live == 1) { echo "selected"; }?> style="background-color: #33CC33;">مفعل</option>
							</select>
				  </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");?>
