<?php
/**
 * Displays a quiz.
 *
 * Available Variables:
 *
 * $course_id                   : (int) ID of the course
 * $course                      : (object) Post object of the course
 * $course_settings             : (array) Settings specific to current course
 * $course_status               : Course Status
 * $has_access                  : User has access to course or is enrolled.
 *
 * $courses_options             : Options/Settings as configured on Course Options page
 * $lessons_options             : Options/Settings as configured on Lessons Options page
 * $quizzes_options             : Options/Settings as configured on Quiz Options page
 *
 * $user_id                     : (object) Current User ID
 * $logged_in                   : (true/false) User is logged in
 * $current_user                : (object) Currently logged in user object
 * $post                        : (object) The quiz post object () (Deprecated in LD 3.1. User $quiz_post instead).
 * $quiz_post                   : (object) The quiz post object ().
 * $lesson_progression_enabled  : (true/false)
 * $show_content                : (true/false) true if user is logged in and lesson progression is disabled or if previous lesson and topic is completed.
 * $attempts_left               : (true/false)
 * $attempts_count              : (integer) No of attempts already made
 * $quiz_settings               : (array)
 *
 * Note:
 *
 * To get lesson/topic post object under which the quiz is added:
 * $lesson_post = !empty($quiz_settings["lesson"])? get_post($quiz_settings["lesson"]):null;
 *
 * @since 2.1.0
 *
 * @package LearnDash\Quiz
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ( ! isset( $quiz_post ) ) || ( ! is_a( $quiz_post, 'WP_Post' ) ) ) {
	return;
}
?>
<div class="<?php echo esc_attr( learndash_the_wrapper_class() ); ?>">
<?php
	/**
	 * Fires before the quiz content starts.
	 *
	 * @since 3.0.0
	 *
	 * @param int $quiz_id Quiz ID.
	 * @param int $course_id Course ID.
	 * @param int $user_id   User ID.
	 */
	do_action( 'learndash-quiz-before', $quiz_post->ID, $course_id, $user_id );

	learndash_get_template_part(
		'modules/infobar.php',
		array(
			'context'   => 'quiz',
			'course_id' => $course_id,
			'user_id'   => $user_id,
			'post'      => $quiz_post,
		),
		true
	);

	if ( ! empty( $lesson_progression_enabled ) ) :
		$last_incomplete_step = is_quiz_accessable( null, $quiz_post, true, $course_id );
		if ( ( $last_incomplete_step ) && ( is_a( $last_incomplete_step, 'WP_Post' ) ) ) {
			$show_content = false;

			$sub_context = '';
			if ( 'on' === learndash_get_setting( $last_incomplete_step->ID, 'lesson_video_enabled' ) ) {
				if ( ! empty( learndash_get_setting( $last_incomplete_step->ID, 'lesson_video_url' ) ) ) {
					if ( 'BEFORE' === learndash_get_setting( $last_incomplete_step->ID, 'lesson_video_shown' ) ) {
						if ( ! learndash_video_complete_for_step( $last_incomplete_step->ID, $course_id, $user_id ) ) {
							$sub_context  = 'video_progression';
						}
					}
				}
			}

			/**
			 * Fires before the quiz progression.
			 *
			 * @since 3.0.0
			 *
			 * @param int $quiz_id   Quiz ID.
			 * @param int $course_id Course ID.
			 * @param int $user_id   User ID.
			 */
			do_action( 'learndash-quiz-progression-before', $quiz_post->ID, $course_id, $user_id );

				learndash_get_template_part(
					'modules/messages/lesson-progression.php',
					array(
						'previous_item' => $last_incomplete_step,
						'course_id'     => $course_id,
						'user_id'       => $user_id,
						'context'       => 'quiz',
						'sub_context'   => $sub_context,
					),
					true
				);

			/**
			 * Fires after the quiz progress.
			 *
			 * @since 3.0.0
			 *
			 * @param int $quiz_id   Quiz ID.
			 * @param int $course_id Course ID.
			 * @param int $user_id   User ID.
			 */
			do_action( 'learndash-quiz-progression-after', $quiz_post->ID, $course_id, $user_id );

		}
	endif;

	if ( $show_content ) :

		/**
		 * Content and/or tabs
		 */
		learndash_get_template_part(
			'modules/tabs.php',
			array(
				'course_id' => $course_id,
				'post_id'   => $quiz_post->ID,
				'user_id'   => $user_id,
				'content'   => $content,
				'materials' => $materials,
				'context'   => 'quiz',
			),
			true
		);

		if ( $attempts_left ) :

			/**
			 * Fires before the actual quiz content (not WP_Editor content).
			 *
			 * @since 3.0.0
			 *
			 * @param int $quiz_id   Quiz ID.
			 * @param int $course_id Course ID.
			 * @param int $user_id   User ID.
			 */
			do_action( 'learndash-quiz-actual-content-before', $quiz_post->ID, $course_id, $user_id );

			echo $quiz_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Post content

			/**
			 * Fires after the actual quiz content (not WP_Editor content).
			 *
			 * @since 3.0.0
			 *
			 * @param int $quiz_id   Quiz ID.
			 * @param int $course_id Course ID.
			 * @param int $user_id   User ID.
			 */
			do_action( 'learndash-quiz-actual-content-after', $quiz_post->ID, $course_id, $user_id );

		else :

			/**
			 * Display an alert
			 */

			/**
			 * Fires before the quiz attempts alert.
			 *
			 * @since 3.0.0
			 *
			 * @param int $quiz_id   Quiz ID.
			 * @param int $course_id Course ID.
			 * @param int $user_id   User ID.
			 */
			do_action( 'learndash-quiz-attempts-alert-before', $quiz_post->ID, $course_id, $user_id );

			learndash_get_template_part(
				'modules/alert.php',
				array(
					'type'    => 'warning',
					'icon'    => 'alert',
					'message' => sprintf(
						// translators: placeholders: quiz, attempts count.
						esc_html_x( 'You have already taken this %1$s %2$d time(s) and may not take it again.', 'placeholders: quiz, attempts count', 'learndash' ),
						learndash_get_custom_label_lower( 'quiz' ),
						$attempts_count
					),
				),
				true
			);

			/**
			 * Fires after the quiz attempts alert.
			 *
			 * @since 3.0.0
			 *
			 * @param int $quiz_id   Quiz ID.
			 * @param int $course_id Course ID.
			 * @param int $user_id   User ID.
			 */
			do_action( 'learndash-quiz-attempts-alert-after', $quiz_post->ID, $course_id, $user_id );

		endif;

	endif;

/**
 * Fires before the quiz content starts.
 *
 * @since 3.0.0
 *
 * @param int $quiz_id   Quiz ID.
 * @param int $course_id Course ID.
 * @param int $user_id   User ID.
 */
do_action( 'learndash-quiz-after', $quiz_post->ID, $course_id, $user_id );
learndash_load_login_modal_html();
?>

</div> <!--/.learndash-wrapper-->
