<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_questions";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if($_POST){

	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['class']) || trim($_POST['class'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select Class.");
	}
    elseif(!isset($_POST['parent_subject']) || trim($_POST['parent_subject'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select subject.");
	}

    $papertype = $_POST["paper_type"];

    if(($papertype == 2 || $papertype == 3) && $pgAct=="add")
    {
        $checkExists = $db->getRow("select * from assignment_paper where class='".$_POST["class"]."' and parent_subject='".$_POST["parent_subject"]."' and sub_subject='".$_POST["sub_subject"]."' and chapter='".$_POST["chapter"]."' and paper_type='".$papertype."' ");
        if(count($checkExists)>1  && $pgAct=="add"){
    		$flgEr=TRUE;
    		array_push($alert_err,"Paper under this Class->Subject->Sub subject->Chapter already exists. Please use 'Add more' option to add Questions in this chapter.");
        }
    }

	if($flgEr!=TRUE && $pgAct=="add")
	{
	    
	        $paperArray = array(
                            "class"=> $_POST["class"],
                            "parent_subject" => $_POST["parent_subject"],
                            "sub_subject" => $_POST["sub_subject"],
                            "chapter" => $_POST["chapter"],
                            "per_que_marks" => $_POST["per_que_marks"],
                            "level" => $_POST["level"],
                            "paper_description" => $_POST["paper_description"],
                            "duration" => $_POST["duration"],
                            "total_questions" => $_POST["total_questions"],
                            "paper_type" => $_POST["paper_type"],
                            "paper_status" => $_POST["paper_status"],
                            "livedate" => $_POST["livedate"],
                            "livetime" => $_POST["livetime"],
                            "session"    => $_SESSION["admin"]["session"]
                            );
 		    $flgIn = $db->insertAry("assignment_paper", $paperArray);

        if ($flgIn>0) {

    			if($_POST['total_questions']>0){

                for($i=1;$i<=($_POST['total_questions']);$i++){

					$option=array();
					$m_options=array();
					$m_ans=array();
					$answerO=array();
					
					$quemark = 0;
					$quesession = '';

                    $question = $_POST['question'.$i];

					$qtype = $_POST['question_type'.$i];                    

					if($qtype==1 || $qtype==3){ // Single choice and fill in the blanks

                    	for($x=1; $x<=4; $x++){
                    		$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$i.'_'.$x]);
                    		}

                    	$ans=$_POST['a_section_'.$i];
                    	$answerO[]=$ans;

                    }

					if($qtype==2){

						for($x=1; $x<=4; $x++){
							$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$i.'_'.$x]);
							}
						$ans=$_POST['a_section_'.$i];

						$answerO=array();
						foreach ($ans as $key) {
    					$answerO[] = $key;
//							$answerO[] =$_POST['o_section_'.$i.'_'.$key];
						}

					}


					if($qtype==4){ // True False
    					$true = array("type"=>"text","answer"=>"True");
    					$false = array("type"=>"text","answer"=>"False");
    					$option = array($true,$false);
						$answerO[]=$_POST['a_section_'.$i];
					}

					if($qtype==5){ // Match the following
						for($x=1; $x<=4; $x++){
							$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$i.'_'.$x]);
							$m_options[] = array("type"=>"text","answer"=>$_POST['m_section_'.$i.'_'.$x]);
							$answerO[]=$_POST['m_ans'.$i.$x];
							}
					}

					if($qtype==6){ // Match the following
						for($x=1; $x<=4; $x++){
							$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$i.'_'.$x]);
							$m_options[] = array("type"=>"text","answer"=>$_POST['m_section_'.$i.'_'.$x]);
							$answerO[] =$_POST['m_ans'.$i.$x];
							}
					}

                    $hint = $_POST['hint'.$i];

					if($qtype==7 || $qtype==8){
					        $quemark = $_POST['quemark'.$i];
					        $quesession = $_POST["quesession$i"];
							$hint = [];
					}

                    $solution = $_POST['solution'.$i];


    				$aryData=array(
                        'paper_id'  => $flgIn,
    				    'question'  => $question,
    				    'options'	=> json_encode($option),
    				    'm_options' => json_encode($m_options),
    				    'answer'	=> json_encode($answerO),
    				    'hint'      => $hint,
    				    'solution'  => $solution,
         				'added_by'  => $_SESSION['admin']['uid'],
        				'school_id' => $_SESSION['admin']['school_id'],
        				'session'   => $_SESSION['admin']['session'],
        				'qtype'     => $qtype,
        				'quemark'   => $quemark,
        				'quesession'=> $quesession
    				);
    				
                    if($aryData['question'] != ''){
    					$qentry = $db->insertAry("assignment_questions", $aryData);
                    }



                    if(isset($_FILES["upload_question$i"]["name"]) && !empty($_FILES["upload_question$i"]["name"])){
                        $countfiles = count($_FILES["upload_question$i"]["name"]);
                        print_r($countfiles);
                            $questionimage = array();
                            for($j=0;$j<$countfiles;$j++){
                        		$lfilename = basename($_FILES["upload_question$i"]["name"][$j]);
                        		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                        		$lnewfile=md5(microtime()).".".$lext;
                        		if(move_uploaded_file($_FILES["upload_question$i"]["tmp_name"][$j],"uploads/questionimages/".$lnewfile))
                            		{
                            		    $keytermArray = array(
                            		        "paper_id" => $flgIn,
                            		        "question_id" => $qentry,
                            		        "name" => "$lnewfile",
                            		        "image_type" => "question_image",
                            		        "qtype" => $qtype,
                            		        );
                            		        $db->insertAry("assignment_question_files", $keytermArray);
                            		}
                            }
                	}


			if($qtype==1 || $qtype==3){ // Single choice and fill in the blanks
            	for($x=1; $x<=4; $x++){
                if(isset($_FILES["upload_option$i$x"]["name"]) && !empty($_FILES["upload_option$i$x"]["name"])){
                    $countfiles = count($_FILES["upload_option$i$x"]["name"]);
                        $questionimage = array();
                        for($j=0;$j<$countfiles;$j++){
                    		$lfilename = basename($_FILES["upload_option$i$x"]["name"][$j]);
                    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                    		$lnewfile=md5(microtime()).".".$lext;
                    		if(move_uploaded_file($_FILES["upload_option$i$x"]["tmp_name"][$j],"uploads/questionimages/".$lnewfile))
                        		{
                        		    $optionImageArray = array(
                        		        "paper_id" => $flgIn,
                        		        "question_id" => $qentry,
                        		        "name" => "$lnewfile",
                        		        "image_type" => "option_image",
                        		        "option_no" => $x,
                        		        "qtype" => $qtype,
                        		        );
                        		        $db->insertAry("assignment_question_files", $optionImageArray);
                        		}
                        }
                	}
        		}
            }


			if($qtype==2){
				for($x=1; $x<=4; $x++){
                    if(isset($_FILES["upload_option$i$x"]["name"]) && !empty($_FILES["upload_option$i$x"]["name"])){
                        $countfiles = count($_FILES["upload_option$i$x"]["name"]);
                            $questionimage = array();
                            for($j=0;$j<$countfiles;$j++){
                        		$lfilename = basename($_FILES["upload_option$i$x"]["name"][$j]);
                        		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                        		$lnewfile=md5(microtime()).".".$lext;
                        		if(move_uploaded_file($_FILES["upload_option$i$x"]["tmp_name"][$j],"uploads/questionimages/".$lnewfile))
                            		{
                            		    $optionImageArray = array(
                            		        "paper_id" => $flgIn,
                            		        "question_id" => $qentry,
                            		        "name" => "$lnewfile",
                            		        "image_type" => "option_image",
                            		        "option_no" => $x,
                            		        "qtype" => $qtype,
                            		        );
                            		        $db->insertAry("assignment_question_files", $optionImageArray);
                            		}
                            }
                	}
				}
			}
			
			
			if($qtype==7 || $qtype==8){
    	        if(isset($_FILES["upload_solution$i"]["name"]) && !empty($_FILES["upload_solution$i"]["name"])){
                    $countfiles = count($_FILES["upload_solution$i"]["name"]);
                        $questionimage = array();
                        for($j=0;$j<$countfiles;$j++){
                    		$lfilename = basename($_FILES["upload_solution$i"]["name"][$j]);
                    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                    		$lnewfile=md5(microtime()).".".$lext;
                    		if(move_uploaded_file($_FILES["upload_solution$i"]["tmp_name"][$j],"uploads/questionimages/".$lnewfile))
                        		{
                        		    $keytermArray = array(
                        		        "paper_id" => $flgIn,
                        		        "question_id" => $qentry,
                        		        "name" => "$lnewfile",
                        		        "image_type" => "solution_image",
                        		        "qtype" => $qtype,
                        		        );
                        		        $db->insertAry("assignment_question_files", $keytermArray);
                        		}
                        }
            	}
			}


                }
    			    
    			}

        }

