<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
  <script id="MathJax-script" async
          src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
  </script>
<?php
$pgMod="add_que_session";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if($_POST){

	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['que_session']) || trim($_POST['que_session'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter session.");
	}
	

	if($flgEr!=TRUE && $pgAct=="add")
	{
	    
        $sessionArray = array(
                        "que_session"=> $_POST["que_session"]
                        );
 	    $flgIn = $db->insertAry("assignment_question_session", $sessionArray);

        if(!is_null($flgIn))
		{
			$_SESSION['msg']='Added successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}

	}
	
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
            $sessionArray = array(
                "que_session"=> $_POST["que_session"]
            );
 	    $db->updateAry("assignment_question_session", $sessionArray, "where id='".$_GET['id']."' ");

        if(!is_null($_GET['id']))
		{
			$_SESSION['msg']='Updated successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}

	}

}


elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
 	$res=$db->delete("assignment_question_session","where id='".$_GET['id']."' ");

	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
}

if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
?>
<div class="alert alert-success">
 <a class="close" data-dismiss="alert">×</a>
 <i class="icon icon-thumbs-up-alt"></i> <b></b><?php echo $_SESSION['msg'] ?></i>
</div>
<?php
unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
?>
<div class="alert alert-error">
 <a class="close" data-dismiss="alert">×</a>
 <i class="icon icon-warning-sign"></i> <b></b> <?php echo $alert_err[0] ?></i>
</div>
<?php

}


if(($pgAct=="edit" && $_GET["id"]!="") || ($pgAct=="addmore" && $_GET["id"]!=""))
{
	$aryForm=$db->getRow("SELECT * FROM assignment_question_session WHERE id='".$_GET['id']."'");
}

