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.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 / Question.php
tutor / classes Last commit date
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 Dashboard.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 }