<style>
/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
  -webkit-animation-name: fadeIn; /* Fade in the background */
  -webkit-animation-duration: 0.4s;
  animation-name: fadeIn;
  animation-duration: 0.4s
}

/* Modal Content */
.modal-content {
  position: fixed;
  /*bottom: 0;*/
  background-color: #fefefe;
  margin-left:18%;
  margin-top:50px;
  width: 900px;
  height:550px;
  padding:0px;
  -webkit-animation-name: slideIn;
  -webkit-animation-duration: 0.4s;
  animation-name: slideIn;
  animation-duration: 0.4s
}

/* The Close Button */
.close {
  color: white;
  float: right;
  font-size: 38px;
  font-weight: 900;
  margin-right:-50px;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.modal-header {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}

/* Add Animation */
@-webkit-keyframes slideIn {
  from {bottom: -300px; opacity: 0} 
  to {bottom: 0; opacity: 1}
}

@keyframes slideIn {
  from {bottom: -300px; opacity: 0}
  to {bottom: 0; opacity: 1}
}

@-webkit-keyframes fadeIn {
  from {opacity: 0} 
  to {opacity: 1}
}

@keyframes fadeIn {
  from {opacity: 0} 
  to {opacity: 1}
}

    @media print {
        #myPrntbtn {
            display :  none;
        }
    }

    .nav-link{
        background:red;
        color:white;
    }

</style>

<script>
    function printme(){
	window.print();
}
</script>


