dozent_get_courses_by_instructor( null|int|object $instructor = null, array $args = array() )

Get courses by instructors


Description

This function will return all of the courses for an instructors. If any course admin added instructors, then those instructors will get this course as my courses

Pass $limit parameter value = "-1" to get no limit at the query

Example usage:

 $args = array(
     'not_in_ids'      => [],
     'start'         => 0,
     'per_page'      => 10,
 )

$instructors = dozent_get_instructors( $instructor_id, $args );

Check the default arguments

See also


Parameters

$instructor

(null|int|object) (Optional) Wp User instance of user id

Default value: null

$args

(array) (Optional) default query arguments

Default value: array()


Return

(mixed|object|void)


Source

File: includes/user-functions.php

	function dozent_get_courses_by_instructor( $instructor = null, $args = [] ) {
		global $wpdb;

		$default_per_page = dozent_show_per_page();
		$current_page     = (int) dozent_input_text( 'current_page' );
		$page             = max( 1, $current_page );
		$start            = ( $page - 1 ) * $default_per_page;

		$defaults = apply_filters( 'dozent_get_courses_by_instructor_default_args', [
			'not_in_ids'  => [],
			'start'       => $start,
			'per_page'    => $default_per_page,
			'course_status' => [ 'publish' ],
		] );

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

		$instructor_sql = "";
		if ( ! empty( $instructor ) ) {
			$instructor = new WP_User( $instructor );

			if ( empty( $instructor->ID ) ){
				return new DozentDBResponse();
			}

			$instructor_sql = " AND instructor_courses.instructor_id = {$instructor->ID} ";
		}

		$not_in_ids_query = '';
		if ( ! empty( $not_in_ids ) && dozent_count( $not_in_ids ) ) {
			$not_in_ids_string = implode( ",", $not_in_ids );
			$not_in_ids_query  = " AND courses.ID NOT IN( $not_in_ids_string ) ";
		}

		/**
		 * In Course status SQL
		 */
		$in_sql       = "";
		if ( ! empty( $course_status ) ) {
			$where_in_sql = dozent_array_to_in_sql( $course_status );
			if ( $where_in_sql ) {
				$in_sql = " AND courses.post_status IN($where_in_sql) ";
			}
		}

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

		$course_count = (int) $wpdb->get_var( "SELECT COUNT(courses.ID) 
		FROM {$wpdb->dozent_instructor_courses} instructor_courses
		INNER JOIN {$wpdb->posts} courses ON instructor_courses.course_id = courses.ID
		WHERE 1 = 1 {$instructor_sql} {$in_sql} $not_in_ids_query ;" );

		$course_results = $wpdb->get_results( "SELECT courses.*, instructor_courses.ID as instructor_courses_table_id
		FROM {$wpdb->dozent_instructor_courses} instructor_courses
		INNER JOIN {$wpdb->posts} courses ON instructor_courses.course_id = courses.ID
		WHERE 1 = 1 {$instructor_sql} {$in_sql} $not_in_ids_query ORDER BY instructor_courses.ID DESC {$limit_query} ;" );

		$response = new DozentDBResponse();
		$response->count = $course_count;
		$response->results = $course_results;

		/**
		 * Filter the return of dozent_get_courses_by_instructor() function
		 *
		 * @since Dozent LMS v.1.0.0
		 *
		 * @param DozentDBResponse $response Query Results with
		 * @param object|mixed $instructor Instructor
		 */

		return apply_filters( 'dozent_get_courses_by_instructor', $response, $instructor );
	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.