dozent_get_course_reviews( array $args = array() )

Query rating and reviews


Description

Get the courses rating and reviews results by query it from the database. The results you can filter by passing $args value as array

Example usage:

dozent_get_course_reviews( array(
 'course_id' => 0,
 'user_id'   => 0,
 's'         => null, //Search term
 'rating'    => null,
 'status'    => 1,
 'start'     => 0,
 'per_page'     => 10 //per page
 ) );

Parameters

$args

(array) (Optional) Arguments of the query

Default value: array()


Return

(object)


Source

File: includes/core-functions.php

	function dozent_get_course_reviews( $args = [] ) {
		global $wpdb;

		/**
		 * Filter the default query parameter of dozent_get_course_reviews();
		 *
		 * @since DozentLMS 1.0.0
		 *
		 * @see dozent_get_course_reviews();
		 *
		 * @param  array  $query_params  Parameter of the query
		 */

		$defaults = apply_filters( 'dozent_get_course_reviews_args', [
			'course_id' => 0,
			'user_id'   => 0,
			's'         => null,
			'rating'    => null,
			'status'    => 1,
			'start'     => 0,
			'per_page'  => dozent_show_per_page(),
			'order_by'  => 'DESC',
		] );

		$r = wp_parse_args( $args, $defaults );

		//set default start result
		if ( empty( $r['start'] ) ) {
			$per_page = isset( $r['per_page'] ) ? $r['per_page'] : dozent_show_per_page();

			if ( dozent_input_text( 'current_page' ) ) {
				$page = max( 1, dozent_input_text( 'current_page' ) );
			} else {
				$page = max( 1, dozent_input_text( 'page' ) );
			}

			$r['start'] = ( $page - 1 ) * $per_page;
		}

		extract( $r, EXTR_SKIP );


		$where = " WHERE 1 = 1 ";

		if ( ! empty( $course_id ) ) {
			$where .= " AND reviews.course_id = {$course_id} ";
		}
		if ( ! empty( $user_id ) ) {
			$where .= " AND reviews.user_id = {$user_id} ";
		}
		if ( ! empty( $s ) ) {
			$where .= " AND reviews.review like '%{$s}%'  ";
		}
		if ( ! empty( $rating ) ) {
			$where .= " AND reviews.rating = {$rating}  ";
		}
		if ( ! empty( $status ) && $status !== 'any' ) {
			$where .= " AND reviews.status = {$status}  ";
		}

		$limit_query = '';
		if ( ! empty( $per_page ) && $per_page != '-1' ) {
			$limit_query = " LIMIT {$start}, {$per_page} ";
		}

		$order = '';
		if ( ! empty( $order_by ) ) {
			$order = " ORDER BY reviews.ID {$order_by} ";
		}

		$where = apply_filters( 'dozent_get_course_reviews_where', $where );

		$count = $wpdb->get_var( " 
			SELECT COUNT(reviews.ID)
			
			FROM {$wpdb->dozent_reviews} reviews 
			LEFT JOIN {$wpdb->users} review_author ON reviews.user_id = review_author.ID
			LEFT JOIN {$wpdb->posts} course ON reviews.course_id = course.ID
		 
			{$where};" );

				$query = $wpdb->get_results( " 
			SELECT reviews.* ,
			review_author.display_name as user_display_name,
			course.post_title as course_title
			
			FROM {$wpdb->dozent_reviews} reviews 
			LEFT JOIN {$wpdb->users} review_author ON reviews.user_id = review_author.ID
			LEFT JOIN {$wpdb->posts} course ON reviews.course_id = course.ID
		 
			{$where} {$order} {$limit_query};" );

		return (object) [ 'count' => (int) $count, 'results' => $query ];
	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.