function submit_answer($quiz_id, $answer)
 {
     $questions = learn_press_get_question_answers(null, $quiz_id);
     if (!is_array($questions)) {
         $questions = array();
     }
     $questions[$quiz_id][$this->get('ID')] = is_array($answer) ? reset($answer) : $answer;
     learn_press_save_question_answer(null, $quiz_id, $this->get('ID'), is_array($answer) ? reset($answer) : $answer);
 }
/**
 * Get the information result of a quiz
 *
 * @param int $user_id
 * @param int $quiz_id
 * @return array
 */
function learn_press_get_quiz_result($user_id = null, $quiz_id = null)
{
    global $quiz;
    if (!$user_id) {
        $user_id = get_current_user_id();
    }
    $quiz_id = $quiz_id ? $quiz_id : ($quiz ? $quiz->ID : 0);
    $questions = learn_press_get_quiz_questions($quiz_id);
    $answers = learn_press_get_question_answers($user_id, $quiz_id);
    $mark = 0;
    $correct_questions = 0;
    $wrong_questions = 0;
    $empty_questions = 0;
    $quiz_start = learn_press_get_user_quiz_data('_lpr_quiz_start_time', $user_id, $quiz_id);
    $quiz_end = learn_press_get_user_quiz_data('_lpr_quiz_completed', $user_id, $quiz_id);
    $mark_total = learn_press_get_quiz_mark($quiz_id);
    $quiz_time = $quiz_end ? $quiz_end - $quiz_start : 0;
    $info = false;
    if ($questions) {
        foreach ($questions as $question_id => $options) {
            $ques_object = LPR_Question_Type::instance($question_id);
            if ($ques_object && isset($answers[$question_id])) {
                $check = $ques_object->check(array('answer' => $answers[$question_id]));
                if ($check['correct']) {
                    //$mark += $check['mark'];
                    $correct_questions++;
                } else {
                    $wrong_questions++;
                }
                $mark += isset($check['mark']) ? $check['mark'] : 0;
            } else {
                $empty_questions++;
            }
        }
        $question_count = count($questions);
        if (is_float($mark)) {
            $mark = round($mark, 1);
        }
        $info = array('mark' => $mark, 'correct' => $correct_questions, 'wrong' => $wrong_questions, 'empty' => $empty_questions, 'questions_count' => $question_count, 'mark_total' => round($mark_total, 2), 'mark_percent' => round($mark / $mark_total, 2), 'correct_percent' => round($correct_questions / $question_count * 100, 2), 'wrong_percent' => round($wrong_questions / $question_count * 100, 2), 'empty_percent' => round($empty_questions / $question_count * 100, 2), 'quiz_time' => $quiz_time);
    }
    return apply_filters('learn_press_get_quiz_result', $info, $user_id, $quiz_id);
}
function learn_press_show_answer()
{
    $quiz_id = !empty($_REQUEST['quiz_id']) ? intval($_REQUEST['quiz_id']) : 0;
    $question_id = !empty($_REQUEST['question_id']) ? intval($_REQUEST['question_id']) : 0;
    $question_answer = isset($_REQUEST['question_answer']) ? $_REQUEST['question_answer'] : null;
    $user_id = get_current_user_id();
    $json = array();
    ob_start();
    $ques = lpr_get_question($question_id);
    if ($ques) {
        $ques->submit_answer($quiz_id, $question_answer);
    }
    global $quiz;
    $user_id = get_current_user_id();
    $quiz_answers = learn_press_get_question_answers(null, $quiz_id);
    $answer = isset($quiz_answers[$question_id]) ? $quiz_answers[$question_id] : array();
    switch ($ques->get_type()) {
        case 'multi_choice':
            ?>
			<ul class="lpr-question-hint">
				<?php 
            if ($answers = $ques->get('options.answer')) {
                foreach ($answers as $k => $ans) {
                    $classes = array();
                    if (in_array($k, $answer)) {
                        if ($ques->get("options.answer.{$k}.is_true")) {
                            $classes[] = "correct";
                        } else {
                            $classes[] = "wrong";
                        }
                    } else {
                        if ($ques->get("options.answer.{$k}.is_true")) {
                            $classes[] = "correct";
                        }
                    }
                    ?>
					<li <?php 
                    echo $classes ? 'class="' . join(" ", $classes) . '"' : '';
                    ?>
>
						<label>
							<input type="checkbox"
								   disabled="disabled" <?php 
                    checked(in_array($k, $answer) ? 1 : 0);
                    ?>
 />
							<?php 
                    echo $ques->get("options.answer.{$k}.text");
                    ?>
						</label>
					</li>
				<?php 
                }
            }
            ?>
			</ul>
			<?php 
            break;
        case 'single_choice':
            ?>
			<ul class="lpr-question-hint">
				<?php 
            if ($answers = $ques->get('options.answer')) {
                foreach ($answers as $k => $ans) {
                    $classes = array();
                    if ($k == $answer) {
                        if ($ques->get("options.answer.{$k}.is_true")) {
                            $classes[] = "correct";
                        } else {
                            $classes[] = "wrong";
                        }
                    } else {
                        if ($ques->get("options.answer.{$k}.is_true")) {
                            $classes[] = "correct";
                        }
                    }
                    ?>
					<li <?php 
                    echo $classes ? 'class="' . join(" ", $classes) . '"' : '';
                    ?>
>
						<label>
							<input type="radio" disabled="disabled" <?php 
                    checked($k == $answer ? 1 : 0);
                    ?>
 />
							<?php 
                    echo $ques->get("options.answer.{$k}.text");
                    ?>
						</label>
					</li>
				<?php 
                }
            }
            ?>
			</ul>
			<?php 
            break;
        case 'true_or_false':
            ?>
			<ul class="lpr-question-hint">
				<?php 
            for ($k = 0; $k < 2; $k++) {
                $classes = array();
                if ($k == $answer) {
                    if ($ques->get("options.answer.{$k}.is_true")) {
                        $classes[] = "correct";
                    } else {
                        $classes[] = "wrong";
                    }
                } else {
                    if ($ques->get("options.answer.{$k}.is_true")) {
                        $classes[] = "correct";
                    }
                }
                ?>
					<li <?php 
                echo $classes ? 'class="' . join(" ", $classes) . '"' : '';
                ?>
>
						<label>
							<input type="radio" disabled="disabled" <?php 
                checked($answer == $k ? 1 : 0);
                ?>
 />
							<?php 
                echo $ques->get('options.answer.' . $k . '.text');
                ?>
						</label>
					</li>
					<?php 
            }
            ?>
			</ul>
			<?php 
            break;
        default:
            do_action('learn_press_question_suggestion_' . $ques->get_type(), $ques, $answer);
    }
    ?>
	<h4><?php 
    _e("Answer explanation", 'learn_press');
    ?>
</h4>
	<p><?php 
    echo $ques->get('options.explaination');
    ?>
</p>
	<?php 
    $json['html'] = ob_get_clean();
    wp_send_json($json);
    die;
}
    /**
     * Display the correct/wrong answers of a quiz in result page
     *
     * @param $question_id
     */
    function learn_press_quiz_hint($question_id)
    {
        global $quiz;
        $user_id = get_current_user_id();
        if (!learn_press_user_has_completed_quiz($user_id, $quiz->id) || !get_post_meta($quiz->id, '_lpr_show_quiz_result', true)) {
            return;
        }
        if ($ques = lpr_get_question($question_id)) {
            $quiz_answers = learn_press_get_question_answers(null, $quiz->id);
            $answer = isset($quiz_answers[$question_id]) ? $quiz_answers[$question_id] : array();
            switch ($ques->get_type()) {
                case 'multi_choice':
                    ?>
					<ul class="lpr-question-hint">
						<?php 
                    if ($answers = $ques->get('options.answer')) {
                        foreach ($answers as $k => $ans) {
                            $classes = array();
                            if (in_array($k, $answer)) {
                                if ($ques->get("options.answer.{$k}.is_true")) {
                                    $classes[] = "correct";
                                } else {
                                    $classes[] = "wrong";
                                }
                            } else {
                                if ($ques->get("options.answer.{$k}.is_true")) {
                                    $classes[] = "correct";
                                }
                            }
                            ?>
							<li <?php 
                            echo $classes ? 'class="' . join(" ", $classes) . '"' : '';
                            ?>
>
								<label>
									<input type="checkbox"
										   disabled="disabled" <?php 
                            checked(in_array($k, $answer) ? 1 : 0);
                            ?>
 />
									<?php 
                            echo $ques->get("options.answer.{$k}.text");
                            ?>
								</label>
							</li>
						<?php 
                        }
                    }
                    ?>
					</ul>
					<?php 
                    break;
                case 'single_choice':
                    ?>
					<ul class="lpr-question-hint">
						<?php 
                    if ($answers = $ques->get('options.answer')) {
                        foreach ($answers as $k => $ans) {
                            $classes = array();
                            if ($k == $answer) {
                                if ($ques->get("options.answer.{$k}.is_true")) {
                                    $classes[] = "correct";
                                } else {
                                    $classes[] = "wrong";
                                }
                            } else {
                                if ($ques->get("options.answer.{$k}.is_true")) {
                                    $classes[] = "correct";
                                }
                            }
                            ?>
							<li <?php 
                            echo $classes ? 'class="' . join(" ", $classes) . '"' : '';
                            ?>
>
								<label>
									<input type="radio" disabled="disabled" <?php 
                            checked($k == $answer ? 1 : 0);
                            ?>
 />
									<?php 
                            echo $ques->get("options.answer.{$k}.text");
                            ?>
								</label>
							</li>
						<?php 
                        }
                    }
                    ?>
					</ul>
					<?php 
                    break;
                case 'true_or_false':
                    ?>
					<ul class="lpr-question-hint">
						<?php 
                    for ($k = 0; $k < 2; $k++) {
                        $classes = array();
                        if ($k == $answer) {
                            if ($ques->get("options.answer.{$k}.is_true")) {
                                $classes[] = "correct";
                            } else {
                                $classes[] = "wrong";
                            }
                        } else {
                            if ($ques->get("options.answer.{$k}.is_true")) {
                                $classes[] = "correct";
                            }
                        }
                        ?>
							<li <?php 
                        echo $classes ? 'class="' . join(" ", $classes) . '"' : '';
                        ?>
>
								<label>
									<input type="radio" disabled="disabled" <?php 
                        checked($answer == $k ? 1 : 0);
                        ?>
 />
									<?php 
                        echo $ques->get('options.answer.' . $k . '.text');
                        ?>
								</label>
							</li>
							<?php 
                    }
                    ?>
					</ul>
					<?php 
                    break;
                default:
                    do_action('learn_press_question_suggestion_' . $ques->get_type(), $ques, $answer);
            }
        }
    }