Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
31.25% |
10 / 32 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LtiUtils | |
31.25% |
10 / 32 |
|
0.00% |
0 / 3 |
67.92 | |
0.00% |
0 / 1 |
| mapLTIRole2TaoRole | |
23.81% |
5 / 21 |
|
0.00% |
0 / 1 |
36.31 | |||
| mapTaoRole2LTIRoles | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| mapCode2InterfaceLanguage | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
| 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 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoLti\models\classes; |
| 23 | |
| 24 | use common_Exception; |
| 25 | use common_exception_Error; |
| 26 | use common_Logger; |
| 27 | use core_kernel_classes_Class; |
| 28 | use core_kernel_classes_Resource; |
| 29 | use tao_models_classes_LanguageService; |
| 30 | |
| 31 | /** |
| 32 | * |
| 33 | * @author joel.bout, <joel@taotesting.com> |
| 34 | * |
| 35 | */ |
| 36 | class LtiUtils |
| 37 | { |
| 38 | public const LIS_CONTEXT_ROLE_NAMESPACE = 'urn:lti:role:ims/lis/'; |
| 39 | |
| 40 | /** |
| 41 | * Maps a fuly qualified or abbreviated lti role |
| 42 | * to an existing tao role |
| 43 | * |
| 44 | * @param string $role |
| 45 | * @throws common_Exception |
| 46 | * @throws common_exception_Error |
| 47 | * @return core_kernel_classes_Resource the tao role or null |
| 48 | */ |
| 49 | public static function mapLTIRole2TaoRole($role) |
| 50 | { |
| 51 | $taoRole = null; |
| 52 | if (filter_var($role, FILTER_VALIDATE_URL)) { |
| 53 | // url found |
| 54 | $taoRole = new core_kernel_classes_Resource($role); |
| 55 | } else { |
| 56 | // if not fully qualified prepend LIS context role NS |
| 57 | if (strtolower(substr($role, 0, 4)) !== 'urn:') { |
| 58 | $role = self::LIS_CONTEXT_ROLE_NAMESPACE . $role; |
| 59 | } |
| 60 | list($prefix, $nid, $nss) = explode(':', $role, 3); |
| 61 | if ($nid != 'lti') { |
| 62 | common_Logger::w('Non LTI URN ' . $role . ' passed via LTI'); |
| 63 | } |
| 64 | $urn = 'urn:' . strtolower($nid) . ':' . $nss; |
| 65 | |
| 66 | // search for fitting role |
| 67 | $class = new core_kernel_classes_Class(LtiRoles::CLASS_URI); |
| 68 | $cand = $class->searchInstances([ |
| 69 | LtiRoles::PROPERTY_URN => $urn |
| 70 | ]); |
| 71 | if (count($cand) > 1) { |
| 72 | throw new common_exception_Error('Multiple instances share the URN ' . $urn); |
| 73 | } |
| 74 | if (count($cand) == 1) { |
| 75 | $taoRole = current($cand); |
| 76 | } else { |
| 77 | common_Logger::w('Unknown LTI role with urn: ' . $urn); |
| 78 | } |
| 79 | } |
| 80 | if (!is_null($taoRole) && $taoRole->exists()) { |
| 81 | return $taoRole->getUri(); |
| 82 | } else { |
| 83 | return null; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Adds the LTI roles to the tao roles |
| 89 | * |
| 90 | * @param string $roleUri |
| 91 | * @return array |
| 92 | */ |
| 93 | public static function mapTaoRole2LTIRoles($roleUri) |
| 94 | { |
| 95 | $roles = [$roleUri]; |
| 96 | if ($roleUri == 'http://www.tao.lu/Ontologies/TAO.rdf#DeliveryRole') { |
| 97 | $roles[] = 'http://www.imsglobal.org/imspurl/lis/v1/vocab/membership#Learner'; |
| 98 | } |
| 99 | |
| 100 | return $roles; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the tao language code that corresponds to the code provided |
| 105 | * not yet implemented, will always use default |
| 106 | * |
| 107 | * @param string $code |
| 108 | * |
| 109 | * @return string |
| 110 | * @throws common_exception_Error |
| 111 | */ |
| 112 | public static function mapCode2InterfaceLanguage($code) |
| 113 | { |
| 114 | if (!empty($code)) { |
| 115 | $languageService = tao_models_classes_LanguageService::singleton(); |
| 116 | $usage = new core_kernel_classes_Resource(tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_GUI); |
| 117 | if ($languageService->isLanguageAvailable($code, $usage)) { |
| 118 | return $code; |
| 119 | } |
| 120 | \common_Logger::d('[Fallback] The provided launch language is unavailable: ' . $code); |
| 121 | } |
| 122 | |
| 123 | return DEFAULT_LANG; |
| 124 | } |
| 125 | } |