cognifit/cognifit-sdk-php

Library for managing CogniFit user accounts

2.1.1 2024-03-04 14:01 UTC

This package is auto-updated.

Last update: 2024-11-04 15:27:16 UTC


README

Description

This library helps developers to connect with CogniFit API(https://api.cognifit.com).

CogniFit API allows CogniFit partners to integrate CogniFit Assessment Batteries and Training Games seamless into their own websites. CogniFit partners can be clinicians, hospitals, schools, universities, researchers and associates.

Reference

Health Check

use CognifitSdk\Api\HealthCheck;

$cognifitApiHealthCheck = new HealthCheck(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiHealthCheck->getInfo();

if(!$response->hasError()){
    
}

User registration

Create new user account

use CognifitSdk\Api\UserAccount;
use CognifitSdk\Lib\UserData;

$userName               = 'Joe';
$userEmail              = 'joe@example.com';
$userBirth              = '1981-07-15';
$locale                 = 'en';
$userPassword           = 'RANDOM_PASSWORD_OR_USER_KNOWN_PASSWORD';
$cognifitApiUserAccount = new UserAccount(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserAccount->registration(new UserData([
    'user_name'     => $userName,
    'user_email'    => $userEmail,
    'user_birthday' => $userBirth,
    'user_locale'   => $locale,
    'user_password' => $userPassword 
]));

if(!$response->hasError()){
    $cognifitUserToken = $response->get('user_token');
    if($cognifitUserToken){
        // Save this user_token for future requests 
    }
}

Associate existing user account

If the user already exists in CogniFit, their account can be associated. Redirect the user to the url returned by this method.

If the user accepts to associate the CogniFit accout with the partner, the user_token will be sent back to the callback_url provided.

The callback_url must be one of the callback urls communicated to CogniFit when the partner was created.

State parameter is a hash generated by the partner. This value will be added to the callback response.

use CognifitSdk\Api\UserAccount;

$cognifitApiUserAccount = new UserAccount(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);

$userEmail = 'joe@example.com';
$callbackUrl = '';
$stateHash = 'RANDOM';

$response = $cognifitApiUserAccount->getAssociateExistingUserAccountUrl(
    $userEmail,
    $callbackUrl,
    $stateHash 
);

if(!$response->hasError()){
    $cognifitUserToken = $response->get('user_token');
    if($cognifitUserToken){
        // Save this user_token for future requests 
    }
}

User authentication

Issue user access token

use CognifitSdk\Api\UserAccessToken;

$cognifitUserToken          = 'USER_TOKEN';
$cognifitApiUserAccessToken = new UserAccessToken(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserAccessToken->issue($cognifitUserToken);
if(!$response->hasError()){
    return $response->get('access_token');
}

Use access token to access CogniFit

use CognifitSdk\Api\UserStartSession;

$cognifitStartSession = new UserStartSession(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET'),
    false,
    env('COGNIFIT_HASH')
);

$userAccessToken    = 'USER_ACCESS_TOKEN';
$callbackUrl        = '';

$url = $cognifitStartSession->getUrl($userAccessToken, $callbackUrl);

User manager

Update user account

use CognifitSdk\Api\UserAccount;
use CognifitSdk\Lib\UserData;

$userToken              = 'USER_TOKEN';
$userName               = 'Joe';
$userEmail              = 'joe@example.com';
$userBirth              = '1981-07-15';
$locale                 = 'en';
$userPassword           = 'RANDOM_PASSWORD_OR_USER_KNOWN_PASSWORD';
$cognifitApiUserAccount = new UserAccount(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserAccount->update($userToken, new UserData([
    'user_name'     => $userName,
    'user_email'    => $userEmail,
    'user_birthday' => $userBirth,
    'user_locale'   => $locale,
    'user_password' => $userPassword 
]));

if(!$response->hasError()){
    // Success
}

User activation

If a user was previously deactivated, this method will set it to activated. Users need to be activated to log in CogniFit. Activate a user is not allocating any license or program to that user.

use CognifitSdk\Api\UserAccount;

$userToken              = 'USER_TOKEN';
$cognifitApiUserAccount = new UserAccount(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserAccount->activate($userToken);

if(!$response->hasError()){
    // Success
}

User deactivation

Allows to deactivate users and free their licenses. As long as users are deactivated, their cognitive information can not be reached nor can they log in CogniFit.

use CognifitSdk\Api\UserAccount;

$userToken              = 'USER_TOKEN';
$cognifitApiUserAccount = new UserAccount(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserAccount->deactivate($userToken);

if(!$response->hasError()){
    // Success
}

User grant training subscription

Allows to activate monthly recurring subscriptions for users. User will have access to all training programs and all games. By default, the personalised training will be set as training program.

use CognifitSdk\Api\UserAccount;

$userToken              = 'USER_TOKEN';
$cognifitApiUserAccount = new UserAccount(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserAccount->grantTrainingLicense($userToken);

if(!$response->hasError()){
    // Success
}

User cancel training subscription

Allows to deactivate monthly recurring subscriptions for users. User will have access until the end of his subscription period. After that subscription will not be renewed and user will not have anymore access to trainings and games.

use CognifitSdk\Api\UserAccount;

$userToken              = 'USER_TOKEN';
$cognifitApiUserAccount = new UserAccount(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserAccount->revokeTrainingLicense($userToken);

if(!$response->hasError()){
    // Success
}

User account deletion

Allows to delete user accounts. User data would be removed. User would not be able to access CogniFit anymore with this account. The partner would not be able to access user data. Partners are only able to delete accounts created by them.

use CognifitSdk\Api\UserAccount;

$userToken              = 'USER_TOKEN';
$cognifitApiUserAccount = new UserAccount(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserAccount->delete($userToken);

if(!$response->hasError()){
    // Success
}

Cognitive assessments

Assessments list

List of allowed assessment keys to use when setting a program.

use CognifitSdk\Api\Product;

$localesForAssets   = ['en', 'es'];
$product            = new Product(env('COGNIFIT_CLIENT_ID'), true);
$assessments        = $product->getAssessments($localesForAssets);
foreach ($assessments as $assessmentKey => $assessment){
    echo $assessment->getKey();
}   

Single task assessment list

List of allowed single task assessment keys to use when setting a program.

use CognifitSdk\Api\Product;

$localesForAssets   = ['en', 'es'];
$product            = new Product(env('COGNIFIT_CLIENT_ID'), true);
$assessments        = $product->getAssessmentTasks($localesForAssets);
foreach ($assessments as $assessmentKey => $assessment){
    echo $assessment->getKey();
}   

Questionnaires list

List of allowed questionnaire keys to use when setting a program.

use CognifitSdk\Api\Product;

$localesForAssets   = ['en', 'es'];
$product            = new Product(env('COGNIFIT_CLIENT_ID'), true);
$questionnaires     = $product->getQuestionnaires($localesForAssets);
foreach ($questionnaires as $questionnaireKey => $questionnaire){
    echo $questionnaire->getKey();
}   

Brain training Programs

Training list

List of allowed training keys to use when setting a training program.

use CognifitSdk\Api\Product;

$localesForAssets  = ['en', 'es'];
$product           = new Product(env('COGNIFIT_CLIENT_ID'), true);
$trainingPrograms  = $product->getTraining($localesForAssets);
foreach ($trainingPrograms as $trainingProgramKey => $trainingProgram){
    echo $trainingProgram->getKey();
}   

Brain Games

Brain Game list

List of allowed games keys to use when starting a unique game session.

use CognifitSdk\Api\Product;

$localesForAssets   = ['en', 'es'];
$product            = new Product(env('COGNIFIT_CLIENT_ID'), true);
$games              = $product->getGames($localesForAssets);
foreach ($games as $gameKey => $game){
    echo $game->getKey();
}   

Cognitive Skill list

List of cognitive skills measured and trained by CogniFit.

use CognifitSdk\Api\Skills;

$localesForAssets   = ['en', 'es'];
$skills             = new Skills(env('COGNIFIT_CLIENT_ID'), true);
$skillList          = $skills->getSkills($localesForAssets);
foreach ($skillList as $skill){
    echo $skill->getKey();
}   

Login & Launch

After obtaining a user access token, it must be used before it expires: 1. by getting a url with next method and redirect there the user 2. by using CogniFit Launcher SDK for JavaScript to embed CogniFit in your site

Training program

use CognifitSdk\Api\UserStartSession;

$userStartSession   = new UserStartSession(getenv('TEST_CLIENT_ID'), '', true, getenv('TEST_CLIENT_HASH'));
$urlToStartSession  = $userStartSession->getUrlStartCognifitForTraining($this->userToken, $this->callbackUrl, 'DRIVING');   

Assessment, Single Task Assessment & Questionnaire program

use CognifitSdk\Api\UserStartSession;

$userStartSession   = new UserStartSession(getenv('TEST_CLIENT_ID'), '', true, getenv('TEST_CLIENT_HASH'));
$urlToStartSession  = $userStartSession->getUrlStartCognifitForAssessment('USER_TOKEN', '', 'DRIVING_ASSESSMENT');   

Task program

use CognifitSdk\Api\UserStartSession;

$userStartSession   = new UserStartSession(getenv('TEST_CLIENT_ID'), '', true, getenv('TEST_CLIENT_HASH'));
$urlToStartSession  = $userStartSession->getUrlStartCognifitForGame('USER_TOKEN', '', 'MAHJONG');   

Activity and evolution report

Get Historical Skills

use CognifitSdk\Api\UserActivity;

$cognifitUserToken       = 'USER_TOKEN';
$cognifitApiUserActivity = new UserActivity(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserActivity->getHistoricalScoreAndSkills($cognifitUserToken);
if(!$response->hasError()){
    
}

Get Played Games

use CognifitSdk\Api\UserActivity;

$cognifitUserToken       = 'USER_TOKEN';
$cognifitApiUserActivity = new UserActivity(
    env('COGNIFIT_CLIENT_ID'),
    env('COGNIFIT_CLIENT_SECRET')
);
$response = $cognifitApiUserActivity->getPlayedGames($cognifitUserToken);
if(!$response->hasError()){
    
}