PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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 4.2.1 All 138 releases
learnpress / inc / databases / class-lp-section-db.php

class-lp-section-db.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7, at inc/databases/class-lp-section-db.php

337 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 /**
8 * Class LP_Section_DB
9 */
10
11 class LP_Section_DB extends LP_Database {
12 public static $_instance;
13
14 public function __construct() {
15 parent::__construct();
16 }
17
18 public static function getInstance() {
19 if ( is_null( self::$_instance ) ) {
20 self::$_instance = new self();
21 }
22
23 return self::$_instance;
24 }
25
26 /**
27 * Get sections
28 *
29 * @throws Exception
30 * @since 4.1.6
31 * @version 1.0.0
32 */
33 public function get_sections( LP_Section_Filter $filter ) {
34 $default_fields = $this->get_cols_of_table( $this->tb_lp_sections );
35 $filter->fields = array_merge( $default_fields, $filter->fields );
36
37 if ( empty( $filter->collection ) ) {
38 $filter->collection = $this->tb_posts;
39 }
40
41 if ( empty( $filter->collection_alias ) ) {
42 $filter->collection_alias = 's';
43 }
44
45 $this->execute( $filter );
46 }
47
48 /**
49 * Get course id by section id
50 *
51 * @param int $section_Id
52 *
53 * @return mixed
54 */
55 public function get_course_id_by_section_id( int $section_Id = 0 ) {
56
57 }
58
59 /**
60 * Get section ids by course id
61 *
62 * @param int $course_id
63 * @author tungnx
64 * @since 4.1.4.1
65 * @version 1.0.0
66 * @return array
67 */
68 public function get_section_ids_by_course( int $course_id ): array {
69 $query = $this->wpdb->prepare(
70 "SELECT section_id
71 FROM $this->tb_lp_sections
72 WHERE section_course_id = %d
73 ",
74 $course_id
75 );
76
77 return $this->wpdb->get_col( $query );
78 }
79
80 /**
81 * Remove rows IN user_item_ids
82 *
83 * @param LP_Section_Filter $filter $filter->section_ids, $filter->author_id_course
84 *
85 * @throws Exception
86 * @since 4.1.4.1
87 * @version 1.0.0
88 */
89 public function delete_section( LP_Section_Filter $filter ) {
90 // Check valid user.
91 if ( ! is_user_logged_in() || ( ! current_user_can( ADMIN_ROLE ) && get_current_user_id() != $filter->author_id_course ) ) {
92 throw new Exception( __FUNCTION__ . ': User invalid!' );
93 }
94
95 if ( empty( $filter->section_ids ) ) {
96 return 1;
97 }
98
99 $where = 'WHERE 1=1 ';
100
101 $where .= $this->wpdb->prepare(
102 'AND section_id IN(' . LP_Helper::db_format_array( $filter->section_ids, '%d' ) . ')',
103 $filter->section_ids
104 );
105
106 return $this->wpdb->query(
107 "DELETE FROM $this->tb_lp_sections
108 $where
109 "
110 );
111 }
112
113 /**
114 * Remove rows IN user_item_ids
115 *
116 * @param LP_Section_Filter $filter $filter->section_ids, $filter->author_id_course
117 *
118 * @throws Exception
119 * @since 4.1.4.1
120 * @version 1.0.0
121 */
122 public function delete_section_items( LP_Section_Filter $filter ) {
123 // Check valid user.
124 if ( ! is_user_logged_in() || ( ! current_user_can( ADMIN_ROLE ) && get_current_user_id() != $filter->author_id_course ) ) {
125 throw new Exception( __FUNCTION__ . ': User invalid!' );
126 }
127
128 if ( empty( $filter->section_ids ) ) {
129 return 1;
130 }
131
132 $where = 'WHERE 1=1 ';
133
134 $where .= $this->wpdb->prepare(
135 'AND section_id IN(' . LP_Helper::db_format_array( $filter->section_ids, '%d' ) . ')',
136 $filter->section_ids
137 );
138
139 return $this->wpdb->query(
140 "DELETE FROM $this->tb_lp_section_items
141 $where
142 "
143 );
144 }
145
146 /**
147 * Get rows in sections table
148 *
149 * WP_Error || array
150 *
151 * @author Nhamdv <email@email.com>
152 */
153 public function get_sections_by_course_id( LP_Section_Filter $filter ) {
154 if ( empty( $filter->section_course_id ) ) {
155 return new WP_Error( 'no_course_id', __( 'No course id', 'learnpress' ), array( 'status' => 404 ) );
156 }
157
158 $where = 'WHERE 1=1';
159
160 $where .= $this->wpdb->prepare( ' AND section_course_id = %d', $filter->section_course_id );
161
162 // SEARCH
163 if ( $filter->search_section ) {
164 $where .= $this->wpdb->prepare( ' AND section_name LIKE %s ', '%' . $filter->search_section . '%' );
165 }
166
167 if ( $filter->section_ids ) {
168 $section_ids = LP_Helper::db_format_array( $filter->section_ids, '%d' );
169 $where .= $this->wpdb->prepare( " AND section_id IN (" . $section_ids . ")", $filter->section_ids ); // phpcs:ignore
170 }
171
172 if ( $filter->section_not_ids ) {
173 $section_not_ids = LP_Helper::db_format_array( $filter->section_not_ids, '%d' );
174 $where .= $this->wpdb->prepare( " AND section_id NOT IN (" . $section_not_ids . ")", $filter->section_not_ids ); // phpcs:ignore
175 }
176
177 $orderby = ' ORDER BY section_order ' . $filter->order ?? 'ASC';
178
179 // PER_PAGE
180 $limit = '';
181 if ( $filter->limit != -1 ) {
182 $offset = $filter->limit * ( $filter->page - 1 );
183 $limit = $this->wpdb->prepare( ' LIMIT %d, %d', $offset, $filter->limit );
184 }
185
186 $query = "SELECT * FROM {$this->tb_lp_sections} {$where} {$orderby} {$limit}";
187
188 $results = $this->wpdb->get_results( $query, ARRAY_A );
189
190 $total = 0;
191
192 if ( $results ) {
193 $query_total = "SELECT COUNT(*) FROM {$this->tb_lp_sections} {$where}";
194
195 $total = $this->wpdb->get_var( $query_total );
196 }
197
198 return array(
199 'results' => $results,
200 'total' => $total,
201 'pages' => (int) ceil( $total / (int) $filter->limit ),
202 );
203 }
204
205 /**
206 * Get items in section_items table
207 *
208 * WP_Error || array
209 *
210 * @author Nhamdv <email@email.com>
211 */
212 public function get_section_items_by_section_id( LP_Section_Items_Filter $filter ) {
213 if ( empty( $filter->section_id ) ) {
214 return new WP_Error( 'no_section_id', __( 'No section id', 'learnpress' ), array( 'status' => 404 ) );
215 }
216
217 $where = 'WHERE 1=1';
218
219 $select = "SELECT post_title, post_type, post_name, post_status, post_date, post_author, ID, post_content FROM {$this->wpdb->posts} AS p";
220
221 $inner_join = "INNER JOIN {$this->tb_lp_section_items} AS si ON p.ID = si.item_id";
222
223 $where .= $this->wpdb->prepare( ' AND si.section_id = %d', $filter->section_id );
224
225 // Check item type is avaliable( Assignments , H5P )
226 $types = learn_press_get_block_course_item_types();
227 $db_types = LP_Helper::db_format_array( $types, '%s' );
228 $where .= $this->wpdb->prepare( " AND si.item_type IN (" . $db_types . ")", $types ); // phpcs:ignore
229
230 // PER_PAGE
231 $limit = '';
232 if ( $filter->limit != -1 ) {
233 $offset = $filter->limit * ( $filter->page - 1 );
234 $limit = $this->wpdb->prepare( ' LIMIT %d, %d', $offset, $filter->limit );
235 }
236
237 // SEARCH
238 if ( $filter->search_title ) {
239 $where .= $this->wpdb->prepare( ' AND p.post_title LIKE %s ', '%' . $filter->search_title . '%' );
240 }
241
242 // INCLUDE
243 if ( $filter->item_ids ) {
244 $item_ids = LP_Helper::db_format_array( $filter->item_ids, '%d' );
245 $where .= $this->wpdb->prepare( " AND p.ID IN (" . $item_ids . ")", $filter->item_ids ); // phpcs:ignore
246 }
247
248 // EXCLUDE
249 if ( $filter->item_not_ids ) {
250 $item_not_ids = LP_Helper::db_format_array( $filter->item_not_ids, '%d' );
251 $where .= $this->wpdb->prepare( " AND section_id NOT IN (" . $item_not_ids . ")", $filter->item_not_ids ); // phpcs:ignore
252 }
253
254 $orderby = ' ORDER BY si.item_order ' . $filter->order ?? 'ASC';
255
256 $query = "{$select} {$inner_join} {$where} {$orderby} {$limit}";
257
258 $results = $this->wpdb->get_results( $query, ARRAY_A );
259
260 $total = 0;
261
262 if ( $results ) {
263 $query_total = "SELECT COUNT(*) FROM {$this->wpdb->posts} AS p {$inner_join} {$where}";
264
265 $total = $this->wpdb->get_var( $query_total );
266 }
267
268 return array(
269 'results' => $results,
270 'total' => $total,
271 'pages' => (int) ceil( $total / (int) $filter->limit ),
272 );
273 }
274
275 public function get_course_id_by_section( int $section_id ) : int {
276 static $output;
277
278 global $wpdb;
279
280 if ( empty( $section_id ) ) {
281 return false;
282 }
283
284 if ( ! isset( $output ) ) {
285 $output = $wpdb->get_var( $wpdb->prepare( "SELECT section_course_id FROM {$wpdb->learnpress_sections} WHERE section_id = %d ORDER BY section_id DESC LIMIT 1", $section_id ) );
286 }
287
288 if ( $output ) {
289 return absint( $output );
290 }
291
292 return false;
293 }
294
295 public function get_section_id_by_item_id( $item_id ) {
296 global $wpdb;
297
298 if ( empty( $item_id ) ) {
299 return false;
300 }
301
302 $section_id = $wpdb->get_var( $wpdb->prepare( "SELECT section_id FROM {$wpdb->learnpress_section_items} WHERE item_id = %d ORDER BY section_id DESC LIMIT 1", $item_id ) );
303
304 if ( $section_id ) {
305 return absint( $section_id );
306 }
307
308 return false;
309 }
310
311 /**
312 * Get last section order of course
313 *
314 * @param int $course_id
315 *
316 * @return int
317 * @throws Exception
318 * @since 4.1.6.9
319 * @version 1.0.0
320 */
321 public function get_last_number_order( int $course_id = 0 ): int {
322 $query = $this->wpdb->prepare(
323 "SELECT MAX(section_order)
324 FROM $this->tb_lp_sections
325 WHERE section_course_id = %d",
326 $course_id
327 );
328
329 $number_order = intval( $this->wpdb->get_var( $query ) );
330
331 $this->check_execute_has_error();
332
333 return $number_order;
334 }
335 }
336
337