-
Notifications
You must be signed in to change notification settings - Fork 1
feat(masterstudy-lms): add user enroll/unenroll & course/lesson completion actions #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
53dcd2f
9406c0a
829381b
69e2742
6d9c9ce
f358e20
43adf0d
d247b6b
8650b99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |
|
|
||
| namespace BitApps\Integrations\Actions\MasterStudyLms; | ||
|
|
||
| use BitApps\Integrations\Config; | ||
| use BitApps\Integrations\Core\Util\Common; | ||
| use BitApps\Integrations\Core\Util\Hooks; | ||
| use BitApps\Integrations\Log\LogHandler; | ||
| use STM_LMS_Course; | ||
| use STM_LMS_Helpers; | ||
|
|
@@ -12,6 +15,24 @@ | |
|
|
||
| class RecordApiHelper | ||
| { | ||
| private const COMPLETE_COURSE = 1; | ||
|
|
||
| private const COMPLETE_LESSON = 2; | ||
|
|
||
| private const COMPLETE_QUIZ = 3; | ||
|
|
||
| private const RESET_COURSE = 4; | ||
|
|
||
| private const RESET_LESSON = 5; | ||
|
|
||
| private const ENROLL_USER = 6; | ||
|
|
||
| private const UNENROLL_USER = 7; | ||
|
|
||
| private const MARK_COURSE_COMPLETE = 8; | ||
|
|
||
| private const MARK_LESSON_COMPLETE = 9; | ||
|
|
||
| private $integrationID; | ||
|
|
||
| private $_integrationDetails; | ||
|
|
@@ -305,17 +326,23 @@ public function execute( | |
| $integrationData | ||
| ) { | ||
| $response = []; | ||
| $fieldData = []; | ||
| $fieldData = static::generateReqDataFromFieldMap($integrationDetails->field_map ?? [], $fieldValues); | ||
|
|
||
| $defaultResponse = [ | ||
| 'success' => false, | ||
| // translators: %s: Plugin name | ||
| 'message' => wp_sprintf(__('%s plugin is not installed or activate', 'bit-integrations'), 'Bit Integrations Pro'), | ||
| ]; | ||
|
|
||
| if ($mainAction == 1) { | ||
| if ((int) $mainAction === self::COMPLETE_COURSE) { | ||
| $courseId = $integrationDetails->courseId; | ||
| $response = self::complete_course($courseId); | ||
| if ($response) { | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'course-complete', 'type_name' => 'user-course-complete']), 'success', __('Course completed successfully', 'bit-integrations')); | ||
| } else { | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'course-complete', 'type_name' => 'user-course-complete']), 'error', __('Failed to completed course', 'bit-integrations')); | ||
| } | ||
| } elseif ($mainAction == 2) { | ||
| } elseif ((int) $mainAction === self::COMPLETE_LESSON) { | ||
| $courseId = $integrationDetails->courseId; | ||
| $lessonId = $integrationDetails->lessonId; | ||
| $response = self::complete_lesson($courseId, $lessonId); | ||
|
|
@@ -324,7 +351,7 @@ public function execute( | |
| } else { | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'lesson-complete', 'type_name' => 'user-lesson-complete']), 'error', __('Failed to completed lesson', 'bit-integrations')); | ||
| } | ||
| } elseif ($mainAction == 3) { | ||
| } elseif ((int) $mainAction === self::COMPLETE_QUIZ) { | ||
| $courseId = $integrationDetails->courseId; | ||
| $quizId = $integrationDetails->quizId; | ||
| $response = self::complete_quiz($courseId, $quizId); | ||
|
|
@@ -333,15 +360,15 @@ public function execute( | |
| } else { | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'quiz-complete', 'type_name' => 'user-quiz-complete']), 'error', __('Failed to completed quiz', 'bit-integrations')); | ||
| } | ||
| } elseif ($mainAction == 4) { | ||
| } elseif ((int) $mainAction === self::RESET_COURSE) { | ||
| $courseId = $integrationDetails->courseId; | ||
| $response = self::reset_course($courseId); | ||
| if ($response) { | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'course-reset', 'type_name' => 'user-course-reset']), 'success', __('Course reset successfully', 'bit-integrations')); | ||
| } else { | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'course-reset', 'type_name' => 'user-course-reset']), 'error', __('Failed to reset course', 'bit-integrations')); | ||
| } | ||
| } elseif ($mainAction == 5) { | ||
| } elseif ((int) $mainAction === self::RESET_LESSON) { | ||
| $course_id = $integrationDetails->courseId; | ||
| $lesson_id = $integrationDetails->lessonId; | ||
| $response = self::reset_lesson($course_id, $lesson_id); | ||
|
|
@@ -350,8 +377,66 @@ public function execute( | |
| } else { | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'lesson-reset', 'type_name' => 'user-lesson-reset']), 'error', __('Failed to reset lesson', 'bit-integrations')); | ||
| } | ||
| } elseif ((int) $mainAction === self::ENROLL_USER) { | ||
| if (empty($fieldData['user_email'])) { | ||
| return new WP_Error('REQ_FIELD_EMPTY', __('User email is required', 'bit-integrations')); | ||
| } | ||
| $response = Hooks::apply(Config::withPrefix('master_study_lms_enroll_user'), $defaultResponse, [ | ||
| 'course_id' => $integrationDetails->courseId ?? null, | ||
| 'email' => $fieldData['user_email'], | ||
| ]); | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'enroll-user', 'type_name' => 'enroll-user-to-course']), (\is_array($response) && !empty($response['success'])) ? 'success' : 'error', \is_array($response) ? ($response['message'] ?? '') : ''); | ||
| } elseif ((int) $mainAction === self::UNENROLL_USER) { | ||
| if (empty($fieldData['user_email'])) { | ||
| return new WP_Error('REQ_FIELD_EMPTY', __('User email is required', 'bit-integrations')); | ||
| } | ||
| $response = Hooks::apply(Config::withPrefix('master_study_lms_unenroll_user'), $defaultResponse, [ | ||
| 'course_id' => $integrationDetails->courseId ?? null, | ||
| 'email' => $fieldData['user_email'], | ||
| ]); | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'unenroll-user', 'type_name' => 'unenroll-user-from-course']), (\is_array($response) && !empty($response['success'])) ? 'success' : 'error', \is_array($response) ? ($response['message'] ?? '') : ''); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When handling responses that may return a WP_Error object, always use the is_wp_error() function to validate the response before accessing it as an array or object. This prevents fatal errors in PHP 8.0+ and ensures that the actual error message is correctly logged. if (is_wp_error($response)) {
LogHandler::save($this->integrationID, wp_json_encode(['type' => 'unenroll-user', 'type_name' => 'unenroll-user-from-course']), 'error', $response->get_error_message());
} else {
LogHandler::save($this->integrationID, wp_json_encode(['type' => 'unenroll-user', 'type_name' => 'unenroll-user-from-course']), !empty($response['success']) ? 'success' : 'error', $response['message'] ?? '');
}References
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log line already guards with |
||
| } elseif ((int) $mainAction === self::MARK_COURSE_COMPLETE) { | ||
| if (empty($fieldData['user_email'])) { | ||
| return new WP_Error('REQ_FIELD_EMPTY', __('User email is required', 'bit-integrations')); | ||
| } | ||
| $response = Hooks::apply(Config::withPrefix('master_study_lms_mark_course_complete'), $defaultResponse, [ | ||
| 'course_id' => $integrationDetails->courseId ?? null, | ||
| 'email' => $fieldData['user_email'], | ||
| ]); | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'mark-course-complete', 'type_name' => 'mark-course-complete-for-user']), (\is_array($response) && !empty($response['success'])) ? 'success' : 'error', \is_array($response) ? ($response['message'] ?? '') : ''); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When handling responses that may return a WP_Error object, always use the is_wp_error() function to validate the response before accessing it as an array or object. This prevents fatal errors in PHP 8.0+ and ensures that the actual error message is correctly logged. if (is_wp_error($response)) {
LogHandler::save($this->integrationID, wp_json_encode(['type' => 'mark-course-complete', 'type_name' => 'mark-course-complete-for-user']), 'error', $response->get_error_message());
} else {
LogHandler::save($this->integrationID, wp_json_encode(['type' => 'mark-course-complete', 'type_name' => 'mark-course-complete-for-user']), !empty($response['success']) ? 'success' : 'error', $response['message'] ?? '');
}References
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log line already guards with |
||
| } elseif ((int) $mainAction === self::MARK_LESSON_COMPLETE) { | ||
| if (empty($fieldData['user_email'])) { | ||
| return new WP_Error('REQ_FIELD_EMPTY', __('User email is required', 'bit-integrations')); | ||
| } | ||
| $response = Hooks::apply(Config::withPrefix('master_study_lms_mark_lesson_complete'), $defaultResponse, [ | ||
| 'course_id' => $integrationDetails->courseId ?? null, | ||
| 'lesson_id' => $integrationDetails->lessonId ?? null, | ||
| 'email' => $fieldData['user_email'], | ||
| ]); | ||
| LogHandler::save($this->integrationID, wp_json_encode(['type' => 'mark-lesson-complete', 'type_name' => 'mark-lesson-complete-for-user']), (\is_array($response) && !empty($response['success'])) ? 'success' : 'error', \is_array($response) ? ($response['message'] ?? '') : ''); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When handling responses that may return a WP_Error object, always use the is_wp_error() function to validate the response before accessing it as an array or object. This prevents fatal errors in PHP 8.0+ and ensures that the actual error message is correctly logged. if (is_wp_error($response)) {
LogHandler::save($this->integrationID, wp_json_encode(['type' => 'mark-lesson-complete', 'type_name' => 'mark-lesson-complete-for-user']), 'error', $response->get_error_message());
} else {
LogHandler::save($this->integrationID, wp_json_encode(['type' => 'mark-lesson-complete', 'type_name' => 'mark-lesson-complete-for-user']), !empty($response['success']) ? 'success' : 'error', $response['message'] ?? '');
}References
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log line already guards with |
||
| } | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| protected static function generateReqDataFromFieldMap($fieldMap, $fieldValues) | ||
| { | ||
| $data = []; | ||
|
|
||
| foreach ($fieldMap as $map) { | ||
| if (empty($map->msLmsFormField)) { | ||
| continue; | ||
| } | ||
|
|
||
| $formField = $map->formField ?? ''; | ||
|
|
||
| if ($formField === 'custom' && isset($map->customValue)) { | ||
| $data[$map->msLmsFormField] = Common::replaceFieldWithValue($map->customValue, $fieldValues); | ||
| } else { | ||
| $data[$map->msLmsFormField] = $fieldValues[$formField] ?? ''; | ||
| } | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When handling responses that may return a WP_Error object, always use the is_wp_error() function to validate the response before accessing it as an array or object. This prevents fatal errors in PHP 8.0+ and ensures that the actual error message is correctly logged.
References
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The log line already guards with
\is_array($response), which prevents the array-access fatal for any non-array value (including aWP_Error). In this branch$responseis the return ofHooks::apply(...)— always the array the Pro handler returns, or the$defaultResponsearray when Pro is inactive. TheWP_Errorpaths (emptyuser_email)returnearlier, before the hook call, so there's noWP_Errorto unwrap here. No change needed.