<?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'];

$del = "";
if(isset($_GET['del'])){ $del = $_GET['del']; }
if ($del == 1)
{
	mysqli_query($con, "UPDATE `questions` SET `img` = '' WHERE  `id`='$artistid'");
	$msg="تم حذف الصورة بنجاح";
}

$artist = @mysqli_fetch_array(mysqli_query($con, "select * from questions where id=$artistid limit 0,1"));
$quizid = $artist['quizid'];
$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
	$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'];
	mysqli_query($con, "UPDATE `questions` SET `question` = '$question', ".$img_sql."`quizid` = '$quizid', qorder = '$qorder', an1 = '$an1', sc1 = '$sc1', an2 = '$an2', sc2 = '$sc2', an3 = '$an3', sc3 = '$sc3' WHERE `id` = $artistid");
	$msg="السؤال [ <font color='green'><b>".base64_decode($question)."</b></font> ] تم تعديله";
   }
$artist = @mysqli_fetch_array(mysqli_query($con, "select * from questions where id=$artistid limit 0,1"));
$quizid = $artist['quizid'];
$quiz = @mysqli_fetch_array(mysqli_query($con, "select * from quiz where id=$quizid 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="question_edit.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=<?=$quiz['id'];?>"><b><?=base64_decode($quiz['title'])?></b></a></td>
                </tr>
                <tr>
                  <td height="26">اسم السؤال</td>
				<td><input type="text" name="name" size="50" value="<?=base64_decode($artist['question'])?>" required></td>
                </tr>
                <tr>
                  <td height="26">ترتيب السؤال</td>
				<td><input type="text" style="text-align: center" name="qorder" size="1" value="<?=$artist['qorder']?>" required></td>
                </tr>
                <tr>
                  <td height="26">الاجابة #1</td>
				<td><input type="text" name="an1" size="50" value="<?=base64_decode($artist['an1'])?>" required>
				<br>النقط:<input type="radio" name="sc1" value="1" <? if ($artist['sc1'] == 1) { echo 'checked="checked"'; }?>>1<input type="radio" name="sc1" value="2" <? if ($artist['sc1'] == 2) { echo 'checked="checked"'; }?>>2<input type="radio" name="sc1" value="3" <? if ($artist['sc1'] == 3) { echo 'checked="checked"'; }?>>3</td>
                </tr>
                <tr>
                  <td height="26">الاجابة #2</td>
				<td><input type="text" name="an2" size="50" value="<?=base64_decode($artist['an2'])?>" required>
				<br>النقط:<input type="radio" name="sc2" value="1" <? if ($artist['sc2'] == 1) { echo 'checked="checked"'; }?>>1<input type="radio" name="sc2" value="2" <? if ($artist['sc2'] == 2) { echo 'checked="checked"'; }?>>2<input type="radio" name="sc2" value="3" <? if ($artist['sc2'] == 3) { echo 'checked="checked"'; }?>>3</td>
                </tr>
                <tr>
                  <td height="26">الاجابة #3</td>
				<td><input type="text" name="an3" size="50" value="<?=base64_decode($artist['an3'])?>" required>
				<br>النقط:<input type="radio" name="sc3" value="1" <? if ($artist['sc3'] == 1) { echo 'checked="checked"'; }?>>1<input type="radio" name="sc3" value="2" <? if ($artist['sc3'] == 2) { echo 'checked="checked"'; }?>>2<input type="radio" name="sc3" value="3" <? if ($artist['sc3'] == 3) { echo 'checked="checked"'; }?>>3</td>
                </tr>
                <tr>
                  <td height="26">صورة تعرض تحت السؤال (اختياري)</td>
                  <td>
<?
if ($artist['img'] != "")
{
?>
<img src="../img/<?=$artist['img']?>" width="300"/><br>
<input name="imgn" type="hidden" value="<?=$artist['img']?>"/>
<a href="question_edit.php?id=<?=$artistid?>&del=1" onclick="return confirm('هل انت متاكد من حذف هذه الصورة , اذا كنت تريد تغيير الصورة فبرجاء رفع صورة جديده مباشرة');" style="text-decoration: none; color: red;"> [ حذف الصورة الحقيقية ]</a>
<?
}else{
?>
<input name="imgn" type="hidden" value=""/>
<?
}
?>
				  <input name="img" type="file" id="img" size="50" />
				  </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");?>
