-
Notifications
You must be signed in to change notification settings - Fork 1
feat(integrations): add Sensei LMS action integration #188
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
Open
RishadAlam
wants to merge
7
commits into
main
Choose a base branch
from
feat/senseilms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4b6a734
feat(sensei-lms): add Sensei LMS action integration
RishadAlam a5729d9
refactor(sensei-lms): move static choices to dropdowns, switch-case d…
RishadAlam 3713cf1
refactor(sensei-lms): drop unused utilities arg from action filters
RishadAlam 4791022
refactor: front end prettier format
RishadAlam a68bd3a
fix(sensei-lms): rebuild edit-mode config, harden auth and static cal…
RishadAlam acf8b52
Merge branch 'main' into feat/senseilms
RishadAlam 8243072
refactor(SenseiLMS): remove authorization logic and integrate Authori…
RishadAlam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Sensei LMS Record Api | ||
| */ | ||
|
|
||
| namespace BitApps\Integrations\Actions\SenseiLMS; | ||
|
|
||
| use BitApps\Integrations\Config; | ||
| use BitApps\Integrations\Core\Util\Common; | ||
| use BitApps\Integrations\Core\Util\Hooks; | ||
| use BitApps\Integrations\Log\LogHandler; | ||
|
|
||
| /** | ||
| * Provide functionality for Record insert, update | ||
| */ | ||
| class RecordApiHelper | ||
| { | ||
| private $_integrationID; | ||
|
|
||
| private $_integrationDetails; | ||
|
|
||
| public function __construct($integrationDetails, $integId) | ||
| { | ||
| $this->_integrationDetails = $integrationDetails; | ||
| $this->_integrationID = $integId; | ||
| } | ||
|
|
||
| public function execute($fieldValues, $fieldMap, $utilities) | ||
| { | ||
| if (!class_exists('Sensei_Main')) { | ||
| return [ | ||
| 'success' => false, | ||
| 'message' => __('Sensei LMS is not installed or activated', 'bit-integrations') | ||
| ]; | ||
| } | ||
|
|
||
| $fieldData = static::generateReqDataFromFieldMap($fieldMap, $fieldValues); | ||
|
|
||
| $mainAction = $this->_integrationDetails->mainAction ?? null; | ||
|
|
||
| $defaultResponse = [ | ||
| 'success' => false, | ||
| // translators: %s: Plugin name | ||
| 'message' => wp_sprintf(__('%s plugin is not installed or activated', 'bit-integrations'), 'Bit Integrations Pro') | ||
| ]; | ||
|
|
||
| switch ($mainAction) { | ||
| case 'enroll_user_in_course': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_enroll_user_in_course'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'withdraw_user_from_course': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_withdraw_user_from_course'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'start_course_for_user': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_start_course_for_user'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'complete_course_for_user': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_complete_course_for_user'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'reset_course_for_user': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_reset_course_for_user'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'start_lesson_for_user': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_start_lesson_for_user'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'update_lesson_status': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_update_lesson_status'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'reset_lesson_for_user': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_reset_lesson_for_user'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'grade_quiz': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_grade_quiz'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'create_course': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_create_course'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'create_lesson': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_create_lesson'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| case 'create_certificate': | ||
| $response = Hooks::apply(Config::withPrefix('sensei_lms_create_certificate'), $defaultResponse, $fieldData, $this->_integrationDetails); | ||
|
|
||
| break; | ||
| default: | ||
| $response = ['success' => false, 'message' => __('Invalid action', 'bit-integrations')]; | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| $responseType = isset($response['success']) && $response['success'] ? 'success' : 'error'; | ||
| LogHandler::save($this->_integrationID, ['type' => 'SenseiLMS', 'type_name' => $mainAction], $responseType, $response); | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| private static function generateReqDataFromFieldMap($fieldMap, $fieldValues) | ||
| { | ||
| $dataFinal = []; | ||
| foreach ($fieldMap as $item) { | ||
| if (empty($item->senseiLMSField)) { | ||
| continue; | ||
| } | ||
|
|
||
| $triggerValue = $item->formField; | ||
| $actionValue = $item->senseiLMSField; | ||
|
|
||
| $dataFinal[$actionValue] = $triggerValue === 'custom' && isset($item->customValue) | ||
| ? Common::replaceFieldWithValue($item->customValue, $fieldValues) | ||
| : $fieldValues[$triggerValue] ?? ''; | ||
| } | ||
|
|
||
| return $dataFinal; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| if (!defined('ABSPATH')) { | ||
| exit; | ||
| } | ||
|
|
||
| use BitApps\Integrations\Actions\SenseiLMS\SenseiLMSController; | ||
| use BitApps\Integrations\Core\Util\Route; | ||
|
|
||
| Route::post('refresh_sensei_lms_courses', [SenseiLMSController::class, 'refreshCourses']); | ||
| Route::post('refresh_sensei_lms_lessons', [SenseiLMSController::class, 'refreshLessons']); | ||
| Route::post('refresh_sensei_lms_quizzes', [SenseiLMSController::class, 'refreshQuizzes']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Sensei LMS Integration | ||
| */ | ||
|
|
||
| namespace BitApps\Integrations\Actions\SenseiLMS; | ||
|
|
||
| use WP_Error; | ||
|
|
||
| /** | ||
| * Provide functionality for Sensei LMS integration | ||
| */ | ||
| class SenseiLMSController | ||
| { | ||
| public static function isExists() | ||
| { | ||
| if (!class_exists('Sensei_Main')) { | ||
| wp_send_json_error( | ||
| __('Sensei LMS is not activated or not installed', 'bit-integrations'), | ||
| 400 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public static function refreshCourses() | ||
| { | ||
| self::isExists(); | ||
|
|
||
| $response['courses'] = self::postOptions('course'); | ||
| wp_send_json_success($response, 200); | ||
| } | ||
|
|
||
| public static function refreshLessons() | ||
| { | ||
| self::isExists(); | ||
|
|
||
| $response['lessons'] = self::postOptions('lesson'); | ||
| wp_send_json_success($response, 200); | ||
| } | ||
|
|
||
| public static function refreshQuizzes() | ||
| { | ||
| self::isExists(); | ||
|
|
||
| $response['quizzes'] = self::postOptions('quiz'); | ||
| wp_send_json_success($response, 200); | ||
| } | ||
|
|
||
| public function execute($integrationData, $fieldValues) | ||
| { | ||
| $integrationDetails = $integrationData->flow_details; | ||
| $integId = $integrationData->id; | ||
| $fieldMap = $integrationDetails->field_map; | ||
| $utilities = isset($integrationDetails->utilities) ? $integrationDetails->utilities : []; | ||
|
|
||
| if (empty($fieldMap)) { | ||
| return new WP_Error('field_map_empty', __('Field map is empty', 'bit-integrations')); | ||
| } | ||
|
|
||
| $recordApiHelper = new RecordApiHelper($integrationDetails, $integId); | ||
|
|
||
| return $recordApiHelper->execute($fieldValues, $fieldMap, $utilities); | ||
| } | ||
|
|
||
| private static function postOptions($postType) | ||
| { | ||
| $options = []; | ||
|
|
||
| $posts = get_posts( | ||
| [ | ||
| 'post_type' => $postType, | ||
| 'post_status' => 'publish', | ||
| 'numberposts' => -1, | ||
| ] | ||
| ); | ||
|
|
||
| foreach ($posts as $post) { | ||
| $options[] = (object) [ | ||
| 'value' => $post->ID, | ||
| 'label' => $post->post_title, | ||
| ]; | ||
| } | ||
|
|
||
| return $options; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
frontend/src/components/AllIntegrations/SenseiLMS/EditSenseiLMS.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { create } from 'mutative' | ||
| import { useEffect, useState } from 'react' | ||
| import { useNavigate, useParams } from 'react-router' | ||
| import { useRecoilState, useRecoilValue } from 'recoil' | ||
| import { $actionConf, $formFields, $newFlow } from '../../../GlobalStates' | ||
| import { __ } from '../../../Utils/i18nwrap' | ||
| import SnackMsg from '../../Utilities/SnackMsg' | ||
| import { saveActionConf } from '../IntegrationHelpers/IntegrationHelpers' | ||
| import IntegrationStepThree from '../IntegrationHelpers/IntegrationStepThree' | ||
| import SetEditIntegComponents from '../IntegrationHelpers/SetEditIntegComponents' | ||
| import { | ||
| checkMappedFields, | ||
| handleInput, | ||
| refreshCourses, | ||
| refreshLessons, | ||
| refreshQuizzes | ||
| } from './SenseiLMSCommonFunc' | ||
| import SenseiLMSIntegLayout from './SenseiLMSIntegLayout' | ||
| import { courseActions, lessonActions, quizActions, senseiLMSStaticData } from './staticData' | ||
|
|
||
| export default function EditSenseiLMS({ allIntegURL }) { | ||
| const navigate = useNavigate() | ||
| const { id, formID } = useParams() | ||
|
|
||
| const [senseiLMSConf, setSenseiLMSConf] = useRecoilState($actionConf) | ||
| const [flow, setFlow] = useRecoilState($newFlow) | ||
| const formFields = useRecoilValue($formFields) | ||
| const [isLoading, setIsLoading] = useState(false) | ||
| const [snack, setSnackbar] = useState({ show: false }) | ||
|
|
||
| // On edit, rebuild the non-persisted field definitions from the saved action and | ||
| // repopulate the resource dropdown options. The saved field_map mapping is untouched. | ||
| useEffect(() => { | ||
| const action = senseiLMSConf?.mainAction | ||
| if (!action) { | ||
| return | ||
| } | ||
|
|
||
| setSenseiLMSConf(prevConf => | ||
| create(prevConf, draftConf => { | ||
| draftConf.senseiLMSFields = senseiLMSStaticData[action] || [] | ||
| }) | ||
| ) | ||
|
|
||
| if (courseActions.includes(action)) { | ||
| refreshCourses(setSenseiLMSConf, setIsLoading) | ||
| } else if (lessonActions.includes(action)) { | ||
| refreshLessons(setSenseiLMSConf, setIsLoading) | ||
| } else if (quizActions.includes(action)) { | ||
| refreshQuizzes(setSenseiLMSConf, setIsLoading) | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, []) | ||
|
|
||
| return ( | ||
| <div style={{ width: 900 }}> | ||
| <SnackMsg snack={snack} setSnackbar={setSnackbar} /> | ||
|
|
||
| <div className="flx mt-3"> | ||
| <b className="wdt-200 d-in-b">{__('Integration Name:', 'bit-integrations')}</b> | ||
| <input | ||
| className="btcd-paper-inp w-5" | ||
| onChange={e => handleInput(e, senseiLMSConf, setSenseiLMSConf)} | ||
| name="name" | ||
| value={senseiLMSConf.name} | ||
| type="text" | ||
| placeholder={__('Integration Name...', 'bit-integrations')} | ||
| /> | ||
| </div> | ||
| <br /> | ||
|
|
||
| <SetEditIntegComponents entity={flow.triggered_entity} setSnackbar={setSnackbar} /> | ||
|
|
||
| <SenseiLMSIntegLayout | ||
| formID={formID} | ||
| formFields={formFields} | ||
| senseiLMSConf={senseiLMSConf} | ||
| setSenseiLMSConf={setSenseiLMSConf} | ||
| setSnackbar={setSnackbar} | ||
| setIsLoading={setIsLoading} | ||
| isLoading={isLoading} | ||
| /> | ||
|
|
||
| <IntegrationStepThree | ||
| edit | ||
| saveConfig={() => | ||
| saveActionConf({ | ||
| flow, | ||
| setFlow, | ||
| allIntegURL, | ||
| conf: senseiLMSConf, | ||
| navigate, | ||
| id, | ||
| edit: 1, | ||
| setIsLoading, | ||
| setSnackbar | ||
| }) | ||
| } | ||
| disabled={!checkMappedFields(senseiLMSConf)} | ||
| isLoading={isLoading} | ||
| dataConf={senseiLMSConf} | ||
| setDataConf={setSenseiLMSConf} | ||
| formFields={formFields} | ||
| /> | ||
| <br /> | ||
| </div> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.