| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Databases; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LearnPress\Filters\CourseSectionFilter; |
| 7 |
use LearnPress\Models\CourseModel; |
| 8 |
use LearnPress\Models\CourseSectionItemModel; |
| 9 |
use LearnPress\Models\CourseSectionModel; |
| 10 |
use LearnPress\Models\PostModel; |
| 11 |
use LP_Helper; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class CourseSectionDB |
| 19 |
* |
| 20 |
* Refactor of LP_Section_DB |
| 21 |
* |
| 22 |
* @since 4.3.0 |
| 23 |
* @version 1.0.0 |
| 24 |
*/ |
| 25 |
class CourseSectionDB extends DataBase { |
| 26 |
public static $_instance; |
| 27 |
|
| 28 |
public function __construct() { |
| 29 |
parent::__construct(); |
| 30 |
} |
| 31 |
|
| 32 |
public static function getInstance() { |
| 33 |
if ( is_null( self::$_instance ) ) { |
| 34 |
self::$_instance = new self(); |
| 35 |
} |
| 36 |
|
| 37 |
return self::$_instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get sections |
| 42 |
* |
| 43 |
* @throws Exception |
| 44 |
* @since 4.1.6 |
| 45 |
* @version 1.0.2 |
| 46 |
*/ |
| 47 |
public function get_sections( CourseSectionFilter $filter, &$total_rows = 0 ) { |
| 48 |
$default_fields = $filter->all_fields; |
| 49 |
$filter->fields = array_merge( $default_fields, $filter->fields ); |
| 50 |
|
| 51 |
if ( empty( $filter->collection ) ) { |
| 52 |
$filter->collection = $this->tb_lp_sections; |
| 53 |
} |
| 54 |
|
| 55 |
if ( empty( $filter->collection_alias ) ) { |
| 56 |
$filter->collection_alias = 'st'; |
| 57 |
} |
| 58 |
|
| 59 |
$filter->field_count = 'st.section_id'; |
| 60 |
|
| 61 |
if ( isset( $filter->section_id ) ) { |
| 62 |
$filter->where[] = $this->wpdb->prepare( 'AND st.section_id = %d', $filter->section_id ); |
| 63 |
} |
| 64 |
|
| 65 |
if ( isset( $filter->section_course_id ) ) { |
| 66 |
$filter->where[] = $this->wpdb->prepare( 'AND st.section_course_id = %d', $filter->section_course_id ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( isset( $filter->section_name ) ) { |
| 70 |
$filter->where[] = $this->wpdb->prepare( 'AND st.section_name LIKE %s', '%' . $filter->section_name . '%' ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! empty( $filter->section_ids ) ) { |
| 74 |
$section_ids_format = LP_Helper::db_format_array( $filter->section_ids, '%d' ); |
| 75 |
$filter->where[] = $this->wpdb->prepare( 'AND st.section_id IN (' . $section_ids_format . ')', $filter->section_ids ); |
| 76 |
} |
| 77 |
|
| 78 |
// Default Order |
| 79 |
if ( empty( $filter->order ) ) { |
| 80 |
$filter->order_by = 'st.section_order'; |
| 81 |
$filter->order = 'ASC'; |
| 82 |
} |
| 83 |
|
| 84 |
return $this->execute( $filter, $total_rows ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get last section order of course |
| 89 |
* |
| 90 |
* @param int $course_id |
| 91 |
* |
| 92 |
* @return int |
| 93 |
* @throws Exception |
| 94 |
* @since 4.1.6.9 |
| 95 |
* @version 1.0.0 |
| 96 |
*/ |
| 97 |
public function get_last_number_order( int $course_id = 0 ): int { |
| 98 |
$query = $this->wpdb->prepare( |
| 99 |
"SELECT MAX(section_order) |
| 100 |
FROM $this->tb_lp_sections |
| 101 |
WHERE section_course_id = %d", |
| 102 |
$course_id |
| 103 |
); |
| 104 |
|
| 105 |
$number_order = intval( $this->wpdb->get_var( $query ) ); |
| 106 |
|
| 107 |
$this->check_execute_has_error(); |
| 108 |
|
| 109 |
return $number_order; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Update sections position |
| 114 |
* Update section_order of each section in course |
| 115 |
* |
| 116 |
* @throws Exception |
| 117 |
* @since 4.2.8.6 |
| 118 |
* @version 1.0.0 |
| 119 |
*/ |
| 120 |
public function update_sections_position( array $section_ids, $section_course_id ) { |
| 121 |
$filter = new CourseSectionFilter(); |
| 122 |
$filter->collection = $this->tb_lp_sections; |
| 123 |
$SET_SQL = 'section_order = CASE'; |
| 124 |
|
| 125 |
foreach ( $section_ids as $position => $section_id ) { |
| 126 |
++ $position; |
| 127 |
$section_id = absint( $section_id ); |
| 128 |
if ( empty( $section_id ) ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
$SET_SQL .= $this->wpdb->prepare( ' WHEN section_id = %d THEN %d', $section_id, $position ); |
| 133 |
} |
| 134 |
|
| 135 |
$SET_SQL .= ' ELSE section_order END'; |
| 136 |
$filter->set[] = $SET_SQL; |
| 137 |
$filter->where[] = $this->wpdb->prepare( 'AND section_course_id = %d', $section_course_id ); |
| 138 |
|
| 139 |
$this->update_execute( $filter ); |
| 140 |
} |
| 141 |
} |
| 142 |
|