Discussion


Source

File: classes/Discussion.php

class Discussion{

    public function __construct(){
        add_action('wp_ajax_dozent_discussion_ask_question', [$this, 'dozent_discussion_ask_question']);
        add_action('wp_ajax_dozent_discussion_reply_question', [$this, 'dozent_discussion_reply_question']);
    }

    public function dozent_discussion_ask_question(){

        dozent_checking_nonce();

        global $wpdb;

        $lecture_id      = (int) dozent_input_text('post_ID');
        $question_title = dozent_input_text('question_title');
        $question       = dozent_input_textarea('question_details');

        if ( empty( $question ) || empty( $question_title ) ){
            wp_send_json_error( __('Empty question title or details', 'dozent') );
        }

        $question = [
            'title' => $question_title,
            'question' => $question,
        ];

        $user_id = get_current_user_id();
        $user = get_userdata($user_id);
        $date = date("Y-m-d H:i:s", dozent_time());

        do_action('dozent_ask_question_before', $lecture_id);

        $data = apply_filters('dozent_ask_question_data', [
            'comment_post_ID'   => $lecture_id,
            'comment_author'    => $user->user_login,
            'comment_date'      => $date,
            'comment_date_gmt'  => get_gmt_from_date( $date ),
            'comment_content'   => wp_json_encode($question),
            'comment_approved'  => 'answer_pending',
            'comment_agent'     => 'DozentLMSPlugin',
            'comment_type'      => 'dozent_discussion',
            'user_id'           => $user_id,
        ]);

        $wpdb->insert($wpdb->comments, $data);
        $comment_id = (int) $wpdb->insert_id;

        do_action('dozent_ask_question_after', $lecture_id, $comment_id, $data );

        wp_send_json_success(__('Question has been added successfully', 'dozent'));
    }


    public function dozent_discussion_reply_question(){
        dozent_checking_nonce();

        global $wpdb;

        $lecture_id      = (int) dozent_input_text('post_ID');
        $parent_question_id      = (int) dozent_input_text('parent_question_id');
        $question       = dozent_input_textarea('message');

        if ( empty( $question ) ){
            wp_send_json_error( [ 'message' => __('Empty question details', 'dozent') ] );
        }

        $user_id = get_current_user_id();
        $user = get_userdata($user_id);
        $date = date("Y-m-d H:i:s", dozent_time());


        $parent = $wpdb->get_row( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = {$parent_question_id} " );

        if ( $parent->user_id != $user_id ){
            $wpdb->update( $wpdb->comments,
                ['comment_approved' => 'answered'],
                ['comment_ID' => $parent_question_id]
            );
        }

        do_action('dozent_reply_question_before', $lecture_id);

        $data = apply_filters('dozent_reply_question_data', [
            'comment_post_ID'   => $lecture_id,
            'comment_author'    => $user->user_login,
            'comment_date'      => $date,
            'comment_date_gmt'  => get_gmt_from_date( $date ),
            'comment_content'   => $question,
            'comment_approved'  => 'discussion_reply',
            'comment_agent'     => 'DozentLMSPlugin',
            'comment_type'      => 'dozent_discussion',
            'comment_parent'    => $parent_question_id,
            'user_id'           => $user_id,
        ]);

        $wpdb->insert($wpdb->comments, $data);
        $comment_id = (int) $wpdb->insert_id;

        do_action('dozent_reply_question_after', $lecture_id, $comment_id, $data );

        wp_send_json_success( ['message' => __('Your reply has been added successfully', 'dozent') ] );
    }


}

Methods