llms_update_3160_update_question_data()

Update question & choice data to new structure


Return Return

(void)


Top ↑

Source Source

File: includes/functions/updates/llms-functions-updates-3160.php

function llms_update_3160_update_question_data() {

	if ( 'complete' !== get_transient( 'llms_update_3160_ensure_no_lesson_dupe_rels' ) ) {
		return true;
	}

	$skip = get_transient( 'llms_3160_skipper_qdata' );
	if ( ! $skip ) {
		$skip = 0;
	}
	set_transient( 'llms_3160_skipper_qdata', $skip + 100, DAY_IN_SECONDS );

	global $wpdb;
	$res = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT post_id AS quiz_id, meta_value AS questions
		 FROM {$wpdb->postmeta}
		 WHERE meta_key = '_llms_questions'
		 ORDER BY post_id ASC
		 LIMIT %d, 100;",
			$skip
		)
	); // db call ok; no-cache ok.

	// Finished.
	if ( ! $res ) {
		set_transient( 'llms_update_3160_update_question_data', 'complete', DAY_IN_SECONDS );
		return false;
	}

	foreach ( $res as $data ) {
		$questions = maybe_unserialize( $data->questions );
		if ( is_array( $questions ) ) {
			foreach ( $questions as $raw_question ) {

				$points = isset( $raw_question['points'] ) ? $raw_question['points'] : 1;

				$question = llms_get_post( $raw_question['id'] );

				if ( ! $question ) {
					continue;
				}

				$question->set( 'parent_id', $data->quiz_id );
				$question->set( 'question_type', 'choice' );
				$question->set( 'points', $points );
				update_post_meta( $question->get( 'id' ), '_llms_legacy_question_title', $question->get( 'title' ) );
				$question->set( 'title', strip_tags( str_replace( array( '<p>', '</p>' ), '', $question->get( 'content' ) ), '<b><em><u><strong><i>' ) );

				$options = get_post_meta( $question->get( 'id' ), '_llms_question_options', true );

				update_post_meta( $question->get( 'id' ), '_llms_legacy_question_options', $options );
				delete_post_meta( $question->get( 'id' ), '_llms_question_options' );

				if ( ! $options ) {
					continue;
				}
				$clarify = '';

				$markers = range( 'A', 'Z' );

				foreach ( (array) $options as $index => $option ) {

					if ( ! isset( $option['option_text'] ) ) {
						continue;
					}

					$correct = false;
					// No correct_option set for the choice, set it to false.
					if ( ! isset( $option['correct_option'] ) ) {
						$correct = false;
						/**
						 * Handle bool strings like "on" "off" "yes" "no"
						 * and questions imported from a 3rd party Excel to LifterLMS plugin
						 * that doesn't save options in the expected format...
						 *  dev if you're reading this I love you but you caused me a pretty large headache
						 * trying to figure out where in our codebase we went wrong...
						 */
					} elseif ( is_string( $option['correct_option'] ) && '' !== $option['correct_option'] ) {
						$correct = true;
						// Catch everything else and filter var it.
					} else {

						$correct = filter_var( $option['correct_option'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );

						// Nothing should get here but I'm tired...
						if ( is_null( $correct ) ) {
							$correct = true;
						}
					}

					$question->create_choice(
						array(
							'choice'  => $option['option_text'],
							'correct' => $correct,
							'marker'  => $markers[ $index ],
						)
					);

					// If an option desc is set.
					if ( ! empty( $option['option_description'] ) ) {
						// If the description hasn't already been added to the new clarification.
						if ( false === strpos( $clarify, $option['option_description'] ) ) {
							$clarify .= $option['option_description'] . '<br><br>';
						}
					}
				}

				if ( $clarify ) {
					$question->set( 'clarifications', trim( rtrim( $clarify, '<br><br>' ) ) );
					$question->set( 'clarifications_enabled', 'yes' );
				}
			}
		}
	}

	// Run it again.
	return true;

}


Top ↑

Changelog Changelog

Changelog
Version Description
3.16.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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