$subject = $db->getVal("select subject from subject_new where id='".$_POST['parent_subject']."' and type='subject' ");
$chapter = $db->getVal("select subject from subject_new where id='".$_POST['chapter']."' and type='chapter' ");

$session = $_SESSION['admin']['session'];
$class = $_POST['class'];
$set_title = "Assignment Notice.";
$msg = "Dear Parents, A new paper of $subject in chapter $chapter has been uploaded.";

// Enabling error reporting
error_reporting(-1);
ini_set('display_errors', 'On');

 $upOne = realpath(__DIR__ . '/..');

 $upOne2 = realpath(__DIR__ . '/..'.'api_new/firebase/push.php');

require_once  $upOne.'/api_new/firebase/firebase.php';
require_once  $upOne.'/api_new/firebase/push.php';

$firebase = new Firebase();
$push = new Push();

$tokenlist=$db->getRows("SELECT idno,name,fcm_token from student where class='$class' and session='$session' and status='1' ");

$result_arr = array();

foreach ($tokenlist as $value) {
        $stu_id =  $value['idno'];
        $target_user_name =  $value['name'];
        $fb_token =  $value['fcm_token'];

        if (strlen($fb_token) > 0 ) {
            // optional payload
            $payload = array();
            $payload['name'] = $target_user_name;
            $payload['msg'] = $msg; // whether to include to image or not
            $payload['stu_id'] = $stu_id;
            $include_image = TRUE;
            $push->setTitle($set_title);
            $push->setMessage($msg);
            if ($include_image) {
                $push->setImage('https://api.androidhive.info/images/minion.jpg');
            } else {
                $push->setImage('');
            }
            $push->setIsBackground(FALSE);
            $push->setPayload($payload);
            $json = '';
            $response = '';
            $json = $push->getPush();
       
            $response = $firebase->send($fb_token, $json);
            
            array_push($result_arr,$response);
            
        }
}



        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'])!='')
	{

        $paperArray = array(
                "per_que_marks" => $_POST["per_que_marks"],
                "level" => $_POST["level"],
                "paper_description" => $_POST["paper_description"],
                "paper_status" => $_POST["paper_status"],
                "livedate" => $_POST["livedate"],
                "livetime" => $_POST["livetime"],                
                "duration" => $_POST["duration"],
                "total_questions" => $_POST["total_questions"],
            );
 	    $db->updateAry("assignment_paper", $paperArray, "where id='".$_GET['id']."' and session='".$_SESSION["admin"]["session"]."' ");

	    
 	    foreach($_POST['totalquestions'] as $qid){


        if(isset($_FILES["upload_question$qid"]["name"]) && !empty($_FILES["upload_question$qid"]["name"])){
            $countfiles = count($_FILES["upload_question$qid"]["name"]);
                $questionimage = array();
                for($i=0;$i<$countfiles;$i++){
            		$lfilename = basename($_FILES["upload_question$qid"]["name"][$i]);
            		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
            		$lnewfile=md5(microtime()).".".$lext;
            		if(move_uploaded_file($_FILES["upload_question$qid"]["tmp_name"][$i],"uploads/questionimages/".$lnewfile))
                		{
                		    $keytermArray = array(
                		        "paper_id" => $_GET['id'],
                		        "question_id" => $qid,
                		        "name" => "$lnewfile",
                		        "image_type" => "question_image",
                		        "qtype" => $_POST['question_type'.$qid],
                		        );
                		        $db->insertAry("assignment_question_files", $keytermArray);
                		}
                }
    	}

			$option=array();
			$m_options=array();
			$m_ans=array();
			$answerO=array();

			$quemark = "";
			$quesession = "";

            $question = $_POST['question'.$qid];

 			$qtype = $_POST['question_type'.$qid];

			if($qtype==1 || $qtype==3){ // Single choice and fill in the blanks
            	for($x=1; $x<=4; $x++){
            		$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$qid.'_'.$x]);

                if(isset($_FILES["upload_option$qid$x"]["name"]) && !empty($_FILES["upload_option$qid$x"]["name"])){
                    $countfiles = count($_FILES["upload_option$qid$x"]["name"]);
                        $questionimage = array();
                        for($i=0;$i<$countfiles;$i++){
                    		$lfilename = basename($_FILES["upload_option$qid$x"]["name"][$i]);
                    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                    		$lnewfile=md5(microtime()).".".$lext;
                    		if(move_uploaded_file($_FILES["upload_option$qid$x"]["tmp_name"][$i],"uploads/questionimages/".$lnewfile))
                        		{
                        		    $keytermArray = array(
                        		        "paper_id" => $_GET['id'],
                        		        "question_id" => $qid,
                        		        "name" => "$lnewfile",
                        		        "image_type" => "option_image",
                        		        "option_no" => $x,
                        		        "qtype" => $_POST['question_type'.$qid],
                        		        );
                        		        $db->insertAry("assignment_question_files", $keytermArray);
                        		}
                        }
            	}

    		}
            	$ans=$_POST['a_section_'.$qid];
            	$answerO[]=$ans;
            // 	$answerO[]=$_POST['o_section_'.$qid.'_'.$ans];

            }


			if($qtype==2){
				for($x=1; $x<=4; $x++){
					$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$qid.'_'.$x]);
					
                    if(isset($_FILES["upload_option$qid$x"]["name"]) && !empty($_FILES["upload_option$qid$x"]["name"])){
                        $countfiles = count($_FILES["upload_option$qid$x"]["name"]);
                            $questionimage = array();
                            for($i=0;$i<$countfiles;$i++){
                        		$lfilename = basename($_FILES["upload_option$qid$x"]["name"][$i]);
                        		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                        		$lnewfile=md5(microtime()).".".$lext;
                        		if(move_uploaded_file($_FILES["upload_option$qid$x"]["tmp_name"][$i],"uploads/questionimages/".$lnewfile))
                            		{
                            		    $keytermArray = array(
                            		        "paper_id" => $_GET['id'],
                            		        "question_id" => $qid,
                            		        "name" => "$lnewfile",
                            		        "image_type" => "option_image",
                            		        "option_no" => $x,
                            		        "qtype" => $_POST['question_type'.$qid],
                            		        );
                            		        $db->insertAry("assignment_question_files", $keytermArray);
                            		}
                            }
                	}					
					
					}
				$ans=$_POST['a_section_'.$qid];
				$answerO=array();
				foreach ($ans as $key) {
					$answerO[] = $key;
				}
			}


			if($qtype==4){ // True False
				$true = array("type"=>"text","answer"=>"True");
				$false = array("type"=>"text","answer"=>"False");
				$option = array($true,$false);
				$answerO[]=$_POST['a_section_'.$qid];
			}
	
			if($qtype==5){ // Match the following
				for($x=1; $x<=4; $x++){
					$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$qid.'_'.$x]);
					$m_options[] = array("type"=>"text","answer"=>$_POST['m_section_'.$qid.'_'.$x]);
					$answerO[]=$_POST['m_ans'.$qid.$x];
					}
			}

			if($qtype==6){ // Match the following
				for($x=1; $x<=4; $x++){
					$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$qid.'_'.$x]);
					$m_options[] = array("type"=>"text","answer"=>$_POST['m_section_'.$qid.'_'.$x]);
					$answerO[] =$_POST['m_ans'.$qid.$x];
					}
			}

            $hint = $_POST['hint'.$qid];

			if($qtype==7 || $qtype==8){
					$hint = [];
			        $quemark = $_POST['quemark'.$qid];
			        $quesession = $_POST['quesession'.$qid];					
					
        	        if(isset($_FILES["upload_solution$qid"]["name"]) && !empty($_FILES["upload_solution$qid"]["name"])){
                        $countfiles = count($_FILES["upload_solution$qid"]["name"]);
                            $questionimage = array();
                            for($i=0;$i<$countfiles;$i++){
                        		$lfilename = basename($_FILES["upload_solution$qid"]["name"][$i]);
                        		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                        		$lnewfile=md5(microtime()).".".$lext;
                        		if(move_uploaded_file($_FILES["upload_solution$qid"]["tmp_name"][$i],"uploads/questionimages/".$lnewfile))
                            		{
                            		    $keytermArray = array(
                            		        "paper_id" => $_GET['id'],
                            		        "question_id" => $qid,
                            		        "name" => "$lnewfile",
                            		        "image_type" => "solution_image",
                            		        "qtype" => $_POST['question_type'.$qid],
                            		        );
                            		        $db->insertAry("assignment_question_files", $keytermArray);
                            		}
                            }
                	}

			}


            $solution = $_POST['solution'.$qid];


        				
			$aryData=array(
			    'id'        => $qid,
			    'paper_id'  => $_GET['id'],
			    'question'  => $question,
			    'options'	=> json_encode($option, false),
			    'm_options' => json_encode($m_options),
			    'answer'	=> json_encode($answerO),
			    'hint'      => $hint,
			    'solution'  => $solution,
			    'quemark'   => $quemark,
			    'quesession'=> $quesession,
                'qtype'     => $qtype,			    
 				'added_by'  => $_SESSION['admin']['uid'],
				'school_id' => $_SESSION['admin']['school_id'],
				'session'   => $_SESSION['admin']['session'],
			);

            $db->delete("assignment_questions", "where id='".$qid."' and paper_id='".$_GET['id']."' ");

			$qentry = $db->insertAry("assignment_questions", $aryData, "where id='".$qid."' and paper_id='".$_GET['id']."' " );


 	    }
	    
        if(!is_null($_GET['id']))
		{
			$_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=="addmore" && isset($_GET['id']) && trim($_GET['id'])!='')
	{

        $paperArray = array(
                "per_que_marks" => $_POST["per_que_marks"],
                "level" => $_POST["level"],
                "paper_description" => $_POST["paper_description"],
                "paper_status" => $_POST["paper_status"],
                "livedate" => $_POST["livedate"],
                "livetime" => $_POST["livetime"],
                "duration" => $_POST["duration"],
                "total_questions" => $_POST["total_questions"],
            );
 	    $db->updateAry("assignment_paper", $paperArray, "where id='".$_GET['id']."' and session='".$_SESSION["admin"]["session"]."' ");

	    
		if($_POST['total_questions']>0){

        for($i=0;$i<=($_POST['total_questions']);$i++){

			$option=array();
			$m_options=array();
			$m_ans=array();
			$answerO=array();

            $quemark = '';
            $quesession = '';

            $question = $_POST['question'.$i];

			$qtype = $_POST['question_type'.$i];                    

			if($qtype==1 || $qtype==3){ // Single choice and fill in the blanks

            	for($x=1; $x<=4; $x++){
            		$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$i.'_'.$x]);
            		}

            	$ans=$_POST['a_section_'.$i];
            	$answerO[]=$ans;

            }

			if($qtype==2){

				for($x=1; $x<=4; $x++){
					$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$i.'_'.$x]);
					}
				$ans=$_POST['a_section_'.$i];

				$answerO=array();
				foreach ($ans as $key) {
					$answerO[] = $key;
//					$answerO[] =$_POST['o_section_'.$i.'_'.$key];
				}

			}


			if($qtype==4){ // True False
				$true = array("type"=>"text","answer"=>"True");
				$false = array("type"=>"text","answer"=>"False");
				$option = array($true,$false);
				$answerO[]=$_POST['a_section_'.$i];
			}

			if($qtype==5){ // Match the following
				for($x=1; $x<=4; $x++){
					$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$i.'_'.$x]);
					$m_options[] = array("type"=>"text","answer"=>$_POST['m_section_'.$i.'_'.$x]);
					$answerO[]=$_POST['m_ans'.$i.$x];
					}
			}

			if($qtype==6){ // Matrix matching
				for($x=1; $x<=4; $x++){
					$option[] = array("type"=>"text","answer"=>$_POST['o_section_'.$i.'_'.$x]);
					$m_options[] = array("type"=>"text","answer"=>$_POST['m_section_'.$i.'_'.$x]);
					$answerO[] =$_POST['m_ans'.$i.$x];
					}
			}

            $hint = $_POST['hint'.$i];

			if($qtype==7 || $qtype==8){
					$hint = [];
			        $quemark = $_POST['quemark'.$i];
			        $quesession = $_POST['quesession'.$i];
			}

            $solution = $_POST['solution'.$i];

			$aryData=array(
                'paper_id'  => $_GET['id'],
			    'question'  => $question,
			    'options'	=> json_encode($option),
			    'm_options' => json_encode($m_options),
			    'answer'	=> json_encode($answerO),
			    'hint'      => $hint,
			    'solution'  => $solution,
 				'added_by'  => $_SESSION['admin']['uid'],
				'school_id' => $_SESSION['admin']['school_id'],
				'session'   => $_SESSION['admin']['session'],
				'qtype'     => $qtype,
				'quemark'   => $quemark,
				'quesession'=> $quesession
			);


				$qentry = $db->insertAry("assignment_questions", $aryData);
				
				


                if(isset($_FILES["upload_question$i"]["name"]) && !empty($_FILES["upload_question$i"]["name"])){
                    $countfiles = count($_FILES["upload_question$i"]["name"]);
                    print_r($countfiles);
                        $questionimage = array();
                        for($j=0;$j<$countfiles;$j++){
                    		$lfilename = basename($_FILES["upload_question$i"]["name"][$j]);
                    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                    		$lnewfile=md5(microtime()).".".$lext;
                    		if(move_uploaded_file($_FILES["upload_question$i"]["tmp_name"][$j],"uploads/questionimages/".$lnewfile))
                        		{
                        		    $keytermArray = array(
                        		        "paper_id" => $_GET["id"],
                        		        "question_id" => $qentry,
                        		        "name" => "$lnewfile",
                        		        "image_type" => "question_image",
                        		        "qtype" => $qtype,
                        		        );
                        		        $db->insertAry("assignment_question_files", $keytermArray);
                        		}
                        }
            	}

			if($qtype==1 || $qtype==3){ // Single choice and fill in the blanks
            	for($x=1; $x<=4; $x++){
                if(isset($_FILES["upload_option$i$x"]["name"]) && !empty($_FILES["upload_option$i$x"]["name"])){
                    $countfiles = count($_FILES["upload_option$i$x"]["name"]);
                        $questionimage = array();
                        for($j=0;$j<$countfiles;$j++){
                    		$lfilename = basename($_FILES["upload_option$i$x"]["name"][$j]);
                    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                    		$lnewfile=md5(microtime()).".".$lext;
                    		if(move_uploaded_file($_FILES["upload_option$i$x"]["tmp_name"][$j],"uploads/questionimages/".$lnewfile))
                        		{
                        		    $optionImageArray = array(
                        		        "paper_id" => $_GET["id"],
                        		        "question_id" => $qentry,
                        		        "name" => "$lnewfile",
                        		        "image_type" => "option_image",
                        		        "option_no" => $x,
                        		        "qtype" => $qtype,
                        		        );
                        		        $db->insertAry("assignment_question_files", $optionImageArray);
                        		}
                        }
                	}
        		}
            }


			if($qtype==2){
				for($x=1; $x<=4; $x++){
                    if(isset($_FILES["upload_option$i$x"]["name"]) && !empty($_FILES["upload_option$i$x"]["name"])){
                        $countfiles = count($_FILES["upload_option$i$x"]["name"]);
                            $questionimage = array();
                            for($j=0;$j<$countfiles;$j++){
                        		$lfilename = basename($_FILES["upload_option$i$x"]["name"][$j]);
                        		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                        		$lnewfile=md5(microtime()).".".$lext;
                        		if(move_uploaded_file($_FILES["upload_option$i$x"]["tmp_name"][$j],"uploads/questionimages/".$lnewfile))
                            		{
                            		    $optionImageArray = array(
                            		        "paper_id" => $_GET["id"],
                            		        "question_id" => $qentry,
                            		        "name" => "$lnewfile",
                            		        "image_type" => "option_image",
                            		        "option_no" => $x,
                            		        "qtype" => $qtype,
                            		        );
                            		        $db->insertAry("assignment_question_files", $optionImageArray);
                            		}
                            }
                	}
				}
			}
			
			
			if($qtype==7 || $qtype==8){
    	        if(isset($_FILES["upload_solution$i"]["name"]) && !empty($_FILES["upload_solution$i"]["name"])){
                    $countfiles = count($_FILES["upload_solution$i"]["name"]);
                        $questionimage = array();
                        for($j=0;$j<$countfiles;$j++){
                    		$lfilename = basename($_FILES["upload_solution$i"]["name"][$j]);
                    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
                    		$lnewfile=md5(microtime()).".".$lext;
                    		if(move_uploaded_file($_FILES["upload_solution$i"]["tmp_name"][$j],"uploads/questionimages/".$lnewfile))
                        		{
                        		    $keytermArray = array(
                        		        "paper_id" => $_GET["id"],
                        		        "question_id" => $qentry,
                        		        "name" => "$lnewfile",
                        		        "image_type" => "solution_image",
                        		        "qtype" => $qtype,
                        		        );
                        		        $db->insertAry("assignment_question_files", $keytermArray);
                        		}
                        }
            	}
			}



            }
    			    
		}
		
		
		
