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-question-db.php

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

198 lines 5.3 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_Lesson_DB
9 *
10 * @since 3.2.8
11 * @version 1.0.1
12 */
13 class LP_Question_DB extends LP_Database {
14 private static $_instance;
15
16 protected function __construct() {
17 parent::__construct();
18 }
19
20 public static function getInstance() {
21 if ( is_null( self::$_instance ) ) {
22 self::$_instance = new self();
23 }
24
25 return self::$_instance;
26 }
27
28 /**
29 * Get questions
30 *
31 * @return array|null|int|string
32 * @throws Exception
33 * @since 4.1.6
34 * @version 1.0.0
35 */
36 public function get_questions( LP_Question_Filter $filter, &$total_rows = 0 ) {
37 $default_fields = $this->get_cols_of_table( $this->tb_posts );
38 $filter->fields = array_merge( $default_fields, $filter->fields );
39
40 if ( empty( $filter->collection ) ) {
41 $filter->collection = $this->tb_posts;
42 }
43
44 if ( empty( $filter->collection_alias ) ) {
45 $filter->collection_alias = 'q';
46 }
47
48 $ca = $filter->collection_alias;
49
50 // Where
51 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_type = %s", $filter->post_type );
52
53 // Status
54 $filter->post_status = (array) $filter->post_status;
55 if ( ! empty( $filter->post_status ) ) {
56 $post_status_format = LP_Helper::db_format_array( $filter->post_status, '%s' );
57 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_status IN (" . $post_status_format . ')', $filter->post_status );
58 }
59
60 // Term ids
61 if ( ! empty( $filter->term_ids ) ) {
62 $filter->join[] = "INNER JOIN $this->tb_term_relationships AS r_term ON p.ID = r_term.object_id";
63
64 $term_ids_format = LP_Helper::db_format_array( $filter->term_ids, '%d' );
65 $filter->where[] = $this->wpdb->prepare( 'AND r_term.term_taxonomy_id IN (' . $term_ids_format . ')', $filter->term_ids );
66 }
67
68 // Question ids
69 if ( ! empty( $filter->post_ids ) ) {
70 $list_ids_format = LP_Helper::db_format_array( $filter->post_ids, '%d' );
71 $filter->where[] = $this->wpdb->prepare( "AND $ca.ID IN (" . $list_ids_format . ')', $filter->post_ids );
72 }
73
74 // Title
75 if ( $filter->post_title ) {
76 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_title LIKE %s", '%' . $filter->post_title . '%' );
77 }
78
79 // Author
80 if ( $filter->post_author ) {
81 $filter->where[] = $this->wpdb->prepare( "AND $ca.post_author = %d", $filter->post_author );
82 }
83
84 // Authors
85 if ( ! empty( $filter->post_authors ) ) {
86 $post_authors_format = LP_Helper::db_format_array( $filter->post_authors, '%d' );
87 $filter->where[] = $this->wpdb->prepare( "AND $ca.ID IN (" . $post_authors_format . ')', $filter->post_authors );
88 }
89
90 $filter = apply_filters( 'lp/question/query/filter', $filter );
91
92 return $this->execute( $filter, $total_rows );
93 }
94
95 /**
96 * Get all questions are unassigned to any quiz.
97 *
98 * @return array|null|int|string
99 * @throws Exception
100 * @since 4.1.6
101 * @version 1.0.0
102 */
103 public function get_questions_not_assign_quiz( LP_Question_Filter $filter = null ) {
104 $lp_qq_filter = new LP_Quiz_Questions_Filter();
105 $lp_qq_filter->return_string_query = true;
106 $lp_qq_filter->only_fields = array( 'question_id' );
107 $query_question_ids_assigned = LP_Quiz_Questions_DB::getInstance()->get_quiz_questions( $lp_qq_filter );
108
109 if ( is_null( $filter ) ) {
110 $filter = new LP_Question_Filter();
111 }
112 $filter->collection_alias = 'q';
113 $filter->only_fields = array( 'q.ID' );
114 $filter->where[] = 'AND ID NOT IN(' . $query_question_ids_assigned . ')';
115 $filter->where[] = $this->wpdb->prepare( 'AND q.post_status not IN(%s, %s)', 'trash', 'auto-draft' );
116
117 return $this->get_questions( $filter );
118 }
119
120 /**
121 * Count all questions are unassigned to any quiz.
122 *
123 * @param LP_Question_Filter|null $filter
124 *
125 * @return int
126 * @throws Exception
127 * @since 3.0.0
128 * @version 1.0.1
129 */
130 function get_total_question_unassigned( LP_Question_Filter $filter = null ): int {
131 if ( is_null( $filter ) ) {
132 $filter = new LP_Question_Filter();
133 }
134
135 $filter->query_count = true;
136 $filter->post_status = array();
137 $filter->field_count = 'ID';
138
139 return $this->get_questions_not_assign_quiz( $filter );
140 }
141
142 /**
143 * Get list Question ids of Quiz
144 *
145 * @param LP_Question_Filter $filter
146 *
147 * Clear cache when save quiz same id
148 *
149 * @return array
150 * @throws Exception
151 * @see LP_Quiz_Post_Type::save
152 *
153 */
154 public function get_list_question_ids_of_quiz( LP_Question_Filter $filter = null ): array {
155 $key_cache = "$filter->quiz_id/question_ids";
156
157 // Get cache
158 $lp_quiz_cache = LP_Quiz_Cache::instance();
159 $quiz_ids = $lp_quiz_cache->get_cache( $key_cache );
160
161 if ( $quiz_ids ) {
162 return $quiz_ids;
163 }
164
165 $statues = array( 'publish' );
166 if ( ! empty( $filter->statues ) ) {
167 $statues = $filter->statues;
168 }
169
170 $format = LP_Helper::format_query_IN( $statues );
171 $args = array_merge( array( LP_QUESTION_CPT, $filter->quiz_id ), $statues );
172
173 $query = $this->wpdb->prepare(
174 "
175 SELECT question_id
176 FROM $this->tb_lp_quiz_questions AS quiz_q
177 INNER JOIN $this->tb_posts as p
178 ON p.ID = quiz_q.question_id
179 AND p.post_type = %s
180 AND quiz_q.quiz_id = %d
181 AND p.post_status IN(" . $format . ')
182 ORDER BY question_order
183 ',
184 $args
185 );
186
187 $ids = $this->wpdb->get_col( $query );
188
189 $this->check_execute_has_error();
190
191 // Set cache
192 $lp_quiz_cache->set_cache( $key_cache, $ids, DAY_IN_SECONDS );
193
194 return $ids;
195 }
196 }
197
198