| 1 |
<?php |
| 2 |
/** |
| 3 |
* Lesson Model |
| 4 |
* |
| 5 |
* @package Tutor\Models |
| 6 |
* @author Themeum <support@themeum.com> |
| 7 |
* @link https://themeum.com |
| 8 |
* @since 2.0.10 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Tutor\Models; |
| 12 |
|
| 13 |
use Tutor\Helpers\QueryHelper; |
| 14 |
|
| 15 |
/** |
| 16 |
* LessonModel Class |
| 17 |
* |
| 18 |
* @since 2.0.10 |
| 19 |
*/ |
| 20 |
class LessonModel { |
| 21 |
|
| 22 |
/** |
| 23 |
* Get total number of lesson |
| 24 |
* |
| 25 |
* @since 2.0.2 |
| 26 |
* |
| 27 |
* @since 3.7.1 Course ids param added |
| 28 |
* |
| 29 |
* @param array $course_ids Array of course ids. |
| 30 |
* |
| 31 |
* @return int |
| 32 |
*/ |
| 33 |
public static function get_total_lesson( array $course_ids = array() ) { |
| 34 |
global $wpdb; |
| 35 |
$lesson_type = tutor()->lesson_post_type; |
| 36 |
|
| 37 |
$course_in_clause = ''; |
| 38 |
if ( count( $course_ids ) ) { |
| 39 |
$prepare_ids = QueryHelper::prepare_in_clause( $course_ids ); |
| 40 |
$course_in_clause = "AND course.ID IN ({$prepare_ids})"; |
| 41 |
} |
| 42 |
|
| 43 |
$post_status = QueryHelper::prepare_in_clause( array( 'publish', 'future', 'draft', 'private', 'pending' ) ); |
| 44 |
$post_status_in_clause = "AND course.post_status IN ({$post_status})"; |
| 45 |
|
| 46 |
$sql = "SELECT COUNT(DISTINCT lesson.ID) |
| 47 |
FROM {$wpdb->posts} lesson |
| 48 |
INNER JOIN {$wpdb->posts} topic ON lesson.post_parent=topic.ID |
| 49 |
INNER JOIN {$wpdb->posts} course ON topic.post_parent=course.ID |
| 50 |
WHERE 1 = 1 |
| 51 |
{$course_in_clause} |
| 52 |
AND lesson.post_type = %s |
| 53 |
AND lesson.post_status = %s |
| 54 |
{$post_status_in_clause} |
| 55 |
AND topic.post_status = %s"; |
| 56 |
|
| 57 |
//phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 58 |
$result = $wpdb->get_var( $wpdb->prepare( $sql, $lesson_type, 'publish', 'publish' ) ); |
| 59 |
return $result; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get total lesson count by a course |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* |
| 67 |
* @param int $course_id course id. |
| 68 |
* @return int |
| 69 |
*/ |
| 70 |
public function get_lesson_count_by_course( $course_id = 0 ) { |
| 71 |
$course_id = tutor_utils()->get_post_id( $course_id ); |
| 72 |
return count( tutor_utils()->get_course_content_ids_by( tutor()->lesson_post_type, tutor()->course_post_type, $course_id ) ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get lesson reading info by key |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @param int $lesson_id lesson id. |
| 81 |
* @param int $user_id user id. |
| 82 |
* @param string $key key. |
| 83 |
* |
| 84 |
* @return array|bool|mixed |
| 85 |
*/ |
| 86 |
public function get_lesson_reading_info( $lesson_id = 0, $user_id = 0, $key = '' ) { |
| 87 |
$lesson_id = tutor_utils()->get_post_id( $lesson_id ); |
| 88 |
$user_id = tutor_utils()->get_user_id( $user_id ); |
| 89 |
$lesson_info = $this->get_lesson_reading_info_full( $lesson_id, $user_id ); |
| 90 |
|
| 91 |
return tutor_utils()->avalue_dot( $key, $lesson_info ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get student lesson reading current info |
| 96 |
* |
| 97 |
* @since 1.0.0 |
| 98 |
* |
| 99 |
* @param int $lesson_id lesson id. |
| 100 |
* @param int $user_id user id. |
| 101 |
* |
| 102 |
* @return array|bool|mixed |
| 103 |
*/ |
| 104 |
public function get_lesson_reading_info_full( $lesson_id = 0, $user_id = 0 ) { |
| 105 |
$lesson_id = tutor_utils()->get_post_id( $lesson_id ); |
| 106 |
$user_id = tutor_utils()->get_user_id( $user_id ); |
| 107 |
|
| 108 |
$lesson_info = (array) maybe_unserialize( get_user_meta( $user_id, '_lesson_reading_info', true ) ); |
| 109 |
return tutor_utils()->avalue_dot( $lesson_id, $lesson_info ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Update student lesson reading info |
| 114 |
* |
| 115 |
* @since 1.0.0 |
| 116 |
* |
| 117 |
* @param int $lesson_id lesson id. |
| 118 |
* @param int $user_id user id. |
| 119 |
* @param string $key key. |
| 120 |
* @param string $value value. |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public static function update_lesson_reading_info( $lesson_id = 0, $user_id = 0, $key = '', $value = '' ) { |
| 125 |
$lesson_id = tutor_utils()->get_post_id( $lesson_id ); |
| 126 |
$user_id = tutor_utils()->get_user_id( $user_id ); |
| 127 |
|
| 128 |
if ( $key ) { |
| 129 |
$lesson_info = (array) maybe_unserialize( get_user_meta( $user_id, '_lesson_reading_info', true ) ); |
| 130 |
$lesson_info[ $lesson_id ][ $key ] = $value; |
| 131 |
update_user_meta( $user_id, '_lesson_reading_info', $lesson_info ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Mark lesson complete |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
* |
| 140 |
* @param int $post_id post id. |
| 141 |
* @param int $user_id user id. |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public static function mark_lesson_complete( $post_id = 0, $user_id = 0 ) { |
| 146 |
$post_id = tutor_utils()->get_post_id( $post_id ); |
| 147 |
$user_id = tutor_utils()->get_user_id( $user_id ); |
| 148 |
|
| 149 |
do_action( 'tutor_mark_lesson_complete_before', $post_id, $user_id ); |
| 150 |
update_user_meta( $user_id, '_tutor_completed_lesson_id_' . $post_id, tutor_time() ); |
| 151 |
do_action( 'tutor_mark_lesson_complete_after', $post_id, $user_id ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get lesson details. |
| 156 |
* |
| 157 |
* @since 3.7.0 |
| 158 |
* |
| 159 |
* @param int $lesson_id lesson id. |
| 160 |
* |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
public static function get_lesson_details( $lesson_id ) { |
| 164 |
$post = get_post( $lesson_id, ARRAY_A ); |
| 165 |
|
| 166 |
if ( $post ) { |
| 167 |
$post['thumbnail_id'] = get_post_meta( $lesson_id, '_thumbnail_id', true ); |
| 168 |
$post['thumbnail'] = get_the_post_thumbnail_url( $lesson_id ); |
| 169 |
$post['attachments'] = tutor_utils()->get_attachments( $lesson_id ); |
| 170 |
|
| 171 |
$video = maybe_unserialize( get_post_meta( $lesson_id, '_video', true ) ); |
| 172 |
if ( $video ) { |
| 173 |
$source = $video['source'] ?? ''; |
| 174 |
if ( 'html5' === $source ) { |
| 175 |
$poster_url = wp_get_attachment_url( $video['poster'] ?? 0 ); |
| 176 |
$source_html5 = wp_get_attachment_url( $video['source_video_id'] ?? 0 ); |
| 177 |
$video['poster_url'] = $poster_url; |
| 178 |
$video['source_html5'] = $source_html5; |
| 179 |
} |
| 180 |
} |
| 181 |
$post['video'] = $video; |
| 182 |
} else { |
| 183 |
$post = array(); |
| 184 |
} |
| 185 |
|
| 186 |
$data = apply_filters( 'tutor_lesson_details_response', $post, $lesson_id ); |
| 187 |
|
| 188 |
return $data; |
| 189 |
} |
| 190 |
} |
| 191 |
|