PluginProbe
Tutor LMS – eLearning and online course solution / 1.0.7
Tutor LMS – eLearning and online course solution v1.0.7
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
tutor / classes / Ajax.php

Ajax.php in Tutor LMS – eLearning and online course solution 1.0.7, at classes/Ajax.php

253 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace TUTOR;
3
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 class Ajax{
8 public function __construct() {
9 add_action('wp_ajax_sync_video_playback', array($this, 'sync_video_playback'));
10 add_action('wp_ajax_nopriv_sync_video_playback', array($this, 'sync_video_playback_noprev'));
11 add_action('wp_ajax_tutor_place_rating', array($this, 'tutor_place_rating'));
12
13 add_action('wp_ajax_tutor_ask_question', array($this, 'tutor_ask_question'));
14 add_action('wp_ajax_tutor_add_answer', array($this, 'tutor_add_answer'));
15
16 add_action('wp_ajax_tutor_course_add_to_wishlist', array($this, 'tutor_course_add_to_wishlist'));
17 add_action('wp_ajax_nopriv_tutor_course_add_to_wishlist', array($this, 'tutor_course_add_to_wishlist'));
18
19 /**
20 * Addon Enable Disable Control
21 */
22 add_action('wp_ajax_addon_enable_disable', array($this, 'addon_enable_disable'));
23 }
24
25 /**
26 * Update video information and data when necessary
27 *
28 * @since v.1.0.0
29 */
30 public function sync_video_playback(){
31 tutor_utils()->checking_nonce();
32
33 $duration = sanitize_text_field($_POST['duration']);
34 $currentTime = sanitize_text_field($_POST['currentTime']);
35 $post_id = sanitize_text_field($_POST['post_id']);
36
37 /**
38 * Update posts attached video
39 */
40 $video = tutor_utils()->get_video($post_id);
41
42 if ($duration) {
43 $video['duration_sec'] = $duration; //secs
44 $video['playtime'] = tutor_utils()->playtime_string( $duration );
45 $video['runtime'] = tutor_utils()->playtime_array( $duration );
46 }
47 tutor_utils()->update_video($post_id, $video);
48
49 /**
50 * Sync Lesson Reading Info by Users
51 */
52
53 $user_id = get_current_user_id();
54
55 $best_watch_time = tutor_utils()->get_lesson_reading_info($post_id, $user_id, 'video_best_watched_time');
56 if ($best_watch_time < $currentTime){
57 tutor_utils()->update_lesson_reading_info($post_id, $user_id, 'video_best_watched_time', $currentTime);
58 }
59
60 if (tutor_utils()->avalue_dot('is_ended', $_POST)){
61 tutor_utils()->mark_lesson_complete($post_id);
62 }
63 exit();
64 }
65
66 public function sync_video_playback_noprev(){
67
68 }
69
70
71 public function tutor_place_rating(){
72 global $wpdb;
73
74 //TODO: Check nonce
75
76 $rating = sanitize_text_field(tutor_utils()->avalue_dot('rating', $_POST));
77 $course_id = sanitize_text_field(tutor_utils()->avalue_dot('course_id', $_POST));
78 $review = wp_kses_post(tutor_utils()->avalue_dot('review', $_POST));
79
80 $user_id = get_current_user_id();
81 $user = get_userdata($user_id);
82 $date = date("Y-m-d H:i:s");
83
84 do_action('tutor_before_rating_placed');
85
86 $previous_rating_id = $wpdb->get_var("select comment_ID from {$wpdb->comments} WHERE comment_post_ID={$course_id} AND user_id = {$user_id} AND comment_type = 'tutor_course_rating' LIMIT 1;");
87
88 $review_ID = $previous_rating_id;
89 if ( $previous_rating_id){
90 $wpdb->update( $wpdb->comments, array('comment_content' => $review),
91 array('comment_ID' => $previous_rating_id)
92 );
93 $wpdb->update( $wpdb->commentmeta, array('meta_value' => $rating),
94 array('comment_id' => $previous_rating_id, 'meta_key' => 'tutor_rating')
95 );
96 }else{
97 $data = array(
98 'comment_post_ID' => $course_id,
99 'comment_approved' => 'approved',
100 'comment_type' => 'tutor_course_rating',
101 'comment_date' => $date,
102 'comment_date_gmt' => get_gmt_from_date($date),
103 'user_id' => $user_id,
104 'comment_author' => $user->user_login,
105 'comment_agent' => 'TutorLMSPlugin',
106 );
107 if ($review){
108 $data['comment_content'] = $review;
109 }
110
111 $wpdb->insert($wpdb->comments, $data);
112 $comment_id = (int) $wpdb->insert_id;
113 $review_ID = $comment_id;
114
115 if ($comment_id && $rating){
116 $result = $wpdb->insert( $wpdb->commentmeta, array(
117 'comment_id' => $comment_id,
118 'meta_key' => 'tutor_rating',
119 'meta_value' => $rating
120 ) );
121
122 do_action('tutor_after_rating_placed', $comment_id);
123 }
124 }
125
126 $data = array('msg' => __('Rating placed success', 'tutor'), 'review_id' => $review_ID, 'review' => $review);
127 wp_send_json_success($data);
128 }
129
130 public function tutor_ask_question(){
131 tutor_utils()->checking_nonce();
132
133 global $wpdb;
134
135 $course_id = (int) sanitize_text_field($_POST['tutor_course_id']);
136 $question_title = sanitize_text_field($_POST['question_title']);
137 $question = wp_kses_post($_POST['question']);
138
139 if (empty($question) || empty($question_title)){
140 wp_send_json_error(__('Empty question title or body', 'tutor'));
141 }
142
143 $user_id = get_current_user_id();
144 $user = get_userdata($user_id);
145 $date = date("Y-m-d H:i:s");
146
147 do_action('tutor_before_add_question', $course_id);
148 $data = apply_filters('tutor_add_question_data', array(
149 'comment_post_ID' => $course_id,
150 'comment_author' => $user->user_login,
151 'comment_date' => $date,
152 'comment_date_gmt' => get_gmt_from_date($date),
153 'comment_content' => $question,
154 'comment_approved' => 'waiting_for_answer',
155 'comment_agent' => 'TutorLMSPlugin',
156 'comment_type' => 'tutor_q_and_a',
157 'user_id' => $user_id,
158 ));
159
160 $wpdb->insert($wpdb->comments, $data);
161 $comment_id = (int) $wpdb->insert_id;
162
163 if ($comment_id){
164 $result = $wpdb->insert( $wpdb->commentmeta, array(
165 'comment_id' => $comment_id,
166 'meta_key' => 'tutor_question_title',
167 'meta_value' => $question_title
168 ) );
169 }
170 do_action('tutor_after_add_question', $course_id, $comment_id);
171
172 wp_send_json_success(__('Question has been added successfully', 'tutor'));
173 }
174
175
176 public function tutor_add_answer(){
177 tutor_utils()->checking_nonce();
178 global $wpdb;
179
180 $answer = wp_kses_post($_POST['answer']);
181 if ( ! $answer){
182 wp_send_json_error(__('Please write answer', 'tutor'));
183 }
184
185 $question_id = (int) sanitize_text_field($_POST['question_id']);
186 $question = tutor_utils()->get_qa_question($question_id);
187
188 $user_id = get_current_user_id();
189 $user = get_userdata($user_id);
190 $date = date("Y-m-d H:i:s");
191
192 do_action('tutor_before_answer_to_question');
193 $data = apply_filters('tutor_add_answer_data', array(
194 'comment_post_ID' => $question->comment_post_ID,
195 'comment_author' => $user->user_login,
196 'comment_date' => $date,
197 'comment_date_gmt' => get_gmt_from_date($date),
198 'comment_content' => $answer,
199 'comment_approved' => 'approved',
200 'comment_agent' => 'TutorLMSPlugin',
201 'comment_type' => 'tutor_q_and_a',
202 'comment_parent' => $question_id,
203 'user_id' => $user_id,
204 ));
205
206 $wpdb->insert($wpdb->comments, $data);
207 $comment_id = (int) $wpdb->insert_id;
208 do_action('tutor_after_answer_to_question', $comment_id);
209
210 wp_send_json_success(__('Answer has been added successfully', 'tutor'));
211 }
212
213
214 public function tutor_course_add_to_wishlist(){
215 $course_id = (int) sanitize_text_field($_POST['course_id']);
216 if ( ! is_user_logged_in()){
217 wp_send_json_error(array('redirect_to' => wp_login_url( wp_get_referer() ) ) );
218 }
219 global $wpdb;
220
221 $user_id = get_current_user_id();
222 $if_added_to_list = $wpdb->get_row("select * from {$wpdb->usermeta} WHERE user_id = {$user_id} AND meta_key = '_tutor_course_wishlist' AND meta_value = {$course_id} ;");
223
224 if ( $if_added_to_list){
225 $wpdb->delete($wpdb->usermeta, array('user_id' => $user_id, 'meta_key' => '_tutor_course_wishlist', 'meta_value' => $course_id ));
226 wp_send_json_success(array('status' => 'removed', 'msg' => __('Course removed from wish list', 'tutor')));
227 }else{
228 update_user_meta($user_id, '_tutor_course_wishlist', $course_id);
229 wp_send_json_success(array('status' => 'added', 'msg' => __('Course added to wish list', 'tutor')));
230 }
231 }
232
233 /**
234 * Method for enable / disable addons
235 */
236 public function addon_enable_disable(){
237 $addonsConfig = maybe_unserialize(get_option('tutor_addons_config'));
238
239 $isEnable = (bool) sanitize_text_field(tutor_utils()->avalue_dot('isEnable', $_POST));
240 $addonFieldName = sanitize_text_field(tutor_utils()->avalue_dot('addonFieldName', $_POST));
241
242 if ($isEnable){
243 $addonsConfig[$addonFieldName]['is_enable'] = 1;
244 }else{
245 $addonsConfig[$addonFieldName]['is_enable'] = 0;
246 }
247
248 update_option('tutor_addons_config', $addonsConfig);
249
250 wp_send_json_success();
251 }
252
253 }