public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/classes/unitfrontend.php
/**
		 * Save the user's quiz progress to the database using their verbose answers.
		 *
		 * @param Array $resultDetails The full result details for the user who's completed the quiz.
		 * @param Array $checkedAnswerList The full list of checked answers.
		 */
Save the user's quiz progress to the database using their verbose answers.
@param Array $resultDetails The full result details for the user who's completed the quiz.
@param Array $checkedAnswerList The full list of checked answers.
Save the user ' s quiz progress to the database using their verbose answers . @param Array $resultDetails The full result details for the user who ' s completed the quiz . @param Array $checkedAnswerList The full list of checked answers .
public function update_quizzes_saveUserProgress_completeQuiz( $resultDetails, $checkedAnswerList, $possibleAnswers ) {
			global $wpdb, $wpcwdb;
			$wpdb->show_errors();
			$data = array();
			$data['quiz_correct_questions'] = count( $resultDetails['correct'] );
			$data['quiz_question_total']    = count( $this->unitQuizDetails->questions );
			$data['quiz_completed_date']    = current_time( 'mysql' );
			$data['user_id']                = $this->currentUserID;
			$data['unit_id']                = $this->unitPost->ID;
			$data['quiz_id']                = $this->unitQuizDetails->quiz_id;
			$data['quiz_needs_marking']      = false;
			$data['quiz_needs_marking_list'] = false;
			if ( ! empty( $this->unitQuizProgress ) && '0000-00-00 00-00-00' != $this->unitQuizProgress->quiz_started_date ) {
				$data['quiz_completion_time_seconds'] = ( strtotime( $data['quiz_completed_date'] ) - strtotime( $this->unitQuizProgress->quiz_started_date ) );
			}
			$fullDetails = $this->fetch_quizzes_extractQuizStatusFromAnswers( $checkedAnswerList, $possibleAnswers );
			$data['quiz_needs_marking'] = 0;
			if ( isset( $resultDetails['needs_marking'] ) ) {
				if ( is_array( $resultDetails['needs_marking'] ) ) {
					$data['quiz_needs_marking_list'] = maybe_serialize( array_keys( $resultDetails['needs_marking'] ) ); 
					if ( ! empty( $resultDetails['needs_marking'] ) ) {
						$data['quiz_needs_marking'] = count( $resultDetails['needs_marking'] );
					}
					$quiz_course_id = $this->unitQuizDetails->parent_course_id;
					if ( $quiz_course_id ) {
						wpcw_delete_course_meta( $quiz_course_id, 'quizzes_need_grading' );
					}
					delete_transient( 'wpcw_quizzes_need_grading' );
				} else {
					$data['quiz_needs_marking_list'] = maybe_serialize( array() ); 
				}
			}
			$data['quiz_data']  = maybe_serialize( $fullDetails );
			$data['quiz_grade'] = WPCW_quizzes_calculateGradeForQuiz( $fullDetails, $data['quiz_needs_marking'] );
			$SQL = $wpdb->prepare( "
			SELECT * 
			FROM $wpcwdb->user_progress_quiz
			WHERE user_id = %d
			  AND unit_id = %d
			  AND quiz_id = %d
			ORDER BY quiz_attempt_id DESC 
			LIMIT 1
		", $this->currentUserID, $this->unitPost->ID, $this->unitQuizDetails->quiz_id );
			$weNeedToUpdateTheMostRecentProgress = false;
			$data['quiz_is_latest']     = 'latest';
			$data['quiz_paging_status'] = 'complete';
			if ( $existingProgress = $wpdb->get_row( $SQL ) ) {
				if ( 'incomplete' == $existingProgress->quiz_paging_status ) {
					$weNeedToUpdateTheMostRecentProgress = true;
					$data['quiz_attempt_id'] = $existingProgress->quiz_attempt_id;
				} 
				else {
					$data['quiz_attempt_id'] = $existingProgress->quiz_attempt_id + 1;
				}
				$SQL = $wpdb->prepare( "
				UPDATE $wpcwdb->user_progress_quiz
				SET quiz_is_latest = ''
				WHERE user_id = %d
				  AND unit_id = %d
				  AND quiz_id = %d
			", $this->currentUserID, $this->unitPost->ID, $this->unitQuizDetails->quiz_id );
				$wpdb->query( $SQL );
			}
			if ( $weNeedToUpdateTheMostRecentProgress ) {
				$SQL = arrayToSQLUpdate( $wpcwdb->user_progress_quiz, $data, array( 'user_id', 'unit_id', 'quiz_id', 'quiz_attempt_id' ) );
			} 
			else {
				$SQL = arrayToSQLInsert( $wpcwdb->user_progress_quiz, $data );
			}
			$wpdb->query( $SQL );
			$this->unitQuizProgress = WPCW_quizzes_getUserResultsForQuiz( $this->currentUserID, $this->unitPost->ID, $this->unitQuizDetails->quiz_id );
			$this->updateInternalVariable_quizzes_getIncompleteQuestions();
		}
public function update_quizzes_saveUserProgress_completeQuiz ( $ resultDetails , $ checkedAnswerList , $ possibleAnswers ) { global $ wpdb , $ wpcwdb ; $ wpdb -> show_errors ( ) ; $ data = array ( ) ; $ data [ 'quiz_correct_questions' ] = count ( $ resultDetails [ 'correct' ] ) ; $ data [ 'quiz_question_total' ] = count ( $ this -> unitQuizDetails -> questions ) ; $ data [ 'quiz_completed_date' ] = current_time ( 'mysql' ) ; $ data [ 'user_id' ] = $ this -> currentUserID ; $ data [ 'unit_id' ] = $ this -> unitPost -> ID ; $ data [ 'quiz_id' ] = $ this -> unitQuizDetails -> quiz_id ; $ data [ 'quiz_needs_marking' ] = false ; $ data [ 'quiz_needs_marking_list' ] = false ; if ( ! empty ( $ this -> unitQuizProgress ) && '0000-00-00 00-00-00' != $ this -> unitQuizProgress -> quiz_started_date ) { $ data [ 'quiz_completion_time_seconds' ] = ( strtotime ( $ data [ 'quiz_completed_date' ] ) - strtotime ( $ this -> unitQuizProgress -> quiz_started_date ) ) ; } $ fullDetails = $ this -> fetch_quizzes_extractQuizStatusFromAnswers ( $ checkedAnswerList , $ possibleAnswers ) ; $ data [ 'quiz_needs_marking' ] = 0 ; if ( isset ( $ resultDetails [ 'needs_marking' ] ) ) { if ( is_array ( $ resultDetails [ 'needs_marking' ] ) ) { $ data [ 'quiz_needs_marking_list' ] = maybe_serialize ( array_keys ( $ resultDetails [ 'needs_marking' ] ) ) ; if ( ! empty ( $ resultDetails [ 'needs_marking' ] ) ) { $ data [ 'quiz_needs_marking' ] = count ( $ resultDetails [ 'needs_marking' ] ) ; } $ quiz_course_id = $ this -> unitQuizDetails -> parent_course_id ; if ( $ quiz_course_id ) { wpcw_delete_course_meta ( $ quiz_course_id , 'quizzes_need_grading' ) ; } delete_transient ( 'wpcw_quizzes_need_grading' ) ; } else { $ data [ 'quiz_needs_marking_list' ] = maybe_serialize ( array ( ) ) ; } } $ data [ 'quiz_data' ] = maybe_serialize ( $ fullDetails ) ; $ data [ 'quiz_grade' ] = WPCW_quizzes_calculateGradeForQuiz ( $ fullDetails , $ data [ 'quiz_needs_marking' ] ) ; $ SQL = $ wpdb -> prepare ( " 
			SELECT * 
			FROM  $ wpcwdb -> user_progress_quiz 
			WHERE user_id = %d
			  AND unit_id = %d
			  AND quiz_id = %d
			ORDER BY quiz_attempt_id DESC 
			LIMIT 1
		 " , $ this -> currentUserID , $ this -> unitPost -> ID , $ this -> unitQuizDetails -> quiz_id ) ; $ weNeedToUpdateTheMostRecentProgress = false ; $ data [ 'quiz_is_latest' ] = 'latest' ; $ data [ 'quiz_paging_status' ] = 'complete' ; if ( $ existingProgress = $ wpdb -> get_row ( $ SQL ) ) { if ( 'incomplete' == $ existingProgress -> quiz_paging_status ) { $ weNeedToUpdateTheMostRecentProgress = true ; $ data [ 'quiz_attempt_id' ] = $ existingProgress -> quiz_attempt_id ; } else { $ data [ 'quiz_attempt_id' ] = $ existingProgress -> quiz_attempt_id + 1 ; } $ SQL = $ wpdb -> prepare ( " 
				UPDATE  $ wpcwdb -> user_progress_quiz 
				SET quiz_is_latest = ''
				WHERE user_id = %d
				  AND unit_id = %d
				  AND quiz_id = %d
			 " , $ this -> currentUserID , $ this -> unitPost -> ID , $ this -> unitQuizDetails -> quiz_id ) ; $ wpdb -> query ( $ SQL ) ; } if ( $ weNeedToUpdateTheMostRecentProgress ) { $ SQL = arrayToSQLUpdate ( $ wpcwdb -> user_progress_quiz , $ data , array ( 'user_id' , 'unit_id' , 'quiz_id' , 'quiz_attempt_id' ) ) ; } else { $ SQL = arrayToSQLInsert ( $ wpcwdb -> user_progress_quiz , $ data ) ; } $ wpdb -> query ( $ SQL ) ; $ this -> unitQuizProgress = WPCW_quizzes_getUserResultsForQuiz ( $ this -> currentUserID , $ this -> unitPost -> ID , $ this -> unitQuizDetails -> quiz_id ) ; $ this -> updateInternalVariable_quizzes_getIncompleteQuestions ( ) ; }
Save the user's quiz progress to the database using their verbose answers.
Save the user ' s quiz progress to the database using their verbose answers .
// Store which questions need marking // Calculate how long the quiz took, if it was timed. // Generate a full list of the quiz and the answers given. // Track how many questions need marking // Just check that we have some items that need marking. // Only store the IDs. // If we have items that need answering, copy the number of items to be answered. // Delete Course Transient. // Delete Primary Transient. // Creates an empty list. // Use serialised data for storing full results. // Ensure this is marked as the latest and considered complete. // Already exists, so increment the quiz_attempt_id // If it doesn't exist, we'll just use the database default of 0. // Is the last attempt incomplete? If so, then we want to update the existing progress // to avoid incorrect duplication of attempts. // Retain attempt ID from previous item // We're inserting, so need to increase attempt ID. // Regardless, we need to mark all previous progress items as not being the latest. // We're updating the last progress to be complete now. // No incomplete records before. // #### Update internal store of results once saved to database. // Try to work out which questions are still incomplete.