PluginProbe
OttoKit: All-in-One Automation Platform / 1.0.42
OttoKit: All-in-One Automation Platform v1.0.42
1.1.38 1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 All 124 releases
suretriggers / src / Integrations / masterstudy-lms / actions / marklessoncompleteforuser.php
marklessoncompleteforuser.php
199 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MarkLessonCompleteForUser.
4 * php version 5.6
5 *
6 * @category MarkLessonCompleteForUser
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 * MarkLessonCompleteForUser
19 *
20 * @category MarkLessonCompleteForUser
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 MarkLessonCompleteForUser 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_lesson_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 lesson 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 // Get the lesson, user, and course IDs.
74 $lesson_id_value = $selected_options['lesson'];
75 $course_id = (int) $selected_options['course'];
76 $user_email = $selected_options['wp_user_email'];
77
78 $user = get_user_by( 'email', $user_email );
79 if ( is_object( $user ) ) {
80 $user = get_object_vars( $user );
81 $user_id = $user['ID'];
82 }
83
84 // Get the lessons and lesson associated with the course.
85 $curriculum = get_post_meta( $course_id, 'curriculum', true );
86
87 if ( ! empty( $curriculum ) ) {
88 // Convert the curriculum to an array of numbers.
89 if ( class_exists( '\STM_LMS_Helpers' ) ) {
90 /**
91 * Ignoring next line
92 *
93 * @phpstan-ignore-next-line
94 * */
95 $curriculum_items = \STM_LMS_Helpers::only_array_numbers( explode( ',', $curriculum ) );
96
97 // Get all lessons associated with the curriculum.
98 $curriculum_posts = get_posts(
99 [
100 'post__in' => $curriculum_items,
101 'posts_per_page' => 100,
102 'post_type' => [ 'stm-lessons', 'stm-quizzes' ],
103 'post_status' => 'publish',
104 ]
105 );
106 }
107
108 if ( ! empty( $curriculum_posts ) ) {
109
110 // Enroll the user in the course if they are not already enrolled.
111 if ( function_exists( 'stm_lms_get_user_course' ) ) {
112 $course = stm_lms_get_user_course( $user_id, $course_id, [ 'user_course_id' ] );
113
114 if ( ! count( $course ) ) {
115 if ( class_exists( '\STM_LMS_Course' ) ) {
116 \STM_LMS_Course::add_user_course( $course_id, $user_id, \STM_LMS_Course::item_url( $course_id, '' ), 0 );
117 \STM_LMS_Course::add_student( $course_id );
118 }
119 }
120 }
121
122 foreach ( $curriculum_posts as $post_object ) {
123
124 if ( 'stm-lessons' === $post_object->post_type ) {
125 if ( 'all' === $lesson_id_value ) {
126 $lesson_id = $post_object->ID;
127 } else {
128 $lesson_id = (int) $lesson_id_value;
129 }
130
131 // Process only if the lesson is selected.
132 if ( $lesson_id !== $post_object->ID ) {
133 continue;
134 }
135
136 if ( class_exists( '\STM_LMS_Lesson' ) ) {
137 if ( \STM_LMS_Lesson::is_lesson_completed( $user_id, $course_id, $lesson_id ) ) {
138 continue;
139 }
140 }
141
142 $end_time = time();
143 /**
144 * Ignoring next line
145 *
146 * @phpstan-ignore-next-line
147 * */
148 $start_time = get_user_meta( $user_id, "stm_lms_course_started_{$course_id}_{$lesson_id}", true );
149
150 if ( function_exists( 'stm_lms_add_user_lesson' ) ) {
151 stm_lms_add_user_lesson( compact( 'user_id', 'course_id', 'lesson_id', 'start_time', 'end_time' ) );
152 }
153
154 if ( class_exists( '\STM_LMS_Course' ) ) {
155 \STM_LMS_Course::update_course_progress( $user_id, $course_id );
156 }
157
158 do_action( 'stm_lms_lesson_passed', $user_id, $lesson_id );
159 /**
160 * Ignoring next line
161 *
162 * @phpstan-ignore-next-line
163 * */
164 delete_user_meta( $user_id, "stm_lms_course_started_{$course_id}_{$lesson_id}" );
165 }
166 }
167
168 if ( class_exists( '\STM_LMS_Course' ) ) {
169 \STM_LMS_Course::update_course_progress( $user_id, $course_id );
170 }
171
172 // Set the success response.
173 $response = [
174 'status' => esc_attr__( 'Success', 'suretriggers' ),
175 'response' => esc_attr__( 'Lesson marked complete successfully.', 'suretriggers' ),
176 ];
177 } else {
178 $error = [
179 'status' => esc_attr__( 'Error', 'suretriggers' ),
180 'response' => esc_attr__( 'Something went wrong. Please check the action step configuration.', 'suretriggers' ),
181 ];
182
183 return $error;
184 }
185 } else {
186 $error = [
187 'status' => esc_attr__( 'Error', 'suretriggers' ),
188 'response' => esc_attr__( 'Something went wrong. Please check the action step configuration.', 'suretriggers' ),
189 ];
190
191 return $error;
192 }
193
194 return $response;
195 }
196 }
197
198 MarkLessonCompleteForUser::get_instance();
199