UserData


Source

File: classes/UserData.php

class UserData {

	private $user_id = 0;
	private $user_data;

	public static function instance( $user_ID ) {
		static $instance = null;

		// Only run these methods if they haven't been run previously
		if ( null === $instance ) {
			$instance = new self();

			if ( ! is_user_logged_in() ) {
				return $instance;
			}

			$instance->user_id = $user_ID;

			$dozent_user_data    = (array) maybe_unserialize( get_user_meta( $instance->user_id, '_dozent_user_data',
				true ) );
			$instance->user_data = $dozent_user_data;

		}

		// Always return the instance
		return $instance;
	}

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

	/**
	 * Get user data by Key
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @param  null  $key
	 *
	 * @return array|bool|mixed
	 */

	public function get( $key = null ) {
		$user_data = (array) $this->user_data;
		if ( $key ) {
			return dozent_array_get( $key, $user_data );
		}

		return $user_data;
	}

	/**
	 * Complete course content and save it to data
	 *
	 * long description
	 *
	 * Example usage:
	 *
	 *     $completed_id = dozent_complete_content( $content_id , $user_id );
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @see dozent_get_course_id_by_content();
	 *
	 * @param $content_id
	 * @param string $scope Scope of completion
	 *
	 * @return int
	 */

	public function complete_course_content( $content_id, $scope = 'complete' ) {
		global $wpdb;

		/**
		 * Complete the course content first
		 */

		$user_id = $this->user_id;

		if ( ! $user_id ) {
			return false;
		}

		$course_id = dozent_get_course_id_by_content( $content_id );

		$isCompleted
			= $wpdb->get_row( "SELECT * FROM {$wpdb->dozent_completes} WHERE course_id = {$course_id} AND content_id = {$content_id} AND user_id = {$user_id} " );

		if ( ! $isCompleted ) {

			$data = [
				'user_id'      => $user_id,
				'course_id'    => $course_id,
				'content_id'   => $content_id,
				'completed_at' => dozent_mysql_time(),
			];

			do_action( 'dozent/course_content/complete/before', $content_id, $data );

			$wpdb->insert( $wpdb->dozent_completes, $data );
			$complete_id = (int) $wpdb->insert_id;

			do_action( 'dozent/course_content/complete/after', $content_id, $data );

		} else {
			$complete_id = $isCompleted->complete->ID;
		}

		/**
		 * Then save all completed contents into the user data
		 */

		$this->_sync_complete_course_content_with_user_data( $course_id, $content_id, $scope);

		return $complete_id;
	}

	private function _sync_complete_course_content_with_user_data( $course_id, $content_id, $scope = 'complete' ) {
		global $wpdb;

		$user_id = $this->user_id;

		//Delete First
		$delete_state = true;
		if ( $scope === 'delete' ) {

			$content = get_post( $content_id );
			if ( $content->post_type === 'dozent_quiz' ) {
				$quiz_attempts = dozent_get_attempts( [ 'quiz_id' => $content_id ] );
				if ( $quiz_attempts->count ) {
					$delete_state = false;
				}
			}
		}

		/**
		 * now delete if state is deleted
		 */
		if ( $delete_state ) {
			$wpdb->delete( $wpdb->dozent_completes, [ 'content_id' => $content_id ] );
		}

		$total_contents = (int) dozent_count( dozent_get_course_contents( $course_id ) );

		$completes = $wpdb->get_results( "SELECT * FROM {$wpdb->dozent_completes} WHERE user_id = {$user_id} AND course_id = {$course_id} ", ARRAY_A );

		$completes = wp_list_pluck( $completes, 'content_id' );

		$completed_count = dozent_count( $completes );
		$percent         = 0;
		if ( $total_contents && $completed_count ) {
			$percent = (int) number_format( ( $completed_count * 100 ) / $total_contents );
		}

		$completed_courses                          = (array) $this->get( 'completed_courses' );
		$completed_courses[ $course_id ]['percent'] = $percent;

		//Save Array Unique
		$content_ids   = $completes;
		$content_ids[] = $content_id;

		/**
		 * Remove content iD from the user cache,
		 * if state is delete
		 */

		if ( $delete_state ) {
			if ( ( $key = array_search( $content_id, $content_ids ) ) !== false ) {
				unset( $content_ids[ $key ] );
			}
		}

		$completed_courses[ $course_id ]['content_ids'] = array_unique( $content_ids );

		$this->update( 'completed_courses', array_filter( $completed_courses ) );
	}


	/**
	 * Complete course save in the user data
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @see complete_course();
	 *
	 * @param  int  $course_id
	 */

	public function complete_course( $course_id = 0 ) {
		if ( $course_id ) {
			$completed_courses                                     = (array) $this->get( 'completed_courses' );
			$completed_courses[ $course_id ]['is_course_complete'] = 1;
			$this->update( 'completed_courses', array_filter( $completed_courses ) );
		}
	}

