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