Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

LLMS_Quiz_Attempt::get_new_questions()

Retrieve an array of blank questions for insertion into a new attempt during initialization


Return Return

(array)


Top ↑

Source Source

File: includes/models/model.llms.quiz.attempt.php

	private function get_new_questions() {

		$quiz = llms_get_post( $this->get( 'quiz_id' ) );

		$questions = array();

		if ( $quiz ) {

			$randomize = llms_parse_bool( $quiz->get( 'random_questions' ) );

			// Array of indexes that will be locked during shuffling.
			$locks = array();

			foreach ( $quiz->get_questions() as $index => $question ) {

				// If randomization is enabled, store the questions index so we can lock it during randomization.
				if ( $randomize && $question->supports( 'random_lock' ) ) {
					$locks[] = $index;
				}

				$questions[] = array(
					'id'      => $question->get( 'id' ),
					'earned'  => 0,
					'points'  => $question->supports( 'points' ) ? $question->get( 'points' ) : 0,
					'answer'  => null,
					'correct' => null,
				);

			}

			if ( $randomize ) {

				// Lifted from https://stackoverflow.com/a/28491007/400568.
				// I generally comprehend this code but also in a truer way i have no idea...
				$inc = array();
				$i   = 0;
				$j   = 0;
				$l   = count( $questions );
				$le  = count( $locks );
				while ( $i < $l ) {
					if ( $j >= $le || $i < $locks[ $j ] ) {
						$inc[] = $i;
					} else {
						$j++;
					}
					$i++;
				}

				// Fisher-yates-knuth shuffle variation O(n).
				$num = count( $inc );
				while ( $num-- ) {
					$perm                       = rand( 0, $num );
					$swap                       = $questions[ $inc[ $num ] ];
					$questions[ $inc[ $num ] ]  = $questions[ $inc[ $perm ] ];
					$questions[ $inc[ $perm ] ] = $swap;
				}
			}
		}

		return $questions;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
3.9.0
3.16.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.