From b1348f3dfe51a4bc9dfa64dc0ea26cc5bea0663f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Ramirez=20Giraldo?= <52968528+julianramirez2@users.noreply.github.com> Date: Wed, 8 Nov 2023 18:21:39 -0500 Subject: [PATCH] feat!: show enrollment status in enrollments table (#56) * Merge branch 'enrollment-status' of https://github.com/eduNEXT/openedx-wordpress-ecommerce into enrollment-status * docs: added docs to explain some lines of code BREAKING CHANGE: this change causes old enrollment request entries not to be displayed because they do not have status --- ...s-openedx-woocommerce-plugin-api-calls.php | 4 ++ ...-openedx-woocommerce-plugin-enrollment.php | 38 ++++++++++++------- .../class-openedx-woocommerce-plugin-log.php | 18 ++++++--- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/includes/model/class-openedx-woocommerce-plugin-api-calls.php b/includes/model/class-openedx-woocommerce-plugin-api-calls.php index 18b054a..8265764 100644 --- a/includes/model/class-openedx-woocommerce-plugin-api-calls.php +++ b/includes/model/class-openedx-woocommerce-plugin-api-calls.php @@ -132,6 +132,10 @@ public function request_handler( $enrollment_data, $enrollment_action ) { $access_token = $this->check_access_token(); $access_token_string = $this->get_access_token( $access_token ); + if ( 'save_no_process' === $enrollment_action ) { + return array( 'no_process', 'The enrollment process has been skipped, saved locally.' ); + } + if ( 'enroll' === $request_type ) { if ( 'enrollment_process' === $enrollment_action ) { diff --git a/includes/model/class-openedx-woocommerce-plugin-enrollment.php b/includes/model/class-openedx-woocommerce-plugin-enrollment.php index 8a09c7a..af80f4a 100644 --- a/includes/model/class-openedx-woocommerce-plugin-enrollment.php +++ b/includes/model/class-openedx-woocommerce-plugin-enrollment.php @@ -146,9 +146,9 @@ public function register_save_hook() { */ public function register_status() { register_post_status( - 'enrollment-success', + 'success', array( - 'label' => __( 'Success', 'wp-openedx-woocommerce-plugin' ), + 'label' => __( 'success', 'wp-openedx-woocommerce-plugin' ), 'public' => false, 'internal' => true, 'private' => true, @@ -160,9 +160,23 @@ public function register_status() { ) ); register_post_status( - 'enrollment-pending', + 'no_process', array( - 'label' => __( 'Pending', 'wp-openedx-woocommerce-plugin' ), + 'label' => __( 'success', 'wp-openedx-woocommerce-plugin' ), + 'public' => false, + 'internal' => true, + 'private' => true, + 'exclude_from_search' => false, + 'show_in_admin_all_list' => true, + 'show_in_admin_status_list' => true, + // translators: %s: number of items. + 'label_count' => _n_noop( 'No process (%s)', 'No process (%s)', 'wp-openedx-woocommerce-plugin' ), + ) + ); + register_post_status( + 'pending', + array( + 'label' => __( 'pending', 'wp-openedx-woocommerce-plugin' ), 'public' => false, 'internal' => true, 'private' => true, @@ -174,9 +188,9 @@ public function register_status() { ) ); register_post_status( - 'enrollment-error', + 'error', array( - 'label' => __( 'Error', 'wp-openedx-woocommerce-plugin' ), + 'label' => __( 'error', 'wp-openedx-woocommerce-plugin' ), 'public' => false, 'internal' => true, 'private' => true, @@ -334,15 +348,11 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action, $or $this->update_post( $post_id ); } - $non_valid_statuses = array( 'enrollment-success', 'enrollment-pending', 'enrollment-error' ); - - // Only update the post status if it has no custom status yet. - if ( ! in_array( $post->post_status, $non_valid_statuses, true ) ) { - $this->update_post( $post_id, 'enrollment-pending' ); - } - $api = new Openedx_Woocommerce_Plugin_Api_Calls(); $enrollment_api_response = $api->request_handler( $enrollment_data, $enrollment_action ); + + // The $enrollment_api_response[0] is the status of the request. + $this->update_post( $post_id, $enrollment_api_response[0] ); $this->log_manager->create_change_log( $post_id, $old_data, $enrollment_data, $enrollment_action, $enrollment_api_response ); if ( null !== $order_id ) { @@ -435,7 +445,7 @@ public function update_post( $post_id, $status = null ) { $post_update = array( 'ID' => $post_id, - 'post_title' => $enrollment_course_id . ' | ' . $enrollment_email . ' | Mode: ' . $enrollment_mode, + 'post_title' => $enrollment_course_id . ' | ' . $enrollment_email . ' | Mode: ' . $enrollment_mode . ' | Status: ' . $status, ); if ( $status ) { diff --git a/includes/model/class-openedx-woocommerce-plugin-log.php b/includes/model/class-openedx-woocommerce-plugin-log.php index a8d8692..9f2bb43 100644 --- a/includes/model/class-openedx-woocommerce-plugin-log.php +++ b/includes/model/class-openedx-woocommerce-plugin-log.php @@ -76,19 +76,25 @@ public function create_change_log( $post_id, $old_data_array, $enrollment_arr, $ */ public function check_api_response( $response ) { - switch ( $response[0] ) { + $response_status = $response[0]; + $response_message = $response[1]; + + switch ( $response_status ) { case 'error': - return $response[1]; + return $response_message; case 'success': - return wp_json_encode( json_decode( $response[1], true ) ); + // Convert the response to JSON to be able to display it in the log. + // The json_decode is needed to convert the response (string) to an asociative array. + // The json_encode is needed to convert the asociative array to a JSON and be able to access the variables. + return wp_json_encode( json_decode( $response_message, true ) ); case 'not_api': - return 'Enrollment saved locally, action does not require an API call'; + return $response_message; - default: - return 'API did not provide a response'; + case 'no_process': + return $response_message; } }