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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / models / CourseModel.php
tutor / models Last commit date
CourseModel.php 3 years ago WithdrawModel.php 3 years ago
CourseModel.php
164 lines
1 <?php
2 namespace Tutor\Models;
3
4 /**
5 * Class CourseModel
6 * @since 2.0.6
7 */
8 class CourseModel {
9 /**
10 * WordPress course type name
11 * @var string
12 */
13 const POST_TYPE = 'courses';
14
15 const STATUS_PUBLISH = 'publish';
16 const STATUS_DRAFT = 'draft';
17 const STATUS_AUTO_DRAFT = 'auto-draft';
18 const STATUS_PENDING = 'pending';
19
20 /**
21 * Course record count
22 *
23 * @return int
24 *
25 * @since 2.0.7
26 */
27 public static function count( $status = self::STATUS_PUBLISH ) {
28 $count_obj = wp_count_posts( self::POST_TYPE );
29 if ( 'all' === $status ) {
30 return array_sum( (array) $count_obj );
31 }
32
33 return (int) $count_obj->{$status};
34 }
35
36 /**
37 * Get courses
38 *
39 * @param array $excludes exclude course ids
40 *
41 * @return array|null|object
42 *
43 * @since 1.0.0
44 */
45 public function get_courses( $excludes = array(), $post_status = array( 'publish' ) ) {
46 global $wpdb;
47
48 $excludes = (array) $excludes;
49 $exclude_query = '';
50
51 if ( count( $excludes ) ) {
52 $exclude_query = implode( "','", $excludes );
53 }
54
55 $post_status = array_map(
56 function ( $element ) {
57 return "'" . $element . "'";
58 },
59 $post_status
60 );
61
62 $post_status = implode( ',', $post_status );
63 $course_post_type = tutor()->course_post_type;
64
65 $query = $wpdb->get_results(
66 $wpdb->prepare(
67 "SELECT ID,
68 post_author,
69 post_title,
70 post_name,
71 post_status,
72 menu_order
73 FROM {$wpdb->posts}
74 WHERE post_status IN ({$post_status})
75 AND ID NOT IN('$exclude_query')
76 AND post_type = %s;
77 ",
78 $course_post_type
79 )
80 );
81
82 return $query;
83 }
84
85 /**
86 * Get courses for instructors
87 *
88 * @param int $instructor_id Instructor ID
89 *
90 * @return array|null|object
91 *
92 * @since 1.0.0
93 */
94 public function get_courses_for_instructors( $instructor_id = 0 ) {
95 $instructor_id = tutor_utils()->get_user_id( $instructor_id );
96 $course_post_type = tutor()->course_post_type;
97
98 $courses = get_posts(
99 array(
100 'post_type' => $course_post_type,
101 'author' => $instructor_id,
102 'post_status' => array( 'publish', 'pending' ),
103 'posts_per_page' => 5,
104 )
105 );
106
107 return $courses;
108 }
109
110 /**
111 * Mark the course as completed
112 *
113 * @param int $course_id course id which is completed
114 * @param int $user_id student id who completed the course
115 * @return bool
116 *
117 * @since 2.0.7
118 */
119 public static function mark_course_as_completed( $course_id, $user_id ) {
120 if ( ! $course_id || ! $user_id ) {
121 return false;
122 }
123
124 do_action( 'tutor_course_complete_before', $course_id );
125
126 /**
127 * Marking course completed at Comment
128 */
129 global $wpdb;
130
131 $date = date( 'Y-m-d H:i:s', tutor_time() );
132
133 // Making sure that, hash is unique
134 do {
135 $hash = substr( md5( wp_generate_password( 32 ) . $date . $course_id . $user_id ), 0, 16 );
136 $hasHash = (int) $wpdb->get_var(
137 $wpdb->prepare(
138 "SELECT COUNT(comment_ID) from {$wpdb->comments}
139 WHERE comment_agent = 'TutorLMSPlugin' AND comment_type = 'course_completed' AND comment_content = %s ",
140 $hash
141 )
142 );
143
144 } while ( $hasHash > 0 );
145
146 $data = array(
147 'comment_post_ID' => $course_id,
148 'comment_author' => $user_id,
149 'comment_date' => $date,
150 'comment_date_gmt' => get_gmt_from_date( $date ),
151 'comment_content' => $hash, // Identification Hash
152 'comment_approved' => 'approved',
153 'comment_agent' => 'TutorLMSPlugin',
154 'comment_type' => 'course_completed',
155 'user_id' => $user_id,
156 );
157
158 $wpdb->insert( $wpdb->comments, $data );
159
160 do_action( 'tutor_course_complete_after', $course_id, $user_id );
161
162 return true;
163 }
164 }