Moodle php code : To get user quiz attempt detail

get user quiz attempt question wise detail as max mark, response, question type status, question etc


// parameter $cmid
// paramerter $user object

$cm = get_coursemodule_from_id('quiz', $cmid);
$quizobj = quiz::create($cm->instance);
$quiz = $quizobj->get_quiz();

$attempts = quiz_get_user_attempts($quiz->id, $user->id);
    // for last attempt, you can also loop through this
    $attempt = array_pop($attempts);

    $attempt = (array)$attempt;
    $attempt['timetaken'] = format_time($attempt['timefinish']-$attempt['timestart']);
    $result['attempt'] = $attempt;

    $attemptobj = quiz_create_attempt_handling_errors($attempt['id'], $cmid);
    $sections = [];
$questions = [];
    foreach ($attemptobj->get_slots('all') as $slot) {
        $question = array(
            'slot' => $slot,
            'type' => $attemptobj->get_question_type_name($slot),
            'page' => $attemptobj->get_question_page($slot),
            'mark'=>$attemptobj->get_question_mark($slot),
            'maxmark'=> $attemptobj->get_question_attempt($slot)->get_max_mark(),
            'status' => $attemptobj->get_question_status($slot, 1),
            'response' => $attemptobj->get_question_attempt($slot)->get_response_summary($slot),
            'number'=> $attemptobj->get_question_number($slot),
            'heading'=>$attemptobj->get_heading_before_slot($slot),
            'question_name'=>$attemptobj->get_question_attempt($slot)->get_question()->name,
            'question'=>$attemptobj->get_question_attempt($slot)->get_question()->get_question_summary(),
            
        );
$questions[] = $question[];

Leave a Reply

Your email address will not be published. Required fields are marked *