PluginProbe ʕ •ᴥ•ʔ
Tutor LMS – eLearning and online course solution / 1.7.8
Tutor LMS – eLearning and online course solution v1.7.8
3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.11 1.2.12 1.2.13 1.2.20 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.15 1.9.16 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.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 / classes / Quiz.php
tutor / classes Last commit date
Addons.php 5 years ago Admin.php 5 years ago Ajax.php 5 years ago Assets.php 5 years ago Course.php 5 years ago Course_Filter.php 5 years ago Course_Settings_Tabs.php 5 years ago Course_Widget.php 5 years ago Custom_Validation.php 5 years ago Dashboard.php 5 years ago Delete_Enrollment_With_Order.php 5 years ago Email.php 5 years ago FormHandler.php 5 years ago Frontend.php 5 years ago Gutenberg.php 5 years ago Instructor.php 5 years ago Instructors_List.php 5 years ago Lesson.php 5 years ago Options.php 5 years ago Post_types.php 5 years ago Private_Course_Access.php 5 years ago Q_and_A.php 5 years ago Question_Answers_List.php 5 years ago Quiz.php 5 years ago Quiz_Attempts_List.php 5 years ago RestAPI.php 5 years ago Rewrite_Rules.php 5 years ago Shortcode.php 5 years ago Student.php 5 years ago Students_List.php 5 years ago Taxonomies.php 5 years ago Template.php 5 years ago Theme_Compatibility.php 5 years ago Tools.php 5 years ago Tutor.php 5 years ago TutorEDD.php 5 years ago Tutor_Base.php 5 years ago Tutor_List_Table.php 5 years ago Tutor_Setup.php 5 years ago Upgrader.php 5 years ago User.php 5 years ago Utils.php 5 years ago Video_Stream.php 5 years ago Withdraw.php 5 years ago Withdraw_Requests_List.php 5 years ago WooCommerce.php 5 years ago
Quiz.php
1117 lines
1 <?php
2
3 /**
4 * Quize class
5 *
6 * @author: themeum
7 * @author_uri: https://themeum.com
8 * @package Tutor
9 * @since v.1.0.0
10 */
11
12 namespace TUTOR;
13
14 if ( ! defined( 'ABSPATH' ) )
15 exit;
16
17 class Quiz {
18
19 public function __construct() {
20 add_action('save_post_tutor_quiz', array($this, 'save_quiz_meta'));
21
22 add_action('wp_ajax_tutor_load_quiz_builder_modal', array($this, 'tutor_load_quiz_builder_modal'));
23 add_action('wp_ajax_remove_quiz_from_post', array($this, 'remove_quiz_from_post'));
24
25 add_action('wp_ajax_tutor_quiz_timeout', array($this, 'tutor_quiz_timeout'));
26
27 //User take the quiz
28 add_action('template_redirect', array($this, 'start_the_quiz'));
29 add_action('template_redirect', array($this, 'answering_quiz'));
30 add_action('template_redirect', array($this, 'finishing_quiz_attempt'));
31
32 add_action('admin_action_review_quiz_answer', array($this, 'review_quiz_answer'));
33 add_action('wp_ajax_review_quiz_answer', array($this, 'review_quiz_answer'));
34 add_action('wp_ajax_tutor_instructor_feedback', array($this, 'tutor_instructor_feedback')); // Instructor Feedback Action
35
36 /**
37 * New Design Quiz
38 */
39
40 add_action('wp_ajax_tutor_create_quiz_and_load_modal', array($this, 'tutor_create_quiz_and_load_modal'));
41 add_action('wp_ajax_tutor_delete_quiz_by_id', array($this, 'tutor_delete_quiz_by_id'));
42 add_action('wp_ajax_tutor_quiz_builder_quiz_update', array($this, 'tutor_quiz_builder_quiz_update'));
43 add_action('wp_ajax_tutor_load_edit_quiz_modal', array($this, 'tutor_load_edit_quiz_modal'));
44 add_action('wp_ajax_tutor_quiz_builder_get_question_form', array($this, 'tutor_quiz_builder_get_question_form'));
45 add_action('wp_ajax_tutor_quiz_modal_update_question', array($this, 'tutor_quiz_modal_update_question'));
46 add_action('wp_ajax_tutor_quiz_builder_question_delete', array($this, 'tutor_quiz_builder_question_delete'));
47 add_action('wp_ajax_tutor_quiz_add_question_answers', array($this, 'tutor_quiz_add_question_answers'));
48 add_action('wp_ajax_tutor_quiz_edit_question_answer', array($this, 'tutor_quiz_edit_question_answer'));
49 add_action('wp_ajax_tutor_save_quiz_answer_options', array($this, 'tutor_save_quiz_answer_options'));
50 add_action('wp_ajax_tutor_update_quiz_answer_options', array($this, 'tutor_update_quiz_answer_options'));
51 add_action('wp_ajax_tutor_quiz_builder_get_answers_by_question', array($this, 'tutor_quiz_builder_get_answers_by_question'));
52 add_action('wp_ajax_tutor_quiz_builder_delete_answer', array($this, 'tutor_quiz_builder_delete_answer'));
53 add_action('wp_ajax_tutor_quiz_question_sorting', array($this, 'tutor_quiz_question_sorting'));
54 add_action('wp_ajax_tutor_quiz_answer_sorting', array($this, 'tutor_quiz_answer_sorting'));
55 add_action('wp_ajax_tutor_mark_answer_as_correct', array($this, 'tutor_mark_answer_as_correct'));
56 add_action('wp_ajax_tutor_quiz_modal_update_settings', array($this, 'tutor_quiz_modal_update_settings'));
57
58 /**
59 * Frontend Stuff
60 */
61 add_action('wp_ajax_tutor_render_quiz_content', array($this, 'tutor_render_quiz_content'));
62 }
63
64 public function tutor_instructor_feedback(){
65 $feedback = sanitize_text_field($_POST['feedback']);
66 $attempt_id = (int) tutor_utils()->avalue_dot('attempts_id', $_POST);
67
68 if ($attempt_id && tutils()->can_user_manage('attempt', $attempt_id)) {
69 update_post_meta($attempt_id, 'instructor_feedback', $feedback);
70 do_action('tutor_quiz/attempt/submitted/feedback', $attempt_id);
71 }
72 }
73
74 public function save_quiz_meta($post_ID){
75 if (isset($_POST['quiz_option'])){
76 $quiz_option = tutor_utils()->sanitize_array($_POST['quiz_option']);
77 update_post_meta($post_ID, 'tutor_quiz_option', $quiz_option);
78 }
79 }
80
81 /**
82 * Tutor Quiz Builder Modal
83 */
84 public function tutor_load_quiz_builder_modal(){
85 ob_start();
86 include tutor()->path.'views/modal/add_quiz.php';
87 $output = ob_get_clean();
88
89 wp_send_json_success(array('output' => $output));
90 }
91
92 public function remove_quiz_from_post(){
93 global $wpdb;
94 $quiz_id = (int) tutor_utils()->avalue_dot('quiz_id', $_POST);
95
96 if(!tutils()->can_user_manage('quiz', $quiz_id)) {
97 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
98 }
99
100 $wpdb->update($wpdb->posts, array('post_parent' => 0), array('ID' => $quiz_id) );
101 wp_send_json_success();
102 }
103
104 /**
105 *
106 * Start Quiz from here...
107 *
108 * @since v.1.0.0
109 */
110
111 public function start_the_quiz(){
112 if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_start_quiz' ){
113 return;
114 }
115 //Checking nonce
116 tutor_utils()->checking_nonce();
117
118 if ( ! is_user_logged_in()){
119 //TODO: need to set a view in the next version
120 die('Please sign in to do this operation');
121 }
122
123 global $wpdb;
124
125 $user_id = get_current_user_id();
126 $user = get_userdata($user_id);
127
128 $quiz_id = (int) sanitize_text_field($_POST['quiz_id']);
129
130 $quiz = get_post($quiz_id);
131 $course = tutor_utils()->get_course_by_quiz($quiz_id);
132 if ( empty($course->ID)){
133 die('There is something went wrong with course, please check if quiz attached with a course');
134 }
135
136 do_action('tutor_quiz/start/before', $quiz_id, $user_id);
137
138 $date = date("Y-m-d H:i:s", tutor_time());
139
140 $tutor_quiz_option = (array) maybe_unserialize(get_post_meta($quiz_id, 'tutor_quiz_option', true));
141 $attempts_allowed = tutor_utils()->get_quiz_option($quiz_id, 'attempts_allowed', 0);
142
143 $time_limit = tutor_utils()->get_quiz_option($quiz_id, 'time_limit.time_value');
144 $time_limit_seconds = 0;
145 $time_type = 'seconds';
146 if ($time_limit){
147 $time_type = tutor_utils()->get_quiz_option($quiz_id, 'time_limit.time_type');
148
149 switch ($time_type){
150 case 'seconds':
151 $time_limit_seconds = $time_limit;
152 break;
153 case 'minutes':
154 $time_limit_seconds = $time_limit * 60;
155 break;
156 case 'hours':
157 $time_limit_seconds = $time_limit * 60 * 60;
158 break;
159 case 'days':
160 $time_limit_seconds = $time_limit * 60 * 60 * 24;
161 break;
162 case 'weeks':
163 $time_limit_seconds = $time_limit * 60 * 60 * 24 * 7;
164 break;
165 }
166 }
167
168 $max_question_allowed = tutor_utils()->max_questions_for_take_quiz($quiz_id);
169 $tutor_quiz_option['time_limit']['time_limit_seconds'] = $time_limit_seconds;
170
171 $attempt_data = array(
172 'course_id' => $course->ID,
173 'quiz_id' => $quiz_id,
174 'user_id' => $user_id,
175 'total_questions' => $max_question_allowed,
176 'total_answered_questions' => 0,
177 'attempt_info' => maybe_serialize($tutor_quiz_option),
178 'attempt_status' => 'attempt_started',
179 'attempt_ip' => tutor_utils()->get_ip(),
180 'attempt_started_at' => $date,
181 );
182
183 $wpdb->insert($wpdb->prefix.'tutor_quiz_attempts', $attempt_data);
184 $attempt_id = (int) $wpdb->insert_id;
185
186 do_action('tutor_quiz/start/after', $quiz_id, $user_id, $attempt_id);
187
188 wp_redirect(get_permalink($quiz_id));
189 die();
190 }
191
192 public function answering_quiz(){
193
194 if ( tutils()->array_get('tutor_action', $_POST) !== 'tutor_answering_quiz_question' ){
195 return;
196 }
197 //Checking nonce
198 tutor_utils()->checking_nonce();
199
200 $attempt_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('attempt_id', $_POST));
201 $attempt = tutor_utils()->get_attempt($attempt_id);
202
203 $attempt_answers = isset($_POST['attempt']) ? $_POST['attempt'] : false;
204 if ( ! is_user_logged_in()){
205 die('Please sign in to do this operation');
206 }
207
208 global $wpdb;
209 $user_id = get_current_user_id();
210
211 do_action('tutor_quiz/attempt_analysing/before', $attempt_id);
212
213 if ($attempt_answers && is_array($attempt_answers) && count($attempt_answers)){
214 foreach ($attempt_answers as $attempt_id => $attempt_answer){
215
216 /**
217 * Get total marks of all question comes
218 */
219 $question_ids = tutor_utils()->avalue_dot('quiz_question_ids', $attempt_answer);
220 if (is_array($question_ids) && count($question_ids)){
221 $question_ids_string = "'".implode("','", $question_ids)."'";
222 $total_question_marks = $wpdb->get_var("SELECT SUM(question_mark) FROM {$wpdb->prefix}tutor_quiz_questions WHERE question_id IN({$question_ids_string}) ;");
223 $wpdb->update($wpdb->prefix.'tutor_quiz_attempts', array('total_marks' =>$total_question_marks ), array('attempt_id' => $attempt_id ));
224 }
225
226 if ( ! $attempt || $user_id != $attempt->user_id){
227 die('Operation not allowed, attempt not found or permission denied');
228 }
229
230 $quiz_answers = tutor_utils()->avalue_dot('quiz_question', $attempt_answer);
231
232 $total_marks = 0;
233 $review_required = false;
234
235 if ( tutils()->count($quiz_answers)) {
236
237 foreach ( $quiz_answers as $question_id => $answers ) {
238 $question = tutor_utils()->get_quiz_question_by_id( $question_id );
239 $question_type = $question->question_type;
240
241 $is_answer_was_correct = false;
242 $given_answer = '';
243
244 if ( $question_type === 'true_false' || $question_type === 'single_choice' ) {
245
246 $given_answer = $answers;
247 $is_answer_was_correct = (bool) $wpdb->get_var( "SELECT is_correct FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE answer_id = {$answers} " );
248
249 } elseif ( $question_type === 'multiple_choice' ) {
250
251 $given_answer = (array) ( $answers );
252 $get_original_answers = (array) $wpdb->get_col( "SELECT answer_id FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id = {$question->question_id} AND belongs_question_type = '{$question_type}' AND is_correct = 1 ;" );
253 if (count(array_diff($get_original_answers, $given_answer)) === 0 && count($get_original_answers) === count($given_answer)) {
254 $is_answer_was_correct = true;
255 }
256 $given_answer = maybe_serialize( $answers );
257
258 } elseif ( $question_type === 'fill_in_the_blank' ) {
259
260 $given_answer = (array) array_map( 'sanitize_text_field', $answers );
261 $given_answer = maybe_serialize( $given_answer );
262
263 $get_original_answer = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id = {$question->question_id} AND belongs_question_type = '{$question_type}' ;" );
264 $gap_answer = (array) explode( '|', $get_original_answer->answer_two_gap_match );
265
266 $gap_answer = array_map( 'sanitize_text_field', $gap_answer );
267 if ( strtolower($given_answer) == strtolower(maybe_serialize( $gap_answer )) ) {
268 $is_answer_was_correct = true;
269 }
270 } elseif ( $question_type === 'open_ended' || $question_type === 'short_answer' ) {
271 $review_required = true;
272 $given_answer = wp_kses_post( $answers );
273
274 } elseif ( $question_type === 'ordering' || $question_type === 'matching' || $question_type === 'image_matching' ) {
275
276 $given_answer = (array) array_map( 'sanitize_text_field', tutor_utils()->avalue_dot( 'answers', $answers ) );
277 $given_answer = maybe_serialize( $given_answer );
278
279 $get_original_answers = (array) $wpdb->get_col( "SELECT answer_id FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id = {$question->question_id} AND belongs_question_type = '{$question_type}' ORDER BY answer_order ASC ;" );
280 $get_original_answers = array_map( 'sanitize_text_field', $get_original_answers );
281
282 if ( $given_answer == maybe_serialize( $get_original_answers ) ) {
283 $is_answer_was_correct = true;
284 }
285
286 } elseif ( $question_type === 'image_answering' ) {
287 $image_inputs = tutor_utils()->avalue_dot( 'answer_id', $answers );
288 $image_inputs = (array) array_map( 'sanitize_text_field', $image_inputs );
289 $given_answer = maybe_serialize( $image_inputs );
290 $is_answer_was_correct = false;
291
292 $db_answer = $wpdb->get_col( "SELECT answer_title FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id = {$question_id} AND belongs_question_type =
293 'image_answering' ORDER BY answer_order asc ;" );
294
295 if ( is_array( $db_answer ) && count( $db_answer ) ) {
296 $is_answer_was_correct = ( strtolower( maybe_serialize( array_values( $image_inputs ) ) ) == strtolower( maybe_serialize( $db_answer ) ) );
297 }
298 }
299
300 $question_mark = $is_answer_was_correct ? $question->question_mark : 0;
301 $total_marks += $question_mark;
302
303 $answers_data = array(
304 'user_id' => $user_id,
305 'quiz_id' => $attempt->quiz_id,
306 'question_id' => $question_id,
307 'quiz_attempt_id' => $attempt_id,
308 'given_answer' => $given_answer,
309 'question_mark' => $question->question_mark,
310 'achieved_mark' => $question_mark,
311 'minus_mark' => 0,
312 'is_correct' => $is_answer_was_correct ? 1 : 0,
313 );
314
315 /*
316 check if question_type open ended or short ans the set is_correct default value null before saving
317 */
318 if($question_type==="open_ended" || $question_type ==="short_answer")
319 {
320 $answers_data['is_correct'] = NULL;
321 }
322
323 $wpdb->insert( $wpdb->prefix . 'tutor_quiz_attempt_answers', $answers_data );
324 }
325 }
326
327 $attempt_info = array(
328 'total_answered_questions' => tutils()->count($quiz_answers),
329 'earned_marks' => $total_marks,
330 'attempt_status' => 'attempt_ended',
331 'attempt_ended_at' => date("Y-m-d H:i:s", tutor_time()),
332 );
333
334 if ($review_required){
335 $attempt_info['attempt_status'] = 'review_required';
336 }
337
338 $wpdb->update($wpdb->prefix.'tutor_quiz_attempts', $attempt_info, array('attempt_id' => $attempt_id));
339 }
340
341 do_action('tutor_quiz/attempt_ended', $attempt_id);
342 }
343
344 wp_redirect(get_the_permalink($attempt->quiz_id));
345 die();
346 }
347
348
349 /**
350 * Quiz attempt will be finish here
351 *
352 */
353
354 public function finishing_quiz_attempt(){
355
356 if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_finish_quiz_attempt' ){
357 return;
358 }
359 //Checking nonce
360 tutor_utils()->checking_nonce();
361
362 if ( ! is_user_logged_in()){
363 die('Please sign in to do this operation');
364 }
365
366 global $wpdb;
367
368 $quiz_id = (int) sanitize_text_field($_POST['quiz_id']);
369 $attempt = tutor_utils()->is_started_quiz($quiz_id);
370 $attempt_id = $attempt->attempt_id;
371
372 $attempt_info = array(
373 'total_answered_questions' => 0,
374 'earned_marks' => 0,
375 'attempt_status' => 'attempt_ended',
376 'attempt_ended_at' => date("Y-m-d H:i:s", tutor_time()),
377 );
378
379 do_action('tutor_quiz_before_finish', $attempt_id, $quiz_id, $attempt->user_id);
380 $wpdb->update($wpdb->prefix.'tutor_quiz_attempts', $attempt_info, array('attempt_id' => $attempt_id));
381 do_action('tutor_quiz_finished', $attempt_id, $quiz_id, $attempt->user_id);
382
383 wp_redirect(tutor_utils()->input_old('_wp_http_referer'));
384 }
385
386 /**
387 * Quiz timeout by ajax
388 */
389 public function tutor_quiz_timeout(){
390 global $wpdb;
391
392 $quiz_id = (int) sanitize_text_field($_POST['quiz_id']);
393 $attempt = tutor_utils()->is_started_quiz($quiz_id);
394
395 if ($attempt) {
396 $attempt_id = $attempt->attempt_id;
397
398 $data = array(
399 'attempt_status' => 'attempt_timeout',
400 'attempt_ended_at' => date("Y-m-d H:i:s", tutor_time()),
401 );
402 $wpdb->update($wpdb->prefix.'tutor_quiz_attempts', $data, array('attempt_id' => $attempt->attempt_id));
403
404 do_action('tutor_quiz_timeout', $attempt_id, $quiz_id, $attempt->user_id);
405
406 wp_send_json_success();
407 }
408
409 wp_send_json_error(__('Quiz has been timeout already', 'tutor'));
410 }
411
412 /**
413 * Review the answer and change individual answer result
414 */
415
416 public function review_quiz_answer(){
417
418 global $wpdb;
419
420 $attempt_id = (int) sanitize_text_field($_GET['attempt_id']);
421 $attempt_answer_id = (int) sanitize_text_field($_GET['attempt_answer_id']);
422 $mark_as = sanitize_text_field($_GET['mark_as']);
423
424 if(!tutils()->can_user_manage('attempt', $attempt_id) || !tutils()->can_user_manage('attempt_answer', $attempt_answer_id)) {
425 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
426 }
427
428 $attempt_answer = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}tutor_quiz_attempt_answers WHERE attempt_answer_id = %d ", $attempt_answer_id));
429
430 $attempt = tutor_utils()->get_attempt($attempt_id);
431 $question = tutils()->get_quiz_question_by_id($attempt_answer->question_id);
432
433 $previous_ans = $attempt_answer->is_correct;
434
435 do_action('tutor_quiz_review_answer_before', $attempt_answer_id, $attempt_id, $mark_as);
436
437 if ($mark_as === 'correct'){
438
439 $answer_update_data = array(
440 'achieved_mark' => $attempt_answer->question_mark,
441 'is_correct' => 1,
442 );
443 $wpdb->update($wpdb->prefix.'tutor_quiz_attempt_answers', $answer_update_data, array('attempt_answer_id' => $attempt_answer_id ));
444 if($previous_ans ==0 OR $previous_ans ==null)
445 {
446
447 //if previous answer was wrong or in review then add point as correct
448 $attempt_update_data = array(
449 'earned_marks' => $attempt->earned_marks + $attempt_answer->question_mark,
450 'is_manually_reviewed' => 1,
451 'manually_reviewed_at' => date("Y-m-d H:i:s", tutor_time()),
452 );
453
454
455 }
456
457 if ($question->question_type === 'open_ended' || $question->question_type === 'short_answer' ){
458 $attempt_update_data['attempt_status'] = 'attempt_ended';
459 }
460 $wpdb->update($wpdb->prefix.'tutor_quiz_attempts', $attempt_update_data, array('attempt_id' => $attempt_id ));
461 }
462 elseif($mark_as === 'incorrect')
463 {
464
465 $answer_update_data = array(
466 'achieved_mark' => '0.00',
467 'is_correct' => 0,
468 );
469 $wpdb->update($wpdb->prefix.'tutor_quiz_attempt_answers', $answer_update_data, array('attempt_answer_id' => $attempt_answer_id ));
470
471
472 if($previous_ans ==1)
473 {
474
475 //if previous ans was right then mynus
476 $attempt_update_data = array(
477 'earned_marks' => $attempt->earned_marks - $attempt_answer->question_mark,
478 'is_manually_reviewed' => 1,
479 'manually_reviewed_at' => date("Y-m-d H:i:s", tutor_time()),
480 );
481
482 }
483 if ($question->question_type === 'open_ended' || $question->question_type === 'short_answer' ){
484 $attempt_update_data['attempt_status'] = 'attempt_ended';
485 }
486
487 $wpdb->update($wpdb->prefix.'tutor_quiz_attempts', $attempt_update_data, array('attempt_id' => $attempt_id ));
488 }
489 do_action('tutor_quiz_review_answer_after', $attempt_answer_id, $attempt_id, $mark_as);
490
491 if (wp_doing_ajax())
492 {
493 wp_send_json_success();
494 }
495 else{
496 wp_redirect(admin_url("admin.php?page=tutor_quiz_attempts&sub_page=view_attempt&attempt_id=".$attempt_id));
497 }
498
499 die();
500 }
501
502
503 /**
504 * New Design Quiz
505 */
506 public function tutor_create_quiz_and_load_modal(){
507 $topic_id = sanitize_text_field($_POST['topic_id']);
508 $quiz_title = sanitize_text_field($_POST['quiz_title']);
509 $quiz_description = sanitize_text_field($_POST['quiz_description']);
510 $next_order_id = tutor_utils()->get_next_course_content_order_id($topic_id);
511
512 if(!tutils()->can_user_manage('topic', $topic_id)) {
513 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
514 }
515
516 $post_arr = array(
517 'post_type' => 'tutor_quiz',
518 'post_title' => $quiz_title,
519 'post_content' => $quiz_description,
520 'post_status' => 'publish',
521 'post_author' => get_current_user_id(),
522 'post_parent' => $topic_id,
523 'menu_order' => $next_order_id,
524 );
525 $quiz_id = wp_insert_post( $post_arr );
526 do_action('tutor_initial_quiz_created', $quiz_id);
527
528 ob_start();
529 include tutor()->path.'views/modal/edit_quiz.php';
530 $output = ob_get_clean();
531
532 ob_start();
533 ?>
534 <div id="tutor-quiz-<?php echo $quiz_id; ?>" class="course-content-item tutor-quiz tutor-quiz-<?php echo $quiz_id; ?>">
535 <div class="tutor-lesson-top">
536 <i class="tutor-icon-move"></i>
537 <a href="javascript:;" class="open-tutor-quiz-modal" data-quiz-id="<?php echo $quiz_id; ?>" data-topic-id="<?php echo $topic_id;
538 ?>"> <i class=" tutor-icon-doubt"></i>[<?php _e('QUIZ', 'tutor'); ?>] <?php echo stripslashes($quiz_title); ?> </a>
539 <?php do_action('tutor_course_builder_before_quiz_btn_action', $quiz_id); ?>
540 <a href="javascript:;" class="tutor-delete-quiz-btn" data-quiz-id="<?php echo $quiz_id; ?>"><i class="tutor-icon-garbage"></i></a>
541 </div>
542 </div>
543 <?php
544 $output_quiz_row = ob_get_clean();
545
546 wp_send_json_success(array('output' => $output, 'output_quiz_row' => $output_quiz_row));
547 }
548
549 public function tutor_delete_quiz_by_id(){
550 global $wpdb;
551
552 $quiz_id = (int) sanitize_text_field($_POST['quiz_id']);
553 $post = get_post($quiz_id);
554
555
556 if(!tutils()->can_user_manage('quiz', $quiz_id)) {
557 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
558 }
559
560 if ( $post->post_type === 'tutor_quiz'){
561 do_action('tutor_delete_quiz_before', $quiz_id);
562
563 $wpdb->delete($wpdb->prefix.'tutor_quiz_attempts', array('quiz_id' => $quiz_id));
564 $wpdb->delete($wpdb->prefix.'tutor_quiz_attempt_answers', array('quiz_id' => $quiz_id));
565
566 $questions_ids = $wpdb->get_col($wpdb->prepare("SELECT question_id FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = %d ", $quiz_id));
567
568 if (is_array($questions_ids) && count($questions_ids)){
569 $in_question_ids = "'".implode("','", $questions_ids)."'";
570 $wpdb->query("DELETE FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id IN({$in_question_ids}) ");
571 }
572
573 $wpdb->delete($wpdb->prefix.'tutor_quiz_questions', array('quiz_id' => $quiz_id));
574
575 wp_delete_post($quiz_id, true);
576 delete_post_meta($quiz_id, '_tutor_course_id_for_lesson');
577
578 do_action('tutor_delete_quiz_after', $quiz_id);
579
580
581 wp_send_json_success();
582 }
583
584 wp_send_json_error();
585 }
586
587 /**
588 * Update Quiz from quiz builder modal
589 *
590 * @since v.1.0.0
591 */
592 public function tutor_quiz_builder_quiz_update(){
593 $quiz_id = sanitize_text_field($_POST['quiz_id']);
594 $topic_id = sanitize_text_field($_POST['topic_id']);
595 $quiz_title = sanitize_text_field($_POST['quiz_title']);
596 $quiz_description = sanitize_text_field($_POST['quiz_description']);
597
598 if(!tutils()->can_user_manage('quiz', $quiz_id)) {
599 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
600 }
601
602 $post_arr = array(
603 'ID' => $quiz_id,
604 'post_title' => $quiz_title,
605 'post_content' => $quiz_description,
606
607 );
608 $quiz_id = wp_update_post( $post_arr );
609
610 do_action('tutor_quiz_updated', $quiz_id);
611
612 ob_start();
613 ?>
614 <div class="tutor-lesson-top">
615 <i class="tutor-icon-move"></i>
616 <a href="javascript:;" class="open-tutor-quiz-modal" data-quiz-id="<?php echo $quiz_id; ?>" data-topic-id="<?php echo $topic_id;
617 ?>"> <i class=" tutor-icon-doubt"></i>[<?php _e('QUIZ', 'tutor'); ?>] <?php echo stripslashes($quiz_title); ?> </a>
618 <?php do_action('tutor_course_builder_before_quiz_btn_action', $quiz_id); ?>
619 <a href="javascript:;" class="tutor-delete-quiz-btn" data-quiz-id="<?php echo $quiz_id; ?>"><i class="tutor-icon-garbage"></i></a>
620 </div>
621 <?php
622 $output_quiz_row = ob_get_clean();
623
624 wp_send_json_success(array('output_quiz_row' => $output_quiz_row));
625 }
626
627 /**
628 * Load quiz Modal for edit quiz
629 *
630 * @since v.1.0.0
631 */
632 public function tutor_load_edit_quiz_modal(){
633 $quiz_id = sanitize_text_field($_POST['quiz_id']);
634
635 if(!tutils()->can_user_manage('quiz', $quiz_id)) {
636 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
637 }
638
639 ob_start();
640 include tutor()->path.'views/modal/edit_quiz.php';
641 $output = ob_get_clean();
642
643 wp_send_json_success(array('output' => $output));
644 }
645
646 /**
647 * Load quiz question form for quiz
648 *
649 * @since v.1.0.0
650 */
651 public function tutor_quiz_builder_get_question_form(){
652 global $wpdb;
653 $quiz_id = sanitize_text_field($_POST['quiz_id']);
654 $question_id = sanitize_text_field(tutor_utils()->avalue_dot('question_id', $_POST));
655
656 if ( ! $question_id){
657 $next_question_id = tutor_utils()->quiz_next_question_id();
658 $next_question_order = tutor_utils()->quiz_next_question_order_id($quiz_id);
659
660 $new_question_data = array(
661 'quiz_id' => $quiz_id,
662 'question_title' => __('Question', 'tutor').' '.$next_question_id,
663 'question_description' => '',
664 'question_type' => 'true_false',
665 'question_mark' => 1,
666 'question_settings' => maybe_serialize(array()),
667 'question_order' => esc_sql( $next_question_order ) ,
668 );
669
670 $wpdb->insert($wpdb->prefix.'tutor_quiz_questions', $new_question_data);
671 $question_id = $wpdb->insert_id;
672 }
673
674 $question = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}tutor_quiz_questions where question_id = %d ", $question_id));
675
676 ob_start();
677 include tutor()->path.'views/modal/question_form.php';
678 $output = ob_get_clean();
679
680 wp_send_json_success(array('output' => $output));
681 }
682
683 public function tutor_quiz_modal_update_question(){
684 global $wpdb;
685
686 $question_data = $_POST['tutor_quiz_question'];
687
688 foreach ($question_data as $question_id => $question) {
689
690 if(!tutils()->can_user_manage('question', $question_id)) {
691 continue;
692 }
693
694 $question_title = sanitize_text_field($question['question_title']);
695 $question_description = sanitize_text_field($question['question_description']);
696 $question_type = sanitize_text_field($question['question_type']);
697 $question_mark = sanitize_text_field($question['question_mark']);
698
699 unset($question['question_title']);
700 unset($question['question_description']);
701
702 $data = array(
703 'question_title' => $question_title,
704 'question_description' => $question_description,
705 'question_type' => $question_type,
706 'question_mark' => $question_mark,
707 'question_settings' => maybe_serialize($question),
708 );
709
710 $wpdb->update($wpdb->prefix.'tutor_quiz_questions', $data, array('question_id' => $question_id) );
711
712
713 /**
714 * Validation
715 */
716 if ($question_type === 'true_false' || $question_type === 'single_choice'){
717 $question_options = tutils()->get_answers_by_quiz_question($question_id);
718 if (tutils()->count($question_options)){
719 $required_validate = true;
720 foreach ($question_options as $question_option){
721 if ($question_option->is_correct){
722 $required_validate = false;
723 }
724 }
725 if ($required_validate){
726 $validation_msg = "<p class='tutor-error-msg'>".__('Please select the correct answer', 'tutor')."</p>";
727 wp_send_json_error(array('validation_msg' => $validation_msg ));
728 }
729 }else{
730 $validation_msg = "<p class='tutor-error-msg'>".__('Please make sure you have added more than one option and saved them', 'tutor')."</p>";
731 wp_send_json_error(array('validation_msg' => $validation_msg ));
732 }
733 }
734 }
735
736 wp_send_json_success();
737 }
738
739 public function tutor_quiz_builder_question_delete(){
740 global $wpdb;
741
742 $question_id = sanitize_text_field(tutor_utils()->avalue_dot('question_id', $_POST));
743
744 if(!tutils()->can_user_manage('question', $question_id)) {
745 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
746 }
747
748 if ($question_id){
749 $wpdb->delete($wpdb->prefix.'tutor_quiz_questions', array('question_id' => esc_sql( $question_id ) ));
750 }
751
752 wp_send_json_success();
753 }
754
755 /**
756 * Get answers options form for quiz question
757 *
758 * @since v.1.0.0
759 */
760 public function tutor_quiz_add_question_answers(){
761 $question_id = sanitize_text_field($_POST['question_id']);
762 $question = tutor_utils()->avalue_dot($question_id, $_POST['tutor_quiz_question']);
763 $question_type = $question['question_type'];
764
765 ob_start();
766 include tutor()->path.'views/modal/question_answer_form.php';
767 $output = ob_get_clean();
768
769 wp_send_json_success(array('output' => $output));
770 }
771
772 /**
773 * Edit Answer Form
774 *
775 * @since v.1.0.0
776 */
777 public function tutor_quiz_edit_question_answer(){
778 $answer_id = (int) sanitize_text_field($_POST['answer_id']);
779
780 if(!tutils()->can_user_manage('quiz_answer', $answer_id)) {
781 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
782 }
783
784 $old_answer = tutor_utils()->get_answer_by_id($answer_id);
785 foreach ($old_answer as $old_answer);
786 $question_id = $old_answer->belongs_question_id;
787 $question_type = $old_answer->belongs_question_type;
788
789 ob_start();
790 include tutor()->path.'views/modal/question_answer_edit_form.php';
791 $output = ob_get_clean();
792
793 wp_send_json_success(array('output' => $output));
794 }
795
796 public function tutor_save_quiz_answer_options(){
797 global $wpdb;
798
799 $questions = $_POST['tutor_quiz_question'];
800 $answers = $_POST['quiz_answer'];
801
802 foreach ($answers as $question_id => $answer){
803
804 if(!tutils()->can_user_manage('question', $question_id)) {
805 continue;
806 }
807
808 $question = tutor_utils()->avalue_dot($question_id, $questions);
809 $question_type = $question['question_type'];
810
811 //Getting next sorting order
812 $next_order_id = (int) $wpdb->get_var($wpdb->prepare(
813 "SELECT MAX(answer_order)
814 FROM {$wpdb->prefix}tutor_quiz_question_answers
815 where belongs_question_id = %d
816 AND belongs_question_type = %s ", $question_id, esc_sql( $question_type )));
817
818 $next_order_id = $next_order_id + 1;
819
820 if ($question){
821 if ($question_type === 'true_false'){
822 $wpdb->delete($wpdb->prefix.'tutor_quiz_question_answers', array('belongs_question_id' => $question_id, 'belongs_question_type' => $question_type));
823 $data_true_false = array(
824 array(
825 'belongs_question_id' => esc_sql( $question_id ) ,
826 'belongs_question_type' => $question_type,
827 'answer_title' => __('True', 'tutor'),
828 'is_correct' => $answer['true_false'] == 'true' ? 1 : 0,
829 'answer_two_gap_match' => 'true',
830 ),
831 array(
832 'belongs_question_id' => esc_sql( $question_id ) ,
833 'belongs_question_type' => $question_type,
834 'answer_title' => __('False', 'tutor'),
835 'is_correct' => $answer['true_false'] == 'false' ? 1 : 0,
836 'answer_two_gap_match' => 'false',
837 ),
838 );
839
840 foreach ($data_true_false as $true_false_data){
841 $wpdb->insert($wpdb->prefix.'tutor_quiz_question_answers', $true_false_data);
842 }
843
844 }elseif($question_type === 'multiple_choice' || $question_type === 'single_choice' || $question_type === 'ordering' ||
845 $question_type === 'matching' || $question_type === 'image_matching' || $question_type === 'image_answering' ){
846
847 $answer_data = array(
848 'belongs_question_id' => sanitize_text_field( $question_id ),
849 'belongs_question_type' => $question_type,
850 'answer_title' => sanitize_text_field( $answer['answer_title'] ),
851 'image_id' => isset($answer['image_id']) ? $answer['image_id'] : 0,
852 'answer_view_format' => isset($answer['answer_view_format']) ? $answer['answer_view_format'] : 0,
853 'answer_order' => $next_order_id,
854 );
855 if (isset($answer['matched_answer_title'])){
856 $answer_data['answer_two_gap_match'] = sanitize_text_field( $answer['matched_answer_title'] );
857 }
858
859 $wpdb->insert($wpdb->prefix.'tutor_quiz_question_answers', $answer_data);
860
861 }elseif($question_type === 'fill_in_the_blank'){
862 $wpdb->delete($wpdb->prefix.'tutor_quiz_question_answers', array('belongs_question_id' => $question_id, 'belongs_question_type' => $question_type));
863 $answer_data = array(
864 'belongs_question_id' => sanitize_text_field( $question_id ) ,
865 'belongs_question_type' => $question_type,
866 'answer_title' => sanitize_text_field( $answer['answer_title'] ),
867 'answer_two_gap_match' => isset($answer['answer_two_gap_match']) ? sanitize_text_field( trim($answer['answer_two_gap_match']) ) : null,
868 );
869 $wpdb->insert($wpdb->prefix.'tutor_quiz_question_answers', $answer_data);
870 }
871 }
872 }
873
874 wp_send_json_success();
875 }
876
877 /**
878 * Tutor Update Answer
879 *
880 * @since v.1.0.0
881 */
882 public function tutor_update_quiz_answer_options(){
883 global $wpdb;
884
885 $answer_id = (int) sanitize_text_field($_POST['tutor_quiz_answer_id']);
886
887 if(!tutils()->can_user_manage('quiz_answer', $answer_id)) {
888 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
889 }
890
891 $questions = $_POST['tutor_quiz_question'];
892 $answers = $_POST['quiz_answer'];
893
894 foreach ($answers as $question_id => $answer){
895 $question = tutor_utils()->avalue_dot($question_id, $questions);
896 $question_type = $question['question_type'];
897
898 if ($question){
899 if($question_type === 'multiple_choice' || $question_type === 'single_choice' || $question_type === 'ordering' || $question_type === 'matching' || $question_type === 'image_matching' || $question_type === 'fill_in_the_blank' || $question_type === 'image_answering' ){
900
901 $answer_data = array(
902 'belongs_question_id' => $question_id,
903 'belongs_question_type' => $question_type,
904 'answer_title' => sanitize_text_field( $answer['answer_title'] ) ,
905 'image_id' => isset($answer['image_id']) ? $answer['image_id'] : 0,
906 'answer_view_format' => isset($answer['answer_view_format']) ? sanitize_text_field( $answer['answer_view_format'] ) : '',
907 );
908 if (isset($answer['matched_answer_title'])){
909 $answer_data['answer_two_gap_match'] = sanitize_text_field( $answer['matched_answer_title'] ) ;
910 }
911
912 if ($question_type === 'fill_in_the_blank'){
913 $answer_data['answer_two_gap_match'] = isset($answer['answer_two_gap_match']) ? sanitize_text_field(trim($answer['answer_two_gap_match'])) : null;
914 }
915
916 $wpdb->update($wpdb->prefix.'tutor_quiz_question_answers', $answer_data, array('answer_id' => $answer_id));
917 }
918 }
919 }
920
921 //die(print_r($_POST));
922 wp_send_json_success();
923 }
924
925 public function tutor_quiz_builder_get_answers_by_question(){
926 global $wpdb;
927 $question_id = sanitize_text_field($_POST['question_id']);
928 $question_type = sanitize_text_field($_POST['question_type']);
929
930 if(!tutils()->can_user_manage('question', $question_id)) {
931 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
932 }
933
934 $question = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}tutor_quiz_questions WHERE question_id = %d ", $question_id));
935 $answers = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}tutor_quiz_question_answers where belongs_question_id = %d AND belongs_question_type = %s order by answer_order asc ;", $question_id, esc_sql( $question_type ) ));
936
937 ob_start();
938
939 switch ($question_type){
940 case 'true_false':
941 echo '<label>'.__('Answer options &amp; mark correct', 'tutor').'</label>';
942 break;
943 case 'ordering':
944 echo '<label>'.__('Make sure you’re saving the answers in the right order. Students will have to match this order exactly.', 'tutor').'</label>';
945 break;
946 }
947
948 if (is_array($answers) && count($answers)){
949 foreach ($answers as $answer){
950 ?>
951 <div class="tutor-quiz-answer-wrap" data-answer-id="<?php echo $answer->answer_id; ?>">
952 <div class="tutor-quiz-answer">
953 <span class="tutor-quiz-answer-title">
954 <?php
955 echo stripslashes($answer->answer_title);
956 if ($answer->belongs_question_type === 'fill_in_the_blank'){
957 echo ' ('.__('Answer', 'tutor').' : ';
958 echo '<strong>'. stripslashes($answer->answer_two_gap_match). '</strong>)';
959 }
960 if ($answer->belongs_question_type === 'matching'){
961 echo ' - '.stripslashes($answer->answer_two_gap_match);
962 }
963 ?>
964 </span>
965
966 <?php
967 if ($answer->image_id){
968 echo '<span class="tutor-question-answer-image"><img src="'.wp_get_attachment_image_url($answer->image_id).'" /> </span>';
969 }
970 if ($question_type === 'true_false' || $question_type === 'single_choice'){
971 ?>
972 <span class="tutor-quiz-answers-mark-correct-wrap">
973 <input type="radio" name="mark_as_correct[<?php echo $answer->belongs_question_id; ?>]" value="<?php echo $answer->answer_id; ?>" title="<?php _e('Mark as correct', 'tutor'); ?>" <?php checked(1, $answer->is_correct); ?> >
974 </span>
975 <?php
976 }elseif ($question_type === 'multiple_choice'){
977 ?>
978 <span class="tutor-quiz-answers-mark-correct-wrap">
979 <input type="checkbox" name="mark_as_correct[<?php echo $answer->belongs_question_id; ?>]" value="<?php echo $answer->answer_id; ?>" title="<?php _e('Mark as correct', 'tutor'); ?>" <?php checked(1, $answer->is_correct); ?> >
980 </span>
981 <?php
982 }
983 ?>
984 <span class="tutor-quiz-answer-edit">
985 <a href="javascript:;"><i class="tutor-icon-pencil"></i> </a>
986 </span>
987 <span class="tutor-quiz-answer-sort-icon"><i class="tutor-icon-menu-2"></i> </span>
988 </div>
989
990 <div class="tutor-quiz-answer-trash-wrap">
991 <a href="javascript:;" class="answer-trash-btn" data-answer-id="<?php echo $answer->answer_id; ?>"><i class="tutor-icon-garbage"></i> </a>
992 </div>
993 </div>
994 <?php
995 }
996 }
997 $output = ob_get_clean();
998
999 wp_send_json_success(array('output' => $output));
1000 }
1001
1002 public function tutor_quiz_builder_delete_answer(){
1003 global $wpdb;
1004 $answer_id = sanitize_text_field($_POST['answer_id']);
1005
1006 if(!tutils()->can_user_manage('quiz_answer', $answer_id)) {
1007 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
1008 }
1009
1010 $wpdb->delete($wpdb->prefix.'tutor_quiz_question_answers', array('answer_id' => esc_sql( $answer_id ) ));
1011 wp_send_json_success();
1012 }
1013
1014 /**
1015 * Save quiz questions sorting
1016 */
1017 public function tutor_quiz_question_sorting(){
1018 global $wpdb;
1019
1020 $question_ids = tutor_utils()->avalue_dot('sorted_question_ids', $_POST);
1021 if (is_array($question_ids) && count($question_ids) ){
1022 $i = 0;
1023 foreach ($question_ids as $key => $question_id){
1024 if(tutils()->can_user_manage('question', $question_id)) {
1025 $i++;
1026 $wpdb->update($wpdb->prefix.'tutor_quiz_questions', array('question_order' => $i), array('question_id' => $question_id));
1027 }
1028 }
1029 }
1030 }
1031
1032 /**
1033 * Save sorting data for quiz answers
1034 */
1035 public function tutor_quiz_answer_sorting(){
1036 global $wpdb;
1037
1038 if ( ! empty($_POST['sorted_answer_ids']) && is_array($_POST['sorted_answer_ids']) && count($_POST['sorted_answer_ids']) ){
1039 $answer_ids = $_POST['sorted_answer_ids'];
1040 $i = 0;
1041 foreach ($answer_ids as $key => $answer_id){
1042 if(tutils()->can_user_manage('quiz_answer', $answer_id)) {
1043 $i++;
1044 $wpdb->update($wpdb->prefix.'tutor_quiz_question_answers', array('answer_order' => $i), array('answer_id' => $answer_id));
1045 }
1046 }
1047 }
1048 }
1049
1050 /**
1051 * Mark answer as correct
1052 */
1053
1054 public function tutor_mark_answer_as_correct(){
1055 global $wpdb;
1056
1057 $answer_id = sanitize_text_field($_POST['answer_id']);
1058 $inputValue = sanitize_text_field($_POST['inputValue']);
1059
1060 if(!tutils()->can_user_manage('quiz_answer', $answer_id)) {
1061 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
1062 }
1063
1064 $answer = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE answer_id = %d LIMIT 0,1 ;", $answer_id));
1065 if ($answer->belongs_question_type === 'single_choice') {
1066 $wpdb->update($wpdb->prefix.'tutor_quiz_question_answers', array('is_correct' => 0), array('belongs_question_id' => esc_sql( $answer->belongs_question_id ) ));
1067 }
1068 $wpdb->update($wpdb->prefix.'tutor_quiz_question_answers', array('is_correct' => esc_sql( $inputValue ) ), array('answer_id' => esc_sql( $answer_id ) ));
1069 }
1070
1071 /**
1072 * Update quiz settings from modal
1073 *
1074 * @since : v.1.0.0
1075 */
1076 public function tutor_quiz_modal_update_settings(){
1077 $quiz_id = sanitize_text_field($_POST['quiz_id']);
1078 $quiz_option = tutor_utils()->sanitize_array($_POST['quiz_option']);
1079
1080 if(!tutils()->can_user_manage('quiz', $quiz_id)) {
1081 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
1082 }
1083
1084 update_post_meta($quiz_id, 'tutor_quiz_option', $quiz_option);
1085 do_action('tutor_quiz_settings_updated', $quiz_id);
1086 wp_send_json_success();
1087 }
1088
1089
1090 //=========================//
1091 // Front end stuffs
1092 //=========================//
1093
1094 /**
1095 * Rendering quiz for frontend
1096 *
1097 * @since v.1.0.0
1098 */
1099
1100 public function tutor_render_quiz_content(){
1101 $quiz_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('quiz_id', $_POST));
1102
1103 ob_start();
1104 global $post;
1105
1106 $post = get_post($quiz_id);
1107 setup_postdata($post);
1108 //tutor_lesson_content();
1109
1110 single_quiz_contents();
1111 wp_reset_postdata();
1112
1113 $html = ob_get_clean();
1114 wp_send_json_success(array('html' => $html));
1115 }
1116
1117 }