LLMS_Controller_Admin_Quiz_Attempts

Quiz Attempt Forms on the admin panel


Description Description

Allows admins to grade, leave remarks, and delete quiz attempts.


Source Source

File: includes/controllers/class.llms.controller.admin.quiz.attempts.php

class LLMS_Controller_Admin_Quiz_Attempts {

	public function __construct() {

		add_action( 'admin_init', array( $this, 'maybe_run_actions' ) );

	}

	/**
	 * Run actions on form submission
	 * @return   void
	 * @since    3.16.0
	 * @version  3.16.9
	 */
	public function maybe_run_actions() {

		if ( ! llms_verify_nonce( '_llms_quiz_attempt_nonce', 'llms_quiz_attempt_actions', 'POST' ) ) {
			return;
		}

		if ( isset( $_POST['llms_quiz_attempt_action'] ) && isset( $_POST['llms_attempt_id'] ) ) {

			$action = $_POST['llms_quiz_attempt_action'];

			$attempt = new LLMS_Quiz_Attempt( absint( $_POST['llms_attempt_id'] ) );

			if ( ! current_user_can( 'edit_post', $attempt->get( 'quiz_id' ) ) ) {
				return;
			}

			if ( 'llms_attempt_delete' === $action ) {
				$url = add_query_arg( array(
					'page' => 'llms-reporting',
					'tab' => 'quizzes',
					'quiz_id' => $attempt->get( 'quiz_id' ),
					'stab' => 'attempts',
				), admin_url( 'admin.php' ) );
				$attempt->delete();
				wp_safe_redirect( $url );
			} elseif ( 'llms_attempt_grade' === $action && ( isset( $_POST['remarks'] ) || isset( $_POST['points'] )) ) {
				$this->save_grade( $attempt );
			}
		}

	}

	/**
	 * Saves changes to a quiz
	 *
	 * @since 3.16.0
	 * @since 3.30.3 Strip slashes on remarks.
	 *
	 * @param LLMS_Quiz_Attempt $attempt Quiz attempt instance.
	 * @return void
	 */
	private function save_grade( $attempt ) {

		$remarks = isset( $_POST['remarks'] ) ? $_POST['remarks'] : array();
		$points = isset( $_POST['points'] ) ? $_POST['points'] : array();

		$questions = $attempt->get_questions();
		foreach ( $questions as &$question ) {

			if ( isset( $remarks[ $question['id'] ] ) ) {
				$question['remarks'] = wp_kses_post( nl2br( stripslashes( $remarks[ $question['id'] ] ) ) );
			}

			if ( isset( $points[ $question['id'] ] ) ) {
				$earned = absint( $points[ $question['id'] ] );
				$question['earned'] = $earned;
				if ( ( $earned / $question['points'] ) >= 0.5 ) {
					$question['correct'] = 'yes';
				} else {
					$question['correct'] = 'no';
				}
			}
		}

		// update the attempt with new questions
		$attempt->set_questions( $questions, true );

		// attempt to calculate the grade
		$attempt->calculate_grade()->save();

		// if all questions were graded the grade will have been calculated and we can trigger completion actions
		if ( in_array( $attempt->get( 'status' ), array( 'fail', 'pass' ) ) ) {
			$attempt->do_completion_actions();
		}

		do_action( 'llms_quiz_graded', $attempt->get_student()->get_id(), $attempt->get( 'quiz_id' ), $attempt );

	}

}

Top ↑

Changelog Changelog

Changelog
Version Description
3.30.3 Fixed an issue causing backlashes to be saved around escaped characters when leaving remarks.
3.16.0 Introduced.

Top ↑

Methods Methods


Top ↑

User Contributed Notes User Contributed Notes

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