| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Databases; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LearnPress\Filters\QuestionAnswersFilter; |
| 7 |
use LP_Database; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Class QuestionAnswersDB |
| 15 |
* |
| 16 |
* @instead of LP_Question_Answers_DB |
| 17 |
* @since 4.2.9 |
| 18 |
* @version 1.0.1 |
| 19 |
*/ |
| 20 |
class QuestionAnswersDB extends DataBase { |
| 21 |
private static $_instance; |
| 22 |
|
| 23 |
protected function __construct() { |
| 24 |
parent::__construct(); |
| 25 |
} |
| 26 |
|
| 27 |
public static function getInstance() { |
| 28 |
if ( is_null( self::$_instance ) ) { |
| 29 |
self::$_instance = new self(); |
| 30 |
} |
| 31 |
|
| 32 |
return self::$_instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get question answers |
| 37 |
* |
| 38 |
* @throws Exception |
| 39 |
*/ |
| 40 |
public function get_question_answers( QuestionAnswersFilter $filter, &$total_rows = 0 ) { |
| 41 |
$filter->fields = array_merge( $filter->all_fields, $filter->fields ); |
| 42 |
|
| 43 |
if ( empty( $filter->collection ) ) { |
| 44 |
$filter->collection = $this->tb_lp_question_answers; |
| 45 |
} |
| 46 |
|
| 47 |
if ( empty( $filter->collection_alias ) ) { |
| 48 |
$filter->collection_alias = 'qa'; |
| 49 |
} |
| 50 |
|
| 51 |
// By question answer id |
| 52 |
if ( ! empty( $filter->question_answer_id ) ) { |
| 53 |
$filter->where[] = $this->wpdb->prepare( "AND {$filter->collection_alias}.question_answer_id = %d", $filter->question_answer_id ); |
| 54 |
} |
| 55 |
|
| 56 |
// By question id |
| 57 |
if ( ! empty( $filter->question_id ) ) { |
| 58 |
$filter->where[] = $this->wpdb->prepare( "AND {$filter->collection_alias}.question_id = %d", $filter->question_id ); |
| 59 |
} |
| 60 |
|
| 61 |
// By title |
| 62 |
if ( ! empty( $filter->title ) ) { |
| 63 |
$filter->where[] = $this->wpdb->prepare( "AND {$filter->collection_alias}.title LIKE %s", '%' . $this->wpdb->esc_like( $filter->title ) . '%' ); |
| 64 |
} |
| 65 |
|
| 66 |
return $this->execute( $filter, $total_rows ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get last answer number order on question. |
| 71 |
* |
| 72 |
* @throws Exception |
| 73 |
*/ |
| 74 |
public function get_last_number_order( int $question_id = 0 ): int { |
| 75 |
$query = $this->wpdb->prepare( |
| 76 |
"SELECT MAX(`order`) |
| 77 |
FROM $this->tb_lp_question_answers |
| 78 |
WHERE question_id = %d", |
| 79 |
$question_id |
| 80 |
); |
| 81 |
|
| 82 |
$number_order = intval( $this->wpdb->get_var( $query ) ); |
| 83 |
|
| 84 |
$this->check_execute_has_error(); |
| 85 |
|
| 86 |
return $number_order; |
| 87 |
} |
| 88 |
} |
| 89 |
|