llms_update_3160_attempt_migration()

Migrate attempt data from the former location on the wp_usermeta table


Return Return

(void)


Top ↑

Source Source

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

function llms_update_3160_attempt_migration() {

	global $wpdb;
	$query = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'llms_quiz_data' LIMIT 100;" ); // db call ok; no-cache ok.

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

	foreach ( $query as $record ) {

		if ( ! empty( $record->meta_value ) ) {

			foreach ( unserialize( $record->meta_value ) as $attempt ) {

				if ( ! is_array( $attempt ) ) {
					continue;
				}

				$to_insert = array();
				$format    = array();

				$start = $attempt['start_date'];
				$end   = $attempt['end_date'];

				if ( $end ) {
					$to_insert['update_date'] = $end;
					$format[]                 = '%s';
				} elseif ( $start ) {
					$to_insert['update_date'] = $start;
					$format[]                 = '%s';
				} else {
					continue;
				}

				foreach ( $attempt as $key => $val ) {

					$insert_key = $key;
					$insert_val = $val;

					if ( 'assoc_lesson' === $key ) {
						$insert_key = 'lesson_id';
					} elseif ( 'id' === $key ) {
						$insert_key = 'quiz_id';
					} elseif ( 'user_id' === $key ) {
						$insert_key = 'student_id';
					} elseif ( 'wpnonce' === $key ) {
						continue;
					} elseif ( 'current' === $key ) {
						continue;
					} elseif ( 'questions' === $key ) {
						$insert_val = serialize( $val );
					} elseif ( 'passed' === $key ) {
						$insert_key = 'status';
						if ( $val ) {
							$insert_val = 'pass';
						} else {
							// Quiz has been initialized but hasn't been started yet,
							// we don't need to migrate these.
							if ( ! $start && ! $end ) {
								// $insert_val = 'new';
								continue;
							} elseif ( $start && ! $end ) {
								// sSill taking the quiz.
								if ( isset( $attempt['current'] ) && $attempt['current'] ) {
									$insert_val = 'current';
								}
								// Quiz was abandoned.
								$insert_val = 'incomplete';
								// Actual failure.
							} else {
								$insert_val = 'fail';
							}
						}
					}

					switch ( $insert_key ) {

						case 'lesson_id':
						case 'quiz_id':
						case 'student_id':
						case 'attempt':
							$insert_format = '%d';
							break;

						case 'grade':
							$insert_format = '%f';
							break;

						default:
							$insert_format = '%s';

					}

					$to_insert[ $insert_key ] = $insert_val;
					$format[]                 = $insert_format;

				}

				$wpdb->insert( $wpdb->prefix . 'lifterlms_quiz_attempts', $to_insert, $format ); // db call ok; no-cache ok.

			}
		}

		// Backup original.
		update_user_meta( $record->user_id, 'llms_legacy_quiz_data', $record->meta_value );

		// Selete the original so it's not there on the next run.
		delete_user_meta( $record->user_id, 'llms_quiz_data' );

	}

	// Needs to run again.
	return true;

}

Top ↑

Changelog Changelog

Changelog
Version Description
3.24.1 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.