model.llms.instructor.php 7.84 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7
<?php
/**
 * LifterLMS Instructor
 *
 * @package LifterLMS/Models/Classes
 *
 * @since 3.13.0
Cyrille's avatar
Cyrille committed
8
 * @version 6.0.0
cyrille's avatar
cyrille committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
 */

defined( 'ABSPATH' ) || exit;

/**
 * LLMS_Instructor model class
 *
 * Manages data and interactions with a LifterLMS Instructor or Instructor's Assistant.
 *
 * @since 3.13.0
 * @since 3.30.3 Fixed typo in "description" key of the the toArray() method.
 * @since 3.32.0 Add validation to data passed into the `get_students()` method.
 * @since 3.34.0 Fix issue causing `get_assistants()` to return assistants to the currently logged in user instead of using the user id of the current object.
 *               Add `has_student()` method.
 */
class LLMS_Instructor extends LLMS_Abstract_User_Data {

	/**
	 * Add a parent instructor to an assistant instructor
	 *
	 * @param    mixed $parent_ids WP User ID of the parent instructor or array of User IDs to add multiple
	 * @return   boolean
	 * @since    3.13.0
	 * @version  3.14.4
	 */
	public function add_parent( $parent_ids ) {

		// Get existing parents.
		$parents = $this->get( 'parent_instructors' );

		// No existing, use an empty array as the default.
		if ( ! $parents ) {
			$parents = array();
		}

		if ( ! is_array( $parent_ids ) ) {
			$parent_ids = array( $parent_ids );
		}

		// Make ints.
		$parent_ids = array_map( 'absint', $parent_ids );

		// Add the new parents.
		$parents = array_unique( array_merge( $parents, $parent_ids ) );

		// Remove duplicates and save.
		return $this->set( 'parent_instructors', array_unique( $parents ) );

	}

	/**
	 * Retrieve an array of user ids for assistant instructors attached to the instructor
	 *
	 * @since 3.14.4
	 * @since 3.34.0 Uses object ID instead of current user id.
	 *
	 * @return int[]
	 */
	public function get_assistants() {

		global $wpdb;
		$results = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT user_id FROM {$wpdb->usermeta}
			 WHERE meta_key = 'llms_parent_instructors'
			   AND meta_value LIKE %s;",
				'%i:' . $this->get_id() . ';%'
			)
		); // db call ok; no-cache ok.

		return $results;

	}

	/**
	 * Retrieve instructor's courses
	 *
	 * @uses     $this->get_posts()
	 * @param    array  $args    query argument, see $this->get_posts()
	 * @param    string $return  return format, see $this->get_posts()
	 * @return   mixed
	 * @since    3.13.0
	 * @version  3.13.0
	 */
	public function get_courses( $args = array(), $return = 'llms_posts' ) {

		$args = wp_parse_args(
			$args,
			array(
				'post_type' => 'course',
			)
		);
		return $this->get_posts( $args, $return );

	}

	/**
	 * Retrieve instructor's memberships
	 *
	 * @uses     $this->get_posts()
	 * @param    array  $args    query argument, see $this->get_posts()
	 * @param    string $return  return format, see $this->get_posts()
	 * @return   mixed
	 * @since    3.13.0
	 * @version  3.13.0
	 */
	public function get_memberships( $args = array(), $return = 'llms_posts' ) {

		$args = wp_parse_args(
			$args,
			array(
				'post_type' => 'llms_membership',
			)
		);
		return $this->get_posts( $args, $return );

	}

	/**
	 * Retrieve instructor's posts (courses and memberships, mixed)
	 *
	 * @param    array  $args    query arguments passed to WP_Query
	 * @param    string $return  return format [llms_posts|ids|posts|query]
	 * @return   mixed
	 * @since    3.13.0
	 * @version  3.13.0
	 */
	public function get_posts( $args = array(), $return = 'llms_posts' ) {

Cyrille's avatar
Cyrille committed
138
		$serialized_id = serialize( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
cyrille's avatar
cyrille committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
			array(
				'id' => $this->get_id(),
			)
		);
		$serialized_id = str_replace( array( 'a:1:{', '}' ), '', $serialized_id );

		$args = wp_parse_args(
			$args,
			array(
				'post_type'   => array( 'course', 'llms_membership' ),
				'post_status' => 'publish',
				'meta_query'  => array(
					array(
						'compare' => 'LIKE',
						'key'     => '_llms_instructors',
						'value'   => $serialized_id,
					),
				),
			)
		);

		$query = new WP_Query( $args );

		if ( 'llms_posts' === $return ) {
			$ret = array();
			foreach ( $query->posts as $post ) {
				$ret[] = llms_get_post( $post );
			}
			return $ret;
		} elseif ( 'ids' === $return ) {
			return wp_list_pluck( $query->posts, 'ID' );
		} elseif ( 'posts' === $return ) {
			return $query->posts;
		}

		// If 'query' === $return.
		return $query;

	}

	/**
	 * Retrieve instructor's students
	 *
	 * @since 3.13.0
	 * @since 3.32.0 Validate `post_id` data passed into this function to ensure only students
Cyrille's avatar
Cyrille committed
184 185
	 *               in courses/memberships for this instructor are returned.
	 * @since 6.0.0 Don't access `LLMS_Student_Query` properties directly.
cyrille's avatar
cyrille committed
186 187 188 189
	 *
	 * @see LLMS_Student_Query
	 *
	 * @param array $args Array of args passed to LLMS_Student_Query.
Cyrille's avatar
Cyrille committed
190
	 * @return LLMS_Student_Query
cyrille's avatar
cyrille committed
191 192 193 194 195 196 197 198 199 200 201
	 */
	public function get_students( $args = array() ) {

		$ids = $this->get_posts(
			array(
				'posts_per_page' => -1,
			),
			'ids'
		);

		// If post IDs were passed we need to verify they're IDs that the instructor has access to.
Cyrille's avatar
Cyrille committed
202
		if ( ! empty( $args['post_id'] ) ) {
cyrille's avatar
cyrille committed
203 204
			$args['post_id'] = ! is_array( $args['post_id'] ) ? array( $args['post_id'] ) : $args['post_id'];
			$args['post_id'] = array_intersect( $args['post_id'], $ids );
Cyrille's avatar
Cyrille committed
205 206
		} else {
			// No post IDs passed in, query all of the instructor's posts.
cyrille's avatar
cyrille committed
207 208
			$args['post_id'] = $ids;
		}
Cyrille's avatar
Cyrille committed
209
		// The instructor has no posts, so we want to force no results.
cyrille's avatar
cyrille committed
210
		// @todo add an instructor query parameter to the student query.
Cyrille's avatar
Cyrille committed
211 212 213
		if ( empty( $args['post_id'] ) ) {
			$args['per_page']      = 0;
			$args['no_found_rows'] = true;
cyrille's avatar
cyrille committed
214 215
		}

Cyrille's avatar
Cyrille committed
216
		return new LLMS_Student_Query( $args );
cyrille's avatar
cyrille committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

	}

	/**
	 * Determines if the instructor is an instructor to a specific student.
	 *
	 * @since 3.34.0
	 *
	 * @param LLMS_Student|WP_User|int $student Student or user object or WP User ID.
	 * @return bool
	 */
	public function has_student( $student ) {

		$student = llms_get_student( $student );
		if ( ! $student ) {
			return false;
		}

		$ids = $this->get_posts(
			array(
				'posts_per_page' => -1,
			),
			'ids'
		);

		if ( ! $ids ) {
			return false;
		}

		return $student->is_enrolled( $ids, 'any' );

	}

	/**
	 * Determine if the user is an instructor on a post
	 *
	 * @param    int $post_id  WP Post ID of a course or membership
	 * @return   boolean
	 * @since    3.13.0
	 * @version  3.13.0
	 */
	public function is_instructor( $post_id = null ) {

		$ret = false;

		// Use current post if no post is set.
		if ( ! $post_id ) {
			global $post;
			if ( ! $post ) {
				return apply_filters( 'llms_instructor_is_instructor', $ret, $post_id, $this );
			}
			$post_id = $post->ID;
		}

		$check_id = false;

		switch ( get_post_type( $post_id ) ) {

			case 'course':
				$check_id = $post_id;
				break;

			case 'llms_membership':
				$check_id = $post_id;
				break;

			case 'llms_question':
				$question = llms_get_post( $post_id );
				$check_id = array();
				foreach ( $question->get_quizzes() as $qid ) {
					$course = llms_get_post_parent_course( $qid );
					if ( $course ) {
						$check_id[] = $course->get( 'id' );
					}
				}
				break;

			default:
				$course = llms_get_post_parent_course( $post_id );
				if ( $course ) {
					$check_id = $course->get( 'id' );
				}
		}

		if ( $check_id ) {

			$check_ids = ! is_array( $check_id ) ? array( $check_id ) : $check_id;

			$query = $this->get_posts(
				array(
					'post__in'       => $check_ids,
					'posts_per_page' => 1,
				),
				'query'
			);

			$ret = $query->have_posts();

		}

		return apply_filters( 'llms_instructor_is_instructor', $ret, $post_id, $check_id, $this );

	}

	/**
	 * Used by exporter / cloner to get instructor data
	 *
	 * @since 3.16.11
	 * @since 3.30.3 Renamed "descrpition" key to "description".
	 *
	 * @return array
	 */
	public function toArray() {
		return array(
			'description' => $this->get( 'description' ),
			'email'       => $this->get( 'user_email' ),
			'first_name'  => $this->get( 'first_name' ),
			'id'          => $this->get_id(),
			'last_name'   => $this->get( 'last_name' ),
		);
	}

}