<?php
$sdata = array();
//echo $total_question;
if (isset($_SESSION['pageload']) && $_SESSION['pageload'] < $total_question) {
    $_SESSION['pageload'] = $_SESSION['pageload'] + 1;
    $i = $_SESSION['pageload'];
} else {
    $_SESSION['pageload'] = 0;
    $i = $_SESSION['pageload'];
    $this->session->unset_userdata('pre_q');
}

$min = 0;
$max = $total_question - 1;
if ($quiz_info->quiz_random_static == 1) {
    if ($total_question > $quiz_info->total_number_of_question) {
        $quiz_ques_limit = $quiz_info->total_number_of_question;
    } else {
        $quiz_ques_limit = count($question);
    }
} else {
    $quiz_ques_limit = $total_question;
}

if ($total_question >= $quiz_ques_limit) {
    $limit = $quiz_ques_limit;
} else {
    $limit = $total_question;
}


if ($i > $limit - 1) {
    $this->session->unset_userdata('pre_q');
//echo $limit;
//echo $i;
//exit();
    $str_slug = url_title("tt quiz result", 'dash', TRUE);
    redirect("public_course_quiz/quiz_result");
}

if ($quiz_info->quiz_random_static == 1) {
    $q = rand($min, $max);
    $pre_q = '';
    $pre_q = $this->session->userdata('pre_q'); //    
//        echo '<pre>';
//        print_r($min);
//        print_r($max);
//        print_r($q);
//        print_r($pre_q);
//        echo '</pre>';
//        exit();
    while (strstr($pre_q, $q . '.')) {
        $q = rand($min, $max);
    }

    $sdata['pre_q'] = $pre_q . $q . '.';
    $this->session->set_userdata($sdata);

    $pre_question = $this->session->userdata('pre_question');
    $sdata['pre_question'] = $pre_question . $question[$q]->question_id . '.';
    $this->session->set_userdata($sdata);
} else {
    $q = $i;
    $pre_q = '';
    $pre_q = $this->session->userdata('pre_q');
    $sdata['pre_q'] = $pre_q . $q . '.';
    $this->session->set_userdata($sdata);

    $pre_question = $this->session->userdata('pre_question');
    $sdata['pre_question'] = $pre_question . $question[$q]->question_id . '.';
    $this->session->set_userdata($sdata);
}
?>
<div id="tutorial_question" class="col-sm-11 col-sm-offset-1">     
    <!--<h1>Quiz</h1>-->
    <!--<form method="post" role="form">-->
    <div class="col-sm-12 ">
        <div class="row ">
            <form id="formID7" name="quizQuestion" action="javascript:void(0)" 
                  method="post" name="userForm" enctype="multipart/form-data">

                <input type="hidden" name="quiz_question_time"  id="quiz_question_time" value="<?php echo $question[$q]->time_limit * 60; ?>" >
                <input type="hidden" name="points_per_answer" id="points_per_answer" value="<?php echo $question[$q]->points_per_answer ?>" >
                <input type="hidden" name="question_type" id="question_type" value="<?php echo $question[$q]->question_type ?>" >
                <input type="hidden" name="question_id" id="question_id" value="<?php echo $question[$q]->question_id ?>" >

                <input type="hidden" name="quiz_running" id="quiz_running" value="<?php echo $this->session->userdata('running_quiz_status'); ?>" >
                <input type="hidden" name="question_number" id="question_number" value="<?php echo $q; ?>" >

                <input type="hidden" name="quiz_question_time" id="quiz_question_time" value="30" >

                <div class="col-sm-12 padding-zero"><br></div>

                <div class="col-sm-12">

                    <div class="col-sm-8  padding-zero text-left">

                        <div class="col-sm-12 text-left" style="padding: 0 0 5px 20px;">
                            <h4> <?php echo ($i + 1) . '.'; ?>
                                <?php
                                if ($question[$q]->question_type == 4) {

                                    $ques = $question[$q]->question_details;
                                    $ques_mod = str_replace("_", "  ______  ", $ques);
                                    echo $ques_mod;
                                } else {
                            echo $this->utility_model->view_tinyMce_information($creator_info->person_id, $quiz_url, $question[$q]->question_details);
                                    //echo $question[$q]->question_details;
                                }
                                ?>
                            </h4>
                        </div>

                        <div class="col-sm-12">  <hr><br>  </div>

                        <div class="col-sm-12 text-left" style="padding: 0 0 5px 20px;">
                            <div class="form-group col-sm-12">
                                <?php if ($question[$q]->question_type == 3) { ?>
                                    <!--<br/>-->
                                    <h4>Write Your Answer:</h4>
                                    <textarea data-errormessage-value-missing="Please Type Your Descriptive Answer!" 
                                              class="form-control validate[required] text-input"
                                              name='desc_answer' id='desc_answer' class='col-sm-8'></textarea>
                                          <?php } elseif ($question[$q]->question_type == 4) { ?>
                                    <br/><h4>Write Your Answer:</h4>
                                    <textarea data-errormessage-value-missing="Please Type Your Descriptive Answer!" 
                                              class="form-control validate[required] text-input"
                                              name='fill_answer' id='fill_answer' class='col-sm-7' rows='1'></textarea>
                                          <?php } else {
                                              ?>

                                    <div  class="radio radio-success" style="margin: 0">
                                        <?php
                                        foreach ($options[$question[$q]->question_id] as $v_option) {
                                            echo '<h4>';
                                            $v = $v_option->question_option_id;
                                            ?>

                                            <?php
                                            $c_answer = $v_option->question_option;
//                                            $c_answer = str_replace('<', '&lt', $c_answer);
//                                            $c_answer = str_replace('>', '&gt', $c_answer);
//                                        echo $c_answer;
                                            ?>

                                            <input  type="radio" 
                                                    data-errormessage-value-missing="Please Select Answer!" 
                                                    class="form-control validate[required] text-input"
                                                    name='status[]' id='status'   value='<?php echo $v; ?>'>
                                            <label for="free">
                                                <?php
                                                echo $this->utility_model->view_tinyMce_information($creator_info->person_id, $quiz_url, $c_answer);
                                               // echo ' ' . $c_answer;
                                                echo '</h4>';
                                                ?>
                                            </label>
                                            <input type='hidden' name='option_id[]' id="option_id" value='<?php echo $v_option->question_option_id; ?>'>
                                        <?php } ?>
                                    </div>
                                    <?php
                                }

                                $time = $question[$q]->time_limit * 60;

                                $min = $question[$q]->time_limit;
                                ?> 
                            </div>

                            <input type='hidden' name='taken_time' id="taken_time" value=''>

                        </div>

                        <div class="form-group col-sm-12  text-center"> 

                            <span><br><br></span>
                            <span>

                                <?php
                                if ($pre_question == 'need for our next upgrade') {
                                    $questions = explode('.', $pre_question);
                                    array_pop($questions);
                                    $first_question = $questions[0];
                                    $previous_question = $questions[count($questions) - 1];
                                    ?>

                                    <a href="javascript:void()" onclick="firstQuestion('<?php echo $questions[0]; ?>')" ><i class="fa fa-backward" title="first"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                    <a href="javascript:void()" onclick="previousQuestion('<?php echo $previous_question; ?>')"><i class="fa  fa-arrow-left" title="Previous"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <?php } ?>

                                <a href="javascript:void()" onclick="check_validation();"><i class="fa  fa-arrow-right" title="Next"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                <a href="javascript:void()" onclick="getElapsedTime()"><i class="fa  fa-toggle-down" title="Skip"></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                    <a href="javascript:void()" onclick="showResult()"> <i class="fa   fa-forward" title="Finish"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                            </span>
                            <span><br></span>

                        </div>

                        <div class="form-group col-sm-3 "> </div>
                        <div class="col-sm-12"><br></div>
                    </div>

                    <div class="col-sm-4 padding-zero">
                        <div class="row">
                            <div class="col-sm-12"><br><br><br></div>
                            <div id="countdown">
                                <canvas id="countdown360_countdown" width="150" height="150">
                                    <span id="countdown-text" role="status" aria-live="assertive"></span>
                                </canvas>
                            </div>

                        </div>
                    </div>
                </div>

            </form>

        </div>
    </div>

</div>
