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); } } }