PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / curds / class-lp-course-curd.php

class-lp-course-curd.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9, at inc/curds/class-lp-course-curd.php

922 lines 22.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Course_CURD
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes/CURD
7 * @since 3.0.0
8 */
9
10 /**
11 * Prevent loading this file directly
12 */
13 defined( 'ABSPATH' ) || exit();
14
15 if ( ! class_exists( 'LP_Course_CURD' ) ) {
16
17 /**
18 * Class LP_Course_CURD
19 */
20 class LP_Course_CURD extends LP_Object_Data_CURD implements LP_Interface_CURD {
21
22 /**
23 * LP_Course_CURD constructor.
24 */
25 public function __construct() {
26 $this->_error_messages = array(
27 'COURSE_NOT_EXISTS' => __( 'Course does not exist.', 'learnpress' ),
28 );
29 }
30
31 /**
32 * Create course, with default meta.
33 *
34 * @param $args
35 *
36 * @return int|WP_Error
37 * @since 3.0.0
38 */
39 public function create( &$args ) {
40
41 $args = wp_parse_args(
42 $args,
43 array(
44 'id' => '',
45 'status' => 'publish',
46 'title' => __( 'New Course', 'learnpress' ),
47 'content' => '',
48 'author' => learn_press_get_current_user_id(),
49 )
50 );
51
52 $course_id = wp_insert_post(
53 array(
54 'ID' => $args['id'],
55 'post_type' => LP_COURSE_CPT,
56 'post_status' => $args['status'],
57 'post_title' => $args['title'],
58 'post_content' => $args['content'],
59 'post_author' => $args['author'],
60 )
61 );
62
63 if ( $course_id ) {
64 // add default meta for new course
65 $default_meta = LP_Course::get_default_meta();
66
67 if ( is_array( $default_meta ) ) {
68 foreach ( $default_meta as $key => $value ) {
69 update_post_meta( $course_id, '_lp_' . $key, $value );
70 }
71 }
72 }
73
74 return $course_id;
75 }
76
77 public function update( &$course ) {
78 // TODO: Implement update() method.
79 }
80
81 /**
82 * Delete course.
83 *
84 * @param object $course_id
85 *
86 * @since 3.0.0
87 * @editor tungnx
88 * @modify 4.1.4.1
89 */
90 public function delete( &$course_id ) {
91 // section curd
92 // $curd = new LP_Section_CURD( $course_id );
93 // clear course items
94 // $curd->clear();
95 }
96
97 /**
98 * Delete course itself and sections.
99 *
100 * @param int|object $course_id
101 * @param bool $delete_item - Optional. TRUE will delete all items assigned to course
102 */
103 public function delete_course( $course_id, $delete_item = false ) {
104 if ( $delete_item ) {
105 $course = learn_press_get_course( $course_id );
106
107 if ( $course ) {
108 $items = $course->get_items();
109
110 if ( $items ) {
111 foreach ( $items as $item ) {
112 wp_delete_post( $item );
113 }
114 }
115 }
116 }
117
118 wp_delete_post( $course_id );
119 }
120
121 /**
122 * Duplicate course.
123 *
124 * @param $course_id
125 * @param array $args
126 *
127 * @return mixed|WP_Error
128 * @since 3.0.0
129 */
130 public function duplicate_bk( &$course_id, $args = array() ) {
131
132 if ( ! $course_id ) {
133 return new WP_Error( __( '<p>Op! ID not found</p>', 'learnpress' ) );
134 }
135
136 if ( learn_press_get_post_type( $course_id ) != LP_COURSE_CPT ) {
137 return new WP_Error( __( '<p>Op! The course does not exist</p>', 'learnpress' ) );
138 }
139
140 // ensure that user can create course
141 if ( ! current_user_can( 'edit_posts' ) ) {
142 return new WP_Error(
143 __(
144 '<p>Sorry! You don\'t have permission to duplicate this course</p>',
145 'learnpress'
146 )
147 );
148 }
149 // duplicate course
150 $new_course_id = learn_press_duplicate_post( $course_id, $args );
151
152 if ( ! $new_course_id || is_wp_error( $new_course_id ) ) {
153 return new WP_Error( __( '<p>Sorry! Failed to duplicate course!</p>', 'learnpress' ) );
154 } else {
155
156 // original course section curd
157 $course = LP_Course::get_course( $course_id );
158
159 // new course section curd
160 $new_course_section_curd = new LP_Section_CURD( $new_course_id );
161
162 // curriculum course
163 // $course->load_curriculum();
164 $curriculum = $course->get_curriculum_raw();
165
166 // quiz curd
167 $quiz_curd = new LP_Quiz_CURD();
168
169 if ( is_array( $curriculum ) ) {
170
171 foreach ( $curriculum as $section ) {
172
173 $data = array(
174 'section_name' => $section['title'],
175 'section_course_id' => $new_course_id,
176 'section_order' => $section['order'],
177 'section_description' => $section['description'],
178 );
179
180 // clone sections to new course
181 $new_section = $new_course_section_curd->create( $data );
182
183 // get section items of original course
184 $items = $section['items'];
185
186 $new_items = array();
187
188 // duplicate items
189 if ( is_array( $items ) ) {
190 foreach ( $items as $key => $item ) {
191 // duplicate quiz
192 if ( $item['type'] == LP_QUIZ_CPT ) {
193 $new_item_id = $quiz_curd->duplicate(
194 $item['id'],
195 array( 'post_status' => 'publish' )
196 );
197 } else {
198 // clone lesson
199 $new_item_id = learn_press_duplicate_post(
200 $item['id'],
201 array( 'post_status' => 'publish' )
202 );
203 }
204
205 // get new items data to add to section
206 $new_items[ $key ] = array(
207 'id' => $new_item_id,
208 'type' => $item['type'],
209 );
210 }
211
212 // add new clone items to section
213 $new_course_section_curd->add_items_section( $new_section['section_id'], $new_items );
214 }
215 }
216
217 do_action( 'learn-press/after-duplicate', $course_id, $new_course_id, $args );
218
219 return $new_course_id;
220 }
221 }
222
223 return false;
224 }
225
226 /**
227 * Duplicate course.
228 *
229 * @param int $course_id
230 * @param array $args
231 *
232 * @return int|WP_Error
233 * @since 3.0.0
234 * @version 1.0.2
235 */
236 public function duplicate( &$course_id, $args = array() ) {
237 $course_id_new = 0;
238
239 try {
240 if ( ! current_user_can( 'edit_posts' ) ) {
241 throw new Exception( 'Sorry! You don\'t have permission to duplicate this course' );
242 }
243
244 $course_origin = learn_press_get_course( $course_id );
245 if ( ! $course_origin ) {
246 throw new Exception( 'Course is invalid' );
247 }
248
249 $course_id_new = learn_press_duplicate_post( $course_id, $args );
250
251 if ( is_wp_error( $course_id_new ) ) {
252 return $course_id_new;
253 }
254
255 $this->duplicate_sections_and_items( $course_id_new, $course_origin );
256
257 // Call save course duplicate
258 //$course_post = get_post( $course_id_new );
259 LP_Course_Post_Type::instance()->save( $course_id_new );
260
261 do_action( 'learn-press/after-duplicate', $course_id, $course_id_new, $args );
262 } catch ( Throwable $e ) {
263 $course_id_new = new WP_Error( $e->getMessage() );
264 }
265
266 return $course_id_new;
267 }
268
269 /**
270 * Duplicate sections and items of course
271 * @param int $course_id_new
272 * @param LP_Course $course_origin
273 */
274 public function duplicate_sections_and_items( $course_id_new, $course_origin ) {
275 // curriculum course
276 $curriculum = $course_origin->get_curriculum_raw();
277
278 // quiz curd
279 $quiz_curd = new LP_Quiz_CURD();
280
281 if ( is_array( $curriculum ) ) {
282 // new course section curd
283 $new_course_section_curd = new LP_Section_CURD( $course_id_new );
284 foreach ( $curriculum as $section ) {
285 $data = array(
286 'section_name' => $section['title'],
287 'section_course_id' => $course_id_new,
288 'section_order' => $section['order'],
289 'section_description' => $section['description'],
290 );
291
292 // clone sections to new course
293 $new_section = $new_course_section_curd->create( $data );
294
295 // get section items of original course
296 $items = $section['items'];
297
298 $new_items = array();
299
300 // duplicate items
301 if ( ! empty( $items ) ) {
302 foreach ( $items as $key => $item ) {
303 // duplicate quiz
304 if ( $item['type'] == LP_QUIZ_CPT ) {
305 $new_item_id = $quiz_curd->duplicate(
306 $item['id'],
307 array( 'post_status' => 'publish' )
308 );
309 } else {
310 // clone lesson
311 $new_item_id = learn_press_duplicate_post(
312 $item['id'],
313 array( 'post_status' => 'publish' )
314 );
315 }
316
317 // get new items data to add to section
318 $new_items[ $key ] = array(
319 'id' => $new_item_id,
320 'type' => $item['type'],
321 );
322 }
323
324 // add new clone items to section
325 $new_course_section_curd->add_items_section( $new_section['section_id'], $new_items );
326 }
327 }
328 }
329 }
330
331 /**
332 * Load course data
333 *
334 * @param LP_Course|LP_Abstract_Course $course
335 *
336 * @return mixed
337 */
338 public function load( &$course ) {
339 // $this->read_course_curriculum( $course->get_id() );
340
341 return $course;
342 }
343
344 /**
345 * Read all items in a course from database with an array in pair of
346 * post ID and post type.
347 *
348 * @param int $course_id
349 * @param bool $publish_only
350 *
351 * @return array
352 * @depecated 4.1.6.9
353 */
354 /*public function read_course_items( $course_id, $publish_only = true, $section_ids = array() ) {
355 global $wpdb;
356 $where = '';
357
358 if ( $publish_only ) {
359 $where = $wpdb->prepare(
360 '
361 AND c.post_status = %s
362 AND it.post_status = %s
363 ',
364 'publish',
365 'publish'
366 );
367 }
368
369 $types = learn_press_course_get_support_item_types( true );
370 $where .= $wpdb->prepare(
371 ' AND si.item_type IN(' . LP_Helper::db_format_array( $types, '%s' ) . ')',
372 $types
373 );
374
375 if ( $section_ids ) {
376 $where .= $wpdb->prepare(
377 ' AND s.section_id IN(' . LP_Helper::db_format_array(
378 $section_ids,
379 '%d'
380 ) . ')',
381 $section_ids
382 );
383 }
384
385 $query = $wpdb->prepare(
386 "
387 SELECT item_id id, it.post_type `type`, si.section_id
388 FROM {$wpdb->learnpress_section_items} si
389 INNER JOIN {$wpdb->learnpress_sections} s ON si.section_id = s.section_id
390 INNER JOIN {$wpdb->posts} c ON c.ID = s.section_course_id
391 INNER JOIN {$wpdb->posts} it ON it.ID = si.item_id
392 WHERE c.ID = %d
393 {$where}
394 ORDER BY s.section_order, si.item_order, si.section_item_id ASC
395 ",
396 $course_id
397 );
398
399 $results = $wpdb->get_results( $query );
400
401 return $results;
402
403 //////////////////////////////////////////// Code new - tungnx
404
405 $items = [];
406
407 $course = learn_press_get_course( $course_id );
408 if ( ! $course ) {
409 return $items;
410 }
411
412 $sections_items = $course->get_full_sections_and_items_course();
413 foreach ( $sections_items as $section_items ) {
414 $section_id = $section_items->id;
415
416 foreach ( $section_items->items as $item ) {
417 $item->section_id = $section_id;
418 $items[] = $item;
419 }
420 }
421
422 return $items;
423 }*/
424
425 /**
426 * Get all courses that contains an item by item id.
427 * Data returned is an array of all courses found.
428 *
429 * @param int $item_id - ID of any item
430 * @param bool $check_support - Optional. TRUE will check if course is support that item
431 *
432 * @return array|bool
433 * @since 3.1.0
434 */
435 public function get_course_by_item( $item_id, $check_support = true ) {
436 if ( $check_support && ! learn_press_is_support_course_item_type( get_post_type( $item_id ) ) ) {
437 return false;
438 }
439
440 global $wpdb;
441
442 $query = $wpdb->prepare(
443 "
444 SELECT c.ID
445 FROM {$wpdb->posts} c
446 INNER JOIN {$wpdb->learnpress_sections} s ON c.ID = s.section_course_id
447 INNER JOIN {$wpdb->learnpress_section_items} si ON si.section_id = s.section_id
448 WHERE si.item_id = %d
449 ",
450 $item_id
451 );
452
453 return $wpdb->get_col( $query );
454 }
455
456 /**
457 * @depecated 4.1.6.9
458 */
459 /*public static function update_items_format( $item ) {
460
461 if ( empty( $item['args']['course_id'] ) ) {
462 return false;
463 }
464
465 $course_id = absint( $item['args']['course_id'] );
466
467 delete_option( 'update_items_format_' . $course_id );
468
469 global $wpdb;
470
471 $course = learn_press_get_course( $course_id );
472
473 if ( ! $course ) {
474 return false;
475 }
476
477 $item_ids = $course->get_items();
478
479 if ( ! $item_ids ) {
480 return false;
481 }
482
483 $query = $wpdb->prepare(
484 "
485 SELECT object_id AS id, REPLACE(slug, 'post-format-', '') AS format
486 FROM {$wpdb->terms} AS t
487 INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
488 INNER JOIN {$wpdb->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id
489 WHERE tt.taxonomy IN (%s)
490 AND tr.object_id IN (" . join( ',', $item_ids ) . ')
491 ORDER BY t.name ASC
492 ',
493 'post_format'
494 );
495
496 $terms = $wpdb->get_results( $query );
497
498 if ( $terms ) {
499 $updated = array();
500 foreach ( $terms as $term ) {
501 update_post_meta( $term->id, 'post_format', $term->format );
502 $updated[] = $term->id;
503 }
504 $item_ids = array_diff( $item_ids, $updated );
505 }
506
507 if ( $item_ids ) {
508 foreach ( $item_ids as $item_id ) {
509 update_post_meta( $item_id, 'post_format', 'standard' );
510 }
511 }
512
513 return false;
514 }*/
515
516 /**
517 * Read curriculum of a course from db into cache.
518 *
519 * @param $course_id
520 *
521 * @return array|mixed
522 */
523 /*public function read_course_curriculum( $course_id ) {
524
525 if ( learn_press_get_post_type( $course_id ) != LP_COURSE_CPT ) {
526 return false;
527 }
528
529 if ( false !== LP_Course_Utils::get_course_items( $course_id ) ) {
530 return false;
531 }
532
533 if ( $this->get_course_sections( $course_id, 'ids' ) ) {
534 return $this->read_course_items( $course_id );
535 }
536
537 return false;
538 }*/
539
540 /**
541 * Get sections of course
542 *
543 * @param int $course_id
544 * @param string $return
545 *
546 * @return array
547 * @version 4.0.0
548 *
549 */
550 /*public function get_course_sections( $course_id, $return = '' ) {
551 // if ( false === ( $sections = LP_Object_Cache::get( $course_id, 'learn-press/course-sections' ) ) ) {
552 $sections = $this->read_course_sections( $course_id );
553 // }
554
555 // return $return === 'ids' ? LP_Object_Cache::get( $course_id, 'learn-press/course-sections-ids' ) : $sections;
556 return $return === 'ids' ? LP_Course_Utils::get_cached_db_sections( $course_id, 'ids' ) : $sections;
557 }*/
558
559 /**
560 * Retrieve total sections of a course.
561 *
562 * @param int $course_id
563 *
564 * @return int
565 * @since 3.1.0
566 */
567 public function count_sections( $course_id ) {
568 global $wpdb;
569
570 $query = $wpdb->prepare(
571 "
572 SELECT COUNT(section_id)
573 FROM {$wpdb->learnpress_sections}
574 WHERE section_course_id = %d
575 ",
576 $course_id
577 );
578
579 return $wpdb->get_var( $query );
580 }
581
582 /**
583 * Retrieve total items of a course.
584 *
585 * @param int $course_id
586 *
587 * @return array
588 * @since 3.1.0
589 * @editor tungnx
590 * @modify 4.1.4.1 - comment - not use
591 */
592 /*public function count_items( $course_id, $context = 'view' ) {
593 global $wpdb;
594 $params = array( $course_id );
595 $sql = "
596 SELECT COUNT(it.ID) `count`, it.post_type
597 FROM {$wpdb->learnpress_section_items} si
598 INNER JOIN {$wpdb->learnpress_sections} s ON si.section_id = s.section_id
599 INNER JOIN {$wpdb->posts} c ON c.ID = s.section_course_id
600 INNER JOIN {$wpdb->posts} it ON it.ID = si.item_id
601 WHERE s.section_course_id = %d";
602 if ( $context == 'view' ) {
603 $sql .= ' AND c.post_status = %s
604 AND it.post_status = %s ';
605 $params = array_merge( $params, array( 'publish', 'publish' ) );
606 }
607 $sql .= ' GROUP BY it.post_type ';
608 $query = $wpdb->prepare( $sql, $params );
609
610 $stats_object = array();
611 if ( $results = $wpdb->get_results( $query ) ) {
612 foreach ( $results as $result ) {
613 $stats_object[ $result->post_type ] = $result->count;
614 }
615 }
616
617 return $stats_object;
618 }*/
619
620 /**
621 * Read sections of a bundle of courses by ids
622 *
623 * @param int|array $course_id
624 *
625 * @return mixed|array
626 * @version 4.0.0
627 * @depecated 4.1.6.9
628 */
629 /*public function read_course_sections( $course_id ) {
630 global $wpdb;
631
632 settype( $course_id, 'array' );
633 $first_course_sections = false;
634
635 foreach ( $course_id as $cid ) {
636 $course_sections = LP_Course_Utils::get_cached_db_sections( $cid );
637
638 if ( false === $course_sections ) {
639 $query = $wpdb->prepare(
640 "
641 SELECT s.section_id, s.section_name, s.section_course_id, s.section_order, s.section_description, 'asd'
642 FROM {$wpdb->posts} p
643 INNER JOIN {$wpdb->learnpress_sections} s ON p.ID = s.section_course_id
644 WHERE p.ID = %d
645 ORDER BY p.ID, `section_order` ASC
646 ",
647 $cid
648 );
649
650 $course_sections = $wpdb->get_results( $query );
651
652 if ( ! $course_sections ) {
653 $course_sections = array();
654 }
655
656 if ( false === $first_course_sections ) {
657 $first_course_sections = $course_sections;
658 }
659
660 LP_Course_Utils::set_cache_db_sections( $cid, $course_sections );
661 }
662 }
663
664 return $first_course_sections;
665 }*/
666
667 /**
668 * Remove lesson, quiz from course's curriculum.
669 *
670 * @param int $item_id
671 * @param int $course_id - Optional. Added since 3.1.0
672 *
673 * @since 3.0.0
674 */
675 public function remove_item( $item_id, $course_id = 0 ) {
676 $learnpress_user_item_db = LP_User_Items_DB::getInstance();
677 global $wpdb;
678 // allow hook
679 do_action( 'learn-press/before-remove-section-item', $item_id, $course_id );
680
681 if ( $course_id ) {
682 if ( is_array( $course_id ) ) {
683 $format = array_fill( 0, sizeof( $course_id ), '%d' );
684 $where = $wpdb->prepare( 'AND s.section_course_id IN(' . join( ',', $format ) . ')', $course_id );
685 } else {
686 $where = $wpdb->prepare( 'AND s.section_course_id = %d', $course_id );
687 }
688 $query = $wpdb->prepare(
689 "
690 DELETE si
691 FROM {$wpdb->learnpress_section_items} si
692 INNER JOIN {$wpdb->learnpress_sections} s ON s.section_id = si.section_id
693 WHERE item_id = %d
694 {$where}
695 ",
696 $item_id
697 );
698 } else {
699
700 $query = $wpdb->prepare(
701 "
702 SELECT s.section_course_id
703 FROM {$wpdb->learnpress_sections} s
704 INNER JOIN {$wpdb->learnpress_section_items} si ON s.section_id = si.section_id
705 WHERE si.item_id = %d
706 ",
707 $item_id
708 );
709
710 $course_id = $wpdb->get_col( $query );
711 $query = $wpdb->prepare( "DELETE FROM {$wpdb->learnpress_section_items} WHERE item_id = %d", $item_id );
712 }
713
714 // delete item from course's section
715 $wpdb->query( $query );
716
717 if ( $course_id ) {
718 settype( $course_id, 'array' );
719
720 foreach ( $course_id as $cid ) {
721 do_action( 'learn-press/removed-item-from-section', $item_id, $cid );
722 $learnpress_user_item_db->reset_course_current_item( $cid, $item_id );
723 } // end foreach $course_id as $cid
724 }
725
726 learn_press_reset_auto_increment( 'learnpress_section_items' );
727 }
728
729 /**
730 * Remove all course data from database, includes section and course's items
731 *
732 * @param $course_id
733 * @editor tungnx
734 * @modify 4.1.4.1 - comment - not use
735 */
736 /*public function remove_course( $course_id ) {
737 global $wpdb;
738
739 $section_ids = $wpdb->get_col(
740 $wpdb->prepare(
741 "SELECT section_id FROM {$wpdb->prefix}learnpress_sections WHERE section_course_id = %d",
742 $course_id
743 )
744 );
745 if ( $section_ids ) {
746 $wpdb->query(
747 $wpdb->prepare(
748 "DELETE FROM {$wpdb->prefix}learnpress_section_items WHERE %d AND section_id IN(" . join(
749 ',',
750 $section_ids
751 ) . ')',
752 1
753 )
754 );
755 learn_press_reset_auto_increment( 'learnpress_section_items' );
756 }
757
758 // delete all sections
759 $query = $wpdb->prepare(
760 "
761 DELETE FROM {$wpdb->prefix}learnpress_sections
762 WHERE section_course_id = %d",
763 $course_id
764 );
765 $wpdb->query( $query );
766 learn_press_reset_auto_increment( 'learnpress_sections' );
767 }*/
768
769 /**
770 * Get recent courses.
771 *
772 * @param array $args
773 *
774 * @return array
775 */
776 public function get_recent_courses( $args = array() ) {
777 global $wpdb;
778
779 $limit = ! empty( $args['limit'] ) ? $args['limit'] : - 1;
780 $order = ! empty( $args['order'] ) ? $args['order'] : 'DESC';
781
782 if ( $limit <= 0 ) {
783 $limit = 0;
784 }
785
786 $query = apply_filters(
787 'learn-press/course-curd/query-recent-courses',
788 $wpdb->prepare(
789 "
790 SELECT DISTINCT p.ID
791 FROM $wpdb->posts AS p
792 WHERE p.post_type = %s
793 AND p.post_status = %s
794 ORDER BY p.post_date {$order}
795 LIMIT %d
796 ",
797 LP_COURSE_CPT,
798 'publish',
799 $limit
800 )
801 );
802
803 return $wpdb->get_col( $query );
804 }
805
806 /**
807 * Get all ID of users enrolled course ID.
808 *
809 * @param int $course_id
810 * @param int $limit
811 *
812 * @return array
813 * @Todo tungnx from LP 4.1.3.1 use LP_Course_DB::get_user_ids_enrolled replace
814 */
815 public function get_user_enrolled( $course_id, $limit = - 1 ) {
816 global $wpdb;
817 if ( $limit < 0 ) {
818 $limit = PHP_INT_MAX;
819 }
820 $query = $wpdb->prepare(
821 "
822 SELECT DISTINCT user.ID FROM {$wpdb->users} user
823 INNER JOIN {$wpdb->learnpress_user_items} user_item ON user_item.user_id = user.ID
824 WHERE user_item.item_id = %d
825 AND user_item.item_type = %s
826 LIMIT %d
827 ",
828 $course_id,
829 LP_COURSE_CPT,
830 $limit
831 );
832
833 return $wpdb->get_results( $query );
834 }
835
836 public function count_enrolled_users( $course_ids ) {
837 global $wpdb;
838
839 if ( ! $course_ids ) {
840 return 0;
841 }
842
843 $results = LP_Object_Cache::get( 'enrolled-users', 'learn-press/course' );
844
845 if ( $results && is_numeric( $course_ids ) && array_key_exists( $course_ids, $results ) ) {
846 return $results[ $course_ids ];
847 }
848 settype( $course_ids, 'array' );
849 $sql = $wpdb->prepare(
850 "
851 SELECT item_id cid, count(ID) `count`
852 FROM(SELECT DISTINCT user.ID,user_item.item_id FROM {$wpdb->users} user
853 INNER JOIN {$wpdb->learnpress_user_items} user_item ON user_item.user_id = user.ID
854 WHERE user_item.item_id IN(" . join( ',', $course_ids ) . ')
855 AND user_item.item_type = %s
856 ) AS X GROUP BY item_id',
857 LP_COURSE_CPT
858 );
859
860 $rows = $wpdb->get_results( $sql );
861 if ( $rows ) {
862 foreach ( $rows as $row ) {
863 $results[ $row->cid ] = $row->count;
864 }
865 }
866
867 $total = 0;
868 foreach ( $course_ids as $course_id ) {
869 if ( empty( $results[ $course_id ] ) ) {
870 $results[ $course_id ] = 0;
871 }
872 $total += $results[ $course_id ];
873 }
874 LP_Object_Cache::set( 'enrolled-users', $results, 'learn-press/course' );
875
876 return $total;
877 }
878
879 /**
880 * Get feature courses.
881 *
882 * @param array $args
883 *
884 * @return array
885 */
886 public function get_featured_courses( $args = array() ) {
887 global $wpdb;
888
889 $limit = ! empty( $args['limit'] ) ? $args['limit'] : - 1;
890 $order_by = ! empty( $args['order_by'] ) ? $args['order_by'] : 'post_date';
891 $order = ! empty( $args['order'] ) ? $args['order'] : 'DESC';
892
893 if ( $limit <= 0 ) {
894 $limit = 0;
895 }
896
897 $query = apply_filters(
898 'learn-press/course-curd/query-featured-courses',
899 $wpdb->prepare(
900 "
901 SELECT DISTINCT p.ID
902 FROM {$wpdb->posts} p
903 LEFT JOIN {$wpdb->postmeta} as pmeta ON p.ID=pmeta.post_id AND pmeta.meta_key = %s
904 WHERE p.post_type = %s
905 AND p.post_status = %s
906 AND pmeta.meta_value = %s
907 ORDER BY p.{$order_by} {$order}
908 LIMIT %d
909 ",
910 '_lp_featured',
911 LP_COURSE_CPT,
912 'publish',
913 'yes',
914 $limit
915 )
916 );
917
918 return $wpdb->get_col( $query );
919 }
920 }
921 }
922