<?php
    $pid = $_GET['pid'];
    $uid = $_GET['idno'];

    $student = $db->getRow("select * from student where idno='$uid' "); 

    $attemptpaper = $db->getRow("select * from assignment_paper where id='$pid' ");
    
    $subject = $db->getVal("select subject from subject_new where id='$attemptpaper[parent_subject]' ");
    $chapter = $db->getVal("select subject from subject_new where id='$attemptpaper[chapter]' ");
    if(count($attemptpaper) > 0){

    $allquestions = $db->getRows("select * from assignment_questions where paper_id='$pid' ");

?>

<div class="tab-content">

<button onclick="printme()" id="myPrntbtn">Print</button>

    <div style="width:100%;text-align:center;">

        <img src="<?php echo $LinksDetails['logo']; ?>" style="width:200px;float:left;height:80px;"/>

        <h1><?php
            echo $LinksDetails['site_name']; ?>
        </h1>
        <label><?php
                echo $LinksDetails['contact_content']; ?>
        </label>

    </div>
<hr>
<table class="table" border="1" cellspacing="0" cellpadding="5" style="width:100%;">
    <tr>
        <th>Student Name</th>
        <td><?php echo $student['name']; ?></td>
        <th>Class</th>
        <td><?php echo $student['class']." - ".$student['section']; ?></td>
    </tr>
    <tr>
        <th>Subject</th>
        <td><?php echo $subject; ?></td>
        <th>Chapter</th>
        <td><?php echo $chapter; ?></td>
    </tr>
    <tr>
        <th>Admission No.</th>
        <td><?php echo $student['admissionno']; ?></td>
        <th>Session</th>
        <td><?php echo $student['session']; ?></td>
    </tr>
</table>

  <div class="tab-pane fade in active">

    <?php
            if(count($allquestions)>0 ){
            $i = 0;
            foreach($allquestions as $question){ $i++;
             $qtype = $question['qtype'];
             $questatus = '';

                $att_question = $db->getRow("select * from assignment_attempt_question where pid='$pid' and uid='$uid' and qid='$question[id]' ");
                ?>
                    <table class="" style="margin:20px 0px;border:solid 1px #e5e5e5;float:left;background:#fff0f7;width:100%;">
                        <tr>
                            <td style="width:100px;font-weight:bold;padding:5px;">Question.<?php echo $i; ?></td>
                            <td><?php echo $question['question']; ?></td>
                        </tr>

                        <?php if($_GET['type']=="subjective"){
                        $allimages = $db->getRows("select * from assignment_attempt_subj_files where pid='$pid' and qid='$question[id]' and uid='$uid'  ");
                        $correctans = $att_question['answer'];
                        ?>
                        <tr>
                            <td style="width:140px;font-weight:bold;padding:5px;">Answer:</td>
                            <td>
                                <?php
                                    echo $correctans;
                                ?>
                            </td>
                        </tr>

                        <tr>
                            <td></td>
                            <td>
                                <?php foreach($allimages as $image){?>
                                    <img src='../uploads/subjectiveexam/<?php echo $image[name]; ?>' onclick="showmodal('../uploads/subjectiveexam/<?php echo $image[name]; ?>')" style='width:200px;height:150px;margin:5px;' />
                                <?php
                                } ?>
                            </td>
                        </tr>
                        <?php }
                        
                        
                        if($_GET['type']=="objective"){
                            $allimages = $db->getRows("select * from assignment_question_files where paper_id='$pid' and question_id='$question[id]' ");
                            $correctans = json_decode($question['answer'],true);
                            
                                if(count($att_question) > 1){
                
                                    if($question[qtype] == 2){
                
                                        $givenans = json_decode($att_question['answer']);
                
                                        $multipleans = '';
                
                                        foreach($givenans as $ans){
                                            if(in_array($ans, $correctans) ){
                                                $multipleans = "Correct";
                                                $break;
                                            }
                                        }
                
                                        if($multipleans=="Correct"){
                                            $questatus = 1;
                                        }
                                        else {
                                            $questatus = 2;
                                        }
                
                                    }
                                    elseif($question[qtype] == 6){
                                        
                                        $matchingans = 0;
                                        
                                        $givenans = json_decode($att_question['answer']);
                                        
                                        $corrarr1 = $correctans[0];
                                        $corrarr2 = $correctans[1];
                                        $corrarr3 = $correctans[2];
                                        $corrarr4 = $correctans[3];
                                        
                                        $arr1 = $givenans[0][0];
                                        $arr2 = $givenans[1][0];
                                        $arr3 = $givenans[2][0];
                                        $arr4 = $givenans[3][0];
                
                                        if(in_array($arr1, $corrarr1) ){
                                            $matchingans = 1;
                                        }
                
                                        if(in_array($arr2, $corrarr2) ){
                                            $matchingans = 1;
                                        }
                
                                        if(in_array($arr3, $corrarr3) ){
                                            $matchingans = 1;
                                        }
                
                                        if(in_array($arr4, $corrarr4) ){
                                            $matchingans = 1;
                                        }
                                        
                                        if($matchingans==1){
                                            $questatus = 1;
                                        }
                                        else {
                                            $questatus = 2;
                                        }
                
                                    }
                                    
                                    else {
                                        if($question[answer]==$att_question[answer]){
                                            $questatus = 1;
                                        }
                                        else {
                                            $questatus = 2;
                                        }
                                    }
                
                                }
                                else {
                                    $questatus = 3;
                                }

                        ?>
                        <tr>
                            <td></td>
                            <td>
                                <?php foreach($allimages as $image){
                                    if($image['image_type'] == "question_image"){ ?>
                                        <img src='../uploads/questionimages/<?php echo $image[name]; ?>' onclick="showmodal('../uploads/questionimages/<?php echo $image[name]; ?>')" style='width:200px;height:150px;margin:5px;' />
                                <?php
                                    }
                                } ?>
                            </td>
                        </tr>

                        <tr>
                            <td>Options :</td>
                            <td>
                                <?php 
                                    if($qtype == 1 || $qtype == 2 || $qtype == 3 || $qtype == 4){
                                     $options= json_decode($question[options], true);
                                        $j = 0;
                                        foreach($options as $opt){ $j++;
                                            echo "<li style='display:block;'>$j. $opt[answer]</li>";
                                            foreach($allimages as $image){ 
                                                if($image['image_type'] == "option_image" && $image['option_no'] == $j){ ?>
                                                    <img src='../uploads/questionimages/<?php echo $image[name]; ?>' onclick="showmodal('../uploads/questionimages/<?php echo $image[name]; ?>')" style='width:200px;height:150px;margin:5px;' />
                                                    <?php
                                                }
                                            }
                                        }
                                    }
                                    else {
                                        $options= json_decode($question[options], true);
                                        $m_options = json_decode($question[m_options], true);
                                		$marray = array();
                                    		foreach ($m_options as $key ) {
                                    			$marray[] = $key[answer];
                                    		}
                                        $j = 0;$k=0;
                                        echo "<table class='table'><tr style='font-weight:bold;'><td>Column A</td><td>Column B</td>";
                                        foreach($options as $opt){ $j++;
                                            echo "<tr><td>$j. $opt[answer]</td><td>$j. $marray[$k]</td></tr>";
                                            $k++;
                                        }
                                        echo "</table>";
                                    }
                                ?>                                
                            </td>
                        </tr>
                        <tr>
                            <td style="width:140px;font-weight:bold;padding:5px;">Correct Answer:</td>
                            <td>
                                <?php 
                                    if($qtype == 6){
                                    foreach($correctans as $cans){
                                        echo json_encode($cans);
                                    }
                                } else{
                                    foreach($correctans as $cans){
                                        echo "$cans, ";
                                    }
                                }
                                ?>
                            </td>
                        </tr>

                        <tr>
                            <td style="width:140px;font-weight:bold;padding:5px;">Your Answer:</td>
                            <td>
                                <?php $yourans = json_decode($att_question['answer'],true);
                                if($qtype==6){
                                    foreach($yourans as $yans){
                                        echo json_encode($yans);
                                    }
                                    
                                } else{
                                    foreach($yourans as $yans){
                                        echo "$yans, ";
                                    }
                                }
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td style="width:140px;font-weight:bold;padding:5px;">Status:</td>
                            <td>
                                <?php 
                                    if($questatus == 1){
                                        echo "<h3 class='text-success'><i class='fa fa-check'></i> Correct</h3>";
                                    }
                                    if($questatus == 2){
                                        echo "<h3 class='text-danger'><i class='fa fa-times'></i> Wrong</h3>";
                                    }
                                    if($questatus == 3){
                                        echo "<h3 class='text-primary'>Skipped</h3>";
                                    }
                                ?>
                            </td>
                        </tr>

                        <?php } ?>
                    </table>
                <?php
            }
        }

    ?>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">

    <div class="modal-header">
      <span class="close">&times;</span>
    </div>
    <br>
    <div class="modal-body">
        <div id="modalimg"></div>
    </div>

  </div>

</div>



  </div>

</div>

<?php
    }
?>
 
 
 <script>
function showmodal(img){
    document.getElementById('modalimg').innerHTML = "<img src='"+img+"' style='width:100%; height:92%'>";
      modal.style.display = "block";
} 
// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
// btn.onclick = function() {
//   modal.style.display = "block";
// }


// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
</script>