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_Admin_Builder::update_questions( array $questions, LLMS_Quiz|LLMS_Question $parent )

Update quiz questions from heartbeat data


Parameters Parameters

$questions

(array) (Required) Question data array.

$parent

(LLMS_Quiz|LLMS_Question) (Required) Instance of an LLMS_Quiz or LLMS_Question (group).


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/admin/class.llms.admin.builder.php

				if ( ! empty( $lesson_data['quiz'] ) && is_array( $lesson_data['quiz'] ) ) {
					$res['quiz'] = self::update_quiz( $lesson_data['quiz'], $lesson );
				}
			}

			// Allow 3rd parties to update custom data.
			$res = apply_filters( 'llms_builder_update_lesson', $res, $lesson_data, $lesson, $created );

			array_push( $ret, $res );

		}

		return $ret;

	}

	/**
	 * Update quiz questions from heartbeat data
	 *
	 * @since 3.16.0
	 * @since 3.16.11 Unknown.
	 * @since 3.38.2 Make sure that a question as a type set, otherwise set it by default to `'choice'`.
	 *
	 * @param array                   $questions Question data array.
	 * @param LLMS_Quiz|LLMS_Question $parent    Instance of an LLMS_Quiz or LLMS_Question (group).
	 * @return array
	 */
	private static function update_questions( $questions, $parent ) {

		$res = array();

		foreach ( $questions as $q_data ) {

			$ret = array_merge(
				$q_data,
				array(
					'orig_id' => $q_data['id'],
				)
			);

			// Remove temp id if we have one so we'll create a new question.
			if ( self::is_temp_id( $q_data['id'] ) ) {
				unset( $q_data['id'] );
			}

			// Remove choices because we'll add them individually after creation.
			$choices = ( isset( $q_data['choices'] ) && is_array( $q_data['choices'] ) ) ? $q_data['choices'] : false;
			unset( $q_data['choices'] );

			// Remove child questions if it's a question group.
			$questions = ( isset( $q_data['questions'] ) && is_array( $q_data['questions'] ) ) ? $q_data['questions'] : false;
			unset( $q_data['questions'] );

			$question_id = $parent->questions()->update_question( $q_data );

			if ( ! $question_id ) {

				// Translators: %s = Question post id.
				$ret['error'] = sprintf( esc_html__( 'Unable to update question "%s". Invalid question ID.', 'lifterlms' ), $q_data['id'] );

			} else {

				$ret['id'] = $question_id;

				$question = $parent->questions()->get_question( $question_id );

				/**
				 * When saving a question, make sure that it has a question type set
				 * otherwise set it by default to `'choice'`.
				 */
				if ( ! $question->get( 'question_type', true ) ) {
					$question->set( 'question_type', 'choice' );
				}

				if ( $choices ) {

					$ret['choices'] = array();

					foreach ( $choices as $c_data ) {

						$choice_res = array_merge(
							$c_data,
							array(
								'orig_id' => $c_data['id'],
							)
						);

						unset( $c_data['question_id'] );

						// Remove the temp ID so that we create it if it's new.
						if ( self::is_temp_id( $c_data['id'] ) ) {
							unset( $c_data['id'] );


Top ↑

Changelog Changelog

Changelog
Version Description
3.38.2 Make sure that a question as a type set, otherwise set it by default to 'choice'.
3.16.11 Unknown.
3.16.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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