$paper = $db->getRow("select * from assignment_paper where id='".$_GET['id']."' ");

$subject = $db->getVal("select subject from subject_new where id='".$paper['parent_subject']."' and type='subject' ");
$chapter = $db->getVal("select subject from subject_new where id='".$paper['chapter']."' and type='chapter' ");

$session = $_SESSION['admin']['session'];
$class = $paper['class'];
$set_title = "Assignment Notice.";
$msg = "Dear Parents, A new paper of $subject in chapter $chapter has been uploaded.";

// Enabling error reporting
error_reporting(-1);
ini_set('display_errors', 'On');

 $upOne = realpath(__DIR__ . '/..');

 $upOne2 = realpath(__DIR__ . '/..'.'api_new/firebase/push.php');

require_once  $upOne.'/api_new/firebase/firebase.php';
require_once  $upOne.'/api_new/firebase/push.php';

$firebase = new Firebase();
$push = new Push();

$tokenlist=$db->getRows("SELECT idno,name,fcm_token from student where class='$class' and session='$session' and status='1' ");

$result_arr = array();

foreach ($tokenlist as $value) {
        $stu_id =  $value['idno'];
        $target_user_name =  $value['name'];
        $fb_token =  $value['fcm_token'];

        if (strlen($fb_token) > 0 ) {
            // optional payload
            $payload = array();
            $payload['name'] = $target_user_name;
            $payload['msg'] = $msg; // whether to include to image or not
            $payload['stu_id'] = $stu_id;
            $include_image = TRUE;
            $push->setTitle($set_title);
            $push->setMessage($msg);
            if ($include_image) {
                $push->setImage('https://api.androidhive.info/images/minion.jpg');
            } else {
                $push->setImage('');
            }
            $push->setIsBackground(FALSE);
            $push->setPayload($payload);
            $json = '';
            $response = '';
            $json = $push->getPush();
       
            $response = $firebase->send($fb_token, $json);
            
            array_push($result_arr,$response);
            
        }
}
		
		

        if(!is_null($_GET['id']))
		{
			$_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=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
    $files = $db->getRows("select * from assignment_question_files where paper_id='".$_GET['id']."' ");

    if(count($files)>0){
            foreach($files as $file){
                    $img = $file['name'];

                    unlink("uploads/questionimages/$img");
    
            }
    }    
    
 	$res=$db->delete("assignment_paper","where id='".$_GET['id']."' ");
 	$res=$db->delete("assignment_questions","where paper_id='".$_GET['id']."' ");
  	$res=$db->delete("assignment_question_files","where paper_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_paper WHERE session='".$_SESSION["admin"]["session"]."' and id='".$_GET['id']."'");
}

