Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TaoLtiSession | |
0.00% |
0 / 26 |
|
0.00% |
0 / 6 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fromVersion1p3 | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getUserLabel | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| getLaunchData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLtiLinkResource | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getLtiConsumer | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013-2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoLti\models\classes; |
| 22 | |
| 23 | use common_exception_Error; |
| 24 | use common_session_DefaultSession; |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\taoLti\models\classes\ResourceLink\LinkService; |
| 27 | use oat\taoLti\models\classes\user\LtiUserInterface; |
| 28 | |
| 29 | class TaoLtiSession extends common_session_DefaultSession |
| 30 | { |
| 31 | private const VERSION_LTI_1P1 = '1.1'; |
| 32 | private const VERSION_LTI_1P3 = '1.3'; |
| 33 | |
| 34 | /** @var string */ |
| 35 | private $linkId = null; |
| 36 | |
| 37 | /** @var string */ |
| 38 | private $version = self::VERSION_LTI_1P1; |
| 39 | |
| 40 | public function __construct(LtiUserInterface $user, array $contexts = []) |
| 41 | { |
| 42 | parent::__construct($user, $contexts); |
| 43 | } |
| 44 | |
| 45 | public static function fromVersion1p3(LtiUserInterface $user, array $contexts = []): self |
| 46 | { |
| 47 | $session = new self($user, $contexts); |
| 48 | |
| 49 | $session->version = self::VERSION_LTI_1P3; |
| 50 | |
| 51 | return $session; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Override the default label construction |
| 56 | * (non-PHPdoc) |
| 57 | * @see common_session_DefaultSession::getUserLabel() |
| 58 | * |
| 59 | * @throws LtiVariableMissingException |
| 60 | */ |
| 61 | public function getUserLabel() |
| 62 | { |
| 63 | if ($this->getLaunchData()->hasVariable(LtiLaunchData::LIS_PERSON_NAME_FULL)) { |
| 64 | return $this->getLaunchData()->getUserFullName(); |
| 65 | } else { |
| 66 | $parts = []; |
| 67 | if ($this->getLaunchData()->hasVariable(LtiLaunchData::LIS_PERSON_NAME_GIVEN)) { |
| 68 | $parts[] = $this->getLaunchData()->getUserGivenName(); |
| 69 | } |
| 70 | if ($this->getLaunchData()->hasVariable(LtiLaunchData::LIS_PERSON_NAME_FAMILY)) { |
| 71 | $parts[] = $this->getLaunchData()->getUserFamilyName(); |
| 72 | } |
| 73 | return empty($parts) ? __('user') : implode(' ', $parts); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the data that was transmitted during launch |
| 79 | * |
| 80 | * @return LtiLaunchData |
| 81 | */ |
| 82 | public function getLaunchData() |
| 83 | { |
| 84 | return $this->getUser()->getLaunchData(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns a resource representing the incoming link |
| 89 | * |
| 90 | * @throws LtiVariableMissingException |
| 91 | * @throws common_exception_Error |
| 92 | */ |
| 93 | public function getLtiLinkResource(): core_kernel_classes_Resource |
| 94 | { |
| 95 | if ($this->linkId === null) { |
| 96 | /** @var LinkService $service */ |
| 97 | $service = $this->getServiceLocator()->get(LinkService::SERVICE_ID); |
| 98 | |
| 99 | $this->linkId = $service->getLinkId( |
| 100 | $this->getLtiConsumer(), |
| 101 | $this->getLaunchData()->getResourceLinkID() |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | return new core_kernel_classes_Resource($this->linkId); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the consumer based on the LTI version |
| 110 | * |
| 111 | * @throws LtiVariableMissingException |
| 112 | */ |
| 113 | private function getLtiConsumer(): string |
| 114 | { |
| 115 | if ($this->version === self::VERSION_LTI_1P1) { |
| 116 | return $this->getLaunchData() |
| 117 | ->getLtiConsumer() |
| 118 | ->getUri(); |
| 119 | } |
| 120 | |
| 121 | return (string)$this->getLaunchData() |
| 122 | ->getVariable(LtiLaunchData::TOOL_CONSUMER_INSTANCE_ID); |
| 123 | } |
| 124 | } |