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.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 / Q_and_A.php
tutor / classes Last commit date
Addons.php 4 years ago Admin.php 4 years ago Ajax.php 3 years ago Announcements.php 4 years ago Assets.php 3 years ago Backend_Page_Trait.php 4 years ago Course.php 3 years ago Course_Embed.php 3 years ago Course_Filter.php 4 years ago Course_List.php 3 years ago Course_Settings_Tabs.php 4 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 4 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 4 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 4 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 4 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 4 years ago WooCommerce.php 3 years ago
Q_and_A.php
246 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 = (int) sanitize_text_field( $_POST['course_id'] );
49 $question_id = (int) sanitize_text_field( $_POST['question_id'] );
50 $context = sanitize_text_field( $_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 // Provide the html now.
86 ob_start();
87 tutor_load_template_from_custom_path(
88 tutor()->path . '/views/qna/qna-single.php',
89 array(
90 'question_id' => $question_id,
91 'back_url' => isset( $_POST['back_url'] ) ? esc_url( $_POST['back_url'] ) : '',
92 'context' => $context,
93 )
94 );
95
96 wp_send_json_success( array( 'html' => ob_get_clean() ) );
97 }
98
99 /**
100 * Delete question [frontend dashboard]
101 *
102 * @since v.1.6.4
103 */
104 public function tutor_delete_dashboard_question() {
105 tutor_utils()->checking_nonce();
106
107 $question_id = intval( sanitize_text_field( $_POST['question_id'] ) );
108
109 if ( ! $question_id || ! tutor_utils()->can_user_manage( 'qa_question', $question_id ) ) {
110 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
111 }
112
113 $this->delete_qna_permanently( array( $question_id ) );
114
115 wp_send_json_success();
116 }
117
118 private function delete_qna_permanently( $question_ids ) {
119 if ( count( $question_ids ) ) {
120 global $wpdb;
121 $question_ids = implode( ',', $question_ids );
122
123 // Deleting question (comment), child question and question meta (comment meta)
124 $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE {$wpdb->comments}.comment_ID IN($question_ids)" );
125 $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE {$wpdb->comments}.comment_parent IN($question_ids)" );
126 $wpdb->query( "DELETE FROM {$wpdb->commentmeta} WHERE {$wpdb->commentmeta}.comment_id IN($question_ids)" );
127 }
128 }
129
130 function process_bulk_action() {
131 tutor_utils()->checking_nonce();
132
133 $user_id = get_current_user_id();
134 $action = isset( $_POST['bulk-action'] ) ? sanitize_text_field( $_POST['bulk-action'] ) : null;
135
136 switch ( $action ) {
137 case 'delete':
138 $qa_ids = sanitize_text_field( $_POST['bulk-ids'] );
139 $qa_ids = explode( ',', $qa_ids );
140 $qa_ids = array_filter(
141 $qa_ids,
142 function( $id ) use ( $user_id ) {
143 return is_numeric( $id ) && tutor_utils()->can_user_manage( 'qa_question', $id, $user_id );
144 }
145 );
146
147 $this->delete_qna_permanently( $qa_ids );
148 break;
149 }
150
151 wp_send_json_success();
152 }
153
154 private function get_asker_id( $question_id ) {
155 global $wpdb;
156 $author_id = $wpdb->get_var(
157 $wpdb->prepare(
158 "SELECT user_id
159 FROM {$wpdb->comments}
160 WHERE comment_ID=%d",
161 $question_id
162 )
163 );
164
165 return $author_id;
166 }
167
168 public function tutor_qna_single_action() {
169 tutor_utils()->checking_nonce();
170
171 $question_id = intval( sanitize_text_field( $_POST['question_id'] ) );
172
173 if ( ! tutor_utils()->can_user_manage( 'qa_question', $question_id ) ) {
174 wp_send_json_error( array( 'message' => __( 'Permission Denied!', 'tutor' ) ) );
175 }
176
177 // Get who asked the question
178 $asker_id = $this->get_asker_id( $question_id );
179 $asker_prefix = ( isset( $_POST['context'] ) && $_POST['context'] == 'frontend-dashboard-qna-table-student' ) ? '_' . get_current_user_id() : '';
180
181 // Get the existing value from meta
182 $action = sanitize_text_field( $_POST['qna_action'] );
183
184 // If current user asker, then make it unread for self
185 // If it is instructor, then make unread for instructor side
186 $meta_key = 'tutor_qna_' . $action . $asker_prefix;
187
188 $current_value = (int) get_comment_meta( $question_id, $meta_key, true );
189
190 $new_value = $current_value == 1 ? 0 : 1;
191
192 // Update the reverted value
193 update_comment_meta( $question_id, $meta_key, $new_value );
194
195 // Transfer the new status
196 wp_send_json_success( array( 'new_value' => $new_value ) );
197 }
198
199 /**
200 * Available tabs that will visible on the right side of page navbar
201 *
202 * @return array
203 * @since v2.0.0
204 */
205 public static function tabs_key_value( $asker_id = null ) {
206
207 $stats = array(
208 'all' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, null, true ),
209 'read' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, 'read', true ),
210 'unread' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, 'unread', true ),
211 'important' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, 'important', true ),
212 'archived' => tutor_utils()->get_qa_questions( 0, 99999, '', null, null, $asker_id, 'archived', true ),
213 );
214
215 // Assign value, url etc to the tab array
216 $tabs = array_map(
217 function( $tab ) use ( $stats ) {
218 return array(
219 'key' => $tab,
220 'title' => tutor_utils()->translate_dynamic_text( $tab ),
221 'value' => $stats[ $tab ],
222 'url' => add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) ),
223 );
224 },
225 array_keys( $stats )
226 );
227
228 return $tabs;
229 }
230
231 /**
232 * Load more q & a
233 *
234 * @since v2.0.6
235 *
236 * @return void
237 */
238 public static function load_more() {
239 tutor_utils()->checking_nonce();
240 ob_start();
241 tutor_load_template( 'single.course.enrolled.question_and_answer' );
242 $html = ob_get_clean();
243 wp_send_json_success( array('html' => $html) );
244 }
245 }
246