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.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / restapi / REST_Quiz.php
tutor / restapi Last commit date
REST_Author.php 2 years ago REST_Course.php 2 years ago REST_Course_Announcement.php 2 years ago REST_Lesson.php 2 years ago REST_Quiz.php 2 years ago REST_Rating.php 2 years ago REST_Response.php 2 years ago REST_Topic.php 2 years ago RestAuth.php 2 years ago
REST_Quiz.php
354 lines
1 <?php
2 /**
3 * REST API for quiz.
4 *
5 * @package Tutor\RestAPI
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 1.7.1
9 */
10
11 namespace TUTOR;
12
13 use Tutor\Helpers\QueryHelper;
14 use WP_REST_Request;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * Class REST_Quiz
22 */
23 class REST_Quiz {
24
25 use REST_Response;
26
27 /**
28 * Quiz post type
29 *
30 * @var string The post type for quizzes.
31 */
32 private $post_type = 'tutor_quiz';
33
34 /**
35 * Post parent ID
36 *
37 * @var int|null The post parent ID.
38 */
39 private $post_parent;
40
41 /**
42 * Quiz questions table name
43 *
44 * @var string The table name for quiz questions.
45 */
46 private $t_quiz_question = 'tutor_quiz_questions';
47
48 /**
49 * Quiz question answers table name
50 *
51 * @var string The table name for quiz question answers.
52 */
53 private $t_quiz_ques_ans = 'tutor_quiz_question_answers';
54
55 /**
56 * Quiz question answer options table name
57 *
58 * @var string The table name for quiz attempts.
59 */
60 private $t_quiz_attempt = 'tutor_quiz_attempts';
61
62 /**
63 * Quiz attempt answers table name
64 *
65 * @var string The table name for quiz attempt answers.
66 */
67 private $t_quiz_attempt_ans = 'tutor_quiz_attempt_answers';
68
69 /**
70 * Get quiz with settings.
71 *
72 * @since 1.7.1
73 *
74 * @param WP_REST_Request $request REST request object.
75 *
76 * @return mixed
77 */
78 public function quiz_with_settings( WP_REST_Request $request ) {
79 $this->post_parent = $request->get_param( 'id' );
80
81 global $wpdb;
82
83 $quizs = $wpdb->get_results(
84 $wpdb->prepare(
85 "SELECT
86 ID,
87 post_title,
88 post_content,
89 post_name
90 FROM {$wpdb->posts}
91 WHERE post_type = %s
92 AND post_parent = %d
93 ",
94 $this->post_type,
95 $this->post_parent
96 )
97 );
98
99 $data = array();
100
101 if ( count( $quizs ) > 0 ) {
102 foreach ( $quizs as $quiz ) {
103 $quiz->quiz_settings = get_post_meta( $quiz->ID, 'tutor_quiz_option', false );
104
105 array_push( $data, $quiz );
106
107 $response = array(
108 'code' => 'success',
109 'message' => __( 'Quiz retrieved successfully', 'tutor' ),
110 'data' => $data,
111 );
112 }
113 return self::send( $response );
114 }
115
116 $response = array(
117 'code' => 'not_found',
118 'message' => __( 'Quiz not found for given ID', 'tutor' ),
119 'data' => $data,
120 );
121 return self::send( $response );
122 }
123
124 /**
125 * Get quiz question and answers.
126 *
127 * @param WP_REST_Request $request REST request object.
128 *
129 * @return mixed
130 */
131 public function quiz_question_ans( WP_REST_Request $request ) {
132 global $wpdb;
133
134 $this->post_parent = $request->get_param( 'id' );
135
136 $wpdb->q_t = $wpdb->prefix . $this->t_quiz_question; // Question table.
137
138 $wpdb->q_a_t = $wpdb->prefix . $this->t_quiz_ques_ans; // Question answer table.
139
140 $quizs = $wpdb->get_results(
141 $wpdb->prepare(
142 "SELECT
143 question_id,
144 question_title,
145 question_description,
146 question_type,
147 question_mark,
148 question_settings FROM {$wpdb->q_t}
149 WHERE quiz_id = %d
150 ",
151 $this->post_parent
152 )
153 );
154 $data = array();
155
156 if ( count( $quizs ) > 0 ) {
157 // Get question ans by question_id.
158 foreach ( $quizs as $quiz ) {
159 // Un-serialized question settings.
160 $quiz->question_settings = maybe_unserialize( $quiz->question_settings );
161
162 // question options with correct ans.
163 $options = $wpdb->get_results(
164 $wpdb->prepare(
165 "SELECT
166 answer_id,
167 answer_title,
168 is_correct FROM {$wpdb->q_a_t}
169 WHERE belongs_question_id = %d
170 ",
171 $quiz->question_id
172 )
173 );
174
175 // set question_answers as quiz property.
176 $quiz->question_answers = $options;
177
178 array_push( $data, $quiz );
179 }
180
181 $response = array(
182 'code' => 'success',
183 'message' => __( 'Question retrieved successfully', 'tutor' ),
184 'data' => $data,
185 );
186
187 return self::send( $response );
188 }
189
190 $response = array(
191 'code' => 'not_found',
192 'message' => __( 'Question not found for given ID', 'tutor' ),
193 'data' => array(),
194 );
195
196 return self::send( $response );
197 }
198
199 /**
200 * Get quiz attempt details.
201 *
202 * @since 1.7.1
203 *
204 * @param WP_REST_Request $request REST request object.
205 *
206 * @return mixed
207 */
208 public function quiz_attempt_details( WP_REST_Request $request ) {
209 global $wpdb;
210
211 $quiz_id = $request->get_param( 'id' );
212
213 $wpdb->quiz_attempt = $wpdb->prefix . $this->t_quiz_attempt;
214
215 $attempts = $wpdb->get_results(
216 $wpdb->prepare(
217 "SELECT
218 att.user_id,
219 att.total_questions,
220 att.total_answered_questions,
221 att.total_marks,
222 att.earned_marks,
223 att.attempt_info,
224 att.attempt_status,
225 att.attempt_started_at,
226 att.attempt_ended_at,
227 att.is_manually_reviewed,
228 att.manually_reviewed_at
229 FROM {$wpdb->quiz_attempt} att
230 WHERE att.quiz_id = %d
231 ",
232 $quiz_id
233 )
234 );
235
236 if ( count( $attempts ) > 0 ) {
237 // unserialize each attempt info.
238 foreach ( $attempts as $key => $attempt ) {
239 $attempt->attempt_info = maybe_unserialize( $attempt->attempt_info );
240 // attach attempt ans.
241 $answers = $this->get_quiz_attempt_ans( $quiz_id );
242
243 if ( false !== $answers ) {
244 $attempt->attempts_answer = $answers;
245 } else {
246 $attempt->attempts_answer = array();
247 }
248 }
249
250 $response = array(
251 'code' => 'success',
252 'message' => __( 'Quiz attempts retrieved successfully', 'tutor' ),
253 'data' => $attempts,
254 );
255
256 return self::send( $response );
257 }
258
259 $response = array(
260 'code' => 'not_found',
261 'message' => __( 'Quiz attempts not found for given ID', 'tutor' ),
262 'data' => array(),
263 );
264
265 return self::send( $response );
266 }
267
268 /**
269 * Get quiz attempt answers.
270 *
271 * @since 1.7.1
272 *
273 * @param int $quiz_id quiz id.
274 *
275 * @return mixed
276 */
277 protected function get_quiz_attempt_ans( $quiz_id ) {
278 global $wpdb;
279 $wpdb->quiz_attempt_ans = $wpdb->prefix . $this->t_quiz_attempt_ans;
280 $wpdb->quiz_question = $wpdb->prefix . $this->t_quiz_question;
281
282 // get attempt answers.
283 $answers = $wpdb->get_results(
284 $wpdb->prepare(
285 "SELECT
286 q.question_title,
287 att_ans.given_answer,
288 att_ans.question_mark,
289 att_ans.achieved_mark,
290 att_ans.minus_mark,
291 att_ans.is_correct FROM {$wpdb->quiz_attempt_ans} as att_ans
292 JOIN {$wpdb->quiz_question} q ON q.question_id = att_ans.question_id
293 WHERE att_ans.quiz_id = %d
294 ",
295 $quiz_id
296 )
297 );
298
299 if ( count( $answers ) > 0 ) {
300 // unserialize each given answer.
301 foreach ( $answers as $key => $answer ) {
302 $answer->given_answer = maybe_unserialize( $answer->given_answer );
303
304 if ( is_numeric( $answer->given_answer ) || is_array( $answer->given_answer ) ) {
305 $ids = $answer->given_answer;
306 $ans_title = $this->answer_titles_by_id( $ids );
307 $answer->given_answer = $ans_title;
308 }
309 }
310
311 return $answers;
312 }
313 return false;
314 }
315
316 /**
317 * Get answer titles by id.
318 *
319 * @since 1.7.1
320 *
321 * @param int $id answer id.
322 *
323 * @return mixed
324 */
325 protected function answer_titles_by_id( $id ) {
326 global $wpdb;
327 $wpdb->t_quiz_ques_ans = $wpdb->prefix . $this->t_quiz_ques_ans;
328
329 if ( is_array( $id ) ) {
330 $array = QueryHelper::prepare_in_clause( $id );
331
332 $results = $wpdb->get_results(
333 "SELECT
334 answer_title
335 FROM {$wpdb->t_quiz_ques_ans}
336 WHERE
337 answer_id IN ('" . $array . "')"//phpcs:ignore
338 );
339 } else {
340 $results = $wpdb->get_results(
341 $wpdb->prepare(
342 "SELECT
343 answer_title
344 FROM {$wpdb->t_quiz_ques_ans}
345 WHERE answer_id = %d",
346 $id
347 )
348 );
349 }
350
351 return $results;
352 }
353 }
354