_dozent_get_course_ratings_and_sync( int $course_id )
Sync course rating by course ID and save rating over view to the course meta.
Description
This function retrieve the ratings stat from the database by performing query, after query, immediately it saving the results to the course meta. So when we get the course rating via dozent_get_course_ratings(); it actually return from the post meta. At a glance, this function retrieve the course ratings and cache it to course post meta
Example usage:
$rating_stats = _dozent_get_course_ratings_and_sync();
$rating_stats = _dozent_get_course_ratings_and_sync( $course_id );
We will retrieve the course rating via dozent_get_course_ratings() function, it will save us huge DB query.
See also
Parameters
- $course_id
-
(int) (Required)
Return
(mixed|void)
Source
File: includes/core-functions.php
function _dozent_get_course_ratings_and_sync( $course_id = 0 ) { global $wpdb; $course_id = dozent_get_post_id( $course_id ); $rating_info = $wpdb->get_row( "SELECT COUNT(ID) as rating_count, SUM(rating) as rating_sum FROM {$wpdb->dozent_reviews} WHERE course_id = {$course_id} AND status = 1 ;" ); $ratingCount = 0; $ratingSum = 0; if ( ! empty( $rating_info->rating_count ) ) { $ratingCount = (int) $rating_info->rating_count; } if ( ! empty( $rating_info->rating_sum ) ) { $ratingSum = (int) $rating_info->rating_sum; } $five_star_count = (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->dozent_reviews} WHERE course_id = {$course_id} AND rating = 5 AND status = 1 ;" ); $four_star_count = (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->dozent_reviews} WHERE course_id = {$course_id} AND rating = 4 AND status = 1 ;" ); $three_star_count = (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->dozent_reviews} WHERE course_id = {$course_id} AND rating = 3 AND status = 1 ;" ); $two_star_count = (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->dozent_reviews} WHERE course_id = {$course_id} AND rating = 2 AND status = 1 ;" ); $one_star_count = (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->dozent_reviews} WHERE course_id = {$course_id} AND rating = 1 AND status = 1 ;" ); $five_percent = 0; if ( $five_star_count > 0 ) { $five_percent = ( $five_star_count * 100 ) / $ratingCount; } $four_percent = 0; if ( $four_star_count > 0 ) { $four_percent = ( $four_star_count * 100 ) / $ratingCount; } $three_percent = 0; if ( $three_star_count > 0 ) { $three_percent = ( $three_star_count * 100 ) / $ratingCount; } $two_percent = 0; if ( $two_star_count > 0 ) { $two_percent = ( $two_star_count * 100 ) / $ratingCount; } $one_percent = 0; if ( $one_star_count > 0 ) { $one_percent = ( $one_star_count * 100 ) / $ratingCount; } $rating_avg = '0.00'; if ( $ratingCount > 0 ) { $rating_avg = $ratingSum / $ratingCount; } $rating_overview = [ 'rating_count' => $ratingCount, 'rating_avg' => number_format( $rating_avg, 2 ), 'rating_stats' => [ 5 => [ 'count' => $five_star_count, 'percent' => number_format( $five_percent ), ], 4 => [ 'count' => $four_star_count, 'percent' => number_format( $four_percent ), ], 3 => [ 'count' => $three_star_count, 'percent' => number_format( $three_percent ), ], 2 => [ 'count' => $two_star_count, 'percent' => number_format( $two_percent ), ], 1 => [ 'one_count' => $one_star_count, 'percent' => number_format( $one_percent ), ] ] ]; //Save in post meta update_post_meta( $course_id, '_rating_count', dozent_array_get( 'rating_count', $rating_overview ) ); update_post_meta( $course_id, '_rating_avg', dozent_array_get( 'rating_avg', $rating_overview ) ); update_post_meta( $course_id, '_rating_overview', $rating_overview ); return apply_filters( '_dozent_get_course_ratings_and_sync', $rating_overview, $course_id ); }
Changelog
Version | Description |
---|---|
DozentLMS 1.0.0 | Introduced. |