Course


Source

File: classes/Course.php

class Course {

	protected $id = 0;
	protected $title = null;

	//Access method
	protected $price_type = null;
	protected $is_public = null;
	protected $is_protected = null;
	protected $is_free = null;
	protected $is_closed = null;

	/**
	 * Return Course post object
	 *
	 * @var \WP_Post
	 */

	public $post;


	public function get_course( $course = null ) {

		if ( $course instanceof Course ){
			return $course;
		}

		$course = get_post( $course );

		$this->id    = $course->ID;
		$this->title = get_the_title( $course );
		$this->post  = $course;


		$this->price_type = dozent_get_course_price_type( $course );

		$this->is_public    = ( $this->price_type === 'public' );
		$this->is_protected = ( $this->price_type === 'protected' );
		$this->is_free      = ( $this->price_type === 'free' );
		$this->is_closed    = ( $this->price_type === 'closed' );


		return $this;
	}

	/**
	 * Get Course id
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @return int
	 */

	public function get_id() {
		return $this->id;
	}

	/**
	 * Get the course contents from DB Query
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @return mixed|void|null
	 */

	public function get_contents() {
		global $wpdb;

		if ( ! $this->id ) {
			return null;
		}

		$contents = wp_cache_get( $this->id, 'dozent_course_contents' );

		if ( ! $contents ) {
			$contents = $wpdb->get_results( "SELECT items.* FROM {$wpdb->posts} sections
				INNER JOIN {$wpdb->posts} items ON sections.ID = items.post_parent 
				WHERE sections.post_parent = {$this->id} AND items.post_status = 'publish' order by sections.menu_order ASC, items.menu_order ASC;" );

			wp_cache_add( $this->id, $contents, 'dozent_course_contents' );
		}

		/**
		 * Filter the course contents
		 *
		 * @since DozentLMS 1.0.0
		 *
		 * @param  null|object  $contents  Course Contents
		 * @param  object  $this  Course
		 */

		return apply_filters( 'dozent_get_course_contents', $contents, $this );
	}


	/**
	 * Get course price type
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @return null|string
	 */

	public function get_price_type() {
		return $this->price_type;
	}

	/**
	 * Determine if the course is public
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @return bool
	 */

	public function is_public() {
		return $this->is_public;
	}


	/**
	 * Determine if the course is protected
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @return bool
	 */

	public function is_protected() {
		return $this->is_protected;
	}

	/**
	 * Determine if the course is Free
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @return bool
	 */

	public function is_free() {
		return $this->is_free;
	}

	/**
	 * Determine if the course is Closed
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @return bool
	 */

	public function is_closed() {
		return $this->is_closed;
	}

	public function permalink() {

		return get_the_permalink( $this->get_id() );
	}

	public function price() {

		$price = false;

		/**
		 * Filter the course price
		 *
		 * @since Dozent 1.0.0
		 *
		 * @param  bool|float  $price
		 * @param  int  $course_id  Course id
		 * @param  string  $price_type  Course Price Type
		 */

		return apply_filters( 'dozent_course_raw_price', $price, $this->id, $this->get_price_type() );
	}


	/**
	 * Return the start course URL sequently
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @param  null  $user
	 *
	 * @return false|string|null
	 */

	public function start_uri( $user = null ) {
		$contents = $this->get_contents();

		if ( ! dozent_count( $contents ) ) {
			return null;
		}

		$content_item = dozent_array_get( 0, $contents );

		if ( $this->is_public() || $this->is_protected() ) {
			return get_the_permalink( $content_item );
		}

		$completed_content_ids = $this->completed_content_ids( $user );

		foreach ( $contents as $content ) {
			if ( ! in_array( $content->ID, $completed_content_ids ) ) {
				$content_item = $content;
				break;
			}
		}

		return get_the_permalink( $content_item );
	}

	public function completed_content_ids( $user = null ) {
		$user = dozent_user_data( $user );

		$course_complete_data = $user->get_completed_courses( $this->id );

		$completed_content_ids = [];
		if ( dozent_count( $course_complete_data ) ) {

			$content_ids = (array) dozent_array_get( 'content_ids', $course_complete_data );

			if ( dozent_count( $content_ids ) ) {
				$completed_content_ids = (array) array_filter( dozent_array_get( 'content_ids',
					$course_complete_data ) );
			}
		}


		return $completed_content_ids;
	}

	public function has_permission( $user = null ) {
		$user = dozent_user_data( $user );


		if ( $this->is_public() ) {

			/**
			 * Filter the course permission
			 *
			 * @since DozentLMS 1.0.0
			 *
			 * @param  bool  $bool  Bool
			 * @param  object  $course  course Object
			 * @param  object  $user  User Object
			 */

			return apply_filters( 'course_has_permission', true, $this, $user );
		}

		if ( ! is_user_logged_in() ) {
			return apply_filters( 'course_has_permission', false, $this, $user );
		}

		/**
		 * Given access to protected status = need only logged in
		 * Close Status: need to enrol by purchasing product or course
		 */

		if ( $this->is_protected() || dozent_course_is_enrolled( $this->id, $user, true ) ) {
			return apply_filters( 'course_has_permission', true, $this, $user );
		}

		/**
		 * TODO: Need Check Membership Permission and Group Enrolment Permission
		 */

		/**
		 * Filter course permission
		 *
		 * @since DozentLMS 1.0.0
		 * @param bool $bool Default False
		 * @param object|int $user User Object or ID
		 */

		return apply_filters( 'course_has_permission', false, $this, $user );
	}

}

Methods