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