Browse: Home / Snippets /

Return the latest quiz result items

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

/**
 * LearnDash Utility function to return the latest quiz result items
 * @param int $quiz_id The Quiz ID Post to return
 * @param int $user_id The specific user ID. Optional. Will use current user if not set.
 * @return array the attay of quiz information. Will look something like:
 *	Array(
 *		[quiz] => 49
 *		[score] => 1
 *		[count] => 1
 *		[question_show_count] => 1
 *		[pass] => 1
 *		[rank] => -
 *		[time] => 1490300513
 *		[pro_quizid] => 3
 *		[points] => 1
 *		[total_points] => 1
 *		[percentage] => 100
 *		[timespent] => 2.116
 *		[has_graded] => 
 *		[statistic_ref_id] => 1
 *		[started] => 1490300510
 *		[completed] => 1490300512
 *	)
 */
function learndash_get_latest_quiz_results( $quiz_id = 0, $user_id = 0 ) {
	if ( !empty( $quiz_id ) ) {
		if ( empty( $user_id ) ) {
			if ( is_user_logged_in() ) {
				$user_id = get_current_user_id();
			}
		}

		if ( !empty( $user_id ) ) {
			
			$user_quizzes = get_user_meta( $user_id, '_sfwd-quizzes', true );
			krsort( $user_quizzes );
			foreach( $user_quizzes as $user_quiz ) {
				if ( $user_quiz['quiz'] == $quiz_id ) {
					return $user_quiz;
				}
			}
		}
	}
}