	/**
	 * @since DozentLMS 1.0.0
	 *
	 * @param  string  $value
	 *
	 * Update User data
	 *
	 * @param  null  $key
	 */

	public function update( $key = null, $value = '' ) {
		if ( $key ) {
			$data         = $this->get();
			$data[ $key ] = $value;

			/**
			 * Filter data before update user data
			 *
			 * @since DozentLMS 1.0.0
			 *
			 * @param  array  $data  User Data in array
			 * @param  int  $user_id  = $user_id
			 */

			$data = apply_filters( 'dozent/user_data/update', array_filter( $data ), $this->user_id );

			update_user_meta( $this->user_id, '_dozent_user_data', $data );
		}
	}


	/**
	 * Get all completed courses data
	 *
	 * Pass course ID to get specific course completion data
	 *
	 *
	 * @param  int  $course_id
	 *
	 * @return array|bool|mixed
	 */

	public function get_completed_courses( $course_id = 0 ) {
		if ( $course_id ) {
			return dozent_array_get( $course_id, $this->get( 'completed_courses' ) );
		}

		return $this->get( 'completed_courses' );
	}


	/**
	 * Enrol to the course
	 *
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @param  object|int  $course  Course Object or Course ID
	 * @param  int  $order_id
	 * @param  string  $expired_at  Date time mysql format
	 *
	 * @return bool|Model\Enrol
	 */

	public function enrol_course( $course, $order_id = null, $expired_at = '' ) {

		$course    = dozent_get_course( $course );
		$course_id = $course->get_id();

		$product_id = dozent_get_attached_product_id_by_course( $course );

		//Enrolment
		$enrol            = dozent_enrolment();
		$enrol->course_id = $course_id;
		$enrol->user_id   = $this->user_id;

		if ( $order_id ) {
			$enrol->order_id = $order_id;
		}

		if ( $product_id ) {
			$enrol->order_by_product_id = $product_id;
		}

		if ( $course->is_free() ) {
			$enrol->status = 'active';
		} else {
			$enrol->status = apply_filters( 'dozent_course_new_enrol_status', 'pending', $course, $order_id,
				$expired_at = '' );
		}

		if ( $expired_at ) {
			$enrol->expired_at = $expired_at;
		}

		/**
		 * Filter the pre enrol data
		 * Enrolment data is Object Format
		 *
		 * @since DozentLMS 1.0.0
		 *
		 * @param  object  $enrol  Enrolment object, set for insert data into 'dozent_enrols' table
		 * @param  object  $course  Course Object which need enrol
		 * @param  int|null  $order_id  Order ID if exists
		 * @param  string  $expired_at  If Exists
		 *
		 */

		$enrol = apply_filters( 'dozent_pre_course_enrol_data', $enrol, $course, $order_id = null, $expired_at = '' );

		$enrol->do_enrol();
		//End Enrolment

		if ( $course_id ) {
			$enrolled_courses   = (array) $this->get( 'enrolled_courses' );
			$enrolled_courses[] = $course_id;
			$this->update( 'enrolled_courses', array_filter( $enrolled_courses ) );
		}

		return $enrol;
	}

	/**
	 * Get all enrolled courses ids
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @return array
	 */

	public function get_enrolled_course_ids() {
		return (array) $this->get( 'enrolled_courses' );
	}

	/**
	 * Determine if enrolled to a course
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @param $course
	 * @param $force_check bool
	 *
	 * @return bool|mixed|void
	 */

	public function is_enrolled( $course, $force_check = false ) {
		$course = dozent_get_course( $course );

		if ( ! $this->user_id || ! $course->get_id() ) {
			return false;
		}

		$is_enrolled = false;

		if ( $force_check ) {
			global $wpdb;

			$course_id = $course->get_id();
			$user_id   = $this->user_id;

			$query = $wpdb->get_row( "SELECT * FROM {$wpdb->dozent_enrols} WHERE course_id = {$course_id} AND user_id = {$user_id} AND status = 'active' ORDER BY expired_at DESC " );

			if ( $query ) {
				if ( ! empty( $query->expired_at ) ) {
					if ( strtotime( $query->expired_at ) > dozent_time() ) {
						$is_enrolled = $query;
					}
				} else {
					$is_enrolled = $query;
				}
			}

		} else {
			$is_enrolled = in_array( $course->get_id(), $this->get_enrolled_course_ids() );
		}

		/**
		 * Filter the is enrolled return
		 *
		 * @since DozentLMS 1.0.0
		 *
		 * @param  bool  $is_enrolled  IS Enrolled
		 * @param  object  $course  Course Object
		 * @param  int  $user_id  User ID
		 */

		return apply_filters( 'user_data_is_course_enrolled', $is_enrolled, $course, $this->user_id );
	}

}

Methods