Quiz::quiz_attempting( $template )

Check if user attempting to any question right now.


Parameters

$template

(Required)


Return

(string)


Source

File: classes/Quiz.php

	public function quiz_attempting( $template ) {
		global $wpdb;

		if ( is_dozent_single_quiz() && is_user_logged_in() ) {
			$user_id = get_current_user_id();

			$quiz_id = get_the_ID();

			$has_started_attempt = dozent_get_attempts( [
				'quiz_id' => $quiz_id,
				'user_id' => $user_id,
				'status'  => 'started',
			], true );

			if ( $has_started_attempt->count ) {

				$attempt = $has_started_attempt->results;

				$answered = dozent_get_quiz_answers( [
					'quiz_id'    => $quiz_id,
					'user_id'    => $user_id,
					'attempt_id' => $attempt->id
				] );

				$answered_question_ids = [];
				if ( dozent_count( $answered->results ) ) {
					$answered_question_ids = wp_list_pluck( $answered->results, 'question_id' );
				}

				$questions = dozent_get_questions( [ 'quiz_id' => $quiz_id ] );

				$questions_limit = (int) ( $questions->count >= $attempt->questions_limit ) ? $attempt->questions_limit
					: $questions->count;

				if ( $answered->count >= $questions_limit ) {
					//Finished Quiz

					$reviewRequired = dozent_get_quiz_answers( [
						'quiz_id'    => $quiz_id,
						'user_id'    => $user_id,
						'attempt_id' => $attempt->id,
						'where_sql'  => " AND (q_type = 'text' OR q_type = 'textarea') ",
					] );


					$total_scores = array_sum( array_map( function ( $item ) {
						return $item->q_score;
					}, $answered->results ) );

					$earned_scores = array_sum( array_map( function ( $item ) {
						return $item->earned_scores;
					}, $answered->results ) );

					$earned_percentage = ( $earned_scores > 0 && $total_scores > 0 ) ? ( number_format( ( $earned_scores
					                                                                                      * 100 )
					                                                                                    / $total_scores ) )
						: 0;

					$attempt_data = [
						'total_scores'   => $total_scores,
						'total_answered' => $answered->count,
						'earned_scores'  => $earned_scores,
						'earned_percent' => $earned_percentage,
					];

					if ( $reviewRequired->count ) {
						$attempt_data['status'] = 'in_review';
					} else {
						$attempt_data['status'] = 'finished';
					}
					$attempt_data['ended_at'] = dozent_mysql_time();

					$wpdb->update( $wpdb->dozent_quiz_attempts, $attempt_data, [ 'id' => $attempt->id ] );
					dozent_complete_content( $quiz_id );

					dozent_redirect( get_the_permalink( $quiz_id ) );
				}

				$question_number = $answered->count + 1;
				$quiz_title      = get_the_title( $quiz_id );


				/**
				 * Query single random question
				 */

				$question_query_args = [ 'quiz_id' => $quiz_id, 'order_by' => 'rand' ];
				if ( dozent_count( $answered_question_ids ) ) {
					$question_query_args['not_in'] = [ 'question_id' => $answered_question_ids ];
				}

				$question = dozent_get_questions( $question_query_args, true );

				$attempt_option = apply_filters( 'dozent_quiz_attempt_options', [
					'started_at'    => $attempt->created_at,
					'date_time_now' => dozent_mysql_time(),
					'quiz_option'   => json_decode( $attempt->quiz_option, true ),
				] );

				global $question_data;

				$question_data = [
					'quiz_id'         => $quiz_id,
					'quiz_title'      => $quiz_title,
					'question_number' => $question_number,
					'questions_limit' => $questions_limit,
					'question'        => $question,
					'attempt'         => $attempt,
					'attempt_option'  => $attempt_option,
				];

				$template = dozent_get_template_path( 'quiz-attempt' );
			}
		}

		return $template;
	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.