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