sync_course_with_contents( int $course_ID )

Sync course with some data that we will use to reduce database query


Description

Sync course data each time when something change int the course, like calculation of the ratings, total lectures, total assignments, total video time Saving those information to the course meta using post_meta db table.

Example usage:

sync_course_with_contents( $course_ID );

Note: We using this function for the internal purpose

See also


Parameters

$course_ID

(int) (Required) Course ID


Source

File: includes/core-functions.php

	function sync_course_with_contents( $course_ID = 0 ) {
		global $wpdb;

		do_action( 'dozent_course/sync_course_with_contents/before', $course_ID );

		$sections = dozent_db_query_get_sections( $course_ID );

		$lecture_count     = 0;
		$quiz_count        = 0;
		$assignment_count  = 0;
		$attachments_count = 0;
		$total_video_time  = 0;
		$content_ids = [];

		if ( dozent_count( $sections ) ) {

			foreach ( $sections as $section ) {

				$total_lectures_in_section = 0;
				$contents                  = dozent_get_course_contents_by_section( $section->ID );

				if ( dozent_count( $contents->posts ) ) {
					foreach ( $contents->posts as $post ) {

						$content_ids[] = $post->ID;

						if ( $post->post_type === 'dozent_lecture' ) {

							$total_lectures_in_section ++;
							$lecture_count ++;

							$lecture_video_time = video_runtime_to_seconds( $post->ID );
							$total_video_time   = $total_video_time + $lecture_video_time;

						} elseif ( $post->post_type === 'dozent_quiz' ) {
							$quiz_count ++;
						} elseif ( $post->post_type === 'dozent_assignment' ) {
							$assignment_count ++;
						}
					}
				}

				update_post_meta( $section->ID, '_total_lectures', $total_lectures_in_section );

			}
		}

		/**
		 * Get total Attachments and count them.
		 */

		$content_ids = array_filter( $content_ids );
		if ( dozent_count( $content_ids ) ) {
			$content_ids = implode( ',', array_map( 'absint', $content_ids ) );
			$content_attachments
			             = $wpdb->get_col( "SELECT meta_value FROM {$wpdb->postmeta} where meta_key = '_attachments' AND post_id IN({$content_ids}) " );

			if ( dozent_count( $content_attachments ) ) {
				foreach ( $content_attachments as $content_attachment ) {
					$content_attachment        = (array) maybe_unserialize( $content_attachment );
					$content_attachment        = array_filter( $content_attachment );
					$content_attachments_count = dozent_count( $content_attachment );

					if ( $content_attachments_count ) {
						$attachments_count = $attachments_count + $content_attachments_count;
					}

				}
			}
		}

		update_post_meta( $course_ID, '_total_lectures', $lecture_count );
		update_post_meta( $course_ID, '_total_quiz', $quiz_count );
		update_post_meta( $course_ID, '_total_assignments', $assignment_count );
		update_post_meta( $course_ID, '_total_attachments', $attachments_count );
		update_post_meta( $course_ID, '_total_video_time', $total_video_time );

		//Delete course cache contents
		wp_cache_delete( $course_ID, 'dozent_course_contents' );

		do_action( 'dozent_course/sync_course_with_contents/after', $course_ID );

	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.