public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/pages/page-user-progress.php
/**
 * Handles the grading of the quiz questions.
 *
 * @since 1.0.0
 */
Handles the grading of the quiz questions.
Handles the grading of the quiz questions .
function WPCW_showPage_UserProgess_quizAnswers_handingGrading( $quizDetails, $results, $page, $userID, $unitID ) {
	if ( isset( $_POST['grade_answers_submitted'] ) && 'true' == $_POST['grade_answers_submitted'] ) {
		$listOfQuestionsToMark = $results->quiz_needs_marking_list;
		if ( ! empty( $listOfQuestionsToMark ) ) {
			$listOfQuestionsToMark = array_flip( $listOfQuestionsToMark );
		} 
		else {
			$listOfQuestionsToMark = array();
		}
		$course_id = $quizDetails->parent_course_id;
		foreach ( $_POST as $key => $value ) {
			if ( preg_match( '/^grade_quiz_([0-9]+)$/', $key, $keyMatches ) && preg_match( '/^[0-9]+$/', $value ) && $value > 0 ) {
				$questionID = $keyMatches[1];
				unset( $listOfQuestionsToMark[ $questionID ] );
				if ( isset( $results->quiz_data[ $questionID ] ) ) {
					$results->quiz_data[ $questionID ]['their_grade'] = $value;
				}
			}
		}
		$results->quiz_needs_marking_list = array_flip( $listOfQuestionsToMark );
		WPCW_quizzes_updateQuizResults( $results );
		$page->showMessage( __( 'Grades have been successfully updated for this user.', 'wp-courseware' ) );
		$results = WPCW_quizzes_getUserResultsForQuiz( $userID, $unitID, $quizDetails->quiz_id );
		if ( $results->quiz_needs_marking == 0 ) {
			if ( 'quiz_block' == $quizDetails->quiz_type && $results->quiz_grade < $quizDetails->quiz_pass_mark ) {
				$results->sendOutEmails = false;
			} else {
				$results->sendOutEmails = true;
			}
			if ( $results->quiz_grade >= $quizDetails->quiz_pass_mark ) {
				$results->extraEmailDetail = __( 'You have passed the quiz.', 'wp-courseware' );
				printf( '<div id="message" class="wpcw_msg wpcw_msg_success">%s</span></div>',
					__( 'The user has <b>PASSED</b> this quiz, and the associated unit has been marked as complete.', 'wp-courseware' )
				);
				WPCW_units_saveUserProgress_Complete( $userID, $unitID );
				do_action( 'wpcw_user_completed_unit', $userID, $unitID, WPCW_units_getAssociatedParentData( $unitID ) );
			}
		}
		if ( $course_id ) {
			wpcw_delete_course_meta( $course_id, 'quizzes_need_grading' );
		}
		delete_transient( 'wpcw_quizzes_need_grading' );
		$results->quiz_has_just_been_graded = true;
	}
	return $results;
}
function WPCW_showPage_UserProgess_quizAnswers_handingGrading ( $ quizDetails , $ results , $ page , $ userID , $ unitID ) { if ( isset ( $ _POST [ 'grade_answers_submitted' ] ) && 'true' == $ _POST [ 'grade_answers_submitted' ] ) { $ listOfQuestionsToMark = $ results -> quiz_needs_marking_list ; if ( ! empty ( $ listOfQuestionsToMark ) ) { $ listOfQuestionsToMark = array_flip ( $ listOfQuestionsToMark ) ; } else { $ listOfQuestionsToMark = array ( ) ; } $ course_id = $ quizDetails -> parent_course_id ; foreach ( $ _POST as $ key => $ value ) { if ( preg_match ( '/^grade_quiz_([0-9]+)$/' , $ key , $ keyMatches ) && preg_match ( '/^[0-9]+$/' , $ value ) && $ value > 0 ) { $ questionID = $ keyMatches [ 1 ] ; unset ( $ listOfQuestionsToMark [ $ questionID ] ) ; if ( isset ( $ results -> quiz_data [ $ questionID ] ) ) { $ results -> quiz_data [ $ questionID ] [ 'their_grade' ] = $ value ; } } } $ results -> quiz_needs_marking_list = array_flip ( $ listOfQuestionsToMark ) ; WPCW_quizzes_updateQuizResults ( $ results ) ; $ page -> showMessage ( __ ( 'Grades have been successfully updated for this user.' , 'wp-courseware' ) ) ; $ results = WPCW_quizzes_getUserResultsForQuiz ( $ userID , $ unitID , $ quizDetails -> quiz_id ) ; if ( $ results -> quiz_needs_marking == 0 ) { if ( 'quiz_block' == $ quizDetails -> quiz_type && $ results -> quiz_grade < $ quizDetails -> quiz_pass_mark ) { $ results -> sendOutEmails = false ; } else { $ results -> sendOutEmails = true ; } if ( $ results -> quiz_grade >= $ quizDetails -> quiz_pass_mark ) { $ results -> extraEmailDetail = __ ( 'You have passed the quiz.' , 'wp-courseware' ) ; printf ( '<div id="message" class="wpcw_msg wpcw_msg_success">%s</span></div>' , __ ( 'The user has <b>PASSED</b> this quiz, and the associated unit has been marked as complete.' , 'wp-courseware' ) ) ; WPCW_units_saveUserProgress_Complete ( $ userID , $ unitID ) ; do_action ( 'wpcw_user_completed_unit' , $ userID , $ unitID , WPCW_units_getAssociatedParentData ( $ unitID ) ) ; } } if ( $ course_id ) { wpcw_delete_course_meta ( $ course_id , 'quizzes_need_grading' ) ; } delete_transient ( 'wpcw_quizzes_need_grading' ) ; $ results -> quiz_has_just_been_graded = true ; } return $ results ; }
Handles the grading of the quiz questions.
Handles the grading of the quiz questions .
// Switch array so values become keys. // Ensure we always have a valid array // Course Id. // Check $_POST keys for the graded results. // Check that we have a question ID and a matching grade for the quiz. Only want grades that are greater than 0. // Remove from list to be marked, if found // Add the grade information to the quiz // Update the database with the list of questions to mark, plus the updated quiz grading information. // Return to a simple list again, hence using array flip (ID => index) becomes (index => ID) // Update the results in the database. // Success message // Refresh the results - now that we've made changes // All items are marked, so email user, and tell admin that user has been notified. // Send out email only if not a blocking test, or blocking and passed. // Check if the user has passed or not to indicate what to do next. // Just a little note to mark as complete // Unit complete, check if course/module is complete too. // Delete Course Meta. // Delete Primary Transient. // Set flag that the quiz has just literally been graded for use in code around this. // Doing this after the results have been updated above.