PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.33
OttoKit: All-in-One Automation Platform v1.1.33
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 / unrolluserfromcourse.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
unrolluserfromcourse.php
267 lines
1 <?php
2 /**
3 * UnrollUserFromCourse.
4 * php version 5.6
5 *
6 * @category UnrollUserFromCourse
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 * UnrollUserFromCourse
19 *
20 * @category UnrollUserFromCourse
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 UnrollUserFromCourse 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 = 'unroll_user_from_course';
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' => __( 'Unroll User From Course', '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_id = $selected_options['wp_user_email'];
75
76 if ( is_email( $user_id ) ) {
77 $user = get_user_by( 'email', $user_id );
78
79 if ( $user ) {
80 $user_id = $user->ID;
81 $fields['user_id'] = $user_id;
82 } else {
83 // If there's no user found, return default message.
84 $error = [
85 'status' => esc_attr__( 'Error', 'suretriggers' ),
86 'response' => esc_attr__( 'User with the email provided not found.', 'suretriggers' ),
87
88 ];
89 return $error;
90 }
91 } else {
92 $error = [
93 'status' => esc_attr__( 'Error', 'suretriggers' ),
94 'response' => esc_attr__( 'Please enter valid email address.', 'suretriggers' ),
95
96 ];
97
98 return $error;
99 }
100
101 // UnEnroll the user in the course if they are already enrolled.
102 if ( function_exists( 'stm_lms_get_user_course' ) ) {
103 $course = stm_lms_get_user_course( $user_id, $course_id, [ 'user_course_id' ] );
104 if ( empty( $course ) ) {
105 $response = [
106 'status' => esc_attr__( 'Error', 'suretriggers' ),
107 'response' => esc_attr__( 'User not enrolled into this course.', 'suretriggers' ),
108
109 ];
110 } else {
111 // Reset the user's progress.
112 $curriculum_arr = [];
113 // Use curriculum repository class.
114 if ( class_exists( '\MasterStudy\Lms\Repositories\CurriculumRepository' ) ) {
115 $curriculum_repo = new \MasterStudy\Lms\Repositories\CurriculumRepository();
116 } else {
117 $curriculum_repo = false;
118 }
119 if ( $curriculum_repo ) {
120 $curriculum = $curriculum_repo->get_curriculum( absint( $course_id ) );
121 if ( ! empty( $curriculum ) && is_array( $curriculum ) && isset( $curriculum['materials'] ) ) {
122 if ( ! empty( $curriculum['materials'] ) && is_array( $curriculum['materials'] ) ) {
123 foreach ( $curriculum['materials'] as $material ) {
124 $curriculum_arr[] = [
125 'title' => $material['title'],
126 'post_id' => $material['post_id'],
127 'post_type' => $material['post_type'],
128 ];
129 }
130 }
131 }
132 } else {
133 // No materials found, try to get them from meta_key curriculum.
134 $curriculum = get_post_meta( absint( $course_id ), 'curriculum', true );
135 if ( ! empty( $curriculum ) ) {
136 if ( class_exists( '\STM_LMS_Helpers' ) ) {
137 /**
138 * Ignoring next line
139 *
140 * @phpstan-ignore-next-line
141 * */
142 $curriculum = \STM_LMS_Helpers::only_array_numbers( explode( ',', $curriculum ) );
143 $curriculum_posts = get_posts(
144 [
145 'post__in' => $curriculum,
146 'posts_per_page' => -1,
147 'post_type' => [ 'stm-lessons', 'stm-quizzes' ],
148 'post_status' => 'publish',
149 ]
150 );
151 }
152 if ( ! empty( $curriculum_posts ) ) {
153 foreach ( $curriculum_posts as $material ) {
154 $curriculum_arr[] = [
155 'title' => $material->post_title,
156 'post_id' => $material->ID,
157 'post_type' => $material->post_type,
158 ];
159 }
160 }
161 }
162 }
163 if ( ! empty( $curriculum_arr ) ) {
164 foreach ( $curriculum_arr as $post_object ) {
165
166 if ( 'stm-lessons' === $post_object['post_type'] ) {
167 // Reset lesson progress.
168 if ( function_exists( 'stm_lms_delete_user_lesson' ) ) {
169 stm_lms_delete_user_lesson( $user_id, $course_id, $post_object['post_id'] );
170 }
171 }
172
173 if ( 'stm-assignments' === $post_object['post_type'] ) {
174 // Reset assignment progress.
175 $args = [
176 'posts_per_page' => - 1,
177 'post_type' => 'stm-user-assignment',
178 'post_status' => [
179 'pending',
180 'publish',
181 'draft',
182 ],
183 'meta_query' => [
184 'relation' => 'AND',
185 [
186 'key' => 'course_id',
187 'value' => $course_id,
188 'compare' => '=',
189 ],
190 [
191 'key' => 'assignment_id',
192 'value' => $post_object['post_id'],
193 'compare' => '=',
194 ],
195 [
196 'key' => 'student_id',
197 'value' => $user_id,
198 'compare' => '=',
199 ],
200 ],
201 ];
202 $q = new WP_Query( $args );
203 if ( $q->have_posts() ) {
204 while ( $q->have_posts() ) {
205 $q->the_post();
206 wp_delete_post( intval( get_the_ID() ) );
207 }
208 }
209 }
210
211 if ( 'stm-quizzes' === $post_object['post_type'] ) {
212 // Reset quiz progress.
213 if ( function_exists( 'stm_lms_delete_user_quiz' ) ) {
214 stm_lms_delete_user_quiz( $user_id, $course_id, $post_object['post_id'] );
215 }
216 }
217
218 // Reset the user answers.
219 if ( function_exists( 'stm_lms_reset_user_answers' ) ) {
220 stm_lms_reset_user_answers( $course_id, $user_id );
221 }
222
223 // Update course progress.
224 if ( class_exists( '\STM_LMS_Course' ) ) {
225 \STM_LMS_Course::update_course_progress( $user_id, $course_id );
226 }
227 }
228
229 // Set the success response.
230 $response = [
231 'status' => esc_attr__( 'Success', 'suretriggers' ),
232 'response' => esc_attr__( 'User progress reset successfully.', 'suretriggers' ),
233
234 ];
235 } else {
236 // If there's no response, return an error message.
237 $error =
238 [
239 'status' => esc_attr__( 'Error', 'suretriggers' ),
240 'response' => esc_attr__( 'Something went wrong. Please check the action step configuration.', 'suretriggers' ),
241
242 ];
243 return $error;
244 }
245
246 // Unroll the user from the course.
247 if ( function_exists( 'stm_lms_get_delete_user_course' ) ) {
248 stm_lms_get_delete_user_course( $user_id, $course_id );
249 }
250 if ( class_exists( '\STM_LMS_Course' ) ) {
251 \STM_LMS_Course::remove_student( $course_id );
252 }
253
254 $response = [
255 'status' => esc_attr__( 'Success', 'suretriggers' ),
256 'response' => esc_attr__( 'User unrolled from course successfully.', 'suretriggers' ),
257
258 ];
259 }
260
261 return $response;
262 }
263 }
264 }
265
266 UnrollUserFromCourse::get_instance();
267