public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/ajax.php
/**
 * Handle Quiz Response.
 *
 * Called when a user is submitting quiz answers via the frontend form.
 *
 * @since 1.0.0
 */
Handle Quiz Response.
Called when a user is submitting quiz answers via the frontend form.


Handle Quiz Response . Called when a user is submitting quiz answers via the frontend form .
function WPCW_AJAX_units_handleQuizResponse() {
	if ( ! wp_verify_nonce( WPCW_arrays_getValue( $_POST, 'progress_nonce' ), 'wpcw-progress-nonce' ) ) {
		die ( __( 'Security check failed!', 'wp-courseware' ) );
	}
	$quizAndUnitID = WPCW_arrays_getValue( $_POST, 'id' );
	if ( ! preg_match( '/quiz_complete_(\d+)_(\d+)/', $quizAndUnitID, $matches ) ) {
		echo WPCW_UnitFrontend::message_error_getCompletionBox_error();
		die();
	}
	$unitID = $matches[1];
	$quizID = $matches[2];
	$post = get_post( $unitID );
	if ( ! $post ) {
		echo WPCW_UnitFrontend::message_createMessage_error( __( 'Error - could not save your quiz results.', 'wp-courseware' ) . ' ' . __( 'Could not find training unit.', 'wp-courseware' ) );
		die();
	}
	$fe = new WPCW_UnitFrontend( $post );
	$fe->setTriggeredAfterAJAXRequest();
	if ( ! $fe->check_unit_doesUnitHaveParentData() ) {
		echo WPCW_UnitFrontend::message_createMessage_error( __( 'Error - could not save your quiz results.', 'wp-courseware' ) . ' ' . __( 'Could not find course details for unit.', 'wp-courseware' ) );
		die();
	}
	if ( ! $fe->check_user_canUserAccessCourse() ) {
		echo $fe->message_user_cannotAccessCourse();
		die();
	}
	if ( ! $fe->check_quizzes_isQuizValidForUnit( $quizID ) ) {
		echo WPCW_UnitFrontend::message_createMessage_error( __( 'Error - could not save your quiz results.', 'wp-courseware' ) . ' ' . __( 'Quiz data does not match quiz for this unit.', 'wp-courseware' ) );
		die();
	}
	$canContinue = false;
	if ( $fe->check_paging_areWePagingQuestions() ) {
		$readyForMarking = $fe->check_quizzes_canWeContinue_checkAnswersFromPaging( $_POST );
	} else { 
		$readyForMarking = $fe->check_quizzes_canWeContinue_checkAnswersFromOnePageQuiz( $_POST );
	}
	if ( $readyForMarking || $fe->check_timers_doWeHaveAnActiveTimer_thatHasExpired() ) {
		$canContinue = $fe->check_quizzes_gradeQuestionsForQuiz();
	}
	if ( $canContinue ) {
		WPCW_units_saveUserProgress_Complete( $fe->fetch_getUserID(), $fe->fetch_getUnitID(), 'complete' );
		do_action( 'wpcw_user_completed_unit', $fe->fetch_getUserID(), $fe->fetch_getUnitID(), $fe->fetch_getUnitParentData() );
	}
	echo apply_filters( 'the_content', $fe->render_detailsForUnit( false, ! $canContinue ) );
	die();
}
function WPCW_AJAX_units_handleQuizResponse ( ) { if ( ! wp_verify_nonce ( WPCW_arrays_getValue ( $ _POST , 'progress_nonce' ) , 'wpcw-progress-nonce' ) ) { die ( __ ( 'Security check failed!' , 'wp-courseware' ) ) ; } $ quizAndUnitID = WPCW_arrays_getValue ( $ _POST , 'id' ) ; if ( ! preg_match ( '/quiz_complete_(\d+)_(\d+)/' , $ quizAndUnitID , $ matches ) ) { echo WPCW_UnitFrontend :: message_error_getCompletionBox_error ( ) ; die ( ) ; } $ unitID = $ matches [ 1 ] ; $ quizID = $ matches [ 2 ] ; $ post = get_post ( $ unitID ) ; if ( ! $ post ) { echo WPCW_UnitFrontend :: message_createMessage_error ( __ ( 'Error - could not save your quiz results.' , 'wp-courseware' ) . ' ' . __ ( 'Could not find training unit.' , 'wp-courseware' ) ) ; die ( ) ; } $ fe = new WPCW_UnitFrontend ( $ post ) ; $ fe -> setTriggeredAfterAJAXRequest ( ) ; if ( ! $ fe -> check_unit_doesUnitHaveParentData ( ) ) { echo WPCW_UnitFrontend :: message_createMessage_error ( __ ( 'Error - could not save your quiz results.' , 'wp-courseware' ) . ' ' . __ ( 'Could not find course details for unit.' , 'wp-courseware' ) ) ; die ( ) ; } if ( ! $ fe -> check_user_canUserAccessCourse ( ) ) { echo $ fe -> message_user_cannotAccessCourse ( ) ; die ( ) ; } if ( ! $ fe -> check_quizzes_isQuizValidForUnit ( $ quizID ) ) { echo WPCW_UnitFrontend :: message_createMessage_error ( __ ( 'Error - could not save your quiz results.' , 'wp-courseware' ) . ' ' . __ ( 'Quiz data does not match quiz for this unit.' , 'wp-courseware' ) ) ; die ( ) ; } $ canContinue = false ; if ( $ fe -> check_paging_areWePagingQuestions ( ) ) { $ readyForMarking = $ fe -> check_quizzes_canWeContinue_checkAnswersFromPaging ( $ _POST ) ; } else { $ readyForMarking = $ fe -> check_quizzes_canWeContinue_checkAnswersFromOnePageQuiz ( $ _POST ) ; } if ( $ readyForMarking || $ fe -> check_timers_doWeHaveAnActiveTimer_thatHasExpired ( ) ) { $ canContinue = $ fe -> check_quizzes_gradeQuestionsForQuiz ( ) ; } if ( $ canContinue ) { WPCW_units_saveUserProgress_Complete ( $ fe -> fetch_getUserID ( ) , $ fe -> fetch_getUnitID ( ) , 'complete' ) ; do_action ( 'wpcw_user_completed_unit' , $ fe -> fetch_getUserID ( ) , $ fe -> fetch_getUnitID ( ) , $ fe -> fetch_getUnitParentData ( ) ) ; } echo apply_filters ( 'the_content' , $ fe -> render_detailsForUnit ( false , ! $ canContinue ) ) ; die ( ) ; }
Handle Quiz Response.
Handle Quiz Response .
// Quiz ID and Unit ID are combined in the single CSS ID for validation. // So validate both are correct and that user is allowed to access quiz. // e.g. quiz_complete_69_1 or quiz_complete_17_2 (first ID is unit, 2nd ID is quiz) // Use the extracted data for further validation // Get the post object for this quiz item. // Initalise the unit details // Get associated data for this unit. No course/module data, then it's not a unit // User not allowed access to content // Check that the quiz is valid and belongs to this unit // Do we have all the answers that we need so that we can grade the quiz?
 // Answer Check Variation A - Paging // If this is false, then we keep checking for more answers. // Answer Check Variation B - All at once (no paging) // If this is false, then the form is represented asking for fixes. // Now checks are done, $this->unitQuizProgress contains the latest questions so that we can mark them. // Validate the answers that we have, which determines if we can carry on to the next //      unit, or if the user needs to do something else. // Unit complete, check if course/module is complete too. // Show the appropriate messages/forms for the user to look at. This is common for all execution // paths. // DJH 2015-09-09 - Added capability for next button to show when a user completes a quiz.