enroll-course.php
10 months ago
list-enrolled-users.php
10 months ago
mark-course-complete.php
10 months ago
mark-lesson-complete.php
10 months ago
remove-course.php
10 months ago
reset-user-course-progress.php
10 months ago
list-enrolled-users.php
146 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ListEnrolledUsers. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category ListEnrolledUsers |
| 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 | namespace SureTriggers\Integrations\TutorLMS\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | use Tutor\Helpers\QueryHelper; |
| 20 | |
| 21 | /** |
| 22 | * ListEnrolledUsers |
| 23 | * |
| 24 | * @category ListEnrolledUsers |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class ListEnrolledUsers extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'TutorLMS'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'tlms_list_enrolled_users'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'List Enrolled Users', 'suretriggers' ), |
| 58 | 'action' => $this->action, |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * @return array |
| 72 | * @throws Exception Exception. |
| 73 | */ |
| 74 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 75 | $context = []; |
| 76 | global $wpdb; |
| 77 | $course_id = isset( $selected_options['course_id'] ) ? $selected_options['course_id'] : '0'; |
| 78 | |
| 79 | if ( ! function_exists( 'tutor_utils' ) || ! class_exists( 'Tutor\Helpers\QueryHelper' ) || ! function_exists( 'tutor' ) ) { |
| 80 | return [ |
| 81 | 'status' => 'error', |
| 82 | 'message' => __( 'Required Tutor LMS functions or classes are not available.', 'suretriggers' ), |
| 83 | |
| 84 | ]; |
| 85 | } |
| 86 | |
| 87 | $course = get_post( (int) $course_id ); |
| 88 | if ( ! $course ) { |
| 89 | return [ |
| 90 | 'status' => 'error', |
| 91 | 'message' => __( 'Invalid course ID provided.', 'suretriggers' ), |
| 92 | |
| 93 | ]; |
| 94 | } |
| 95 | |
| 96 | $clean_users = []; |
| 97 | $enrolled_users = tutor_utils()->get_students_data_by_course_id( $course_id, 'ID', true ); |
| 98 | |
| 99 | if ( empty( $enrolled_users ) ) { |
| 100 | return [ |
| 101 | 'status' => 'error', |
| 102 | 'message' => __( 'No enrolled users found for this course.', 'suretriggers' ), |
| 103 | |
| 104 | ]; |
| 105 | } |
| 106 | |
| 107 | foreach ( $enrolled_users as $user_obj ) { |
| 108 | $user_array = (array) $user_obj; |
| 109 | unset( $user_array['user_pass'], $user_array['user_activation_key'] ); |
| 110 | $enrollments = QueryHelper::get_all( |
| 111 | $wpdb->posts, |
| 112 | [ |
| 113 | 'post_author' => $user_array['ID'], |
| 114 | 'post_type' => [ |
| 115 | tutor()->enrollment_post_type, |
| 116 | 'course-bundle', |
| 117 | ], |
| 118 | ], |
| 119 | 'ID' |
| 120 | ); |
| 121 | if ( ! empty( $enrollments ) && is_array( $enrollments ) ) { |
| 122 | $enrollment = $enrollments[0]; |
| 123 | $user_array = array_merge( |
| 124 | $user_array, |
| 125 | [ |
| 126 | 'enrollment_id' => isset( $enrollment->ID ) ? $enrollment->ID : '', |
| 127 | 'enrollment_status' => isset( $enrollment->post_status ) ? $enrollment->post_status : '', |
| 128 | 'enrollment_title' => isset( $enrollment->post_title ) ? $enrollment->post_title : '', |
| 129 | 'enrollment_date' => isset( $enrollment->post_date ) ? $enrollment->post_date : '', |
| 130 | ] |
| 131 | ); |
| 132 | } |
| 133 | $clean_users[] = $user_array; |
| 134 | } |
| 135 | |
| 136 | return [ |
| 137 | 'status' => 'success', |
| 138 | 'course_id' => $course_id, |
| 139 | 'course_name' => get_the_title( $course_id ), |
| 140 | 'enrolled_users' => $clean_users, |
| 141 | ]; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | ListEnrolledUsers::get_instance(); |
| 146 |