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_quiz( array $quiz_data, LLMS_Lesson $lesson )

Update quizzes during heartbeats


Parameters Parameters

$quiz_data

(array) (Required) Array of quiz updates.

$lesson

(LLMS_Lesson) (Required) Instance of the parent LLMS_Lesson.


Top ↑

Return Return

(array)


Top ↑

Source Source

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

	private static function update_quiz( $quiz_data, $lesson ) {

		$res = array_merge(
			$quiz_data,
			array(
				'orig_id' => $quiz_data['id'],
			)
		);

		// Create a quiz.
		if ( self::is_temp_id( $quiz_data['id'] ) ) {

			$quiz = new LLMS_Quiz( 'new' );

			// Update existing quiz.
		} else {

			$quiz = llms_get_post( $quiz_data['id'] );

		}

		$lesson->set( 'quiz', $quiz->get( 'id' ) );
		$lesson->set( 'quiz_enabled', 'yes' );

		// We don't have a proper quiz to work with...
		if ( empty( $quiz ) || ! is_a( $quiz, 'LLMS_Quiz' ) ) {

			// Translators: %s = Quiz post id.
			$res['error'] = sprintf( esc_html__( 'Unable to update quiz "%s". Invalid quiz ID.', 'lifterlms' ), $quiz_data['id'] );

		} else {

			// Return the real ID (important when creating a new quiz).
			$res['id'] = $quiz->get( 'id' );

			/**
			 * If the parent lesson was just created the lesson will have a temp id
			 * replace it with the newly created lessons's real ID.
			 */
			if ( ! isset( $quiz_data['lesson_id'] ) || self::is_temp_id( $quiz_data['lesson_id'] ) ) {
				$quiz_data['lesson_id'] = $lesson->get( 'id' );
			}

			$properties = array_merge(
				array_keys( $quiz->get_properties() ),
				array(
					// phpcs:ignore -- commented out code
					// 'content',
					'status',
					'title',
				)
			);

			// Update all updatable properties.
			foreach ( $properties as $prop ) {
				if ( isset( $quiz_data[ $prop ] ) ) {
					$quiz->set( $prop, $quiz_data[ $prop ] );
				}
			}

			if ( isset( $quiz_data['questions'] ) && is_array( $quiz_data['questions'] ) ) {
				$res['questions'] = self::update_questions( $quiz_data['questions'], $quiz );
			}

			// Update all custom fields.
			self::update_custom_schemas( 'quiz', $quiz, $quiz_data );

		}

		return $res;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
3.17.6 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.