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

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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.