Addons.php
6 years ago
Admin.php
6 years ago
Ajax.php
6 years ago
Assets.php
6 years ago
Course.php
6 years ago
Course_Widget.php
6 years ago
Gutenberg.php
6 years ago
Instructor.php
6 years ago
Instructors_List.php
6 years ago
Lesson.php
6 years ago
Options.php
6 years ago
Post_types.php
6 years ago
Q_and_A.php
6 years ago
Question.php
6 years ago
Question_Answers_List.php
6 years ago
Quiz.php
6 years ago
Quiz_Attempts_List.php
6 years ago
Rewrite_Rules.php
6 years ago
Shortcode.php
6 years ago
Student.php
6 years ago
Students_List.php
6 years ago
Taxonomies.php
6 years ago
Template.php
6 years ago
Theme_Compatibility.php
6 years ago
Tools.php
6 years ago
Tutor.php
6 years ago
TutorEDD.php
6 years ago
Tutor_Base.php
6 years ago
Tutor_List_Table.php
6 years ago
Upgrader.php
6 years ago
User.php
6 years ago
Utils.php
6 years ago
Video_Stream.php
6 years ago
Withdraw.php
6 years ago
Withdraw_Requests_List.php
6 years ago
WooCommerce.php
6 years ago
Question.php
304 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace TUTOR; |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) |
| 7 | exit; |
| 8 | |
| 9 | |
| 10 | class Question { |
| 11 | |
| 12 | public function __construct() { |
| 13 | add_action( 'add_meta_boxes', array($this, 'register_meta_box') ); |
| 14 | //save question type during first add question |
| 15 | add_action('save_post_tutor_question', array($this, 'save_question_type'), 10, 1); |
| 16 | |
| 17 | add_action('wp_ajax_quiz_page_add_new_question', array($this, 'quiz_page_add_new_question')); |
| 18 | add_action('wp_ajax_update_tutor_question', array($this, 'update_tutor_question')); |
| 19 | add_action('wp_ajax_quiz_add_answer_to_question', array($this, 'quiz_add_answer_to_question')); |
| 20 | add_action('wp_ajax_quiz_delete_answer_option', array($this, 'quiz_delete_answer_option')); |
| 21 | add_action('wp_ajax_quiz_question_type_changed', array($this, 'quiz_question_type_changed')); |
| 22 | add_action('wp_ajax_quiz_question_delete', array($this, 'quiz_question_delete')); |
| 23 | add_action('wp_ajax_sorting_quiz_questions', array($this, 'sorting_quiz_questions')); |
| 24 | |
| 25 | add_filter( "manage_tutor_question_posts_columns", array($this, 'add_column'), 10,1 ); |
| 26 | add_action( "manage_tutor_question_posts_custom_column" , array($this, 'custom_question_column'), 10, 2 ); |
| 27 | } |
| 28 | |
| 29 | public function register_meta_box(){ |
| 30 | add_meta_box( 'tutor-question', __( 'Question', 'tutor' ), array($this, 'quiz_question'), 'tutor_question' ); |
| 31 | } |
| 32 | |
| 33 | public function save_question_type($post_ID){ |
| 34 | $question_type = get_post_meta($post_ID, '_question_type', true); |
| 35 | if ( ! $question_type){ |
| 36 | update_post_meta($post_ID, '_question_type', 'true_false'); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public function quiz_question(){ |
| 41 | global $post; |
| 42 | $question = $post; |
| 43 | |
| 44 | $is_question_edit_page = true; |
| 45 | |
| 46 | include tutor()->path."views/metabox/quiz/single-question-item.php"; |
| 47 | } |
| 48 | |
| 49 | public function quiz_questions(){ |
| 50 | include tutor()->path.'views/metabox/quiz_questions.php'; |
| 51 | } |
| 52 | |
| 53 | public function quiz_page_add_new_question(){ |
| 54 | global $wpdb; |
| 55 | |
| 56 | $question_title = sanitize_text_field($_POST['question_title']); |
| 57 | $question_type = sanitize_text_field($_POST['question_type']); |
| 58 | $quiz_id = (int) sanitize_text_field($_POST['quiz_id']); |
| 59 | |
| 60 | $question_html = ''; |
| 61 | |
| 62 | $next_question_order = tutor_utils()->quiz_next_question_order_id($quiz_id); |
| 63 | |
| 64 | $post_arr = array( |
| 65 | 'post_type' => 'tutor_question', |
| 66 | 'post_title' => $question_title, |
| 67 | 'post_status' => 'publish', |
| 68 | 'post_author' => get_current_user_id(), |
| 69 | 'post_parent' => $quiz_id, |
| 70 | 'menu_order' => $next_question_order, |
| 71 | ); |
| 72 | $question_id = wp_insert_post( $post_arr ); |
| 73 | |
| 74 | if ($question_id){ |
| 75 | update_post_meta($question_id,'_question_type', $question_type); |
| 76 | |
| 77 | /** |
| 78 | * Insert True/False |
| 79 | */ |
| 80 | if ($question_type === 'true_false') { |
| 81 | $answer_option = array( |
| 82 | 'answer_option_text' => __( 'True', 'tutor' ), |
| 83 | 'is_correct' => '1', |
| 84 | ); |
| 85 | $data = apply_filters( 'tutor_quiz_adding_answer_option_to_question', array( |
| 86 | 'comment_post_ID' => $question_id, |
| 87 | 'comment_content' => json_encode( $answer_option ), |
| 88 | 'comment_approved' => 'approved', |
| 89 | 'comment_agent' => 'TutorLMSPlugin', |
| 90 | 'comment_type' => 'quiz_answer_option', |
| 91 | ) ); |
| 92 | $wpdb->insert( $wpdb->comments, $data ); |
| 93 | |
| 94 | $answer_option = array( |
| 95 | 'answer_option_text' => __( 'False', 'tutor' ), |
| 96 | 'is_correct' => '0', |
| 97 | ); |
| 98 | $data = apply_filters( 'tutor_quiz_adding_answer_option_to_question', array( |
| 99 | 'comment_post_ID' => $question_id, |
| 100 | 'comment_content' => json_encode( $answer_option ), |
| 101 | 'comment_approved' => 'approved', |
| 102 | 'comment_agent' => 'TutorLMSPlugin', |
| 103 | 'comment_type' => 'quiz_answer_option', |
| 104 | ) ); |
| 105 | $wpdb->insert( $wpdb->comments, $data ); |
| 106 | } |
| 107 | |
| 108 | ob_start(); |
| 109 | $question = get_post($question_id); |
| 110 | include tutor()->path."views/metabox/quiz/single-question-item.php"; |
| 111 | $question_html = ob_get_clean(); |
| 112 | } |
| 113 | |
| 114 | wp_send_json_success(array('question_html' => $question_html)); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | public function update_tutor_question(){ |
| 119 | global $wpdb; |
| 120 | $questions = $_POST['tutor_question']; |
| 121 | |
| 122 | if ( ! is_array($questions) || ! count($questions)){ |
| 123 | wp_send_json_error(); |
| 124 | } |
| 125 | |
| 126 | //die(print_r($_POST['tutor_question'])); |
| 127 | |
| 128 | foreach ($questions as $question_ID => $question_data){ |
| 129 | $title = sanitize_text_field(tutor_utils()->avalue_dot('question_title', $question_data)); |
| 130 | $description = wp_kses_post(tutor_utils()->avalue_dot('question_description', $question_data)); |
| 131 | |
| 132 | $type = sanitize_text_field(tutor_utils()->avalue_dot('question_type', $question_data)); |
| 133 | $mark = sanitize_text_field(tutor_utils()->avalue_dot('question_mark', $question_data)); |
| 134 | $hints = sanitize_text_field(tutor_utils()->avalue_dot('question_hints', $question_data)); |
| 135 | |
| 136 | $post_arr = array( |
| 137 | 'ID' => $question_ID, |
| 138 | 'post_title' => $title, |
| 139 | 'post_content' => $description, |
| 140 | ); |
| 141 | wp_update_post($post_arr); |
| 142 | |
| 143 | update_post_meta($question_ID, '_question_hints', $hints); |
| 144 | update_post_meta($question_ID, '_question_mark', $mark); |
| 145 | update_post_meta($question_ID, '_question_type', $type); |
| 146 | |
| 147 | /** |
| 148 | * Answer Option |
| 149 | */ |
| 150 | if ($type === 'true_false'){ |
| 151 | //If true/false, reset answer |
| 152 | $previous_answers = tutor_utils()->get_quiz_answer_options_by_question($question_ID); |
| 153 | |
| 154 | if ($previous_answers){ |
| 155 | foreach ($previous_answers as $previous_answer){ |
| 156 | $answer_content = json_decode($previous_answer->comment_content, true); |
| 157 | $answer_content['is_correct'] = '0'; |
| 158 | $wpdb->update($wpdb->comments, array('comment_content' => json_encode($answer_content)), array('comment_ID' => $previous_answer->comment_ID) ); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $answer_options = tutor_utils()->avalue_dot('answer_option', $question_data); |
| 164 | $answer_corrects = tutor_utils()->avalue_dot('answer_option_is_correct', $question_data); |
| 165 | |
| 166 | if (is_array($answer_options) && count($answer_options)){ |
| 167 | foreach ($answer_options as $answer_option_ID => $answer_option){ |
| 168 | $is_correct = '0'; |
| 169 | |
| 170 | if ($type === 'multiple_choice'){ |
| 171 | $is_correct = isset($answer_corrects[$answer_option_ID]) && $answer_corrects[$answer_option_ID] == '1' ? '1' : '0'; |
| 172 | }elseif ($type === 'single_choice' || $type === 'true_false'){ |
| 173 | $correct_answer_id = sanitize_text_field(tutor_utils()->avalue_dot('answer_option_is_correct', $question_data)); |
| 174 | $is_correct = $correct_answer_id == $answer_option_ID ? '1' : '0'; |
| 175 | } |
| 176 | |
| 177 | $update_data = array( |
| 178 | 'answer_option_text' => $answer_option, |
| 179 | 'is_correct' => $is_correct, |
| 180 | ); |
| 181 | $wpdb->update($wpdb->comments, array('comment_content' => json_encode($update_data)), array('comment_ID' =>$answer_option_ID ) ); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | wp_send_json_success(); |
| 187 | } |
| 188 | |
| 189 | public function quiz_add_answer_to_question(){ |
| 190 | global $wpdb; |
| 191 | |
| 192 | $question_id = (int) sanitize_text_field($_POST['question_id']); |
| 193 | $question_type = get_post_meta($question_id, '_question_type', true); |
| 194 | |
| 195 | $answer_option = array( |
| 196 | 'answer_option_text' => __('New answer option', 'tutor'), |
| 197 | 'is_correct' => '0', |
| 198 | ); |
| 199 | |
| 200 | if ($question_type === 'true_false'){ |
| 201 | $answer_option['answer_option_text'] = __('True/False', 'tutor'); |
| 202 | } |
| 203 | |
| 204 | $data = apply_filters('tutor_quiz_adding_answer_option_to_question', array( |
| 205 | 'comment_post_ID' => $question_id, |
| 206 | 'comment_content' => json_encode($answer_option), |
| 207 | 'comment_approved' => 'approved', |
| 208 | 'comment_agent' => 'TutorLMSPlugin', |
| 209 | 'comment_type' => 'quiz_answer_option', |
| 210 | )); |
| 211 | |
| 212 | $wpdb->insert($wpdb->comments, $data); |
| 213 | $answer_option_id = (int) $wpdb->insert_id; |
| 214 | |
| 215 | $quiz_answer_option = (object) array_merge(array('comment_ID' => $answer_option_id), $data ); |
| 216 | |
| 217 | ob_start(); |
| 218 | include tutor()->path."views/metabox/quiz/individual-answer-option-{$question_type}-tr.php"; |
| 219 | $answer_option_tr = ob_get_clean(); |
| 220 | |
| 221 | wp_send_json_success(array('data_tr' => $answer_option_tr)); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | public function quiz_delete_answer_option(){ |
| 226 | global $wpdb; |
| 227 | $answer_option_id = (int) sanitize_text_field($_POST['answer_option_id']); |
| 228 | $wpdb->delete($wpdb->comments, array('comment_ID' => $answer_option_id)); |
| 229 | wp_send_json_success(); |
| 230 | } |
| 231 | |
| 232 | public function quiz_question_type_changed(){ |
| 233 | global $wpdb; |
| 234 | |
| 235 | $question_id = (int) sanitize_text_field($_POST['question_id']); |
| 236 | $question_type = sanitize_text_field($_POST['question_type']); |
| 237 | |
| 238 | $question = get_post($question_id); |
| 239 | |
| 240 | /** |
| 241 | * If we found true false type, we will keep only 2 answer options |
| 242 | */ |
| 243 | |
| 244 | if ($question_type === 'true_false'){ |
| 245 | $quiz_answer_options = tutor_utils()->get_quiz_answer_options_by_question($question->ID); |
| 246 | $quiz_answer_options = array_slice($quiz_answer_options, 0, 2); |
| 247 | |
| 248 | $keep_answer_ids = wp_list_pluck($quiz_answer_options, 'comment_ID'); |
| 249 | $keep_answer_ids = implode( ',', array_map( 'absint', $keep_answer_ids ) ); |
| 250 | $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_post_ID = {$question_id} AND comment_type = 'quiz_answer_option' AND comment_ID NOT IN($keep_answer_ids)" ); |
| 251 | } |
| 252 | |
| 253 | ob_start(); |
| 254 | include tutor()->path."views/metabox/quiz/multi-answer-options.php"; |
| 255 | $answer_options = ob_get_clean(); |
| 256 | |
| 257 | wp_send_json_success(array('multi_answer_options' =>$answer_options )); |
| 258 | } |
| 259 | |
| 260 | public function quiz_question_delete(){ |
| 261 | global $wpdb; |
| 262 | |
| 263 | $question_id = (int) sanitize_text_field($_POST['question_id']); |
| 264 | wp_delete_post($question_id, true); |
| 265 | |
| 266 | wp_send_json_success(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Sorting Order |
| 271 | */ |
| 272 | |
| 273 | public function sorting_quiz_questions(){ |
| 274 | global $wpdb; |
| 275 | $questions = tutor_utils()->avalue_dot('questions', $_POST); |
| 276 | $question_ids = wp_list_pluck($questions, 'question_id'); |
| 277 | |
| 278 | $i = 1; |
| 279 | foreach ($question_ids as $question_id){ |
| 280 | $wpdb->update($wpdb->posts, array('menu_order' => $i), array('ID'=> $question_id) ); |
| 281 | $i++; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | public function add_column($columns){ |
| 286 | $date_col = $columns['date']; |
| 287 | unset($columns['date']); |
| 288 | $columns['quiz'] = __('Quiz', 'tutor'); |
| 289 | $columns['date'] = $date_col; |
| 290 | |
| 291 | return $columns; |
| 292 | } |
| 293 | |
| 294 | public function custom_question_column($column, $post_id ){ |
| 295 | if ($column === 'quiz'){ |
| 296 | $quiz_id = tutor_utils()->get_quiz_id_by_question($post_id); |
| 297 | |
| 298 | if ($quiz_id){ |
| 299 | echo '<a href="'.admin_url('post.php?post='.$quiz_id.'&action=edit').'">'.get_the_title($quiz_id).'</a>'; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | } |