Quiz::review_attempt()

Review Quiz Attempt


Source

File: classes/Quiz.php

	public function review_attempt() {
		dozent_checking_nonce();

		global $wpdb;

		$review_btn = dozent_input_text( 'review_btn' );
		$attempt_id = (int) dozent_input_text( 'attempt_id' );
		$attempt = dozent_get_attempts( [ 'id' => $attempt_id ], true )->results;

		if ( $review_btn === 'review' ) {
			//Review Attempt

			$answers = dozent_input_array_field( 'answers' );
			$user_id = get_current_user_id();

			if ( dozent_count( $answers ) ) {
				foreach ( $answers as $answer_id => $answer ) {
					$data = [
						'earned_scores' => dozent_array_get( 'review_score', $answer ),
						'is_correct'    => (int) dozent_array_get( 'is_correct', $answer ),
					];

					$wpdb->update( $wpdb->dozent_quiz_answers, $data, [ 'id' => $answer_id ] );
				}

				/**
				 * Update attempt data
				 */

				$total_scores = $attempt->total_scores;

				$earned_scores = $wpdb->get_var( "SELECT SUM(earned_scores) AS earned_scores FROM {$wpdb->dozent_quiz_answers} WHERE attempt_id = {$attempt_id} " );

				$earned_percentage = ( $earned_scores > 0 && $total_scores > 0 ) ? ( ( $earned_scores * 100 ) / $total_scores ) : 0;
				$passed            = $earned_percentage >= $attempt->passing_percent ? 1 : 0;

				$review_data = [
					'reviewer_id'    => $user_id,
					'earned_scores'  => $earned_scores,
					'earned_percent' => $earned_percentage,
					'status'         => 'finished',
					'is_reviewed'    => 1,
					'reviewed_at'    => dozent_mysql_time(),
					'passed'         => $passed,
				];

				$wpdb->update( $wpdb->dozent_quiz_attempts, $review_data, [ 'id' => $attempt_id ] );

				/**
				 * Fire action after quiz attempt review finished
				 *
				 * @since DozentLMS 1.0.0
				 *
				 * @param int $attempt_id Attempt ID
				 */

				do_action( 'dozent_quiz_attempt_review_after', $attempt_id );

				dozent_set_flash_message( __( 'Attempt has been reviewed', 'dozent' ) );
			}
		}

		/**
		 * Delete Review and sync with quiz.
		 */
		if ( $review_btn === 'delete' ) {

			$wpdb->delete( $wpdb->dozent_quiz_attempts, [ 'id' => $attempt_id ] );
			$wpdb->delete( $wpdb->dozent_quiz_answers, [ 'attempt_id' => $attempt_id ] );
			dozent_complete_content( $attempt->quiz_id, $attempt->user_id, 'delete' );

			/**
			 * Fire action after quiz attempt deleted
			 *
			 * @since DozentLMS 1.0.0
			 *
			 * @param int $attempt_id Attempt ID
			 */

			do_action( 'dozent_quiz_attempt_deleted', $attempt_id );

			dozent_set_flash_message( __( 'Attempt has been deleted', 'dozent' ) );
			dozent_redirect( dozent_get_dashboard_permalink( "quiz-attempts/quizzes/?course_id={$attempt->course_id}" ) );
		}

		dozent_redirect();
	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.