aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/courseware/lti.php
blob: 53ec44bb40084f0e81fca7fd9d0e333d4d48e24d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

class Courseware_LtiController extends AuthenticatedController
{

    /**
     * Display the launch form for a tool as an iframe in a courseware LTI block.
     *
     * @param   int $block_id    courseware block id
     */
    public function iframe_action($block_id)
    {
        $cw_block = \Courseware\Block::find($block_id);
        if (!$cw_block->container->structural_element->canRead(User::findCurrent())) {
            throw new AccessDeniedException();
        }

        $cw_block = \Courseware\Block::find($block_id);

        $lti_link = $this->getLtiLink($cw_block);

        $this->launch_url  = $lti_link->getLaunchURL();
        $this->launch_data = $lti_link->getBasicLaunchData();
        $this->signature   = $lti_link->getLaunchSignature($this->launch_data);

        $this->set_layout(null);
        $this->render_template('course/lti/iframe');
    }

    /**
     * Return an LtiLink object for the passed courseware LTI block.
     *
     * @param   \Courseware\Block $cw_block courseware LTI block
     *
     * @return  LtiLink  LTI link representation
     */
    public function getLtiLink($cw_block)
    {
        $block_payload = json_decode($cw_block->payload, true);

        // Collect LTI Data from courseware block payload
        $id = $cw_block->id;
        $context_id = Context::getId();
        $range_id = $cw_block->getStructuralElement()->range_id;
        $title = trim($block_payload['title']);
        $tool_id = $block_payload['tool_id'];
        $launch_url = trim($block_payload['launch_url']);
        $custom_parameters = trim($block_payload['custom_parameters']);
        $document_target = 'iframe';

        if ($tool_id) {
            $tool = LtiTool::find($tool_id);

            // Prefer custom url
            if (!$tool->allow_custom_url && !$tool->deep_linking || !$launch_url) {
                $launch_url = $tool->launch_url;
            }

            $consumer_key = $tool->consumer_key;
            $consumer_secret = $tool->consumer_secret;
            $send_lis_person = $tool->send_lis_person;
            $oauth_signature_method = $tool->oauth_signature_method;
            $custom_parameters = $tool->custom_parameters . "\n" . $custom_parameters;
        } else {
            $consumer_key = trim($block_payload['consumer_key']);
            $consumer_secret = trim($block_payload['consumer_secret']);
            $send_lis_person = $block_payload['send_lis_person'];
            $oauth_signature_method = $block_payload['oauth_signature_method'] ?? 'sha1';
        }

        if ($context_id) {
            // Role in course
            $roles = $GLOBALS['perm']->have_studip_perm('tutor', $context_id) ? 'Instructor' : 'Learner';
        } else {
            // Role in workspace
            $roles = $range_id === $GLOBALS['user']->id ? 'Instructor' : 'Learner';
        }

        // Create LTI Link for setting up launch request
        $lti_link = new LtiLink($launch_url, $consumer_key, $consumer_secret, $oauth_signature_method);
        $lti_link->setResource($id, $title);
        $lti_link->setUser(User::findCurrent(), $roles, $send_lis_person);
        $lti_link->setCourse($range_id);
        $lti_link->addLaunchParameters([
            'launch_presentation_locale' => str_replace('_', '-', $_SESSION['_language']),
            'launch_presentation_document_target' => $document_target,
        ]);

        $custom_parameters = explode("\n", $custom_parameters);
        foreach ($custom_parameters as $param) {
            if (strpos($param, '=') !== false) {
                list($key, $value) = explode('=', $param, 2);
                $lti_link->addCustomParameter(trim($key), trim($value));
            }
        }

        return $lti_link;
    }
}