PHP code example of izumi-kun / yii2-lti-tool-provider

1. Go to this page and download the library: Download izumi-kun/yii2-lti-tool-provider 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/ */

    

izumi-kun / yii2-lti-tool-provider example snippets


$config = [
    'modules' => [
        'lti' => [
            'class' => \izumi\yii2lti\Module::class,
            'tool' => [
                'debugMode' => YII_DEBUG,
                'rsaKey' => 'A PEM formatted private key (for LTI 1.3 support)',
            ],
            'on launch' => [SiteController::class, 'ltiLaunch'],
            'on error' => [SiteController::class, 'ltiError'],
            'as access' => [
                'class' => \yii\filters\AccessControl::class,
                'rules' => [
                    ['allow' => true, 'controllers' => ['lti/tool']],
                    ['allow' => true, 'controllers' => ['lti/platform'], 'roles' => ['admin']],
                ],
            ],
        ],
    ],
];

namespace app\controllers;

use izumi\yii2lti\ToolEvent;
use Yii;
use yii\web\BadRequestHttpException;
use yii\web\Controller;

class SiteController extends Controller
{
    /**
     * basic-lti-launch-request handler
     * @param ToolEvent $event
     */
    public static function ltiLaunch(ToolEvent $event)
    {
        $tool = $event->sender;

        // $userPk can be used for user identity
        $userPk = $tool->user->getRecordId();
        $isAdmin = $tool->user->isStaff() || $tool->user->isAdmin();

        Yii::$app->session->set('isAdmin', $isAdmin);
        Yii::$app->session->set('userPk', $userPk);
        Yii::$app->controller->redirect(['/site/index']);

        $tool->ok = true;
        $event->handled = true;
    }

    /**
     * LTI error handler
     * @param ToolEvent $event
     * @throws BadRequestHttpException
     */
    public static function ltiError(ToolEvent $event)
    {
        $tool = $event->sender;
        $msg = $tool->message;
        if (!empty($tool->reason)) {
            Yii::error($tool->reason);
            if ($tool->debugMode) {
                $msg = $tool->reason;
            }
        }
        throw new BadRequestHttpException($msg);
    }
}

use ceLTIc\LTI;

/* @var \izumi\yii2lti\Module $module */
$module = Yii::$app->getModule('lti');

$user = $module->findUserById(Yii::$app->session->get('userPk'));

$result = '0.8';
$outcome = new LTI\Outcome($result);

if ($module->doOutcomesService(LTI\Enum\ServiceAction::Write, $outcome, $user)) {
    Yii::$app->session->addFlash('success', 'Result sent successfully');
}