Skip to content

Commit

Permalink
feat!: show enrollment status in enrollments table (#56)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
julianramirez2 authored Nov 8, 2023
1 parent 70e8b76 commit b1348f3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
4 changes: 4 additions & 0 deletions includes/model/class-openedx-woocommerce-plugin-api-calls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
38 changes: 24 additions & 14 deletions includes/model/class-openedx-woocommerce-plugin-enrollment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 <span class="count">(%s)</span>', 'No process <span class="count">(%s)</span>', 'wp-openedx-woocommerce-plugin' ),
)
);
register_post_status(
'pending',
array(
'label' => __( 'pending', 'wp-openedx-woocommerce-plugin' ),
'public' => false,
'internal' => true,
'private' => true,
Expand All @@ -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,
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down
18 changes: 12 additions & 6 deletions includes/model/class-openedx-woocommerce-plugin-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit b1348f3

Please sign in to comment.