LLMS_AJAX_Handler::quiz_answer_question( [type] $request )

AJAX Quiz answer question


Description Description


Parameters Parameters

$request

([type]) (Required) [description]


Top ↑

Return Return

([type]) [description]


Top ↑

Source Source

File: includes/class.llms.ajax.handler.php

	public static function quiz_answer_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', 'question_type' );
		foreach ( $required as $key ) {
			if ( ! isset( $request[ $key ] ) ) {
				$err->add( 400, __( 'Missing required parameters. Could not proceed.', 'lifterlms' ) );
				return $err;
			}
		}

		// $quiz_id = absint( $request['quiz_id'] );
		$attempt_key = sanitize_text_field( $request['attempt_key'] );
		$question_id = absint( $request['question_id'] );
		$answer = array_map( 'stripslashes_deep', isset( $request['answer'] ) ? $request['answer'] : array() );

		$attempt = $student->quizzes()->get_attempt_by_key( $attempt_key );
		if ( ! $attempt ) {
			$err->add( 500, __( 'There was an error recording your answer the quiz. Please return to the lesson and begin again.', 'lifterlms' ) );
			return $err;
		}

		// record the answer
		$attempt->answer_question( $question_id, $answer );

		// get the next question
		$question_id = $attempt->get_next_question( $question_id );

		// return html for the next question
		if ( $question_id ) {

			$html = llms_get_template_ajax( 'content-single-question.php', array(
				'attempt' => $attempt,
				'question' => llms_get_post( $question_id ),
			) );

			return array(
				'html' => $html,
				'question_id' => $question_id,
			);

		} else {

			return self::quiz_end( $request, $attempt );

		}

	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.9.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.





Permalink: