public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/quiz/random.php
/**
		 * Given a user ID, get the selection of questions for this random question selection. This
		 * will lock the selection to the user if the selection has not been locked already.
		 *
		 * @param Integer $userID The ID of the user to get the selection of questions for.
		 * @param Integer $parentUnitID The ID of the parent unit to show this random question on.
		 *
		 * @return Array The list of questions that have been locked.
		 */
Given a user ID, get the selection of questions for this random question selection. This
will lock the selection to the user if the selection has not been locked already.

@param Integer $userID The ID of the user to get the selection of questions for.
@param Integer $parentUnitID The ID of the parent unit to show this random question on.

@return Array The list of questions that have been locked.
Given a user ID get the selection of questions for this random question selection . This will lock the selection to the user if the selection has not been locked already . @param Integer $userID The ID of the user to get the selection of questions for . @param Integer $parentUnitID The ID of the parent unit to show this random question on . @return Array The list of questions that have been locked .
public function questionSelection_getLockedQuestionSelection( $userID, $parentUnitID ) {
			global $wpcwdb, $wpdb;
			$wpdb->show_errors();
			$lockedSelection = $wpdb->get_var( $wpdb->prepare( "
				SELECT question_selection_list
				FROM $wpcwdb->question_rand_lock
				WHERE question_user_id = %d
				  AND rand_question_id = %d
				  AND parent_unit_id   = %d
			", $userID, $this->quizItem->question_id, $parentUnitID ) );
			if ( $lockedSelection ) {
				if ( preg_match( '/^([0-9]+,?)+$/', $lockedSelection ) ) {
					$questionsToUseWithIDKey = array();
					$questionsToUse = $wpdb->get_results( "
					SELECT *
					FROM $wpcwdb->quiz_qs
					WHERE question_id IN ($lockedSelection)
					  AND question_type != 'random_selection'
					ORDER BY FIELD(question_id, $lockedSelection)
				" );
					if ( ! empty( $questionsToUse ) ) {
						foreach ( $questionsToUse as $questionsToUse_single ) {
							$questionsToUseWithIDKey[ $questionsToUse_single->question_id ] = $questionsToUse_single;
						}
					}
					return $questionsToUseWithIDKey;
				}
			}
			$tagDetails     = WPCW_quiz_RandomSelection::decodeTagSelection( $this->quizItem->question_question );
			$questionsToUse = WPCW_quiz_RandomSelection::questionSelection_getRandomQuestionsFromTags( $tagDetails );
			if ( ! empty( $questionsToUse ) ) {
				$questionIDList = array();
				foreach ( $questionsToUse as $questionDetails ) {
					$questionIDList[] = $questionDetails->question_id;
				}
				$wpdb->query( $wpdb->prepare( "
				REPLACE INTO $wpcwdb->question_rand_lock
				(question_user_id, rand_question_id, question_selection_list, parent_unit_id)
				VALUES (%d, %d, %s, %d)
			", $userID, $this->quizItem->question_id, implode( ',', $questionIDList ), $parentUnitID ) );
			}
			return $questionsToUse;
		}
public function questionSelection_getLockedQuestionSelection ( $ userID , $ parentUnitID ) { global $ wpcwdb , $ wpdb ; $ wpdb -> show_errors ( ) ; $ lockedSelection = $ wpdb -> get_var ( $ wpdb -> prepare ( " 
				SELECT question_selection_list
				FROM  $ wpcwdb -> question_rand_lock 
				WHERE question_user_id = %d
				  AND rand_question_id = %d
				  AND parent_unit_id   = %d
			 " , $ userID , $ this -> quizItem -> question_id , $ parentUnitID ) ) ; if ( $ lockedSelection ) { if ( preg_match ( '/^([0-9]+,?)+$/' , $ lockedSelection ) ) { $ questionsToUseWithIDKey = array ( ) ; $ questionsToUse = $ wpdb -> get_results ( " 
					SELECT *
					FROM  $ wpcwdb -> quiz_qs 
					WHERE question_id IN ( $ lockedSelection )
					  AND question_type != 'random_selection'
					ORDER BY FIELD(question_id,  $ lockedSelection )
				 " ) ; if ( ! empty ( $ questionsToUse ) ) { foreach ( $ questionsToUse as $ questionsToUse_single ) { $ questionsToUseWithIDKey [ $ questionsToUse_single -> question_id ] = $ questionsToUse_single ; } } return $ questionsToUseWithIDKey ; } } $ tagDetails = WPCW_quiz_RandomSelection :: decodeTagSelection ( $ this -> quizItem -> question_question ) ; $ questionsToUse = WPCW_quiz_RandomSelection :: questionSelection_getRandomQuestionsFromTags ( $ tagDetails ) ; if ( ! empty ( $ questionsToUse ) ) { $ questionIDList = array ( ) ; foreach ( $ questionsToUse as $ questionDetails ) { $ questionIDList [ ] = $ questionDetails -> question_id ; } $ wpdb -> query ( $ wpdb -> prepare ( " 
				REPLACE INTO  $ wpcwdb -> question_rand_lock 
				(question_user_id, rand_question_id, question_selection_list, parent_unit_id)
				VALUES (%d, %d, %s, %d)
			 " , $ userID , $ this -> quizItem -> question_id , implode ( ',' , $ questionIDList ) , $ parentUnitID ) ) ; } return $ questionsToUse ; }
Given a user ID, get the selection of questions for this random question selection.
Given a user ID get the selection of questions for this random question selection .
// #### 1 - See if the question is locked for a user? If so, return locked selection. // #### 2 - Got a selection, turn this into question IDs. // Quickly validate that it's just numbers and commas, then use directly in SQL // for an array search of question IDs that match the selection.  If the string // does not validate as a string of numbers and commas, then we'll generate a new // selection using the code below. // ORDER BY FIELD is a function that will sort the questions in the order they appear in // the comma-separated list. // Need to remap to use ID => question // #### 3 - Selection is not locked, so we generate a new selection for the user and then lock/save it to the database. // Need to get the IDs of this question to lock it. Don't need any other data. // Now insert this list into the database to create the lock. Handling // duplicates elegantly here by using REPLACE INTO intentionally