PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
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.4.4, at inc/Databases/class-lp-section-db.php

523 lines 13.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.2
32 */
33 public function get_sections( LP_Section_Filter $filter, &$total_rows = 0 ) {
34 $default_fields = $filter->all_fields;
35 $filter->fields = array_merge( $default_fields, $filter->fields );
36
37 if ( empty( $filter->collection ) ) {
38 $filter->collection = $this->tb_lp_sections;
39 }
40
41 if ( empty( $filter->collection_alias ) ) {
42 $filter->collection_alias = 'st';
43 }
44
45 $filter->field_count = 'st.section_id';
46
47 if ( ! empty( $filter->section_id ) ) {
48 $filter->where[] = $this->wpdb->prepare( 'AND st.section_id = %d', $filter->section_id );
49 }
50
51 if ( ! empty( $filter->section_course_id ) ) {
52 $filter->where[] = $this->wpdb->prepare( 'AND st.section_course_id = %d', $filter->section_course_id );
53 }
54
55 if ( ! empty( $filter->section_name ) ) {
56 $filter->where[] = $this->wpdb->prepare( 'AND st.section_name LIKE %s', '%' . $filter->section_name . '%' );
57 }
58
59 if ( ! empty( $filter->section_ids ) ) {
60 $section_ids_format = LP_Helper::db_format_array( $filter->section_ids, '%d' );
61 $filter->where[] = $this->wpdb->prepare( 'AND st.section_id IN (' . $section_ids_format . ')', $filter->section_ids );
62 }
63
64 if ( ! empty( $filter->section_not_ids ) ) {
65 $filter->where[] = $this->wpdb->prepare(
66 'AND st.section_id NOT IN(' . LP_Helper::db_format_array( $filter->section_not_ids, '%d' ) . ')',
67 $filter->section_not_ids
68 );
69 }
70
71 // Default Order
72 if ( empty( $filter->order ) ) {
73 $filter->order_by = 'st.section_order';
74 $filter->order = 'ASC';
75 }
76
77 return $this->execute( $filter, $total_rows );
78 }
79
80 /**
81 * Get section ids by course id
82 *
83 * @param int $course_id
84 * @author tungnx
85 * @since 4.1.4.1
86 * @version 1.0.0
87 * @return array
88 */
89 public function get_section_ids_by_course( int $course_id ): array {
90 $query = $this->wpdb->prepare(
91 "SELECT section_id
92 FROM $this->tb_lp_sections
93 WHERE section_course_id = %d
94 ",
95 $course_id
96 );
97
98 return $this->wpdb->get_col( $query );
99 }
100
101 /**
102 * Remove rows IN user_item_ids
103 *
104 * @param LP_Section_Filter $filter $filter->section_ids, $filter->author_id_course
105 *
106 * @throws Exception
107 * @since 4.1.4.1
108 * @version 1.0.0
109 */
110 public function delete_section( LP_Section_Filter $filter ) {
111 // Check valid user.
112 if ( ! is_user_logged_in() || ( ! current_user_can( ADMIN_ROLE ) && get_current_user_id() != $filter->author_id_course ) ) {
113 throw new Exception( __FUNCTION__ . ': Invalid user!' );
114 }
115
116 if ( empty( $filter->section_ids ) ) {
117 return 1;
118 }
119
120 $where = 'WHERE 1=1 ';
121
122 $where .= $this->wpdb->prepare(
123 'AND section_id IN(' . LP_Helper::db_format_array( $filter->section_ids, '%d' ) . ')',
124 $filter->section_ids
125 );
126
127 return $this->wpdb->query(
128 "DELETE FROM $this->tb_lp_sections
129 $where
130 "
131 );
132 }
133
134 /**
135 * Remove rows IN user_item_ids
136 *
137 * @param LP_Section_Filter $filter $filter->section_ids, $filter->author_id_course
138 *
139 * @throws Exception
140 * @since 4.1.4.1
141 * @version 1.0.0
142 */
143 public function delete_section_items( LP_Section_Filter $filter ) {
144 // Check valid user.
145 if ( ! is_user_logged_in() || ( ! current_user_can( ADMIN_ROLE ) && get_current_user_id() != $filter->author_id_course ) ) {
146 throw new Exception( __FUNCTION__ . ': Invalid user!' );
147 }
148
149 if ( empty( $filter->section_ids ) ) {
150 return 1;
151 }
152
153 $where = 'WHERE 1=1 ';
154
155 $where .= $this->wpdb->prepare(
156 'AND section_id IN(' . LP_Helper::db_format_array( $filter->section_ids, '%d' ) . ')',
157 $filter->section_ids
158 );
159
160 return $this->wpdb->query(
161 "DELETE FROM $this->tb_lp_section_items
162 $where
163 "
164 );
165 }
166
167 /**
168 * Get rows in sections table
169 *
170 * WP_Error || array
171 *
172 * @author Nhamdv <email@email.com>
173 */
174 public function get_sections_by_course_id( LP_Section_Filter $filter ) {
175 if ( empty( $filter->section_course_id ) ) {
176 return new WP_Error( 'no_course_id', __( 'No course id', 'learnpress' ), array( 'status' => 404 ) );
177 }
178
179 $where = 'WHERE 1=1';
180
181 $where .= $this->wpdb->prepare( ' AND section_course_id = %d', $filter->section_course_id );
182
183 // SEARCH
184 if ( $filter->search_section ) {
185 $where .= $this->wpdb->prepare( ' AND section_name LIKE %s ', '%' . $filter->search_section . '%' );
186 }
187
188 if ( $filter->section_ids ) {
189 $section_ids = LP_Helper::db_format_array( $filter->section_ids, '%d' );
190 $where .= $this->wpdb->prepare( " AND section_id IN (" . $section_ids . ")", $filter->section_ids ); // phpcs:ignore
191 }
192
193 if ( $filter->section_not_ids ) {
194 $section_not_ids = LP_Helper::db_format_array( $filter->section_not_ids, '%d' );
195 $where .= $this->wpdb->prepare( " AND section_id NOT IN (" . $section_not_ids . ")", $filter->section_not_ids ); // phpcs:ignore
196 }
197
198 $orderby = ' ORDER BY section_order ' . $filter->order ?? 'ASC';
199
200 // PER_PAGE
201 $limit = '';
202 if ( $filter->limit < -1 ) {
203 $filter->limit = 0;
204 }
205
206 if ( $filter->limit != -1 ) {
207 $offset = $filter->limit * ( $filter->page - 1 );
208 $limit = $this->wpdb->prepare( ' LIMIT %d, %d', $offset, $filter->limit );
209 }
210
211 $query = "SELECT * FROM {$this->tb_lp_sections} {$where} {$orderby} {$limit}";
212
213 $results = $this->wpdb->get_results( $query, ARRAY_A );
214
215 $total = 0;
216
217 if ( $results ) {
218 $query_total = "SELECT COUNT(*) FROM {$this->tb_lp_sections} {$where}";
219
220 $total = $this->wpdb->get_var( $query_total );
221 }
222
223 if ( $filter->limit > 0 ) {
224 $pages = LP_Database::get_total_pages( $filter->limit, $total );
225 } else {
226 $pages = 1;
227 }
228 return array(
229 'results' => $results,
230 'total' => $total,
231 'pages' => $pages,
232 );
233 }
234
235 /**
236 * Get items in section_items table
237 *
238 * WP_Error || array
239 *
240 * @author Nhamdv <email@email.com>
241 */
242 public function get_section_items_by_section_id( LP_Section_Items_Filter $filter ) {
243 if ( empty( $filter->section_id ) ) {
244 return new WP_Error( 'no_section_id', __( 'No section id', 'learnpress' ), array( 'status' => 404 ) );
245 }
246
247 $where = 'WHERE 1=1';
248
249 $select = "SELECT post_title, post_type, post_name, post_status, post_date, post_author, ID, post_content FROM {$this->wpdb->posts} AS p";
250
251 $inner_join = "INNER JOIN {$this->tb_lp_section_items} AS si ON p.ID = si.item_id";
252
253 $where .= $this->wpdb->prepare( ' AND si.section_id = %d', $filter->section_id );
254
255 // Check item type is avaliable( Assignments , H5P )
256 $types = learn_press_get_block_course_item_types();
257 $db_types = LP_Helper::db_format_array( $types, '%s' );
258 $where .= $this->wpdb->prepare( " AND si.item_type IN (" . $db_types . ")", $types ); // phpcs:ignore
259
260 // PER_PAGE
261 $limit = '';
262
263 if ( $filter->limit < -1 ) {
264 $filter->limit = 0;
265 }
266
267 if ( $filter->limit != -1 ) {
268 $offset = $filter->limit * ( $filter->page - 1 );
269 $limit = $this->wpdb->prepare( ' LIMIT %d, %d', $offset, $filter->limit );
270 }
271
272 // SEARCH
273 if ( $filter->search_title ) {
274 $where .= $this->wpdb->prepare( ' AND p.post_title LIKE %s ', '%' . $filter->search_title . '%' );
275 }
276
277 // INCLUDE
278 if ( $filter->item_ids ) {
279 $item_ids = LP_Helper::db_format_array( $filter->item_ids, '%d' );
280 $where .= $this->wpdb->prepare( " AND p.ID IN (" . $item_ids . ")", $filter->item_ids ); // phpcs:ignore
281 }
282
283 // EXCLUDE
284 if ( $filter->item_not_ids ) {
285 $item_not_ids = LP_Helper::db_format_array( $filter->item_not_ids, '%d' );
286 $where .= $this->wpdb->prepare( " AND section_id NOT IN (" . $item_not_ids . ")", $filter->item_not_ids ); // phpcs:ignore
287 }
288
289 $orderby = ' ORDER BY si.item_order ' . $filter->order ?? 'ASC';
290
291 $query = "{$select} {$inner_join} {$where} {$orderby} {$limit}";
292
293 $results = $this->wpdb->get_results( $query, ARRAY_A );
294
295 $total = 0;
296
297 if ( $results ) {
298 $query_total = "SELECT COUNT(*) FROM {$this->wpdb->posts} AS p {$inner_join} {$where}";
299
300 $total = $this->wpdb->get_var( $query_total );
301 }
302
303 if ( $filter->limit > 0 ) {
304 $pages = LP_Database::get_total_pages( $filter->limit, $total );
305 } else {
306 $pages = 1;
307 }
308 return array(
309 'results' => $results,
310 'total' => $total,
311 'pages' => $pages,
312 );
313 }
314
315 /**
316 * Get items by section_id in section_items table
317 *
318 * @throws Exception
319 * @version 1.0.0
320 * @since 4.2.4
321 */
322 public function get_items( LP_Section_Items_Filter $filter, &$total_rows = 0 ) {
323 // Get fields from table posts
324 $default_fields = $this->get_cols_of_table( $this->tb_posts );
325 $filter->fields = array_merge( $default_fields, $filter->fields );
326 $filter->collection = $this->tb_posts;
327 $filter->collection_alias = 'p';
328
329 // Join to table learnpress_section_items
330 $filter->join[] = "INNER JOIN {$this->tb_lp_section_items} AS si ON p.ID = si.item_id";
331
332 // Search in section id
333 if ( ! empty( $filter->section_id ) ) {
334 $filter->where[] = $this->wpdb->prepare( 'AND si.section_id = %s', $filter->section_id );
335 }
336
337 // Order items
338 if ( empty( $filter->order_by ) ) {
339 $filter->order_by = 'si.item_order';
340 $filter->order = 'ASC';
341 }
342
343 $filter = apply_filters( 'lp/section/items/query/filter', $filter );
344
345 return $this->execute( $filter, $total_rows );
346 }
347
348 /**
349 * Get course id by section id
350 *
351 * @param int $section_id
352 * @return int
353 * @throws Exception
354 * @since 4.1.4.2
355 * @version 1.0.2
356 */
357 public function get_course_id_by_section( int $section_id ): int {
358 $filter = new LP_Section_Filter();
359 $filter->only_fields = [ 'section_course_id' ];
360 $filter->section_ids = [ $section_id ];
361 $filter->run_query_count = false;
362 $filter->limit = 1;
363 $filter->return_string_query = true;
364 $result = $this->get_sections( $filter );
365 $course_id = (int) $this->wpdb->get_var( $result );
366
367 $this->check_execute_has_error();
368
369 return $course_id;
370 }
371
372 public function get_section_id_by_item_id( $item_id, $course_id = 0 ) {
373 global $wpdb;
374
375 if ( empty( $item_id ) ) {
376 return false;
377 }
378
379 $inner_join = '';
380 if ( ! empty( $course_id ) ) {
381 $inner_join = "INNER JOIN {$wpdb->learnpress_sections} AS s ON s.section_id = si.section_id";
382 $inner_join .= $wpdb->prepare( ' AND s.section_course_id = %d ', $course_id );
383 }
384
385 $query_str = $wpdb->prepare(
386 "SELECT si.section_id
387 FROM {$wpdb->learnpress_section_items} AS si
388 $inner_join
389 WHERE si.item_id = %d
390 LIMIT 1",
391 $item_id
392 );
393
394 $section_id = $wpdb->get_var( $query_str );
395
396 if ( $section_id ) {
397 return absint( $section_id );
398 }
399
400 return false;
401 }
402
403 /**
404 * Get last section order of course
405 *
406 * @param int $course_id
407 *
408 * @return int
409 * @throws Exception
410 * @since 4.1.6.9
411 * @version 1.0.0
412 */
413 public function get_last_number_order( int $course_id = 0 ): int {
414 $query = $this->wpdb->prepare(
415 "SELECT MAX(section_order)
416 FROM $this->tb_lp_sections
417 WHERE section_course_id = %d",
418 $course_id
419 );
420
421 $number_order = intval( $this->wpdb->get_var( $query ) );
422
423 $this->check_execute_has_error();
424
425 return $number_order;
426 }
427
428 /**
429 * Insert data
430 *
431 * @param array $data
432 *
433 * @return int
434 * @throws Exception
435 * @version 1.0.2
436 * @since 4.2.8.6
437 */
438 public function insert_data( array $data ): int {
439 $filter = new LP_Section_Filter();
440
441 foreach ( $data as $col_name => $value ) {
442 if ( ! in_array( $col_name, $filter->all_fields ) ) {
443 unset( $data[ $col_name ] );
444 }
445 }
446
447 // section_id is auto increment.
448 unset( $data['section_id'] );
449
450 $this->wpdb->insert( $this->tb_lp_sections, $data );
451
452 $this->check_execute_has_error();
453
454 return $this->wpdb->insert_id;
455 }
456
457 /**
458 * Update data
459 *
460 * @param array $data
461 *
462 * @return bool
463 *
464 * @throws Exception
465 * @since 4.2.8.6
466 * @version 1.0.0
467 */
468 public function update_data( array $data ): bool {
469 if ( empty( $data['section_id'] ) ) {
470 throw new Exception( __( 'Invalid section_id!', 'learnpress' ) . ' | ' . __FUNCTION__ );
471 }
472
473 $filter = new LP_Section_Filter();
474 $filter->collection = $this->tb_lp_sections;
475 foreach ( $data as $col_name => $value ) {
476 if ( ! in_array( $col_name, $filter->all_fields ) ) {
477 continue;
478 }
479
480 if ( is_null( $value ) ) {
481 $filter->set[] = $col_name . ' = null';
482 } else {
483 $filter->set[] = $this->wpdb->prepare( $col_name . ' = %s', $value );
484 }
485 }
486
487 $filter->where[] = $this->wpdb->prepare( 'AND section_id = %d', $data['section_id'] );
488 $this->update_execute( $filter );
489
490 return true;
491 }
492
493 /**
494 * Update sections position
495 * Update section_order of each section in course
496 *
497 * @throws Exception
498 * @since 4.2.8.6
499 * @version 1.0.0
500 */
501 /*public function update_sections_position( array $section_ids, $section_course_id ) {
502 $filter = new LP_Section_Filter();
503 $filter->collection = $this->tb_lp_sections;
504 $SET_SQL = 'section_order = CASE';
505
506 foreach ( $section_ids as $position => $section_id ) {
507 ++$position;
508 $section_id = absint( $section_id );
509 if ( empty( $section_id ) ) {
510 continue;
511 }
512
513 $SET_SQL .= $this->wpdb->prepare( ' WHEN section_id = %d THEN %d', $section_id, $position );
514 }
515
516 $SET_SQL .= ' ELSE section_order END';
517 $filter->set[] = $SET_SQL;
518 $filter->where[] = $this->wpdb->prepare( 'AND section_course_id = %d', $section_course_id );
519
520 $this->update_execute( $filter );
521 }*/
522 }
523