LLMS_AJAX_Handler::quiz_start( array $request )
Start a Quiz Attempt.
Parameters Parameters
- $request
-
(array) (Required) $_POST data. required: (string) attempt_key or (int) quiz_id (int) lesson_id.
Return Return
(WP_Error|array) WP_Error on error or array containing html template of the first question.
Source Source
File: includes/class.llms.ajax.handler.php
);
return array(
'attempt_key' => $attempt->get_key(),
'html' => $html,
'question_id' => $question_id,
'total' => $attempt->get_count( 'questions' ),
'question_ids' => $question_ids,
'can_be_resumed' => $attempt->can_be_resumed(),
);
}
/**
* AJAX Quiz get question.
*
* @since 7.8.0
*
* @param array $request $_POST data.
* @return WP_Error|array
*/
public static function quiz_get_question( $request ) {
$err = new WP_Error();
$student = llms_get_student();
if ( ! $student ) {
$err->add( 400, __( 'You must be logged in to take quizzes.', 'lifterlms' ) );
return $err;
}
$required = array( 'attempt_key', 'question_id' );
foreach ( $required as $key ) {
if ( empty( $request[ $key ] ) ) {
$err->add( 400, __( 'Missing required parameters. Could not proceed.', 'lifterlms' ) );
return $err;
}
}
$attempt_key = sanitize_text_field( $request['attempt_key'] );
$question_id = absint( $request['question_id'] );
$student_quizzes = $student->quizzes();
$attempt = $student_quizzes->get_attempt_by_key( $attempt_key );
// Don't allow the question to be retrieved if the attempt is not open or can't be resumed.
if ( ! $attempt || ( ! $attempt->get_quiz()->is_open() && ( ! $attempt->get_quiz()->can_be_resumed() || ! $attempt->can_be_resumed() ) ) ) {
$err->add( 500, __( 'There was an error retrieving the question. Please return to the lesson and try again.', 'lifterlms' ) );
return $err;
}
$access_check = self::verify_quiz_access( $student, $attempt->get( 'lesson_id' ) );
if ( is_wp_error( $access_check ) ) {
return $access_check;
}
$question_id = $attempt->get_question( $question_id );
if ( ! $question_id ) {
$err->add( 404, __( 'Cannot find the requested question id. Please return to the lesson and try again.', 'lifterlms' ) );
return $err;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 6.4.0 | Make sure attempts limit was not reached. |
| 3.9.0 | |
| 3.16.4 | Introduced. |