PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / admin / class-lp-install-sample-data.php

class-lp-install-sample-data.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.9, at inc/admin/class-lp-install-sample-data.php

728 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use LearnPress\Models\CourseModel;
4 use LearnPress\Models\CoursePostModel;
5
6 /**
7 * Class LP_Install_Sample_Data
8 *
9 * Create sample course for testing purpose.
10 * This will create sections, items, questions, answers, ...
11 *
12 * @since 3.0.0
13 */
14 class LP_Install_Sample_Data {
15
16 /**
17 * @var array
18 */
19 public static $section_range = array( 5, 10 );
20
21 /**
22 * @var array
23 */
24 public static $item_range = array( 10, 15 );
25
26 /**
27 * @var array
28 */
29 public static $question_range = array( 10, 15 );
30
31 /**
32 * @var array
33 */
34 public static $answer_range = array( 3, 5 );
35
36 /**
37 * @var int
38 */
39 public static $max_content_paragraph = 5;
40
41 /**
42 * @var string
43 */
44 protected $dummy_text = '';
45
46 /**
47 * LP_Install_Sample_Data constructor.
48 */
49 public function __construct() {
50 add_filter( 'learn-press/script-data', array( $this, 'i18n' ), 10, 2 );
51
52 $actions = array(
53 'lp-install-sample-data',
54 'lp-uninstall-sample-data',
55 );
56
57 if ( ! in_array( LP_Request::get_param( 'page' ), $actions ) ) {
58 return;
59 }
60
61 add_action( 'init', array( $this, 'install' ) );
62 add_action( 'init', array( $this, 'uninstall' ) );
63 }
64
65 public function i18n( $data, $handle ) {
66 if ( 'learn-press-global' !== $handle ) {
67 return $data;
68 }
69
70 $i18n = array(
71 'confirm_install_sample_data' => esc_html__( 'Are you sure you want to install the sample course data?', 'learnpress' ),
72 'confirm_uninstall_sample_data' => esc_html__( 'Are you sure you want to delete the sample course data?', 'learnpress' ),
73 );
74
75 if ( empty( $data['i18n'] ) ) {
76 $data['i18n'] = $i18n;
77 } else {
78 $data['i18n'] = array_merge( $data['i18n'], $i18n );
79 }
80
81 return $data;
82 }
83
84 /**
85 * Install
86 *
87 * @TODO - Need write to api, not hook init.
88 */
89 public function install() {
90 if ( ! wp_verify_nonce( LP_Request::get_param( '_wpnonce', 'key' ), 'install-sample-course' ) ) {
91 return;
92 }
93
94 if ( ! current_user_can( ADMIN_ROLE ) ) {
95 return;
96 }
97
98 ini_set( 'max_execution_time', 0 );
99
100 $data = [];
101
102 $dummy_text = LP_WP_Filesystem::instance()->file_get_contents( LP_PLUGIN_PATH . '/dummy-data/dummy-text.txt' );
103 $this->dummy_text = preg_split( '!\s!', $dummy_text );
104
105 $section_range = LP_Request::get_param( 'section-range', 0, 'int' );
106 if ( $section_range ) {
107 self::$section_range = $section_range;
108 }
109
110 $item_range = LP_Request::get_param( 'item-range', 0, 'int' );
111 if ( $item_range ) {
112 self::$item_range = $item_range;
113 }
114
115 $question_range = LP_Request::get_param( 'question-range', 0, 'int' );
116 if ( $question_range ) {
117 self::$question_range = $question_range;
118 }
119
120 $answer_range = LP_Request::get_param( 'answer-range', 0, 'int' );
121 if ( $answer_range ) {
122 self::$answer_range = $answer_range;
123 }
124
125 $data['price'] = LP_Request::get_param( 'course-price', 0, 'float' );
126 $data['name'] = LP_Request::get_param( 'custom-name' );
127
128 try {
129 $course_id = $this->create_course( $data );
130
131 if ( ! $course_id ) {
132 throw new Exception( 'Create course failed.' );
133 }
134
135 $this->create_sections( $course_id );
136
137 $courseModel = CourseModel::find( $course_id, true );
138 // Unset value of keys for calculate again
139 unset( $courseModel->first_item_id );
140 unset( $courseModel->total_items );
141 unset( $courseModel->sections_items );
142 unset( $courseModel->meta_data->_lp_final_quiz );
143 $courseModel->get_first_item_id();
144 $courseModel->get_total_items();
145 $courseModel->get_section_items();
146 $courseModel->get_final_quiz();
147 $courseModel->save();
148
149 $link_course = get_the_permalink( $course_id );
150 $link_course = LP_Helper::handle_lp_permalink_structure( $link_course, get_post( $course_id ) );
151 ?>
152
153 <div class="lp-install-sample__response success">
154 <?php printf( __( 'The Course "%s" has been created', 'learnpress' ), get_the_title( $course_id ) ); ?>
155 <a href="<?php echo esc_url_raw( $link_course ); ?>" target="_blank"><?php esc_html_e( 'View', 'learnpress' ); ?></a>
156 |
157 <a href="<?php echo esc_url_raw( admin_url( 'post.php?post=' . $course_id . '&action=edit' ) ); ?>" target="_blank"><?php esc_html_e( 'Edit', 'learnpress' ); ?></a>
158 </div>
159
160 <?php
161 } catch ( Exception $ex ) {
162 echo '<div class="lp-install-sample__response fail">';
163 echo wp_kses_post( $ex->getMessage() );
164 echo '</div>';
165 }
166
167 ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever );
168
169 die();
170 }
171
172 /**
173 * Un-install
174 */
175 public function uninstall() {
176 if ( ! wp_verify_nonce( LP_Request::get_param( '_wpnonce' ), 'uninstall-sample-course' ) ) {
177 return;
178 }
179
180 $posts = $this->get_sample_posts();
181 try {
182 if ( ! $posts ) {
183 throw new Exception( esc_html__( 'No data sample.', 'learnpress' ) );
184 }
185
186 foreach ( $posts as $post ) {
187 switch ( $post->post_type ) {
188 case LP_COURSE_CPT:
189 $this->_delete_course( $post->ID );
190 break;
191 case LP_QUIZ_CPT:
192 $this->_delete_quiz( $post->ID );
193 break;
194 case LP_QUESTION_CPT:
195 $this->_delete_question( $post->ID );
196 break;
197 }
198
199 $this->_delete_post( $post->ID );
200 }
201 ?>
202
203 <div class="lp-install-sample__response success">
204 <?php esc_html_e( 'The sample data was successfully deleted!', 'learnpress' ); ?>
205 </div>
206
207 <?php
208 } catch ( Exception $ex ) {
209
210 echo '<div class="lp-install-sample__response fail">';
211 echo 'Error: ' . $ex->getMessage();
212 echo '</div>';
213 }
214
215 die();
216 }
217
218 /**
219 * Get all posts marked as "sample data"
220 *
221 * @return array|null|object
222 */
223 public function get_sample_posts() {
224 global $wpdb;
225
226 $query = $wpdb->prepare(
227 "
228 SELECT p.ID, post_type
229 FROM {$wpdb->posts} p
230 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = %s AND pm.meta_value = %s
231 ",
232 '_lp_sample_data',
233 'yes'
234 );
235
236 return $wpdb->get_results( $query );
237 }
238
239 protected function _delete_course( $id ) {
240 global $wpdb;
241 $query = $wpdb->prepare(
242 "
243 SELECT section_id
244 FROM {$wpdb->learnpress_sections}
245 WHERE section_course_id = %d
246 ",
247 $id
248 );
249
250 $section_ids = $wpdb->get_col( $query );
251 if ( $section_ids ) {
252 $wpdb->query( "DELETE FROM {$wpdb->learnpress_section_items} WHERE section_id IN(" . join( ',', $section_ids ) . ')' );
253 $wpdb->query( "DELETE FROM {$wpdb->learnpress_sections} WHERE section_id IN(" . join( ',', $section_ids ) . ')' );
254 }
255 }
256
257 protected function _delete_quiz( $id ) {
258 global $wpdb;
259 $wpdb->query( "DELETE FROM {$wpdb->learnpress_quiz_questions} WHERE quiz_id = {$id}" );
260 }
261
262 protected function _delete_question( $id ) {
263 global $wpdb;
264 $wpdb->query( "DELETE FROM {$wpdb->learnpress_question_answers} WHERE question_id = {$id}" );
265 }
266
267 protected function _delete_post( $post_id ) {
268 global $wpdb;
269 $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id = $post_id" );
270 $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE ID = $post_id" );
271 }
272
273 protected function _delete_user_items( $ids ) {
274 global $wpdb;
275 $format = array_fill( 0, sizeof( $ids ), '%d' );
276 $query = $wpdb->prepare(
277 "
278 DELETE
279 FROM {$wpdb->learnpress_user_items}
280 WHERE item_id IN(" . join( ',', $format ) . ')
281 ',
282 $ids
283 );
284
285 $wpdb->query( $query );
286 }
287
288 /**
289 * Generate content with 'lorem' text.
290 *
291 * @param int $min
292 * @param int $max
293 * @param int $paragraphs
294 *
295 * @return string
296 */
297 protected function generate_content( $min = 100, $max = 500, $paragraphs = 10 ) {
298 $length = rand( $min, $max );
299 $max = sizeof( $this->dummy_text ) - 1;
300 $words = array();
301
302 for ( $i = 0; $i < $length; $i++ ) {
303 $words[] = $this->dummy_text[ rand( 0, $max ) ];
304 }
305
306 $p = array();
307
308 if ( ! $paragraphs ) {
309 $paragraphs = self::$max_content_paragraph;
310 }
311
312 while ( $words && sizeof( $p ) < $paragraphs ) {
313 $len = rand( 10, 20 );
314 $cut = array_splice( $words, 0, $len );
315 $p[] = '<p>' . ucfirst( join( ' ', $cut ) ) . '</p>';
316 }
317
318 return join( '', $p );
319 }
320
321 /**
322 * Generate title with 'lorem' text.
323 *
324 * @param int $min
325 * @param int $max
326 *
327 * @return string
328 */
329 protected function generate_title( $min = 10, $max = 15 ) {
330 $length = rand( $min, $max );
331 $max = sizeof( $this->dummy_text ) - 1;
332 $words = array();
333 for ( $i = 0; $i < $length; $i++ ) {
334 $words[] = $this->dummy_text[ rand( 0, $max ) ];
335 }
336
337 return ucfirst( join( ' ', $words ) );
338 }
339
340 /**
341 * Create course.
342 *
343 * @param array $data
344 *
345 * @return int|WP_Error
346 */
347 protected function create_course( array $data ) {
348 $title = $data['name'] ?? '';
349
350 $data_insert = array(
351 'post_title' => ! empty( $title ) ? $title : __( 'Sample course', 'learnpress' ),
352 'post_type' => LP_COURSE_CPT,
353 'post_status' => 'publish',
354 'post_content' => $this->generate_content( 25, 40, 5 ),
355 );
356
357 $course_id = wp_insert_post( $data_insert );
358
359 if ( $course_id ) {
360 $metas = [
361 CoursePostModel::META_KEY_DURATION => '10 week',
362 CoursePostModel::META_KEY_SAMPLE_DATA => 'yes',
363 CoursePostModel::META_KEY_LEVEL => 'all',
364 ];
365 foreach ( $metas as $key => $value ) {
366 update_post_meta( $course_id, $key, $value );
367 }
368
369 $this->add_extra_info( $course_id );
370 }
371
372 return $course_id;
373 }
374
375 protected function add_extra_info( $course_id ) {
376 $features = array( 'requirements', 'target_audiences', 'key_features' );
377
378 // Requirements, Target audiences, Key Features
379 foreach ( $features as $feature ) {
380 $feature_data = array();
381 for ( $i = 0, $n = rand( 5, 10 ); $i <= $n; $i++ ) {
382 $feature_data[] = $this->generate_title();
383 }
384 update_post_meta( $course_id, '_lp_' . $feature, $feature_data );
385 }
386
387 // FAQs
388 $feature_data = array();
389 for ( $i = 0, $n = rand( 5, 10 ); $i <= $n; $i++ ) {
390 $feature_data[] = array( $this->generate_title() . '?', $this->generate_content( 20, 30, 3 ) );
391 }
392 update_post_meta( $course_id, '_lp_faqs', $feature_data );
393
394 // Featured review
395 update_post_meta( $course_id, '_lp_featured_review', $this->generate_title( 30, 40 ) );
396 }
397
398 /**
399 * Create sections.
400 *
401 * @param int $course_id
402 */
403 protected function create_sections( $course_id ) {
404 $section_length = call_user_func_array( 'rand', ( self::$section_range ) );
405
406 for ( $i = 1; $i <= $section_length; $i++ ) {
407 $section_id = $this->create_section( 'Section ' . $i, $course_id );
408
409 if ( $section_id ) {
410 $this->create_section_items( $section_id, $course_id );
411 }
412 }
413 }
414
415 /**
416 * Create section.
417 *
418 * @param string $name
419 * @param int $course_id
420 *
421 * @return int
422 */
423 protected function create_section( $name, $course_id ) {
424 static $order = 1;
425
426 global $wpdb;
427
428 $data = array(
429 'section_name' => $name,
430 'section_course_id' => $course_id,
431 'section_order' => $order,
432 'section_description' => $this->generate_title(),
433 );
434
435 $wpdb->insert(
436 $wpdb->learnpress_sections,
437 $data,
438 array( '%s', '%d', '%d', '%s' )
439 );
440
441 if ( $wpdb->insert_id ) {
442 ++$order;
443
444 return $wpdb->insert_id;
445 }
446
447 return 0;
448 }
449
450 /**
451 * Create section items.
452 *
453 * @param int $section_id
454 * @param int $course_id
455 */
456 protected function create_section_items( $section_id, $course_id ) {
457
458 static $lesson_count = 1;
459 static $quiz_count = 1;
460
461 $order = 0;
462
463 $item_length = call_user_func_array( 'rand', self::$item_range );
464
465 for ( $i = 1; $i < $item_length; $i++ ) {
466 $lesson_id = $this->create_lesson( 'Lesson ' . $lesson_count++, $section_id, $course_id, $order );
467
468 if ( $lesson_id ) {
469 ++$order;
470
471 if ( $i == 1 ) {
472 update_post_meta( $lesson_id, '_lp_preview', 'yes' );
473 }
474 }
475 }
476
477 $quiz_id = $this->create_quiz( 'Quiz ' . $quiz_count++, $section_id, $course_id, $order );
478
479 if ( $quiz_id ) {
480 ++$order;
481 }
482 }
483
484 /**
485 * Create lesson.
486 *
487 * @param string $name
488 * @param int $section_id
489 * @param int $course_id
490 *
491 * @return int|WP_Error
492 */
493 protected function create_lesson( $name, $section_id, $course_id, $order ) {
494 global $wpdb;
495
496 $data = array(
497 'post_title' => $name,
498 'post_type' => LP_LESSON_CPT,
499 'post_status' => 'publish',
500 'post_content' => $this->generate_content(),
501 );
502
503 $lesson_id = wp_insert_post( $data );
504
505 if ( $lesson_id ) {
506
507 update_post_meta( $lesson_id, '_lp_sample_data', 'yes' );
508
509 $section_data = array(
510 'section_id' => $section_id,
511 'item_id' => $lesson_id,
512 'item_type' => LP_LESSON_CPT,
513 'item_order' => absint( $order ),
514 );
515
516 $wpdb->insert(
517 $wpdb->learnpress_section_items,
518 $section_data,
519 array( '%d', '%d', '%s', '%d' )
520 );
521 }
522
523 return $lesson_id;
524 }
525
526 /**
527 * Create quiz.
528 *
529 * @param string $name
530 * @param int $section_id
531 * @param int $course_id
532 *
533 * @return int|WP_Error
534 */
535 protected function create_quiz( $name, $section_id, $course_id, $order ) {
536 global $wpdb;
537
538 $data = array(
539 'post_title' => $name,
540 'post_type' => LP_QUIZ_CPT,
541 'post_status' => 'publish',
542 'post_content' => $this->generate_content( 25, 40, 2 ),
543 );
544
545 $quiz_id = wp_insert_post( $data );
546
547 if ( $quiz_id ) {
548
549 $metas = array(
550 '_lp_preview' => 'no',
551 // '_lp_minus_points' => 0,
552 // '_lp_show_hide_question' => 'no',
553 // '_lp_review_questions' => 'yes',
554 // '_lp_show_result' => 'yes',
555 '_lp_duration' => ( rand( 1, 5 ) * 10 ) . ' ' . 'minute',
556 '_lp_passing_grade' => rand( 5, 9 ) * 10,
557 // '_lp_retake_count' => rand( 0, 10 ),
558 // '_lp_archive_history' => 'no',
559 // '_lp_show_check_answer' => '0',
560 // '_lp_show_hint' => '0',
561 '_lp_sample_data' => 'yes',
562 '_lp_negative_marking' => 'no',
563 '_lp_minus_skip_questions' => 'no',
564 '_lp_instant_check' => 'no',
565 '_lp_retake_count' => '0',
566 '_lp_pagination' => '1',
567 '_lp_review' => 'yes',
568 '_lp_show_correct_review' => 'yes',
569 );
570
571 foreach ( $metas as $key => $value ) {
572 update_post_meta( $quiz_id, $key, $value );
573 }
574
575 $section_data = array(
576 'section_id' => $section_id,
577 'item_id' => $quiz_id,
578 'item_type' => LP_QUIZ_CPT,
579 'item_order' => absint( $order ),
580 );
581
582 $wpdb->insert(
583 $wpdb->learnpress_section_items,
584 $section_data,
585 array( '%d', '%d', '%s', '%d' )
586 );
587
588 $this->create_quiz_questions( $quiz_id );
589 }
590
591 return $quiz_id;
592 }
593
594 /**
595 * Create questions of a quiz.
596 *
597 * @param int $quiz_id
598 */
599 protected function create_quiz_questions( $quiz_id ) {
600 static $question_index = 1;
601 global $wpdb;
602
603 $question_count = call_user_func_array( 'rand', self::$question_range );
604 for ( $i = 1; $i <= $question_count; $i++ ) {
605 $data = array(
606 'post_title' => 'Question ' . $question_index++,
607 'post_type' => LP_QUESTION_CPT,
608 'post_status' => 'publish',
609 'post_content' => $this->generate_content( 25, 40, 2 ),
610 );
611
612 $question_id = wp_insert_post( $data );
613
614 if ( ! $question_id ) {
615 continue;
616 }
617
618 $type = $this->get_question_type();
619
620 update_post_meta( $question_id, '_lp_type', $type );
621 update_post_meta( $question_id, '_lp_sample_data', 'yes' );
622
623 $quiz_question_data = array(
624 'quiz_id' => $quiz_id,
625 'question_id' => $question_id,
626 );
627
628 $wpdb->insert(
629 $wpdb->learnpress_quiz_questions,
630 $quiz_question_data,
631 array( '%d', '%d' )
632 );
633
634 if ( $wpdb->insert_id ) {
635 $this->create_question_answers( $question_id, $type );
636 } else {
637 error_log( 'create_quiz_questions => ', $wpdb->last_error );
638 }
639 }
640 }
641
642 /**
643 * Create answers for a question.
644 *
645 * @param int $question_id
646 * @param string $type
647 */
648 protected function create_question_answers( $question_id, $type ) {
649 global $wpdb;
650
651 $answers = $this->get_answers( $type );
652 foreach ( $answers as $order => $answer ) {
653 $data = array(
654 'question_id' => $question_id,
655 'title' => $answer['title'],
656 'value' => $answer['value'],
657 'is_true' => $answer['is_true'],
658 'order' => $order + 1,
659 );
660
661 $wpdb->insert(
662 $wpdb->learnpress_question_answers,
663 $data,
664 array( '%d', '%s', '%s', '%s', '%d' )
665 );
666 }
667 }
668
669 /**
670 * Get random answers by type of question.
671 *
672 * @param string $type
673 *
674 * @return array
675 */
676 protected function get_answers( $type ) {
677 $answers = array();
678
679 $option_count = $type === 'true_or_false' ? 2 : call_user_func_array( 'rand', self::$answer_range );
680
681 for ( $i = 1; $i <= $option_count; $i++ ) {
682 $answers[] = array(
683 'title' => $this->generate_title(),
684 'value' => learn_press_random_value(),
685 'is_true' => 'no',
686 );
687 }
688
689 // Set option is TRUE randomize
690 if ( $type !== 'multi_choice' ) {
691 $at = rand( 0, sizeof( $answers ) - 1 );
692 $answers[ $at ]['is_true'] = 'yes';
693 $answers[ $at ]['title'] = _x( '[TRUE] - ', 'install-sample-course', 'learnpress' ) . $answers[ $at ]['title'];
694 } else {
695 $has_true_option = false;
696 while ( ! $has_true_option ) {
697 foreach ( $answers as $k => $v ) {
698 $answers[ $k ]['is_true'] = rand( 0, 100 ) % 2 ? 'yes' : 'no';
699
700 if ( $answers[ $k ]['is_true'] === 'yes' ) {
701 $answers[ $k ]['title'] = _x( ' [TRUE] - ', 'install-sample-course', 'learnpress' ) . $answers[ $k ]['title'];
702 $has_true_option = true;
703 }
704 }
705 }
706 }
707
708 return $answers;
709 }
710
711 /**
712 * Get random type for a question.
713 *
714 * @return string
715 */
716 protected function get_question_type() {
717 $types = array(
718 'true_or_false',
719 'single_choice',
720 'multi_choice',
721 );
722
723 return $types[ rand( 0, sizeof( $types ) - 1 ) ];
724 }
725 }
726
727 new LP_Install_Sample_Data();
728