PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.32
OttoKit: All-in-One Automation Platform v1.1.32
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 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / masterstudy-lms / actions / marklessoncompleteforuser.php
suretriggers / src / Integrations / masterstudy-lms / actions Last commit date
enrollusertocourse.php 10 months ago markcoursecompleteforuser.php 10 months ago marklessoncompleteforuser.php 10 months ago unrolluserfromcourse.php 10 months ago
marklessoncompleteforuser.php
230 lines
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 $curriculum_arr = [];
85 // Use curriculum repository class.
86 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
87 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
88 } else {
89 $curriculum_repo = false;
90 }
91 if ( $curriculum_repo ) {
92 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
93 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
94 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
95 foreach ( $curriculum['materials'] as $material ) {
96 $curriculum_arr[] = [
97 'title' => $material['title'],
98 'post_id' => $material['post_id'],
99 'post_type' => $material['post_type'],
100 ];
101 }
102 }
103 }
104 } else {
105 // No materials found, try to get them from meta_key curriculum.
106 $curriculum = get_post_meta( absint( $course_id ), 'curriculum', true );
107 if ( ! empty( $curriculum ) && is_array( $curriculum ) ) {
108 if ( class_exists( '\STM_LMS_Helpers' ) ) {
109 /**
110 * Ignoring next line
111 *
112 * @phpstan-ignore-next-line
113 * */
114 $curriculum = \STM_LMS_Helpers::only_array_numbers( explode( ',', $curriculum ) );
115 $curriculum_posts = get_posts(
116 [
117 'post__in' => $curriculum,
118 'posts_per_page' => -1,
119 'post_type' => [ 'stm-lessons', 'stm-quizzes' ],
120 'post_status' => 'publish',
121 ]
122 );
123 }
124 if ( ! empty( $curriculum_posts ) ) {
125 foreach ( $curriculum_posts as $material ) {
126 $curriculum_arr[] = [
127 'title' => $material->post_title,
128 'post_id' => $material->ID,
129 'post_type' => $material->post_type,
130 ];
131 }
132 }
133 }
134 }
135 if ( empty( $curriculum_arr ) ) {
136 $error = [
137 'status' => esc_attr__( 'Error', 'suretriggers' ),
138 'response' => esc_attr__( 'No Lessons found in selected Course.', 'suretriggers' ),
139
140 ];
141 return $error;
142 }
143 // Filter $curriculum to only include the lesson we want.
144 $lessons = [];
145 if ( '-1' != $lesson_id_value ) {
146 foreach ( $curriculum_arr as $item ) {
147 if ( absint( $lesson_id_value ) === $item['post_id'] && 'stm-lessons' === $item['post_type'] ) {
148 $lessons[] = $item['post_id'];
149 }
150 }
151 } else {
152 foreach ( $curriculum_arr as $item ) {
153 if ( 'stm-lessons' === $item['post_type'] ) {
154 $lessons[] = $item['post_id'];
155 }
156 }
157 }
158 if ( ! empty( $lessons ) ) {
159
160 // Enroll the user in the course if they are not already enrolled.
161 if ( function_exists( 'stm_lms_get_user_course' ) ) {
162 $course = stm_lms_get_user_course( $user_id, $course_id, [ 'user_course_id' ] );
163
164 if ( ! count( $course ) ) {
165 if ( class_exists( '\STM_LMS_Course' ) ) {
166 \STM_LMS_Course::add_user_course( $course_id, $user_id, \STM_LMS_Course::item_url( $course_id, '' ), 0 );
167 \STM_LMS_Course::add_student( $course_id );
168 }
169 }
170 }
171
172 if ( ! class_exists( '\STM_LMS_Lesson' ) ) {
173 return [
174 'status' => 'error',
175 'message' => __( '\STM_LMS_Lesson class not found.', 'suretriggers' ),
176
177 ];
178 }
179 foreach ( $lessons as $lesson_id ) {
180 if ( ! \STM_LMS_Lesson::is_lesson_completed( $user_id, $course_id, $lesson_id ) ) {
181 $end_time = time();
182 /**
183 * Ignoring next line
184 *
185 * @phpstan-ignore-next-line
186 * */
187 $start_time = get_user_meta( $user_id, "stm_lms_course_started_{$course_id}_{$lesson_id}", true );
188
189 if ( function_exists( 'stm_lms_add_user_lesson' ) ) {
190 stm_lms_add_user_lesson( compact( 'user_id', 'course_id', 'lesson_id', 'start_time', 'end_time' ) );
191 }
192
193 if ( class_exists( '\STM_LMS_Course' ) ) {
194 \STM_LMS_Course::update_course_progress( $user_id, $course_id );
195 }
196
197 do_action( 'stm_lms_lesson_passed', $user_id, $lesson_id );
198 /**
199 * Ignoring next line
200 *
201 * @phpstan-ignore-next-line
202 * */
203 delete_user_meta( $user_id, "stm_lms_course_started_{$course_id}_{$lesson_id}" );
204 if ( class_exists( '\STM_LMS_Course' ) ) {
205 \STM_LMS_Course::update_course_progress( $user_id, $course_id );
206 }
207 }
208 }
209 // Set the success response.
210 $response = [
211 'status' => esc_attr__( 'Success', 'suretriggers' ),
212 'response' => esc_attr__( 'Lesson marked complete successfully.', 'suretriggers' ),
213
214 ];
215 } else {
216 $error = [
217 'status' => esc_attr__( 'Error', 'suretriggers' ),
218 'response' => esc_attr__( 'Lesson not found.', 'suretriggers' ),
219
220 ];
221
222 return $error;
223 }
224
225 return $response;
226 }
227 }
228
229 MarkLessonCompleteForUser::get_instance();
230