if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !='') )
{
?>
<input type="hidden" name="src" value="Notice" />
<input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
<div class="row-fluid">
            <div class="span12">
            <div class="top-bar">
<h3><i class="icon-cog"></i>Add Question session</h3>
<div>

</div>
</div>
<div class="well no-padding"  id="printDiv2">
<div class="controls controls-row">

<div style="padding:20px;">
  <form method="post" action="" enctype="multipart/form-data"  >


    <div class="control-group">
    <label class="control-label">Question Session</label>
      <div  class="controls" id="chapter_div">
            <input type="text" name="que_session" value="<?php echo $aryForm[que_session]; ?>" style="border:solid 1px red;width:600px;" required/>
      </div>
    </div>
 



<?php 
    $allQuestions = $db->getRows("select * from assignment_questions where paper_id='".$aryForm[id]."' "); 
if((count($allQuestions)>0) && $pgAct=='edit'){
    $i = 0;
    foreach($allQuestions as $question){
        $i++;

        $qid  = $question['id'];
        
        $questionFiles = $db->getRows("select * from assignment_question_files where paper_id='".$aryForm[id]."' and question_id='".$qid."' and image_type='question_image' and qtype='".$question[qtype]."' ");

        $optionFiles = $db->getRows("select * from assignment_question_files where paper_id='".$aryForm[id]."' and question_id='".$qid."' and image_type='option_image' and qtype='".$question[qtype]."' ");

    ?>
            <input type="hidden" value="<?php echo $qid; ?>" name="totalquestions[]" />
            <input type="hidden" value="<?php echo $question[qtype]; ?>" name="question_type<?php echo $qid; ?>" />

			<div class="control-group" style="border:solid 1px teal"> <div style="background:#0052CC;color:white;padding:8px;font-size:14px;">Question <?php echo $i; ?>
            <?php if($aryForm['paper_type']==2 || $aryForm['paper_type']==3){ ?>
                &emsp;<input type="radio" name="quemark<?php echo $qid; ?>" value="1" <?php if($question['quemark']==1)echo "checked"; ?> > 1 Mark
                &emsp;<input type="radio" name="quemark<?php echo $qid; ?>" value="2" <?php if($question['quemark']==2)echo "checked"; ?> > 2 Mark
                &emsp;<input type="radio" name="quemark<?php echo $qid; ?>" value="3" <?php if($question['quemark']==3)echo "checked"; ?> > 3 Mark
                &emsp;<input type="radio" name="quemark<?php echo $qid; ?>" value="5" <?php if($question['quemark']==5)echo "checked"; ?> > 5 Mark
                &emsp; <select name="quesession<?php echo $qid; ?>">
                        <option value="">--Select session--</option>
            <?php
                $papersession = $db->getRows("select * from assignment_question_session");
                    foreach($papersession as $psession){ ?>
                        <option value="<?php echo $psession[que_session]; ?>" <?php if($question["quesession"]==$psession["que_session"])echo "selected"; ?> ><?php echo $psession[que_session]; ?></option>
            <?php
                    }
                    echo "</select>";
                } 
            ?>
			<a href="javascript:void()" onclick="deletequestion('<?php echo $qid; ?>')" class="btn btn-primary" style="float:right;margin-top:-5px;">Delete</a> </div>

            <div class="panel-body"  id="questiontype'+i+'">
            <div class="control-group"> <label class="control-label"> Question </label> 
            <div class="controls">
                <div style="width:70%;display:inline-block;">
                <textarea name="question<?php echo $qid; ?>" style="width:93%;height:100px;border:solid 1px red;"  required><?php echo $question["question"]; ?></textarea>
                </div>
                <!--<div style="width:25%;display:inline-block;margin-left:75%;">-->
                <div style="display:inline-block;width:29%;margin-left:75%;float:right;margin-top:-110px;">
                    <?php
                        if(count($questionFiles)>0){
                        foreach($questionFiles as $queimage){
                            echo "<div style='width:40px;height:40px;border:solid 2px red;display:inline-block;margin:5px;'><a href='uploads/questionimages/$queimage[name]' download><img src='uploads/questionimages/$queimage[name]' style='width:40px;height:40px;'></a> <a href='javascript:void()' style='color:brown;padding-top:30px;font-size:12px;' onclick='deletefile($queimage[id])'>Delete</a> </div>";
                        }
                    }
                    ?>
 
                <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('<?php echo $qid; ?>')" id="upload_question<?php echo $qid; ?>" name="upload_question<?php echo $qid; ?>[]" multiple /> </div>
                <div id="question_preview<?php echo $qid; ?>"  style="display:inline-block;"></div>
 
                </div>

            </div>
            </div>



    		 <div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint<?php echo $qid; ?>" value="<?php echo $question["hint"]; ?>" style="width:95%;border:solid 1px red;" /> </div> </div>
    		 <div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution<?php echo $qid; ?>" style="width:95%;border:solid 1px red;"><?php echo $question["solution"]; ?></textarea> </div> </div>
    		 
    		 </div>  </div>
        <?php
   
    }
   
}


?>
    </div>

        <div class="form-actions">
			<input type="submit" value="submit" class="btn btn-primary">
			<button type="button" class="btn"> Cancel</button>
		</div>
    </form>

</div>

        </div>
        <br>
</div>
<!-- / Forms: Content -->
</div>
    </div>

<?php


}
else {
    
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <button style="float:right; margin-top:5px" onClick="window.location='./?src=Notice&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add Question Session</button>
    </div>
    <div class="well no-padding">

    <div id="filterdata">
	<?php
	$aryPgAct=array("src"=>"Notice", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM assignment_question_session "); 
	    $i = 0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Session</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["que_session"]; ?></td>
                    <td>
                        <?php $aryPgAct['action']="edit"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a>
                        <?php $aryPgAct['action']="delete"; ?>
                         <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                	<th></th>
                    <th colspan="4"></th>
                </tr>
            </tfoot>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?><div>
	</div></div></div>
	<?php

}
?>
    
    <!--</form>-->


<style type="text/css">
@media print {
    #myPrntbtn {
        display :  none;
    }
}

@media screen {
   #myPrntbtn1 {
        display: none;
    }
}
</style>

<script type="text/javascript">
	$(document).ready(function(e) {
		var oTable = $('.table').dataTable( {
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": true,
			"bSort": true,
			"bInfo": true,
			"bAutoWidth": false
		});
	});

</script>
	
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script>
    function deletequestion(qid){
      var r = confirm("Are you sure want to delete?");
      if (r == true) {
        $.post( "Notice/ajax.php", { type: "delete_question" , qid: qid } , function( data ) {
            location.reload();
    	});
      }
    }
</script>