if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !='') || ($pgAct=="addmore" && 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 Assignment</h3>
<div>
<!--<button type="button" onclick="printDiv('printDiv2')" class="btn btn-primary " style="float:right !important; margin-top:5px ">Print</button>-->
</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" name="myForm" onsubmit="return validateForm()" >
    
    <div class="control-group">
    <label class="control-label">Select Class</label>
      <div  class="controls">
      <select name="class" id="class" onchange="filterSubject()" style="border:solid 1px green;width:100%;" required>
      <option value="">--Select Class--</option>
      <?php $allClasses = $db->getRows("select * from class where school_id=1 and added_by=1 and status=1");
            foreach($allClasses as $class){
                ?>
                <option <?php if($class["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $class['class']; ?>"><?php echo $class['class']; ?></option>
                <?php
                }
            ?>
        </select>
      </div>
      </div>
      
    <div class="control-group">
    <label class="control-label">Subject</label>
      <div  class="controls" id="parent_subject">
        <select id="mySelect1" name="parent_subject"  style="border:solid 1px green;width:100%;"   onchange="changeSubject()" required>
            <option value="">--Select Subject--</option>
          <?php $allSubjects = $db->getRows("select * from subject_new where class='$aryForm[class]' and session='".$_SESSION["admin"]["session"]."' and type='subject' order by id ASC ");
                foreach($allSubjects as $sub){
                ?>
                <option <?php if($sub["id"]==$aryForm["parent_subject"])echo 'selected';?> value="<?php echo $sub['id']; ?>"><?php echo $sub['subject']; ?></option>
                <?php
                }
            ?>
        </select>
      </div>
    </div>
    
    <div class="control-group">
    <label class="control-label">Sub - Subject</label>
      <div  class="controls" id="sub_subject">
     <select name="sub_subject"  style="border:solid 1px green;width:100%;"  id="subSubjectid" onchange="filterChapter()" required>
            <option value="">--Select sub-Subject--</option>
            <?php $allsubSubjects = $db->getRows("select * from subject_new where parent_subject='".$aryForm["parent_subject"]."' and session='".$_SESSION["admin"]["session"]."' and type='sub_subject'  order by id ASC");
            foreach($allsubSubjects as $sub){
                ?>
                <option <?php if($sub["id"]==$aryForm["sub_subject"])echo 'selected'; ?> value="<?php echo $sub['id']; ?>"><?php echo $sub['subject']; ?></option>
                <?php
                }
            ?>
        </select>
      </div>
    </div>
    
    <div class="control-group">
    <label class="control-label">Chapter</label>
      <div  class="controls" id="chapter_div">
        <select id="mySelect" name="chapter"  style="border:solid 1px green;width:100%;" required>
            <option value="">--Select Chapter--</option>
            <?php $allChapters = $db->getRows("select * from subject_new where sub_subject='".$aryForm["sub_subject"]."' and session='".$_SESSION["admin"]["session"]."' and type='chapter'  order by id ASC ");
            foreach($allChapters as $chapter){
                ?>
                <option <?php if($chapter["id"]==$aryForm["chapter"])echo 'selected'; ?> value="<?php echo $chapter['id']; ?>"><?php echo $chapter['subject']; ?></option>
                <?php
                }
            ?>            
        </select>
      </div>
    </div>
 

    <div class="control-group" id="text" style="display:;">
    <label class="control-label">Assignments & <br> Worksheet</label>
      <div  class="controls">
            <table class="">
                <tr>
                    <td>Per Question Marks</td>
                    <td><input type="text" name="per_que_marks" value="<?php echo $aryForm['per_que_marks']; ?>" style="border:solid 1px red;width:600px;" required/></td>
                </tr>
                <tr>
                    <td>Level Name</td>
                    <td><input type="text" name="level" value="<?php echo $aryForm['level']; ?>"  style="border:solid 1px red;width:600px;"/></td>
                </tr>
                <tr>
                    <td>Paper Description</td>
                    <td><input type="text" name="paper_description" value="<?php echo $aryForm['paper_description']; ?>"  style="border:solid 1px red;width:600px;"/></td>
                </tr>
                <tr>
                    <td>Status</td>
                    <td>
                        <select name="paper_status" style="border:solid 1px red;width:620px;">
                            <option value="Active" <?php if($aryForm['paper_status']=='Active')echo "selected"; ?>>Active</option>
                            <option value="Inactive" <?php if($aryForm['paper_status']=='Inactive')echo "selected"; ?>>Inactive</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Live Date</td>
                    <td><input type="date" id="livedate" name="livedate" style="border:solid 1px red;width:610px;" value="<?php echo $aryForm['livedate']; ?>"/>
                        <a class="btn btn-danger" onclick="clearDate()">Clear Date</a>
                    </td>
                </tr>
                <tr>
                    <td>Live Time</td>
                    <td><input type="time" id="livetime" name="livetime" style="border:solid 1px red;width:610px;" value="<?php echo $aryForm['livetime']; ?>"/>
                        <a class="btn btn-danger" onclick="clearTime()">Clear Time</a>
                    </td>
                </tr>
                <tr>
                    <td>Paper Time <span style="color:black;font-weight:600;">(Minutes)</span></td>
                    <td>
                        <input type="number" style="border:solid 1px red;width:610px;font-size:18px;" name="duration" value="<?php echo $aryForm['duration']; ?>"  min="1" required/>
                    </td> 
                </tr>
                <tr>
                    <td>Total Questions</td>
                    <td><?php if($pgAct=='edit'){
                    $tq = $db->getVal("select count(id) from assignment_questions where paper_id = '".$_GET["id"]."' and session='".$_SESSION["admin"]["session"]."' ");
                    ?>
                        <input type="text" id="totalque" name="total_questions" value="<?php echo $tq; ?>" <?php if($pgAct=='edit')echo "readonly"; ?> style="border:solid 1px red;width:600px;" />
                        <?php } else { ?>
                            <input type="text" id="totalque" name="total_questions" value="" onkeyup="open_question_design()" style="border:solid 1px red;width:600px;" required/>
                        <?php } ?>
                    </td>
                </tr>
                <tr>
                    <td>Paper Type</td>
                    <td>
                        <select id="quemode" name="paper_type" onchange="open_question_design()" style="border:solid 1px red;width:625px;" <?php if($pgAct=='edit' || $pgAct=='addmore')echo "disabled"; ?> required>
                            <!--<option value="">--Select Type--</option>-->
                            <option value="1" <?php if($aryForm['paper_type']==1)echo "selected"; ?> >Objective Type</option>
                            <option value="2" <?php if($aryForm['paper_type']==2)echo "selected"; ?> >Subjective Type</option>
                            <option value="3" <?php if($aryForm['paper_type']==3)echo "selected"; ?> >NCERT Type</option>
                        </select>
                    </td>
                </tr>
            </table>
      </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]."' ");

        $solutionFiles = $db->getRows("select * from assignment_question_files where paper_id='".$aryForm[id]."' and question_id='".$qid."' and image_type='solution_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>

            <?php if(($question[qtype] != 7) && ($question[qtype] != 8))	{ ?>

            <div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> (Check correct Answers from all)
            <?php
			if($question[qtype]==1 || $question[qtype]==3)	{

                $answer=json_decode($question["answer"], true);
				$correctans = $answer[0];

    			$option=json_decode($question["options"],true);
    			if(is_array($option) && count($option)>0){$a=0;
    				foreach($option as $op){$a++;
    					?><div class="input-group">
    			     <div style="display:inline-block;width:70%;">
    				<span class="input-group-addon">
    				  <input value="<?= $a; ?>" type="radio" name="a_section_<?php echo $qid; ?>" class="ans_radio" <?php if($a==$correctans)echo 'checked'; ?> >
    				</span>
                        <span class="input-group-addon"><?= $a ?></span>
                        <input type="text"  name="o_section_<?php echo $qid.'_'.$a; ?>" value="<?php echo $op["answer"] ?>" placeholder="Enter Answer" style="width:88%;border:solid 1px green;display:inline-block;">
                    </div>
                    <div style="display:inline-block;width:29%;margin-left:75%;float:right;margin-top:-30px;">
                    <?php if(count($optionFiles)>0){
                        foreach($optionFiles as $optionimage){
                            if($optionimage['option_no']==$a){
                            echo "<div style='width:40px;height:40px;border:solid 2px red;display:inline-block;margin:5px;margin-top:-5px;'><a href='uploads/questionimages/$optionimage[name]' download><img src='uploads/questionimages/$optionimage[name]' style='width:40px;height:40px;'></a> <a href='javascript:void()' style='color:brown;font-size:11px;' onclick='deletefile($optionimage[id])'>Delete</a> </div>";
                            } 
                        }
                    }
                    ?>
                            <div id="option_preview<?php echo $qid.$a; ?>" style="display:inline-block;margin-top:-20px;"></div>
                            <input type="file" style="display:inline-block;width:100px;" onchange="preview_option('<?php echo $qid.$a; ?>')" id="upload_option<?php echo $qid.$a; ?>" name="upload_option<?php echo $qid.$a; ?>[]" multiple />
                    </div>
    			</div> <?php
    				}
    			}
		    }


			if($question[qtype]==2)	{
                $correctans = json_decode($question["answer"]);

    			$option=json_decode($question["options"],true);
    			if(is_array($option) && count($option)>0){$a=0;
    				foreach($option as $op){$a++;
    					?><div class="input-group">
    			     <div style="display:inline-block;width:70%;">
    				<span class="input-group-addon">
    				  <input value="<?= $a; ?>" type="checkbox" name="a_section_<?php echo $qid; ?>[]" class="ans_radio" <?php if(in_array($a, $correctans) )echo 'checked="checked"'; ?>>
    				</span>
                        <span class="input-group-addon"><?= $a ?></span>
                        <input type="text" class="form-control"  name="o_section_<?php echo $qid.'_'.$a?>" value="<?php echo $op["answer"]; ?>" placeholder="Enter Answer" style="width:88%;border:solid 1px green;display:inline-block;">
                    </div>
                    <div style="display:inline-block;width:29%;margin-left:75%;float:right;margin-top:-30px;">
                    <?php if(count($optionFiles)>0){
                        foreach($optionFiles as $optionimage){
                            if($optionimage['option_no']==$a){
                            echo "<div style='width:40px;height:40px;border:solid 2px red;display:inline-block;margin:5px;margin-top:-5px;'><a href='uploads/questionimages/$optionimage[name]' download><img src='uploads/questionimages/$optionimage[name]' style='width:40px;height:40px;'></a> <a href='javascript:void()' style='color:brown;font-size:11px;' onclick='deletefile($optionimage[id])'>Delete</a> </div>";
                            } 
                        }
                    }
                    ?>                        
                            <div id="option_preview<?php echo $qid.$a; ?>" style="display:inline-block;margin-top:-20px;"></div>
                            <input type="file" style="display:inline-block;width:150px;" onchange="preview_option('<?php echo $qid.$a; ?>')" id="upload_option<?php echo $qid.$a; ?>" name="upload_option<?php echo $qid.$a; ?>[]" multiple />
                    </div>

    			</div> <?php
    				}
    			}

		    }
		    
		    
			if($question[qtype]==4)	{
				$correctans = json_decode($question["answer"],true);
				$option=json_decode($question["options"],true);
				if(is_array($option) && count($option)>0){$a=0;
					foreach($option as $op){$a++;
						?><div class="input-group">
					<span class="input-group-addon">
					  <input value="<?= $op["answer"]?>" type="radio" name="a_section_<?php echo $qid; ?>" class="ans_radio" <?php if(in_array($op["answer"],$correctans,TRUE))echo 'checked="checked"'; ?>>
					</span>
                    <span class="input-group-addon"><?= $a ?></span>
                        <input type="text" class="form-control"  name="o_section_<?php echo $qid; ?>" value="<?php echo $op["answer"] ?>"  readonly style="width:70%;border:solid 1px green;display:inline-block;">
				</div> <?php
					}
				}
			}
		    
		    
			if($question[qtype]==5)	{
			 
 			$que = (json_decode($question["options"],true));
    
    		$moption = (json_decode($question["m_options"],true));
    
    		$marray = array();
    		foreach ($moption as $key ) {
    			$marray[] = $key[answer];
    		}
    
    		$correctans = (json_decode($question["answer"],true));
    
    		$k=0; $l=1;
    		echo '<table class="table-condensed"><tr><td colspan="2" style="text-align:center;">Column A</td><td colspan="2" style="text-align:center;">Column B</td><td colspan="2">Choose correct ans</td></tr>';
    		foreach($que as $aa){
    			
    			echo '<tr>';
    
    			echo '<td><i class="fa fa-circle"></i></td><td><textarea  class="form-control" name="o_section_'.$qid.'_'.$l.'" style="border:solid 1px green;">'.$aa[answer].'</textarea></td>';
    			echo '<td>'.$l.'</td><td><textarea  class="form-control" name="m_section_'.$qid.'_'.$l.'" style="border:solid 1px green;">'.$marray[$k].'</textarea></td>'; ?>
    			<td>
    				<span style="display:inline-block;"> 1<br>
    				<input type="radio" value="1" name="m_ans<? echo $qid.$l; ?>" <?php if($correctans[$k]==1)echo 'checked="checked"'; ?> /></span>
    
    				<span style="display:inline-block;"> 2<br>
    				<input type="radio" value="2"  name="m_ans<? echo $qid.$l; ?>"  <?php if($correctans[$k]==2)echo 'checked="checked"'; ?> /></span>
    
    				<span style="display:inline-block;"> 3<br>
    				<input type="radio" value="3"  name="m_ans<? echo $qid.$l; ?>"  <?php if($correctans[$k]==3)echo 'checked="checked"'; ?> /></span>
    
    				<span style="display:inline-block;"> 4<br>
    				<input type="radio" value="4" name="m_ans<? echo $qid.$l; ?>"  <?php if($correctans[$k]==4)echo 'checked="checked"'; ?> /></span>
    
    				<!--<span style="display:inline-block;"> 5<br>-->
    				<!--<input type="radio" value="5" name="m_ans<? echo $qid.$l; ?>"  <?php if($correctans[$k]==5)echo 'checked="checked"'; ?> /></span>-->
    			</td>
    		<?php
    			echo '</tr>';
    			$l++; $k++;
    		}
    		echo '</table>';

			}
		    
		if($question[qtype]==6)	{

    		$que = (json_decode($question["options"],true));

			$moption = (json_decode($question["m_options"],true));

			$marray = array();
			foreach ($moption as $key ) {
				$marray[] = $key[answer];
			}

			$correctans = (json_decode($question["answer"],true));

			$k=0; $l=1;
			echo '<table class="table-condensed"><tr><td colspan="2" style="text-align:center;">Column A</td><td colspan="2" style="text-align:center;">Column B</td><td colspan="2">Choose correct ans</td></tr>';
			foreach($que as $aa){
				
				echo '<tr>';

				echo '<td><i class="fa fa-circle"></i></td><td><textarea  class="form-control"  name="o_section_'.$qid.'_'.$l.'"  style="border:solid 1px green;">'.$aa[answer].'</textarea></td>';
				echo '<td>'.$l.'</td><td><textarea class="form-control" name="m_section_'.$qid.'_'.$l.'" style="border:solid 1px green;">'.$marray[$k].'</textarea></td>'; ?>
				<td>
					<span style="display:inline-block;"> 1<br>
					<input type="checkbox" value="1" name="m_ans<? echo $qid.$l; ?>[]" <?php if(in_array("1",$correctans[$k]))echo 'checked="checked"'; ?> /></span>

					<span style="display:inline-block;"> 2<br>
					<input type="checkbox" value="2" name="m_ans<? echo $qid.$l; ?>[]"  <?php if(in_array("2",$correctans[$k]))echo 'checked="checked"'; ?> /></span>

					<span style="display:inline-block;"> 3<br>
					<input type="checkbox" value="3" name="m_ans<? echo $qid.$l; ?>[]"  <?php if(in_array("3",$correctans[$k]))echo 'checked="checked"'; ?> /></span>

					<span style="display:inline-block;"> 4<br>
					<input type="checkbox" value="4" name="m_ans<? echo $qid.$l; ?>[]"  <?php if(in_array("4",$correctans[$k]))echo 'checked="checked"'; ?> /></span>

					<!--<span style="display:inline-block;"> 5<br>-->
					<!--<input type="checkbox" value="5" name="m_ans<? echo $qid.$l; ?>[]"  <?php if(in_array("5",$correctans[$k]))echo 'checked="checked"'; ?> /></span>-->
				</td>
			<?php
				echo '</tr>';
				$l++; $k++;
			}
			echo '</table>';

		}

?>

    		 </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>
    		 <?php } else { ?>
    		 <div class="control-group"> 
    		 <label class="control-label">Solution</label> 
    		     <div class="controls">
    		        <!--<textarea name="solution<?php echo $qid; ?>" style="width:70%;border:solid 1px red;height:100px;"><?php echo $question["solution"]; ?></textarea> -->
    		        
    		                        <div style="width:93%;display:inline-block;">
                    <textarea name="solution<?php echo $qid; ?>" style="width:70%;border:solid 1px red;height:100px;"><?php echo $question["solution"]; ?></textarea> 
                </div>
    		        
                <!--<div style="width:25%;display:inline-block;">-->
                <div style="display:inline-block;width:29%;margin-left:75%;float:right;margin-top:-110px;">
                    <?php
                        if(count($solutionFiles)>0){
                        foreach($solutionFiles 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_solution('<?php echo $qid; ?>')" id="upload_solution<?php echo $qid; ?>" name="upload_solution<?php echo $qid; ?>[]" multiple /> </div>
                <div id="solution_preview<?php echo $qid; ?>"  style="display:inline-block;"></div>
 
                </div>
    		        
    		     </div> 
		     </div>    		 
    		 <?php } ?>

    		 </div>  </div>
        <?php
   
    }
   
}


?>

    <div id="questions_design">


    </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 Questions</button>
    </div>
    <div class="well no-padding">
        
        <div style="z-index: 99999;margin: 30px;">

        <table>
            <tr>
                <td>
                    <label class="control-label">Select Class</label>
                    <select name="class" id="class" style="width:100%;" onchange="filterSubject()">
                      <option value="">--Select Class--</option>
                      <?php $allClasses = $db->getRows("select * from class where school_id=1 and added_by=1 and status=1");
                        foreach($allClasses as $class){
                            ?>
                            <option <?php if($class["class"]==$aryForm["class"])echo 'selected';?> value="<?php echo $class['class']; ?>"><?php echo $class['class']; ?></option>
                            <?php
                            }
                        ?>
                    </select>
                </td>
                <td>
                    <label class="control-label">Subject</label>
                    <div  class="controls" id="parent_subject">

                    <select id="mySelect1" name="parent_subject" style="width:100%;"  onchange="changeSubject()">
                        <option value="">--Select Subject--</option>
                      <?php $allSubjects = $db->getRows("select * from subject_new where class='$aryForm[class]' and session='".$_SESSION["admin"]["session"]."' and type='subject' order by id ASC ");
                            foreach($allSubjects as $sub){
                            ?>
                            <option <?php if($sub["id"]==$aryForm["parent_subject"])echo 'selected';?> value="<?php echo $sub['id']; ?>"><?php echo $sub['subject']; ?></option>
                            <?php
                            }
                        ?>
                    </select>  
                    </div>
                </td>
                <td>
                    <label class="control-label">Sub - Subject</label>
                          <div  class="controls" id="sub_subject">

                    <select name="sub_subject" style="width:100%;" id="subSubjectid" onchange="filterChapter()">
                        <option value="">--Select sub-Subject--</option>
                        <?php $allsubSubjects = $db->getRows("select * from subject_new where parent_subject='".$aryForm["parent_subject"]."' and session='".$_SESSION["admin"]["session"]."' and type='sub_subject'  order by id ASC");
                        foreach($allsubSubjects as $sub){
                            ?>
                            <option <?php if($sub["id"]==$aryForm["sub_subject"])echo 'selected'; ?> value="<?php echo $sub['id']; ?>"><?php echo $sub['subject']; ?></option>
                            <?php
                            }
                        ?>
                    </select>
                    </div>
                </td>
                <td>
                    <label class="control-label">Chapter</label>
                          <div  class="controls" id="chapter_div">

                    <select id="chapter" name="chapter" style="width:100%">
                        <option value="">--Select Chapter--</option>
                        <?php $allChapters = $db->getRows("select * from subject_new where sub_subject='".$aryForm["sub_subject"]."' and session='".$_SESSION["admin"]["session"]."' and type='chapter'  order by id ASC ");
                        foreach($allChapters as $chapter){
                            ?>
                            <option <?php if($chapter["id"]==$aryForm["chapter"])echo 'selected'; ?> value="<?php echo $chapter['id']; ?>"><?php echo $chapter['subject']; ?></option>
                            <?php
                            }
                        ?>            
                    </select>
                    </div>
                </td>
                <td>
                    <label></label>
                    <button class="btn" style="margin-top:20px;font-size:15px;padding:3px 8px;" onclick="filterRecord()">Search</button>
                </td>
            </tr>
        </table>

    </div>
    
    <div id="filterdata">
	<?php
	$aryPgAct=array("src"=>"Notice", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM assignment_paper "); 
	    $i = 0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th style="width:50px;">S.No.</th>
                    <th>Class</th>
                    <th>Subject</th>
                    <th>Sub-subjects</th>
                    <th>Chapters</th>
                    <th>Details</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["class"]; ?></td>
                    <td><?php echo $subject = $db->getVal("select subject from subject_new where id = '".$clist["parent_subject"]."' and type='subject' ") ?></td>
                    <td><?php echo $sub_subject = $db->getVal("select subject from subject_new where id = '".$clist["sub_subject"]."' and type='sub_subject' ") ?></td>
                    <td><?php echo $chapter = $db->getVal("select subject from subject_new where id = '".$clist["chapter"]."' and type='chapter' ") ?></td>
                    <td>
                        Total Questions : <?php echo $totalquestions = $db->getVal("select count(id) from assignment_questions where paper_id = '".$clist["id"]."' and session='".$_SESSION["admin"]["session"]."' ") ?>
                        <br><span style="color:crimson;">
                        Paper Type:
                        <?php
                            if($clist['paper_type']==1)echo "Objective";
                            if($clist['paper_type']==2)echo "Subjective";
                            if($clist['paper_type']==3)echo "NCERT";
                        ?>
                        </span>
                        <br><span style="color:green;">
                            Paper Status:
                            <?php 
                                echo $clist['paper_status'];
                            ?>
                        </span>
                    </td>
                    <td>
                    <?php $aryPgAct['action']="addmore"; ?>
                     <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-primary">Add More</button></a>                        
                    <?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>
                     
                     <a href="<?php echo URL_ROOT; ?>print/?page_id=paper&id=<?php echo $clist['id']; ?>" target="_blank" class="btn btn-info">Print</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="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script>
function filterSubject(){

   	var classname = document.getElementById("class").value;

    $.post( "Notice/ajax.php", { type: "filter_subject" ,classname: classname } , function( data ) {
        document.getElementById("parent_subject").innerHTML = data;
	});

}

function changeSubject(){
    var subject = document.getElementById("mySelect1").value;
//    alert(subject);
    $.post( "Notice/ajax.php", { type: "filterSubSubject" ,subject: subject } , function( data ) {
        document.getElementById("sub_subject").innerHTML = data;
	});
    
}

function filterChapter(){
        var subject = document.getElementById("subSubjectid").value;
//    alert(subject);
    $.post( "Notice/ajax.php", { type: "filterChapter" ,subject: subject } , function( data ) {
        document.getElementById("chapter_div").innerHTML = data;
	});
	
}


function showhideAssignment() {
  var checkBox = document.getElementById("myCheck");
  
  if (checkBox.checked == true){
    document.getElementById("text").style.display = "block";
    document.getElementById("questions_design").style.display = "block";
  } else {
    document.getElementById("text").style.display = "none";
    document.getElementById("questions_design").style.display = "none";
  }
}

function open_question_design(){
get_question_session();
    var questions = document.getElementById('totalque').value;
    var type = document.getElementById('quemode').value;

    var datas='';
    if((questions>0) && (type==1)){

		for(i=1; i<=questions;i++){

//			datas +='<div class="control-group" style="border:solid 1px teal"> <div style="background:#0052CC;color:white;padding:8px;font-size:14px;">Question '+i+'  &emsp;<input type="radio" name="question_type'+i+'" value="1" onclick="change_question_type(this.value,'+i+')" checked/>Single Choice &emsp;<input type="radio" name="question_type'+i+'" value="2" onclick="change_question_type(this.value,'+i+')" />Multiple Choice&emsp;<input type="radio" name="question_type'+i+'" value="3" onclick="change_question_type(this.value,'+i+')"/>Fill in the blanks&emsp;<input type="radio" name="question_type'+i+'" value="4" onclick="change_question_type(this.value,'+i+')" />True False&emsp;</td><input type="radio" name="question_type'+i+'" value="5" onclick="change_question_type(this.value,'+i+')"/>Match the following&emsp;<input type="radio" name="question_type'+i+'" value="6" onclick="change_question_type(this.value,'+i+')"/>Matrix Match&emsp;<input type="radio" name="question_type'+i+'" value="7" onclick="change_question_type(this.value,'+i+')"/>Subjective</div>';
			datas +='<div class="control-group" style="border:solid 1px teal"> <div style="background:#0052CC;color:white;padding:8px;font-size:14px;">Question '+i+'  &emsp;<input type="radio" name="question_type'+i+'" value="1" onclick="change_question_type(this.value,'+i+')" checked/>Single Choice &emsp;<input type="radio" name="question_type'+i+'" value="2" onclick="change_question_type(this.value,'+i+')" />Multiple Choice&emsp;<input type="radio" name="question_type'+i+'" value="3" onclick="change_question_type(this.value,'+i+')"/>Fill in the blanks&emsp;<input type="radio" name="question_type'+i+'" value="4" onclick="change_question_type(this.value,'+i+')" />True False&emsp;</td><input type="radio" name="question_type'+i+'" value="5" onclick="change_question_type(this.value,'+i+')"/>Match the following&emsp;<input type="radio" name="question_type'+i+'" value="6" onclick="change_question_type(this.value,'+i+')"/>Matrix Match </div>';
			
            datas += '<div class="panel-body"  id="questiontype'+i+'"><div class="control-group"> <label class="control-label"> Question '+i+' </label> <div class="controls"> <textarea name="question'+i+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;float:right;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+i+')" id="upload_question'+i+'" name="upload_question'+i+'[]" multiple /> </div>  <div id="question_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

            datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> (Check correct Answers from all)';

                for(var j=1;j<=4;j++){
                    datas+='<div class="input-group"><div style="display:inline-block;width:70%"><span class="input-group-addon"><input value="'+j+'" type="radio" name="a_section_'+i+'" class="ans_radio"></span> <span class="input-group-addon">'+j+'</span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+i+'_'+j+'" value="" placeholder="Enter Answer"></div> <div style="display:inline-block;width:29%"> <div id="option_preview'+i+j+'" style="display:inline-block;"></div>  <input type="file" style="display:inline-block;width:150px;" onchange="preview_option('+i+j+')" id="upload_option'+i+j+'" name="upload_option'+i+j+'[]" multiple /> </div> </div>';
                }

    		 datas +='</div> </div>';
    
    		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+i+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
    
    		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+i+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
    
    		 datas += '</div>  </div>';

			}

    }
    
    if((questions>0) && (type==2)){

		for(i=1; i<=questions;i++){

    			datas +='<div class="control-group" style="border:solid 1px teal"> <div style="background:#0052CC;color:white;padding:8px;font-size:14px;">Question '+i+' &emsp;<input type="radio" name="question_type'+i+'" value="7" onclick="change_question_type(this.value,'+i+')" style="display:none;" checked/>Subjective &emsp;&emsp;&emsp;&emsp;<input type="radio" name="quemark'+i+'" value="1" checked> 1 Mark &emsp;<input type="radio" name="quemark'+i+'" value="2"> 2 Mark &emsp;<input type="radio" name="quemark'+i+'" value="3"> 3 Mark &emsp;<input type="radio" name="quemark'+i+'" value="5"> 5 Mark &emsp;&emsp; <div style="display:inline-block;" ><select  name="quesession'+i+'" class="quesession"></select></div> </div>';

                 datas += '<div class="panel-body"  id="questiontype'+i+'"><div class="control-group"> <label class="control-label"> Question '+i+' </label> <div class="controls"> <textarea name="question'+i+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;"></textarea> <div style="width:28%;display:inline-block;float:right;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+i+')" id="upload_question'+i+'" name="upload_question'+i+'[]" multiple /> </div>  <div id="question_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+i+'" style="width:68%;border:solid 1px red;height:100px;"> </textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_solution('+i+')" id="upload_solution'+i+'" name="upload_solution'+i+'[]" multiple /> </div> <div id="solution_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '</div>';

    		 datas += '</div>  </div>';

			}

    }
    
    if((questions>0) && (type==3)){

		for(i=1; i<=questions;i++){

    			datas +='<div class="control-group" style="border:solid 1px teal"> <div style="background:#0052CC;color:white;padding:8px;font-size:14px;">Question '+i+' &emsp;<input type="radio" name="question_type'+i+'" value="8" onclick="change_question_type(this.value,'+i+')" style="display:none;" checked/>NCERT &emsp;&emsp;&emsp;&emsp;<input type="radio" name="quemark'+i+'" value="1" checked> 1 Mark &emsp;<input type="radio" name="quemark'+i+'" value="2"> 2 Mark &emsp;<input type="radio" name="quemark'+i+'" value="3"> 3 Mark &emsp;<input type="radio" name="quemark'+i+'" value="5"> 5 Mark &emsp;&emsp; <div style="display:inline-block;" > <select  name="quesession'+i+'" class="quesession"></select> </div> </div>';

                 datas += '<div class="panel-body"  id="questiontype'+i+'"><div class="control-group"> <label class="control-label"> Question '+i+' </label> <div class="controls"> <textarea name="question'+i+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;"></textarea> <div style="width:28%;display:inline-block;float:right;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+i+')" id="upload_question'+i+'" name="upload_question'+i+'[]" multiple /> </div>  <div id="question_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+i+'" style="width:68%;border:solid 1px red;height:100px;"> </textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_solution('+i+')" id="upload_solution'+i+'" name="upload_solution'+i+'[]" multiple /> </div> <div id="solution_preview'+i+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '</div>';

    		 datas += '</div>  </div>';

			}

    }    
    
        document.getElementById("questions_design").innerHTML = datas;
}

		function change_question_type(qtype, que){
		    
		    var datas = '';
		    
			if(qtype==1 || qtype==3){

                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls">(Check correct Answers from all) ';

                for(var j=1;j<=4;j++){
                    datas+='<div class="input-group"><div style="display:inline-block;width:70%"><span class="input-group-addon"><input value="'+j+'" type="radio" name="a_section_'+que+'" class="ans_radio"></span> <span class="input-group-addon">'+j+'</span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'_'+j+'" value="" placeholder="Enter Answer"></div> <div style="display:inline-block;width:29%"> <div id="option_preview'+que+j+'" style="display:inline-block;"></div>  <input type="file" style="display:inline-block;width:150px;" onchange="preview_option('+que+j+')" id="upload_option'+que+j+'" name="upload_option'+que+j+'[]" multiple /> </div> </div>';
                }

        		 datas +='</div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
        
        		 datas += '</div>';			    
			}

            if(qtype==2){
                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';
    
                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls">(Check correct Answers from all) ';

                for(var j=1;j<=4;j++){
                    //datas+='<div class="input-group"><span class="input-group-addon"><input value="'+j+'" type="checkbox" name="a_section_'+que+'[]" class="ans_radio" ></span> <span class="input-group-addon">'+j+'</span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'_'+j+'" value="" placeholder="Enter Answer"></div>';
                    datas+='<div class="input-group"><div style="display:inline-block;width:70%"><span class="input-group-addon"><input value="'+j+'" type="checkbox" name="a_section_'+que+'[]" class="ans_radio"></span> <span class="input-group-addon">'+j+'</span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'_'+j+'" value="" placeholder="Enter Answer"></div> <div style="display:inline-block;width:29%"> <div id="option_preview'+que+j+'" style="display:inline-block;"></div>  <input type="file" style="display:inline-block;width:150px;" onchange="preview_option('+que+j+')" id="upload_option'+que+j+'" name="upload_option'+que+j+'[]" multiple /> </div> </div>';
                }

        		 datas +='</div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
        
        		 datas += '</div>';			    
    			
            }
            
            
            if(qtype==4){
                
                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';
    
                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> ';
    

                datas+='<div class="input-group">(Check correct Answers from all)<br><span class="input-group-addon"><input value="True" type="radio" name="a_section_'+que+'" class="ans_radio" ></span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'" value="TRUE" placeholder="Enter Answer" readonly /></div>';
                datas +='<div class="input-group"><span class="input-group-addon"><input value="False" type="radio" name="a_section_'+que+'" class="ans_radio" ></span> <input type="text" style="width:92%;border:solid 1px green;" class="form-control" name="o_section_'+que+'" value="FALSE" placeholder="Enter Answer" readonly /></div>';


        		 datas +='</div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
        
        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
        
        		 datas += '</div>';			    
    			
            }
            
            
            if(qtype==5){

                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> <table class="table-condensed"><tr><td colspan="2" style="padding-left:100px;">Column A</td><td colspan="2"  style="padding-left:100px;">Column B</td><td  style="padding-left:40px;">Choose correct answer</td></tr>';

                for(var j=1;j<=4;j++){
   					datas += '<tr>';
					datas += '<td><label style="height:10px;width:10px;background:#000;border-radius:50%;"></label></td> <td><textarea name="o_section_'+que+'_'+j+'" rows="3" cols="30" class="form-control" style="border:solid 1px green;"></textarea></td>';
					datas += '<td>&emsp;&emsp;'+j+'</td><td><textarea  class="form-control" name="m_section_'+que+'_'+j+'"  rows="3" cols="30" style="display:inline-block;border:solid 1px green;"></textarea></td>';
					datas += '<td>&emsp;&emsp;&emsp;&emsp;<span style="display:inline-block;"> 1<br> <input type="radio" name="m_ans'+que+j+'" value="1" /></span> <span style="display:inline-block;">2<br> <input type="radio" name="m_ans'+que+j+'" value="2" /></span> <span style="display:inline-block;">3<br> <input type="radio" name="m_ans'+que+j+'" value="3" /></span> <span style="display:inline-block;"> 4<br> <input type="radio" name="m_ans'+que+j+'" value="4" /></span> </td>';
					datas += '</tr>';

                }

    		 datas +='</table></div> </div>';
    
    		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';
    
    		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"> </textarea> </div> </div>';
    
    		 datas += '</div>';
			
            }
            
            
            if(qtype==6){

                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

                datas += '<div class="control-group"> <label class="control-label"> Answer </label> <div class="controls"> <table class="table-condensed"><tr><td colspan="2" style="padding-left:100px;">Column A</td><td colspan="2"  style="padding-left:100px;">Column B</td><td  style="padding-left:40px;">Choose correct answer</td></tr>';

                for(var j=1;j<=4;j++){
   					datas += '<tr>';
					datas += '<td><label style="height:10px;width:10px;background:#000;border-radius:50%;"></label></td> <td><textarea name="o_section_'+que+'_'+j+'" rows="3" cols="30" class="form-control" style="border:solid 1px green;"></textarea></td>';
					datas += '<td>&emsp;&emsp;'+j+'</td><td><textarea  class="form-control" name="m_section_'+que+'_'+j+'"  rows="3" cols="30" style="display:inline-block;border:solid 1px green;"></textarea></td>';
					datas += '<td>&emsp;&emsp;&emsp;&emsp;<span style="display:inline-block;"> 1<br> <input type="checkbox" name="m_ans'+que+j+'[]" value="1" /></span> <span style="display:inline-block;">2<br> <input type="checkbox" name="m_ans'+que+j+'[]" value="2"/></span> <span style="display:inline-block;">3<br> <input type="checkbox" name="m_ans'+que+j+'[]" value="3"/></span> <span style="display:inline-block;"> 4<br> <input type="checkbox" name="m_ans'+que+j+'[]" value="4" /></span> </td>';
					datas += '</tr>';

                }

        		 datas +='</table></div> </div>';
    
        		 datas += '<div class="control-group"> <label class="control-label">Hint</label> <div class="controls"><input type="text" name="hint'+que+'" style="width:95%;border:solid 1px red;" /> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:95%;border:solid 1px red;"></textarea> </div> </div>';
    
        		 datas += '</div>';

            }
            
            
			if(qtype==7){
                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:68%;border:solid 1px red;height:100px;"> </textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_solution('+que+')" id="upload_solution'+que+'" name="upload_solution'+que+'[]" multiple /> </div> <div id="solution_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';
        		 datas += '</div>';
			}

			if(qtype==8){
                datas += '<div class="panel-body"  id="questiontype'+que+'"><div class="control-group"> <label class="control-label"> Question '+que+' </label> <div class="controls"> <textarea name="question'+que+'" style="width:68%;height:100px;border:solid 1px red;display:inline-block;" ></textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_question('+que+')" id="upload_question'+que+'" name="upload_question'+que+'[]" multiple /> </div>  <div id="question_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';

        		 datas += '<div class="control-group"> <label class="control-label">Solution</label> <div class="controls"><textarea name="solution'+que+'" style="width:68%;border:solid 1px red;height:100px;"> </textarea> <div style="width:28%;display:inline-block;"> <div style="width:100%;margin-top:0px;">Images <input type="file" onchange="preview_solution('+que+')" id="upload_solution'+que+'" name="upload_solution'+que+'[]" multiple /> </div> <div id="solution_preview'+que+'"  style="display:inline-block;"></div> </div> </div> </div>';
        		 datas += '</div>';
			}

 
	        document.getElementById("questiontype"+que).innerHTML = datas;
		}
		
		
function deletequestion(qid){
//    alert(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();
	});
  }
}


