PHP code example of ct-imsglobal / lti-1p3-tool

1. Go to this page and download the library: Download ct-imsglobal/lti-1p3-tool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ct-imsglobal / lti-1p3-tool example snippets



use \IMSGlobal\LTI;

class Example_Database implements LTI\Database {
    public function find_registration_by_issuer($iss) {
        ...
    }
    public function find_deployment($iss, $deployment_id) {
        ...
    }
}

return LTI\LTI_Registration::new()
    ->set_auth_login_url($auth_login_url)
    ->set_auth_token_url($auth_token_url)
    ->set_client_id($client_id)
    ->set_key_set_url($key_set_url)
    ->set_kid($kid)
    ->set_issuer($issuer)
    ->set_tool_private_key($private_key);

return LTI\LTI_Deployment::new()
    ->set_deployment_id($deployment_id);

// From issuer
LTI\JWKS_Endpoint::from_issuer(new Example_Database(), 'http://example.com')->output_jwks();
// From registration
LTI\JWKS_Endpoint::from_registration($registration)->output_jwks();
// From array
LTI\JWKS_Endpoint::new(['a_unique_KID' => file_get_contents('/path/to/private/key.pem')])->output_jwks();

$login = LTI_OIDC_Login::new(new Example_Database());

try {
    $redirect = $login->do_oidc_login_redirect("https://my.tool/launch");
} catch (LTI\OIDC_Exception $e) {
    echo 'Error doing OIDC login';
}

$redirect->do_redirect();

$redirect->do_js_redirect();

$redirect_url = $redirect->get_redirect_url();

$launch = LTI\LTI_Message_Launch::new(new Example_Database());

try {
    $launch->validate();
} catch (Exception $e) {
    echo 'Launch validation failed';
}

if ($launch->is_resource_launch()) {
    echo 'Resource Launch!';
} else if ($launch->is_deep_link_launch()) {
    echo 'Deep Linking Launch!';
} else {
    echo 'Unknown launch type';
}

if ($launch->has_ags()) {
    echo 'Has Assignments and Grades Service';
}
if ($launch->has_nrps()) {
    echo 'Has Names and Roles Service';
}

$launch_id = $launch->get_launch_id().

$launch = LTI_Message_Launch::from_cache($launch_id, new Example_Database());

if ($launch->has_ags()) {
    echo 'Has Assignments and Grades Service';
}

$dl = $launch->get_deep_link();

$resource = LTI\LTI_Deep_Link_Resource::new()
    ->set_url("https://my.tool/launch")
    ->set_custom_params(['my_param' => $my_param])
    ->set_title('My Resource');

$dl->output_response_form([$resource]);

$dl->get_response_jwt([$resource]);

if (!$launch->has_nrps()) {
    throw new Exception("Don't have names and roles!");
}

$nrps = $launch->get_nrps();

$members = $nrps->get_members();

if (!$launch->has_ags()) {
    throw new Exception("Don't have assignments and grades!");
}

$ags = $launch->get_ags();

$grade = LTI\LTI_Grade::new()
    ->set_score_given($grade)
    ->set_score_maximum(100)
    ->set_timestamp(date(DateTime::ISO8601))
    ->set_activity_progress('Completed')
    ->set_grading_progress('FullyGraded')
    ->set_user_id($external_user_id);

$ags->put_grade($grade);

$lineitem = LTI\LTI_Lineitem::new()
    ->set_tag('grade')
    ->set_score_maximum(100)
    ->set_label('Grade');

$ags->put_grade($grade, $lineitem);