コード例 #1
0
/**
 * 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);
}
コード例 #2
0
/**
 * Get the time when user finished a quiz
 *
 * @since 0.9.6
 *
 * @param int $quiz_id
 * @param int $user_id
 *
 * @return int
 */
function learn_press_get_quiz_end_time($quiz_id, $user_id = null)
{
    if (!$user_id) {
        $user_id = get_current_user_id();
    }
    $quiz_start = learn_press_get_user_quiz_data('_lpr_quiz_completed', $user_id, $quiz_id);
    return apply_filters('learn_press_get_quiz_end_time', $quiz_start, $quiz_id, $user_id);
}