enrollusertocourse.php
10 months ago
markcoursecompleteforuser.php
10 months ago
marklessoncompleteforuser.php
10 months ago
unrolluserfromcourse.php
10 months ago
enrollusertocourse.php
144 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EnrollUserToCourse. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category EnrollUserToCourse |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | use SureTriggers\Integrations\AutomateAction; |
| 15 | use SureTriggers\Traits\SingletonLoader; |
| 16 | use STM_LMS\STM_LMS_Mails; |
| 17 | |
| 18 | /** |
| 19 | * EnrollUserToCourse |
| 20 | * |
| 21 | * @category EnrollUserToCourse |
| 22 | * @package SureTriggers |
| 23 | * @author BSF <username@example.com> |
| 24 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 25 | * @link https://www.brainstormforce.com/ |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | class EnrollUserToCourse extends AutomateAction { |
| 29 | |
| 30 | /** |
| 31 | * Integration type. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | public $integration = 'MasterStudyLms'; |
| 36 | |
| 37 | /** |
| 38 | * Action name. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $action = 'enroll_user_to_course'; |
| 43 | |
| 44 | use SingletonLoader; |
| 45 | |
| 46 | /** |
| 47 | * Register a action. |
| 48 | * |
| 49 | * @param array $actions actions. |
| 50 | * @return array |
| 51 | */ |
| 52 | public function register( $actions ) { |
| 53 | $actions[ $this->integration ][ $this->action ] = [ |
| 54 | 'label' => __( 'Enroll User To Course', 'suretriggers' ), |
| 55 | 'action' => $this->action, |
| 56 | 'function' => [ $this, 'action_listener' ], |
| 57 | ]; |
| 58 | return $actions; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Action listener. |
| 63 | * |
| 64 | * @param int $user_id user_id. |
| 65 | * @param int $automation_id automation_id. |
| 66 | * @param array $fields fields. |
| 67 | * @param array $selected_options selectedOptions. |
| 68 | * @psalm-suppress UndefinedMethod |
| 69 | * @throws Exception Exception. |
| 70 | * |
| 71 | * @return array|bool|void |
| 72 | */ |
| 73 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 74 | $course_id = $selected_options['course']; |
| 75 | $user_id = $selected_options['wp_user_email']; |
| 76 | |
| 77 | if ( is_email( $user_id ) ) { |
| 78 | $user = get_user_by( 'email', $user_id ); |
| 79 | |
| 80 | if ( $user ) { |
| 81 | $user_id = $user->ID; |
| 82 | } else { |
| 83 | $email = $user_id; |
| 84 | $username = sanitize_title( $email ); |
| 85 | $password = wp_generate_password(); |
| 86 | |
| 87 | $user_id = wp_create_user( $username, $password, $email ); |
| 88 | |
| 89 | $subject = esc_html__( 'Login credentials for your course', 'suretriggers' ); |
| 90 | |
| 91 | $site_url = get_bloginfo( 'url' ); |
| 92 | $message = sprintf( |
| 93 | esc_html__( 'Login: %1$s Password: %2$s Site URL: %3$s', 'suretriggers' ), |
| 94 | $username, |
| 95 | $password, |
| 96 | $site_url |
| 97 | ); |
| 98 | |
| 99 | if ( class_exists( '\STM_LMS_Mails' ) ) { |
| 100 | // The STM_LMS_Mails class exists, so we can use it. |
| 101 | \STM_LMS_Mails::wp_mail_text_html(); |
| 102 | \STM_LMS_Mails::send_email( $subject, $message, $email, [], 'stm_lms_new_user_creds', compact( 'username', 'password', 'site_url' ) ); |
| 103 | \STM_LMS_Mails::remove_wp_mail_text_html(); |
| 104 | } |
| 105 | } |
| 106 | } else { |
| 107 | $error = [ |
| 108 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 109 | 'response' => esc_attr__( 'Please enter valid email address.', 'suretriggers' ), |
| 110 | |
| 111 | ]; |
| 112 | |
| 113 | return $error; |
| 114 | } |
| 115 | |
| 116 | // Enroll the user in the course if they are not already enrolled. |
| 117 | if ( function_exists( 'stm_lms_get_user_course' ) ) { |
| 118 | $course = stm_lms_get_user_course( $user_id, $course_id, [ 'user_course_id' ] ); |
| 119 | |
| 120 | if ( ! count( $course ) ) { |
| 121 | if ( class_exists( '\STM_LMS_Course' ) ) { |
| 122 | \STM_LMS_Course::add_user_course( $course_id, $user_id, \STM_LMS_Course::item_url( $course_id, '' ), 0 ); |
| 123 | \STM_LMS_Course::add_student( $course_id ); |
| 124 | } |
| 125 | |
| 126 | $response = [ |
| 127 | 'status' => esc_attr__( 'Success', 'suretriggers' ), |
| 128 | 'response' => esc_attr__( 'User enrolled into course successfully.', 'suretriggers' ), |
| 129 | |
| 130 | ]; |
| 131 | } else { |
| 132 | $response = [ |
| 133 | 'status' => esc_attr__( 'Success', 'suretriggers' ), |
| 134 | 'response' => esc_attr__( 'User already enrolled into this course.', 'suretriggers' ), |
| 135 | |
| 136 | ]; |
| 137 | } |
| 138 | return $response; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | EnrollUserToCourse::get_instance(); |
| 144 |