function preview_question(id)
{
 var total_file=document.getElementById("upload_question"+id).files.length;
     for(var i=0;i<total_file;i++)
     {
      $('#question_preview'+id).append("<div style='display:inline-block;width:50px;height:50px;margin:1px;border:solid 2px red;'><img src='"+URL.createObjectURL(event.target.files[i])+"' style='width:50px;height:50px;'></div>");
     }
}

function preview_option(n)
{
 var total_file=document.getElementById("upload_option"+n).files.length;
     for(var i=0;i<total_file;i++)
     {
      $('#option_preview'+n).append("<div style='display:inline-block;width:40px;height:40px;margin:5px;border:solid 2px red;margin-top:-10px;'><img src='"+URL.createObjectURL(event.target.files[i])+"' style='width:40px;height:40px;'></div>");
     }
}


function preview_solution(id)
{
 var total_file=document.getElementById("upload_solution"+id).files.length;
     for(var i=0;i<total_file;i++)
     {
      $('#solution_preview'+id).append("<div style='display:inline-block;width:40px;height:40px;margin:1px;border:solid 2px red;'><img src='"+URL.createObjectURL(event.target.files[i])+"' style='width:40px;height:40px;'></div>");
     }
}

function deletefile(id){
  var r = confirm("Are you sure want to delete?");
  if (r == true) {    
    $.post( "Notice/ajax.php", { type: "delete_question_file" ,file_id:id} , function( data ) {
        location.reload();
    });
    }
}



