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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 / Q_and_A.php
tutor / classes Last commit date
Addons.php 3 years ago Admin.php 3 years ago Ajax.php 3 years ago Announcements.php 3 years ago Assets.php 3 years ago Backend_Page_Trait.php 3 years ago Course.php 3 years ago Course_Embed.php 3 years ago Course_Filter.php 3 years ago Course_List.php 3 years ago Course_Settings_Tabs.php 3 years ago Course_Widget.php 4 years ago Custom_Validation.php 4 years ago Dashboard.php 3 years ago FormHandler.php 4 years ago Frontend.php 3 years ago Gutenberg.php 3 years ago Input.php 3 years ago Instructor.php 4 years ago Instructors_List.php 3 years ago Lesson.php 3 years ago Options_V2.php 3 years ago Post_types.php 3 years ago Private_Course_Access.php 4 years ago Q_and_A.php 3 years ago Question_Answers_List.php 4 years ago Quiz.php 3 years ago Quiz_Attempts_List.php 3 years ago RestAPI.php 4 years ago Reviews.php 3 years ago Rewrite_Rules.php 4 years ago Shortcode.php 4 years ago Student.php 4 years ago Students_List.php 4 years ago Taxonomies.php 4 years ago Template.php 3 years ago Theme_Compatibility.php 5 years ago Tools.php 3 years ago Tools_V2.php 4 years ago Tutor.php 3 years ago TutorEDD.php 4 years ago Tutor_Base.php 5 years ago Tutor_List_Table.php 3 years ago Tutor_Setup.php 3 years ago Upgrader.php 4 years ago User.php 4 years ago Utils.php 3 years ago Video_Stream.php 4 years ago Withdraw.php 3 years ago Withdraw_Requests_List.php 3 years ago WooCommerce.php 3 years ago
Q_and_A.php
257 lines
1 <?php
2
3 namespace TUTOR;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 class Q_and_A {
10
11 public function __construct() {
12 add_action( 'wp_ajax_tutor_qna_create_update', array( $this, 'tutor_qna_create_update' ) );
13
14 /**
15 * Delete question
16 *
17 * @since v.1.6.4
18 */
19 add_action( 'wp_ajax_tutor_delete_dashboard_question', array( $this, 'tutor_delete_dashboard_question' ) );
20
21 /**
22 * Take action against single qna
23 *
24 * @since v2.0.0
25 */
26 add_action( 'wp_ajax_tutor_qna_single_action', array( $this, 'tutor_qna_single_action' ) );
27 add_action( 'wp_ajax_tutor_qna_bulk_action', array( $this, 'process_bulk_action' ) );
28 /**
29 * Q & A load more
30 *
31 * @since v2.0.6
32 */
33 add_action( 'wp_ajax_tutor_q_and_a_load_more', __CLASS__ . '::load_more' );
34 }
35
36 public function tutor_qna_create_update() {
37 tutor_utils()->checking_nonce();
38
39 global $wpdb;
40
41 $qna_text = wp_kses_post( $_POST['answer'] );
42 if ( ! $qna_text ) {
43 // Content validation
44 wp_send_json_error( array( 'message' => __( 'Empty Content Not Allowed!', 'tutor' ) ) );
45 }
46
47 // Prepare course, question info
48 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
49 $question_id = Input::post( 'question_id', 0, Input::TYPE_INT );
50 $context = Input::post( 'context' );
51
52 // Prepare user info
53 $user_id = get_current_user_id();
54 $user = get_userdata( $user_id );
55 $date = date( 'Y-m-d H:i:s', tutor_time() );
56
57 // Insert data prepare
58 $data = apply_filters(
59 'tutor_qna_insert_data',
60 array(
61 'comment_post_ID' => $course_id,
62 'comment_author' => $user->user_login,
63 'comment_date' => $date,
64 'comment_date_gmt' => get_gmt_from_date( $date ),
65 'comment_content' => $qna_text,
66 'comment_approved' => 'approved',
67 'comment_agent' => 'TutorLMSPlugin',
68 'comment_type' => 'tutor_q_and_a',
69 'comment_parent' => $question_id,
70 'user_id' => $user_id,
71 )
72 );
73
74 // Insert new question/answer.
75 $wpdb->insert( $wpdb->comments, $data );
76 ! $question_id ? $question_id = (int) $wpdb->insert_id : 0;
77
78 // Mark the question unseen if action made from student
79 $asker_id = $this->get_asker_id( $question_id );
80 $self = $asker_id == $user_id;
81 update_comment_meta( $question_id, 'tutor_qna_read' . ( $self ? '' : '_' . $asker_id ), 0 );
82
83 do_action( 'tutor_after_asked_question', $data );
84
85 // question_id != 0 means it's a reply
86 $reply_id = Input::post( 'question_id', 0, Input::TYPE_INT );
87 $answer_id = (int) $wpdb->insert_id;
88 if ( $reply_id != 0 && ( current_user_can( 'administrator' ) || tutor_utils()->is_instructor_of_this_course( $user_id, $course_id ) ) ) {
89 do_action( 'tutor_after_answer_to_question', $answer_id );
90 }
91
92 // Provide the html now.
93 ob_start();
94 tutor_load_template_from_custom_path(
95 tutor()->path . '/views/qna/qna-single.php',
96 array(
97 'question_id' => $question_id,
98 'back_url' => isset( $_POST['back_url'] ) ? esc_url( $_POST['back_url'] ) : '',
99 'context' => $context,
100 )
101 );
102 wp_send_json_success(
103 array(
104 'html' => ob_get_clean(),
105 'editor_id' => 'tutor_qna_reply_editor_' . $question_id,
106 )
107 );
108 }
109
110 /**
111 * Delete question [frontend dashboard]
112 *
113 * @since v.1.6.4
114 */
115 public function tutor_delete_dashboard_question() {
116 tutor_utils()->checking_nonce();
117
118 $question_id = intval( sanitize_text_field( $_POST['question_id'] ) );
119
120 if ( ! $question_id || ! tutor_utils()->can_user_manage( 'qa_question', $question_id ) ) {
121 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
122 }
123
124 $this->delete_qna_permanently( array( $question_id ) );
125
126 wp_send_json_success();
127 }
128
129 private function delete_qna_permanently( $question_ids ) {
130 if ( count( $question_ids ) ) {
131 global $wpdb;
132 $question_ids = implode( ',', $question_ids );
133
134 // Deleting question (comment), child question and question meta (comment meta)
135 $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE {$wpdb->comments}.comment_ID IN($question_ids)" );
136 $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE {$wpdb->comments}.comment_parent IN($question_ids)" );
137 $wpdb->query( "DELETE FROM {$wpdb->commentmeta} WHERE {$wpdb->commentmeta}.comment_id IN($question_ids)" );
138 }
139 }
140
141 function process_bulk_action() {
142 tutor_utils()->checking_nonce();
143
144 $user_id = get_current_user_id();
145 $action = isset( $_POST['bulk-action'] ) ? sanitize_text_field( $_POST['bulk-action'] ) : null;
146
147 switch ( $action ) {
148 case 'delete':
149 $qa_ids = sanitize_text_field( $_POST['bulk-ids'] );
150 $qa_ids = explode( ',', $qa_ids );
151 $qa_ids = array_filter(
152 $qa_ids,
153 function( $id ) use ( $user_id ) {
154 return is_numeric( $id ) && tutor_utils()->can_user_manage( 'qa_question', $id, $user_id );
155 }
156 );
157
158 $this->delete_qna_permanently( $qa_ids );
159 break;
160 }
161
162 wp_send_json_success();
163 }
164
165 private function get_asker_id( $question_id ) {
166 global $wpdb;
167 $author_id = $wpdb->get_var(
168 $wpdb->prepare(
169 "SELECT user_id
170 FROM {$wpdb->comments}
171 WHERE comment_ID=%d",
172 $question_id
173 )
174 );
175
176 return $author_id;
177 }
178
179 public function tutor_qna_single_action() {
180 tutor_utils()->checking_nonce();
181
182 $question_id = intval( sanitize_text_field( $_POST['question_id'] ) );
183
184 if ( ! tutor_utils()->can_user_manage( 'qa_question', $question_id ) ) {
185 wp_send_json_error( array( 'message' => __( 'Permission Denied!', 'tutor' ) ) );
186 }
187
188 // Get who asked the question
189 $asker_id = $this->get_asker_id( $question_id );
190 $asker_prefix = ( isset( $_POST['context'] ) && $_POST['context'] == 'frontend-dashboard-qna-table-student' ) ? '_' . get_current_user_id() : '';
191
192 // Get the existing value from meta
193 $action = sanitize_text_field( $_POST['qna_action'] );
194
195 // If current user asker, then make it unread for self
196 // If it is instructor, then make unread for instructor side
197 $meta_key = 'tutor_qna_' . $action . $asker_prefix;
198
199 $current_value = (int) get_comment_meta( $question_id, $meta_key, true );
200
201 $new_value = $current_value == 1 ? 0 : 1;
202
203 // Update the reverted value
204 update_comment_meta( $question_id, $meta_key, $new_value );
205
206 // Transfer the new status
207 wp_send_json_success( array( 'new_value' => $new_value ) );
208 }
209
210 /**
211 * Available tabs that will visible on the right side of page navbar
212 *
213 * @return array
214 * @since v2.0.0
215 */
216 public static function tabs_key_value( $asker_id = null ) {
217
218 $stats = array(
219 'all' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, null, true ),
220 'read' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, 'read', true ),
221 'unread' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, 'unread', true ),
222 'important' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, 'important', true ),
223 'archived' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, 'archived', true ),
224 );
225
226 // Assign value, url etc to the tab array
227 $tabs = array_map(
228 function( $tab ) use ( $stats ) {
229 return array(
230 'key' => $tab,
231 'title' => tutor_utils()->translate_dynamic_text( $tab ),
232 'value' => $stats[ $tab ],
233 'url' => add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) ),
234 );
235 },
236 array_keys( $stats )
237 );
238
239 return $tabs;
240 }
241
242 /**
243 * Load more q & a
244 *
245 * @since v2.0.6
246 *
247 * @return void
248 */
249 public static function load_more() {
250 tutor_utils()->checking_nonce();
251 ob_start();
252 tutor_load_template( 'single.course.enrolled.question_and_answer' );
253 $html = ob_get_clean();
254 wp_send_json_success( array( 'html' => $html ) );
255 }
256 }
257