llms_update_3160_ensure_no_dupe_question_rels()

Create duplicate questions for each question attached to multiple quizzes


Return Return

(void)


Top ↑

Source Source

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

function llms_update_3160_ensure_no_dupe_question_rels() {

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

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

	global $wpdb;
	$question_ids = $wpdb->get_col(
		$wpdb->prepare(
			"SELECT ID
		 FROM {$wpdb->posts}
		 WHERE post_type = 'llms_question'
		 ORDER BY ID ASC
		 LIMIT %d, 20;",
			$skip
		)
	); // db call ok; no-cache ok.

	if ( ! $question_ids ) {
		set_transient( 'llms_update_3160_ensure_no_dupe_question_rels_status', 'complete', DAY_IN_SECONDS );
		return false;
	}

	foreach ( $question_ids as $qid ) {

		$parts = array(
			serialize(
				array(
					'id' => $qid,
				)
			),
			serialize(
				array(
					'id' => absint( $qid ),
				)
			),
		);

		foreach ( $parts as &$part ) {
			$part = substr( $part, 5, -1 );
		}

		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$quiz_ids = $wpdb->get_col(
			"
			SELECT post_id
			FROM {$wpdb->postmeta}
			WHERE meta_key = '_llms_questions'
			  AND ( meta_value LIKE '%{$parts[0]}%' OR meta_value LIKE '%{$parts[1]}%' );"
		); // db call ok; no-cache ok.
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		// Question is attached to 2 or more quizzes.
		if ( count( $quiz_ids ) >= 2 ) {

			// Remove the first quiz and duplicate questions for the remaining quizzes.
			array_shift( $quiz_ids );

			foreach ( $quiz_ids as $quiz_id ) {

				// Copy the question and add update the reference on the quiz.
				$question_copy_id = llms_update_util_post_duplicator( $qid );
				$questions        = get_post_meta( $quiz_id, '_llms_questions', true );
				foreach ( $questions as &$qdata ) {
					if ( $qdata['id'] == $qid ) {
						$qdata['id'] = $question_copy_id;
					}
				}
				update_post_meta( $quiz_id, '_llms_questions', $questions );

				// Update references to the quiz in quiz attempts.
				// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
				$attempt_ids = $wpdb->get_col(
					"
					SELECT id
					FROM {$wpdb->prefix}lifterlms_quiz_attempts
					WHERE quiz_id = {$quiz_id}
					  AND ( questions LIKE '%{$parts[0]}%' OR questions LIKE '%{$parts[1]}%' );"
				); // db call ok; no-cache ok.
				// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

				foreach ( $attempt_ids as $aid ) {

					$attempt    = new LLMS_Quiz_Attempt( $aid );
					$attempt_qs = $attempt->get_questions();
					foreach ( $attempt_qs as &$answer ) {
						if ( $answer['id'] == $qid ) {
							$answer['id'] = $question_copy_id;
						}
					}
					$attempt->set_questions( $attempt_qs, true );

				}
			}
		}
	}

	// Need to run 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.