function filterRecord(){
 var classname = document.getElementById("class").value;
 
 var subject = document.getElementById("mySelect1").value;
  //alert(classname);

 var subsubject = document.getElementById("subSubjectid").value;  
 var chapter = document.getElementById("chapter").value;   

    $.post( "Notice/ajax.php", { type: "filterRecord_question" ,classname: classname ,subject: subject ,subsubject: subsubject , chapter: chapter} , function( data ) {
//alert(data);
  document.getElementById('filterdata').innerHTML = data;
    });
}



function get_question_session(){
    $.post( "Notice/ajax.php", { type: "get_question_session"}, function( data ) {
        $('.quesession').html(data);
    });
}


function validateForm() {
  var x = document.forms["myForm"]["total_questions"].value;
  var status = 0;
  for(i=1;i<=x;i++){ 
        var que = document.forms["myForm"]["question"+i+""].value;
        var qtype = document.forms["myForm"]["question_type"+i+""].value;
        if(qtype==1 || qtype==3 || qtype==4){
        var ans = document.forms["myForm"]["a_section_"+i+""].value;
            if(que !="" && ans==""){
                status = 1;
                alert('Please select an option in Question No. '+i+' ');
                break;
            }
        }
  }

  if (status == 1) {
    return false;
  }
  else return true;
}

function clearDate(){
    document.getElementById("livedate").value = "";
}

function clearTime(){
    document.getElementById("livetime").value = "";
}

</script>
