Discussion


Source

File: classes/Discussion.php

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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