PluginProbe
Tutor LMS – eLearning and online course solution / 2.0.1
Tutor LMS – eLearning and online course solution v2.0.1
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 / Lesson.php

Lesson.php in Tutor LMS – eLearning and online course solution 2.0.1, at classes/Lesson.php

413 lines 12.1 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
8 class Lesson extends Tutor_Base {
9 public function __construct() {
10 parent::__construct();
11
12 add_action( 'add_meta_boxes', array( $this, 'register_meta_box' ) );
13 add_action( 'save_post_' . $this->lesson_post_type, array( $this, 'save_lesson_meta' ) );
14
15 add_action( 'wp_ajax_tutor_load_edit_lesson_modal', array( $this, 'tutor_load_edit_lesson_modal' ) );
16 add_action( 'wp_ajax_tutor_modal_create_or_update_lesson', array( $this, 'tutor_modal_create_or_update_lesson' ) );
17 add_action( 'wp_ajax_tutor_delete_lesson_by_id', array( $this, 'tutor_delete_lesson_by_id' ) );
18
19 add_filter( 'get_sample_permalink', array( $this, 'change_lesson_permalink' ), 10, 2 );
20 add_action( 'admin_init', array( $this, 'flush_rewrite_rules' ) );
21
22 /**
23 * Add Column
24 */
25
26 add_filter( "manage_{$this->lesson_post_type}_posts_columns", array( $this, 'add_column' ), 10, 1 );
27 add_action( "manage_{$this->lesson_post_type}_posts_custom_column", array( $this, 'custom_lesson_column' ), 10, 2 );
28
29 // Frontend Action
30 add_action( 'template_redirect', array( $this, 'mark_lesson_complete' ) );
31
32 add_action( 'wp_ajax_tutor_render_lesson_content', array( $this, 'tutor_render_lesson_content' ) );
33 add_action( 'wp_ajax_nopriv_tutor_render_lesson_content', array( $this, 'tutor_render_lesson_content' ) ); // For public course access
34
35 /**
36 * Autoplay next video
37 *
38 * @since v.1.4.9
39 */
40 add_action( 'wp_ajax_autoload_next_course_content', array( $this, 'autoload_next_course_content' ) );
41
42 /**
43 * Load next course item after click complete button
44 *
45 * @since v.1.5.3
46 */
47 add_action( 'tutor_lesson_completed_after', array( $this, 'tutor_lesson_completed_after' ), 999 );
48
49 add_action( 'wp_ajax_tutor_single_course_lesson_load_more', array($this, 'tutor_single_course_lesson_load_more') );
50 }
51
52 public function tutor_single_course_lesson_load_more() {
53 tutor_utils()->checking_nonce();
54
55 ob_start();
56 tutor_load_template('single.lesson.comment');
57 $html = ob_get_clean();
58
59 wp_send_json_success( array('html' => $html) );
60 }
61
62 /**
63 * Registering metabox
64 */
65 public function register_meta_box() {
66 $lesson_post_type = $this->lesson_post_type;
67
68 add_meta_box( 'tutor-course-select', __( 'Select Course', 'tutor' ), array( $this, 'lesson_metabox' ), $lesson_post_type );
69 add_meta_box( 'tutor-lesson-videos', __( 'Lesson Video', 'tutor' ), array( $this, 'lesson_video_metabox' ), $lesson_post_type );
70 add_meta_box( 'tutor-lesson-attachments', __( 'Attachments', 'tutor' ), array( $this, 'lesson_attachments_metabox' ), $lesson_post_type );
71 }
72
73 public function lesson_metabox() {
74 include tutor()->path . 'views/metabox/lesson-metabox.php';
75 }
76
77 public function lesson_video_metabox() {
78 include tutor()->path . 'views/metabox/video-metabox.php';
79 }
80
81 public function lesson_attachments_metabox() {
82 include tutor()->path . 'views/metabox/lesson-attachments-metabox.php';
83 }
84
85 /**
86 * @param $post_ID
87 *
88 * Saving lesson meta and assets
89 */
90 public function save_lesson_meta( $post_ID ) {
91 //Video
92 $video_source = sanitize_text_field( tutor_utils()->array_get('video.source', $_POST) );
93 if ( $video_source === '-1'){
94 delete_post_meta($post_ID, '_video');
95 }elseif($video_source) {
96 $video = (array)tutor_utils()->array_get('video', $_POST, array());
97 update_post_meta($post_ID, '_video', $video);
98 }
99
100 // Attachments
101 $attachments = array();
102 if ( ! empty($_POST['tutor_attachments'])){
103 $attachments = tutor_utils()->sanitize_array($_POST['tutor_attachments']);
104 $attachments = array_unique($attachments);
105 }
106
107 /**
108 * it !empty attachment then update meta else
109 * delete meta key to prevetn empty data in db
110 *
111 * @since 1.8.9
112 */
113 if ( ! empty( $attachments ) ) {
114 update_post_meta( $post_ID, '_tutor_attachments', $attachments );
115 } else {
116 delete_post_meta( $post_ID, '_tutor_attachments' );
117 }
118
119 }
120
121 public function tutor_load_edit_lesson_modal(){
122 tutor_utils()->checking_nonce();
123
124 $lesson_id = (int) tutor_utils()->avalue_dot( 'lesson_id', tutor_sanitize_data($_POST) );
125 $topic_id = (int) $_POST['topic_id'];
126
127 if(!tutor_utils()->can_user_manage('topic', $topic_id)) {
128 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
129 }
130
131 /**
132 * If Lesson Not Exists, provide dummy
133 */
134 $post_arr = array(
135 'ID' => 0,
136 'post_content' => '',
137 'post_type' => $this->lesson_post_type,
138 'post_title' => __( 'Draft Lesson', 'tutor' ),
139 'post_status' => 'publish',
140 'post_author' => get_current_user_id(),
141 'post_parent' => $topic_id,
142 );
143
144 $post = $lesson_id ? get_post($lesson_id) : (object)$post_arr;
145
146 ob_start();
147 include tutor()->path . 'views/modal/edit-lesson.php';
148 $output = ob_get_clean();
149
150 wp_send_json_success( array( 'output' => $output ) );
151 }
152
153 /**
154 * @since v.1.0.0
155 * @updated v.1.5.1
156 */
157 public function tutor_modal_create_or_update_lesson(){
158 tutor_utils()->checking_nonce();
159
160 global $wpdb;
161
162 $lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST));
163 $topic_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('current_topic_id', $_POST));
164 $current_topic_id = $topic_id;
165 $course_id = tutor_utils()->get_course_id_by('topic', $topic_id);
166 $_lesson_thumbnail_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('_lesson_thumbnail_id', $_POST));
167
168 if(!tutor_utils()->can_user_manage('topic', $topic_id)) {
169 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
170 }
171
172 $lesson_id = (int) sanitize_text_field( tutor_utils()->avalue_dot( 'lesson_id', $_POST ) );
173 $topic_id = (int) sanitize_text_field( tutor_utils()->avalue_dot( 'current_topic_id', $_POST ) );
174 $course_id = tutor_utils()->get_course_id_by( 'topic', $topic_id );
175 $_lesson_thumbnail_id = (int) tutor_utils()->avalue_dot( '_lesson_thumbnail_id', $_POST );
176
177 if ( ! tutils()->can_user_manage( 'topic', $topic_id ) ) {
178 wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) );
179 }
180
181 $title = sanitize_text_field( $_POST['lesson_title'] );
182 $lesson_content = wp_kses_post( $_POST['lesson_content'] );
183
184 $lesson_data = array(
185 'post_type' => $this->lesson_post_type,
186 'post_title' => $title,
187 'post_name' => sanitize_title($title),
188 'post_content' => $lesson_content,
189 'post_status' => 'publish',
190 'comment_status' => 'open',
191 'post_author' => get_current_user_id(),
192 'post_parent' => $topic_id
193 );
194
195 if ( $lesson_id == 0 ) {
196
197 $lesson_data['menu_order'] = tutor_utils()->get_next_course_content_order_id( $topic_id );
198 $lesson_id = wp_insert_post( $lesson_data );
199
200 if ( $lesson_id ) {
201 do_action( 'tutor/lesson/created', $lesson_id );
202 } else {
203 wp_send_json_error( array( 'message' => __( 'Couldn\'t create lesson.', 'tutor' ) ) );
204 }
205 } else {
206 $lesson_data['ID'] = $lesson_id;
207
208 do_action( 'tutor/lesson_update/before', $lesson_id );
209 wp_update_post( $lesson_data );
210 if ( $_lesson_thumbnail_id ) {
211 update_post_meta( $lesson_id, '_thumbnail_id', $_lesson_thumbnail_id );
212 } else {
213 delete_post_meta( $lesson_id, '_thumbnail_id' );
214 }
215 do_action( 'tutor/lesson_update/after', $lesson_id );
216 }
217
218 ob_start();
219 include tutor()->path . 'views/metabox/course-contents.php';
220 $course_contents = ob_get_clean();
221
222 wp_send_json_success( array( 'course_contents' => $course_contents ) );
223 }
224
225 /**
226 * Delete Lesson from course builder
227 */
228 public function tutor_delete_lesson_by_id(){
229 tutor_utils()->checking_nonce();
230
231 $lesson_id = (int) tutor_utils()->avalue_dot( 'lesson_id', $_POST );
232
233 if(!tutor_utils()->can_user_manage('lesson', $lesson_id)) {
234 wp_send_json_error( array('message'=>__('Access Denied', 'tutor')) );
235 }
236
237 wp_delete_post( $lesson_id, true );
238 wp_send_json_success();
239 }
240
241
242 /**
243 * @param $uri
244 * @param $lesson_id
245 *
246 * @return mixed
247 *
248 * Changed the URI based
249 */
250
251 public function change_lesson_permalink( $uri, $lesson_id ) {
252 $post = get_post( $lesson_id );
253
254 if ( $post && $post->post_type === $this->lesson_post_type ) {
255 $uri_base = trailingslashit( site_url() );
256
257 $sample_course = 'sample-course';
258 $is_course = tutor_utils()->get_course_id_by( 'lesson', get_the_ID() );
259 if ( $is_course ) {
260 $course = get_post( $is_course );
261 if ( $course ) {
262 $sample_course = $course->post_name;
263 }
264 }
265
266 $new_course_base = $uri_base . "course/{$sample_course}/lesson/%pagename%/";
267 $uri[0] = $new_course_base;
268 }
269
270 return $uri;
271 }
272
273
274 public function flush_rewrite_rules() {
275 $is_required_flush = get_option( 'required_rewrite_flush' );
276 if ( $is_required_flush ) {
277 flush_rewrite_rules();
278 delete_option( 'required_rewrite_flush' );
279 }
280 }
281
282
283 public function add_column( $columns ) {
284 $date_col = $columns['date'];
285 unset( $columns['date'] );
286 $columns['course'] = __( 'Course', 'tutor' );
287 $columns['date'] = $date_col;
288
289 return $columns;
290 }
291
292 /**
293 * @param $column
294 * @param $post_id
295 */
296 public function custom_lesson_column( $column, $post_id ) {
297 if ( $column === 'course' ) {
298
299 $course_id = tutor_utils()->get_course_id_by( 'lesson', $post_id );
300 if ( $course_id ) {
301 echo '<a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '">' . get_the_title( $course_id ) . '</a>';
302 }
303 }
304 }
305
306 /**
307 *
308 * Mark lesson completed
309 *
310 * @since v.1.0.0
311 */
312 public function mark_lesson_complete() {
313 if ( ! isset( $_POST['tutor_action'] ) || $_POST['tutor_action'] !== 'tutor_complete_lesson' ) {
314 return;
315 }
316 // Checking nonce
317 tutor_utils()->checking_nonce();
318
319 $user_id = get_current_user_id();
320
321 // TODO: need to show view if not signed_in
322 if ( ! $user_id ) {
323 die( __( 'Please Sign-In', 'tutor' ) );
324 }
325
326 $lesson_id = (int) $_POST['lesson_id'];
327
328 do_action( 'tutor_lesson_completed_before', $lesson_id );
329 /**
330 * Marking lesson at user meta, meta format, _tutor_completed_lesson_id_{id} and value = tutor_time();
331 */
332 tutor_utils()->mark_lesson_complete( $lesson_id );
333
334 do_action( 'tutor_lesson_completed_email_after', $lesson_id, $user_id );
335 do_action( 'tutor_lesson_completed_after', $lesson_id, $user_id );
336 }
337
338 /**
339 * Render the lesson content
340 */
341 public function tutor_render_lesson_content(){
342 tutor_utils()->checking_nonce();
343
344 $lesson_id = (int) tutor_utils()->avalue_dot( 'lesson_id', $_POST );
345
346 $ancestors = get_post_ancestors($lesson_id);
347 $course_id = !empty($ancestors) ? array_pop($ancestors): $lesson_id;
348
349 // Course must be public or current user must be enrolled to access this lesson
350 if(get_post_meta($course_id, '_tutor_is_public_course', true)!=='yes' && !tutor_utils()->is_enrolled($course_id)){
351
352 $is_admin = tutor_utils()->has_user_role('administrator');
353 $allowed = $is_admin ? true : tutor_utils()->is_instructor_of_this_course(get_current_user_id(), $course_id);
354
355 if ( ! $allowed ) {
356 http_response_code( 400 );
357 exit;
358 }
359 }
360
361 ob_start();
362 global $post;
363
364 $post = get_post( $lesson_id );
365 setup_postdata( $post );
366 tutor_lesson_content();
367 wp_reset_postdata();
368
369 $html = ob_get_clean();
370
371 wp_send_json_success(array('html' => $html));
372 }
373
374 /**
375 * Load next course item automatically
376 *
377 * @since v.1.4.9
378 */
379 public function autoload_next_course_content() {
380 tutor_utils()->checking_nonce();
381
382 $post_id = sanitize_text_field($_POST['post_id']);
383 $content_id = tutor_utils()->get_post_id($post_id);
384 $contents = tutor_utils()->get_course_prev_next_contents_by_id($content_id);
385
386 $autoload_course_content = (bool) get_tutor_option( 'autoload_next_course_content' );
387 $next_url = false;
388 if ( $autoload_course_content ) {
389 $next_url = get_the_permalink( $contents->next_id );
390 }
391 wp_send_json_success( array( 'next_url' => $next_url ) );
392 }
393
394 /**
395 * Load next course item after click complete button
396 *
397 * @since v.1.5.3
398 */
399 public function tutor_lesson_completed_after( $content_id ) {
400 $contents = tutor_utils()->get_course_prev_next_contents_by_id( $content_id );
401 $autoload_course_content = (bool) get_tutor_option( 'autoload_next_course_content' );
402 if ( $autoload_course_content ) {
403 wp_redirect( get_the_permalink( $contents->next_id ) );
404 } else {
405 wp_redirect( get_the_permalink( $content_id ) );
406 }
407 die();
408 }
409
410 }
411
412
413