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, int $course_id )

Update quizzes during heartbeats


Parameters Parameters

$quiz_data

(array) (Required) Array of quiz updates.

$lesson

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

$course_id

(int) (Required) WP_Post ID of the authorized builder course.


Top ↑

Return Return

(array)


Top ↑

Source Source

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

	private static function update_quiz( $quiz_data, $lesson, $course_id = 0 ) {

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

		// Confirm the current user can edit the course this quiz belongs to, independent of earlier checks.
		$authorized_course_id = $course_id ? absint( $course_id ) : absint( $lesson->get( 'parent_course' ) );
		if ( $authorized_course_id && ! current_user_can( 'edit_course', $authorized_course_id ) ) {
			// Translators: %s = Quiz post id.
			$res['error'] = sprintf( esc_html__( 'Unable to update quiz "%s". You are not allowed to edit the parent course.', 'lifterlms' ), $quiz_data['id'] );
			return $res;
		}

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

			$quiz = new LLMS_Quiz(
				'new',
				array(
					'post_title' => isset( $quiz_data['title'] ) ? $quiz_data['title'] : __( 'New Quiz', 'lifterlms' ),
				)
			);

			// Update existing quiz.
		} else {

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

			// Verify the quiz belongs to this course.
			if ( $course_id && $quiz && is_a( $quiz, 'LLMS_Quiz' ) ) {
				$parent = self::get_object_parent_course_id( $quiz->get( 'id' ) );
				if ( $parent && absint( $parent ) !== absint( $course_id ) ) {
					// Translators: %s = Quiz post id.
					$res['error'] = sprintf( esc_html__( 'Unable to update quiz "%s". Invalid quiz ID.', 'lifterlms' ), $quiz_data['id'] );
					return $res;
				}
				if ( ! $parent && ! current_user_can( 'edit_post', $quiz->get( 'id' ) ) ) {
					$res['error'] = sprintf( esc_html__( 'Unable to update quiz "%s". Invalid quiz ID.', 'lifterlms' ), $quiz_data['id'] );
					return $res;
				}
			}
		}

		$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.
			// Never trust a client-supplied lesson_id; the quiz must belong to the authorized lesson.
			foreach ( $properties as $prop ) {
				if ( isset( $quiz_data[ $prop ] ) && 'lesson_id' !== $prop ) {
					$quiz->set( $prop, $quiz_data[ $prop ] );
				}
			}
			$quiz->set( 'lesson_id', $lesson->get( 'id' ) );
			$res['lesson_id'] = $lesson->get( 'id' );

			// Include permalink and slug in the response so the builder can update the model.
			$res['permalink'] = get_permalink( $quiz->get( 'id' ) );
			$res['name']      = $quiz->get( 'name' );

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

			// 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.