Skip to content

PHP8 Fatal during Quiz submission; $answer is null. #7754

@dd32

Description

@dd32

Uncaught TypeError: Sensei\Internal\Quiz_Submission\Grade\Repositories\Comments_Based_Grade_Repository::create(): Argument # 2 ($answer) must be of type Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface, null given, called in wp-content/plugins/sensei-lms/includes/class-sensei-quiz.php on line 1128 and defined in wp-content/plugins/sensei-lms/includes/internal/quiz-submission/grade/repositories/class-comments-based-grade-repository.php:40

Complete Stack trace:

#0 wp-content/plugins/sensei-lms/includes/class-sensei-quiz.php(1128): Sensei\Internal\Quiz_Submission\Grade\Repositories\Comments_Based_Grade_Repository->create(Object(Sensei\Internal\Quiz_Submission\Submission\Models\Comments_Based_Submission), NULL, 257436, 1)
#1 wp-content/plugins/sensei-lms/includes/class-sensei-grading.php(1116): Sensei_Quiz->set_user_grades(Array, 252798, 23229854)
#2 wp-content/plugins/sensei-lms/includes/class-sensei-quiz.php(967): Sensei_Grading::grade_quiz_auto(252799, Array, 0, 'auto')
#3 wp-content/plugins/sensei-lms/includes/class-sensei-quiz.php(568): Sensei_Quiz::submit_answers_for_grading(Array, Array, 252798, 23229854)
#4 wp-includes/class-wp-hook.php(324): Sensei_Quiz->user_quiz_submit_listener('')
#5 wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#6 wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#7 wp-includes/template-loader.php(13): do_action('template_redire...')
#8 wp-blog-header.php(19): require_once('...')
#9 index.php(18): require('...')
#10 {main}

Looking at the POST payload that triggers it, I don't see anything incorrect, it appears that this is likely triggered from some incorrect data elsewhere in the stack, which ultimately causes the quiz taker to be presenting less answers than the quiz expects.

The relevant code that triggers the null value is line 1127 below

$answers = Sensei()->quiz_answer_repository->get_all( $submission->get_id() );
$answers_map = [];
foreach ( $answers as $answer ) {
$answers_map[ $answer->get_question_id() ] = $answer;
}
foreach ( $quiz_grades as $question_id => $points ) {
$answer = $answers_map[ $question_id ];
Sensei()->quiz_grade_repository->create( $submission, $answer, $question_id, $points );
}

Based on the quiz appearance, and the editor view, it appears this might occur when a question is present that is not filled in:

Front end Edit
Image Image

Unfortunately I'm no longer able to reproduce this, maybe caused by refreshing invalid data as part of loading the editor interface.

This fatal appears to have affected multiple users on multiple quizes, running version Sensei LMS 4.24.5 w/ Sensei Pro 1.24.0.

Let me know if there's any additional information I might be able to dig out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions