llms-trait-student-awards.php 5.65 KB
Newer Older
Cyrille's avatar
Cyrille committed
1 2 3 4 5 6 7 8 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 138 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
<?php
/**
 * LifterLMS singleton trait.
 *
 * @package LifterLMS/Traits
 *
 * @since 6.0.0
 * @version 6.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Retrieve data related to awards earned by a student.
 *
 * This trait should only be used by classes that extend from the {@see LLMS_Abstract_User_Data} class.
 *
 * @since 6.0.0
 */
trait LLMS_Trait_Student_Awards {

	/**
	 * Retrieve achievements that a user has earned.
	 *
	 * @since 2.4.0
	 * @since 3.14.0 Unknown.
	 * @since 6.0.0 Moved from `LLMS_Student` class.
	 *              Introduced alternate usage via `LLMS_Awards_Query` and deprecated previous behavior.
	 *
	 * @param string|array $args_or_orderby An array of arguments to pass to LLMS_Awards_Query. The deprecated method
	 *                                      signature accepts a string representing the field to order the returned results by.
	 * @param string       $order           Deprecated signature only: Ordering method for returned results (ASC or DESC).
	 * @param string       $return          Deprecated signature only: Return type. Accepts "obj" for an array of objects from
	 *                                      $wpdb->get_results and "certificates" for an array of LLMS_User_Certificate instances.
	 * @return LLMS_Awards_Query|object[]|LLMS_User_Achievement[]
	 */
	public function get_achievements( $args_or_orderby = 'updated_date', $order = 'DESC', $return = 'obj' ) {

		// New behavior.
		if ( is_array( $args_or_orderby ) ) {
			return $this->get_awards( $args_or_orderby, 'achievement' );
		}

		_deprecated_function( 'LLMS_Student::get_achievements()', '6.0.0', 'The behavior of this method has changed. Please refer to https://developer.lifterlms.com/reference/classes/llms_student/get_achievements/ for more information.' );

		$orderby = esc_sql( $args_or_orderby );
		$order   = esc_sql( $order );

		global $wpdb;

		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$query = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT post_id, meta_value AS achievement_id, updated_date AS earned_date FROM {$wpdb->prefix}lifterlms_user_postmeta WHERE user_id = %d and meta_key = '_achievement_earned' ORDER BY $orderby $order",
				$this->get_id()
			)
		);// db call ok; no-cache ok.
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		if ( 'achievements' === $return ) {
			$ret = array();
			foreach ( $query as $obj ) {
				$ret[] = new LLMS_User_Achievement( $obj->achievement_id );
			}
			return $ret;
		}

		return $query;

	}

	/**
	 * Query student awards.
	 *
	 * @since 6.0.0
	 *
	 * @param array  $args Query arguments to pass into `LLMS_Awards_Query`.
	 * @param string $type Award type. Accepts "any", "achievement", or "certificate".
	 * @return LLMS_Awards_Query
	 */
	public function get_awards( $args = array(), $type = 'any' ) {

		$args['type']  = $type;
		$args['users'] = $this->get_id();

		// Prevent potential funny business.
		unset( $args['users__exclude'] );

		return new LLMS_Awards_Query( $args );
	}

	/**
	 * Retrieve the total number of awards earned by the student.
	 *
	 * @since 6.0.0
	 *
	 * @param string $type Award type. Accepts "any", "achievement", or "certificate".
	 * @return int
	 */
	public function get_awards_count( $type = 'any' ) {

		$query = $this->get_awards(
			array(
				'per_page' => 1,
				'fields'   => 'ids',
			),
			$type
		);
		return $query->get_found_results();

	}

	/**
	 * Retrieve certificates that the student has been awarded.
	 *
	 * The default behavior of this method is deprecated since version [version]. The previous behavior
	 * is retained for backwards compatibility but will be removed in the next major release.
	 *
	 * @since 2.4.0
	 * @since 3.14.1 Unknown.
	 * @since 6.0.0 Moved from `LLMS_Student` class.
	 *              Introduced alternate usage via `LLMS_Awards_Query` and deprecated previous behavior.
	 *
	 * @param string|array $args_or_orderby An array of arguments to pass to LLMS_Awards_Query. The deprecated method
	 *                                      signature accepts a string representing the field to order the returned results by.
	 * @param string       $order           Deprecated signature only: Ordering method for returned results (ASC or DESC).
	 * @param string       $return          Deprecated signature only: Return type. Accepts "obj" for an array of objects from
	 *                                      $wpdb->get_results and "certificates" for an array of LLMS_User_Certificate instances.
	 * @return LLMS_Awards_Query|object[]|LLMS_User_Certificate[]
	 */
	public function get_certificates( $args_or_orderby = 'updated_date', $order = 'DESC', $return = 'obj' ) {

		// New behavior.
		if ( is_array( $args_or_orderby ) ) {
			return $this->get_awards( $args_or_orderby, 'certificate' );
		}

		_deprecated_function( 'LLMS_Student::get_certificates()', '6.0.0', 'The behavior of this method has changed. Please refer to https://developer.lifterlms.com/reference/classes/llms_student/get_certificates/ for more information.' );

		$orderby = esc_sql( $args_or_orderby );
		$order   = esc_sql( $order );

		global $wpdb;

		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$query = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT post_id, meta_value AS certificate_id, updated_date AS earned_date FROM {$wpdb->prefix}lifterlms_user_postmeta WHERE user_id = %d and meta_key = '_certificate_earned' ORDER BY $orderby $order",
				$this->get_id()
			)
		); // db call ok; no-cache ok.
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		if ( 'certificates' === $return ) {
			$ret = array();
			foreach ( $query as $obj ) {
				$ret[] = new LLMS_User_Certificate( $obj->certificate_id );
			}
			return $ret;
		}

		return $query;

	}

}