enrollusertocourse.php
10 months ago
markcoursecompleteforuser.php
10 months ago
marklessoncompleteforuser.php
10 months ago
unrolluserfromcourse.php
10 months ago
markcoursecompleteforuser.php
225 lines
| 1 | <?php |
| 2 | /** |
| 3 | * MarkCourseCompleteForUser. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category MarkCourseCompleteForUser |
| 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 | |
| 17 | /** |
| 18 | * MarkCourseCompleteForUser |
| 19 | * |
| 20 | * @category MarkCourseCompleteForUser |
| 21 | * @package SureTriggers |
| 22 | * @author BSF <username@example.com> |
| 23 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 24 | * @link https://www.brainstormforce.com/ |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | class MarkCourseCompleteForUser extends AutomateAction { |
| 28 | |
| 29 | /** |
| 30 | * Integration type. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $integration = 'MasterStudyLms'; |
| 35 | |
| 36 | /** |
| 37 | * Action name. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $action = 'mark_course_complete'; |
| 42 | |
| 43 | use SingletonLoader; |
| 44 | |
| 45 | /** |
| 46 | * Register a action. |
| 47 | * |
| 48 | * @param array $actions actions. |
| 49 | * @return array |
| 50 | */ |
| 51 | public function register( $actions ) { |
| 52 | $actions[ $this->integration ][ $this->action ] = [ |
| 53 | 'label' => __( 'Mark a course complete for the user', 'suretriggers' ), |
| 54 | 'action' => $this->action, |
| 55 | 'function' => [ $this, 'action_listener' ], |
| 56 | ]; |
| 57 | return $actions; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Action listener. |
| 62 | * |
| 63 | * @param int $user_id user_id. |
| 64 | * @param int $automation_id automation_id. |
| 65 | * @param array $fields fields. |
| 66 | * @param array $selected_options selectedOptions. |
| 67 | * @psalm-suppress UndefinedMethod |
| 68 | * @throws Exception Exception. |
| 69 | * |
| 70 | * @return array|bool|void |
| 71 | */ |
| 72 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 73 | $course_id = $selected_options['course']; |
| 74 | $user_email = $selected_options['wp_user_email']; |
| 75 | |
| 76 | $user = get_user_by( 'email', $user_email ); |
| 77 | if ( is_object( $user ) ) { |
| 78 | $user = get_object_vars( $user ); |
| 79 | $user_id = $user['ID']; |
| 80 | } |
| 81 | |
| 82 | $curriculum_arr = []; |
| 83 | // Use curriculum repository class. |
| 84 | if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) { |
| 85 | $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository(); |
| 86 | } else { |
| 87 | $curriculum_repo = false; |
| 88 | } |
| 89 | if ( $curriculum_repo ) { |
| 90 | $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) ); |
| 91 | if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) { |
| 92 | if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) { |
| 93 | foreach ( $curriculum['materials'] as $material ) { |
| 94 | $curriculum_arr[] = [ |
| 95 | 'title' => $material['title'], |
| 96 | 'post_id' => $material['post_id'], |
| 97 | 'post_type' => $material['post_type'], |
| 98 | ]; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } else { |
| 103 | // No materials found, try to get them from meta_key curriculum. |
| 104 | $curriculum = get_post_meta( absint( $course_id ), 'curriculum', true ); |
| 105 | if ( ! empty( $curriculum ) && is_array( $curriculum ) ) { |
| 106 | if ( class_exists( '\STM_LMS_Helpers' ) ) { |
| 107 | /** |
| 108 | * Ignoring next line |
| 109 | * |
| 110 | * @phpstan-ignore-next-line |
| 111 | * */ |
| 112 | $curriculum = \STM_LMS_Helpers::only_array_numbers( explode( ',', $curriculum ) ); |
| 113 | $curriculum_posts = get_posts( |
| 114 | [ |
| 115 | 'post__in' => $curriculum, |
| 116 | 'posts_per_page' => -1, |
| 117 | 'post_type' => [ 'stm-lessons', 'stm-quizzes' ], |
| 118 | 'post_status' => 'publish', |
| 119 | ] |
| 120 | ); |
| 121 | } |
| 122 | if ( ! empty( $curriculum_posts ) ) { |
| 123 | foreach ( $curriculum_posts as $material ) { |
| 124 | $curriculum_arr[] = [ |
| 125 | 'title' => $material->post_title, |
| 126 | 'post_id' => $material->ID, |
| 127 | 'post_type' => $material->post_type, |
| 128 | ]; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if ( ! empty( $curriculum_arr ) ) { |
| 135 | // Enroll the user in the course if they are not already enrolled. |
| 136 | if ( function_exists( 'stm_lms_get_user_course' ) ) { |
| 137 | $course = stm_lms_get_user_course( $user_id, $course_id, [ 'user_course_id' ] ); |
| 138 | |
| 139 | if ( ! count( $course ) ) { |
| 140 | if ( class_exists( '\STM_LMS_Course' ) ) { |
| 141 | \STM_LMS_Course::add_user_course( $course_id, $user_id, \STM_LMS_Course::item_url( $course_id, '' ), 0 ); |
| 142 | \STM_LMS_Course::add_student( $course_id ); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | foreach ( $curriculum_arr as $post ) { |
| 147 | if ( 'stm-lessons' === $post['post_type'] ) { |
| 148 | // Complete Lesson. |
| 149 | $lesson_id = $post['post_id']; |
| 150 | if ( class_exists( '\STM_LMS_Lesson' ) ) { |
| 151 | if ( \STM_LMS_Lesson::is_lesson_completed( $user_id, $course_id, $lesson_id ) ) { |
| 152 | continue; |
| 153 | } |
| 154 | } |
| 155 | $end_time = time(); |
| 156 | /** |
| 157 | * Ignoring next line |
| 158 | * |
| 159 | * @phpstan-ignore-next-line |
| 160 | * */ |
| 161 | $start_time = get_user_meta( $user_id, "stm_lms_course_started_{$course_id}_{$lesson_id}", true ); |
| 162 | |
| 163 | if ( function_exists( 'stm_lms_add_user_lesson' ) ) { |
| 164 | stm_lms_add_user_lesson( compact( 'user_id', 'course_id', 'lesson_id', 'start_time', 'end_time' ) ); |
| 165 | } |
| 166 | |
| 167 | if ( class_exists( '\STM_LMS_Course' ) ) { |
| 168 | \STM_LMS_Course::update_course_progress( $user_id, $course_id ); |
| 169 | } |
| 170 | |
| 171 | do_action( 'stm_lms_lesson_passed', $user_id, $lesson_id ); |
| 172 | /** |
| 173 | * Ignoring next line |
| 174 | * |
| 175 | * @phpstan-ignore-next-line |
| 176 | * */ |
| 177 | delete_user_meta( $user_id, "stm_lms_course_started_{$course_id}_{$lesson_id}" ); |
| 178 | } |
| 179 | if ( 'stm-quizzes' === $post['post_type'] ) { |
| 180 | // Complete quiz. |
| 181 | $quiz_id = $post['post_id']; |
| 182 | if ( class_exists( '\STM_LMS_Quiz' ) ) { |
| 183 | if ( \STM_LMS_Quiz::quiz_passed( $quiz_id, $user_id ) ) { |
| 184 | continue; |
| 185 | } |
| 186 | } |
| 187 | $progress = 100; |
| 188 | $status = 'passed'; |
| 189 | $user_quiz = compact( 'user_id', 'course_id', 'quiz_id', 'progress', 'status' ); |
| 190 | if ( function_exists( 'stm_lms_add_user_quiz' ) ) { |
| 191 | stm_lms_add_user_quiz( $user_quiz ); |
| 192 | } |
| 193 | if ( function_exists( 'stm_lms_get_delete_user_quiz_time' ) ) { |
| 194 | stm_lms_get_delete_user_quiz_time( $user_id, $quiz_id ); |
| 195 | } |
| 196 | |
| 197 | if ( class_exists( '\STM_LMS_Course' ) ) { |
| 198 | \STM_LMS_Course::update_course_progress( $user_id, $course_id ); |
| 199 | } |
| 200 | |
| 201 | $user_quiz['progress'] = round( $user_quiz['progress'], 1 ); |
| 202 | do_action( 'stm_lms_quiz_' . $status, $user_id, $quiz_id, $user_quiz['progress'] ); |
| 203 | } |
| 204 | } |
| 205 | $response = [ |
| 206 | 'status' => esc_attr__( 'Success', 'suretriggers' ), |
| 207 | 'response' => esc_attr__( 'Course marked as completed successfully.', 'suretriggers' ), |
| 208 | |
| 209 | ]; |
| 210 | return $response; |
| 211 | } else { |
| 212 | $error = [ |
| 213 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 214 | 'response' => esc_attr__( 'Course does not have any lessons or quizzes added.', 'suretriggers' ), |
| 215 | |
| 216 | ]; |
| 217 | |
| 218 | return $error; |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | MarkCourseCompleteForUser::get_instance(); |
| 225 |