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 |