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 | } |