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.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
351 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 Themeum\Products\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 $table = $wpdb->prefix . 'posts';
84
85 $quizs = $wpdb->get_results(
86 $wpdb->prepare(
87 "SELECT
88 ID,
89 post_title,
90 post_content,
91 post_name
92 FROM $table
93 WHERE post_type = %s
94 AND post_parent = %d
95 ",
96 $this->post_type,
97 $this->post_parent
98 )
99 );
100
101 $data = array();
102
103 if ( count( $quizs ) > 0 ) {
104 foreach ( $quizs as $quiz ) {
105 $quiz->quiz_settings = get_post_meta( $quiz->ID, 'tutor_quiz_option', false );
106
107 array_push( $data, $quiz );
108
109 $response = array(
110 'status_code' => 'success',
111 'message' => __( 'Quiz retrieved successfully', 'tutor' ),
112 'data' => $data,
113 );
114 }
115 return self::send( $response );
116 }
117
118 $response = array(
119 'status_code' => 'not_found',
120 'message' => __( 'Quiz not found for given ID', 'tutor' ),
121 'data' => $data,
122 );
123 return self::send( $response );
124 }
125
126 /**
127 * Get quiz question and answers.
128 *
129 * @param WP_REST_Request $request REST request object.
130 *
131 * @return mixed
132 */
133 public function quiz_question_ans( WP_REST_Request $request ) {
134 global $wpdb;
135
136 $this->post_parent = $request->get_param( 'id' );
137
138 $q_t = $wpdb->prefix . $this->t_quiz_question; // question table
139
140 $q_a_t = $wpdb->prefix . $this->t_quiz_ques_ans; // question answer table
141
142 $quizs = $wpdb->get_results(
143 $wpdb->prepare( "SELECT
144 question_id,
145 question_title,
146 question_description,
147 question_type,
148 question_mark,
149 question_settings FROM $q_t
150 WHERE quiz_id = %d
151 ",
152 $this->post_parent
153 )
154 );
155 $data = array();
156
157 if ( count( $quizs ) > 0 ) {
158 // get question ans by question_id
159 foreach ( $quizs as $quiz ) {
160 // un-serialized question settings.
161 $quiz->question_settings = maybe_unserialize( $quiz->question_settings );
162
163 // question options with correct ans.
164 $options = $wpdb->get_results(
165 $wpdb->prepare( "SELECT
166 answer_id,
167 answer_title,
168 is_correct FROM $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 'status_code' => 'success',
183 'message' => __( 'Question retrieved successfully', 'tutor' ),
184 'data' => $data,
185 );
186
187 return self::send( $response );
188 }
189
190 $response = array(
191 'status_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 $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 $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 'status_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 'status_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 $quiz_attempt_ans = $wpdb->prefix . $this->t_quiz_attempt_ans;
280 $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 $quiz_attempt_ans as att_ans
292 JOIN $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 $table = $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 $table
336 WHERE
337 answer_id IN ('" . $array . "')"
338 );
339 } else {
340 $results = $wpdb->get_results(
341 "SELECT
342 answer_title
343 FROM $table
344 WHERE answer_id = {$id}"
345 );
346 }
347
348 return $results;
349 }
350 }
351