llms_update_3160_ensure_no_lesson_dupe_rels()
Create duplicates for any quiz attached to multiple lessons
Return Return
(void)
Source Source
File: includes/functions/updates/llms-functions-updates-3160.php
function llms_update_3160_ensure_no_lesson_dupe_rels() { if ( 'complete' !== get_transient( 'llms_update_3160_ensure_no_dupe_question_rels_status' ) ) { return true; } $skip = get_transient( 'llms_3160_skipper_dupe_l' ); if ( ! $skip ) { $skip = 0; } set_transient( 'llms_3160_skipper_dupe_l', $skip + 100, DAY_IN_SECONDS ); global $wpdb; $res = $wpdb->get_results( $wpdb->prepare( "SELECT post_id AS lesson_id, meta_value AS quiz_id FROM {$wpdb->postmeta} WHERE meta_key = '_llms_quiz' AND meta_value != 0 ORDER BY lesson_id ASC LIMIT %d, 100 ;", $skip ) ); // db call ok; no-cache ok. if ( ! $res ) { set_transient( 'llms_update_3160_ensure_no_lesson_dupe_rels', 'complete', DAY_IN_SECONDS ); return false; } $quizzes_set = array(); foreach ( $res as $data ) { $lesson = llms_get_post( $data->lesson_id ); if ( ! $lesson ) { continue; } // Quiz no longer exists, unset the data from the lesson. $quiz = llms_get_post( $data->quiz_id ); if ( ! $quiz ) { $lesson->set( 'quiz', 0 ); $lesson->set( 'quiz_enabled', 'no' ); continue; } /** * Quiz already attached to a lesson * + duplicate it * + assign lesson/quiz relationships off new quiz * + find quiz attempts by old quiz / lesson * + update attempt quiz id * + update attempt question ids */ if ( in_array( $data->quiz_id, $quizzes_set ) ) { $orig_questions = get_post_meta( $data->quiz_id, '_llms_questions', true ); $qid_map = array(); $dupe_quiz_id = llms_update_util_post_duplicator( $data->quiz_id ); foreach ( $orig_questions as &$oqdata ) { $dupe_q = llms_update_util_post_duplicator( $oqdata['id'] ); $qid_map[ $oqdata['id'] ] = $dupe_q; $oqdata['id'] = $dupe_q; } update_post_meta( $dupe_quiz_id, '_llms_questions', $orig_questions ); update_post_meta( $dupe_quiz_id, '_llms_lesson_id', $data->lesson_id ); $lesson->set( 'quiz', $dupe_quiz_id ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $attempt_ids = $wpdb->get_col( " SELECT id FROM {$wpdb->prefix}lifterlms_quiz_attempts WHERE quiz_id = {$data->quiz_id} AND lesson_id = {$data->lesson_id}" ); // db call ok; no-cache ok. // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared foreach ( $attempt_ids as $aid ) { $attempt = new LLMS_Quiz_Attempt( $aid ); $questions = $attempt->get_questions(); foreach ( $questions as &$aqd ) { if ( isset( $qid_map[ $aqd['id'] ] ) ) { $aqd['id'] = $qid_map[ $aqd['id'] ]; } } $attempt->set_questions( $questions, true ); $attempt->set( 'quiz_id', $dupe_quiz_id ); $attempt->save(); } } $quizzes_set[] = $data->quiz_id; $lesson->set( 'quiz_enabled', 'yes' ); // Ensure the new quiz enabled key is set. } // Run it again. return true; }
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
3.16.0 | Introduced. |