chamilo /
chamilo-lms
| 1 | <?php |
||
| 2 | |||
| 3 | /* For licensing terms, see /license.txt */ |
||
| 4 | |||
| 5 | use Chamilo\PluginBundle\Entity\XApi\Cmi5Item; |
||
| 6 | use Symfony\Component\HttpFoundation\Request as HttpRequest; |
||
|
0 ignored issues
–
show
|
|||
| 7 | use Xabbuh\XApi\Model\Account; |
||
| 8 | use Xabbuh\XApi\Model\Activity; |
||
| 9 | use Xabbuh\XApi\Model\Agent; |
||
| 10 | use Xabbuh\XApi\Model\Context; |
||
| 11 | use Xabbuh\XApi\Model\Definition; |
||
| 12 | use Xabbuh\XApi\Model\DocumentData; |
||
| 13 | use Xabbuh\XApi\Model\InverseFunctionalIdentifier; |
||
| 14 | use Xabbuh\XApi\Model\IRI; |
||
| 15 | use Xabbuh\XApi\Model\IRL; |
||
| 16 | use Xabbuh\XApi\Model\LanguageMap; |
||
| 17 | use Xabbuh\XApi\Model\State; |
||
| 18 | use Xabbuh\XApi\Model\StateDocument; |
||
| 19 | use Xabbuh\XApi\Model\Statement; |
||
| 20 | use Xabbuh\XApi\Model\StatementId; |
||
| 21 | use Xabbuh\XApi\Model\Uuid; |
||
| 22 | use Xabbuh\XApi\Model\Verb; |
||
| 23 | |||
| 24 | require_once __DIR__.'/../../../main/inc/global.inc.php'; |
||
| 25 | |||
| 26 | api_protect_course_script(true); |
||
| 27 | api_block_anonymous_users(); |
||
| 28 | |||
| 29 | $request = HttpRequest::createFromGlobals(); |
||
| 30 | |||
| 31 | $em = Database::getManager(); |
||
| 32 | |||
| 33 | $item = $em->find(Cmi5Item::class, $request->query->getInt('id')); |
||
| 34 | $toolLaunch = $item->getTool(); |
||
| 35 | |||
| 36 | if ($toolLaunch->getId() !== $request->query->getInt('tool')) { |
||
| 37 | api_not_allowed( |
||
| 38 | false, |
||
| 39 | Display::return_message(get_lang('NotAllwed'), 'error') |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | $plugin = XApiPlugin::create(); |
||
| 44 | $user = api_get_user_entity(api_get_user_id()); |
||
| 45 | $nowDate = api_get_utc_datetime(null, false, true)->format('c'); |
||
| 46 | |||
| 47 | $registration = (string) Uuid::uuid4(); |
||
| 48 | $actor = new Agent( |
||
| 49 | InverseFunctionalIdentifier::withAccount( |
||
| 50 | new Account( |
||
| 51 | $user->getCompleteName(), |
||
| 52 | IRL::fromString(api_get_path(WEB_PATH)) |
||
| 53 | ) |
||
| 54 | ), |
||
| 55 | $user->getCompleteName() |
||
| 56 | ); |
||
| 57 | $verb = new Verb( |
||
| 58 | IRI::fromString('http://adlnet.gov/expapi/verbs/launched'), |
||
| 59 | LanguageMap::create($plugin->getLangMap('Launched')) |
||
| 60 | ); |
||
| 61 | $customActivityId = $plugin->generateIri($item->getId(), 'cmi5_item'); |
||
| 62 | |||
| 63 | $activity = new Activity( |
||
| 64 | $customActivityId, |
||
| 65 | new Definition( |
||
| 66 | LanguageMap::create($item->getTitle()), |
||
| 67 | LanguageMap::create($item->getDescription()), |
||
| 68 | IRI::fromString($item->getIdentifier()) |
||
| 69 | ) |
||
| 70 | ); |
||
| 71 | |||
| 72 | $context = (new Context()) |
||
| 73 | ->withPlatform( |
||
| 74 | api_get_setting('Institution').' - '.api_get_setting('siteName') |
||
| 75 | ) |
||
| 76 | ->withLanguage(api_get_language_isocode()) |
||
| 77 | ->withRegistration($registration); |
||
| 78 | |||
| 79 | $statementUuid = Uuid::uuid5( |
||
| 80 | $plugin->get(XApiPlugin::SETTING_UUID_NAMESPACE), |
||
| 81 | "cmi5_item/{$item->getId()}" |
||
| 82 | ); |
||
| 83 | |||
| 84 | $statement = new Statement( |
||
| 85 | StatementId::fromUuid($statementUuid), |
||
| 86 | $actor, |
||
| 87 | $verb, |
||
| 88 | $activity, |
||
| 89 | null, |
||
| 90 | null, |
||
| 91 | api_get_utc_datetime(null, false, true), |
||
| 92 | null, |
||
| 93 | $context |
||
| 94 | ); |
||
| 95 | |||
| 96 | $statementClient = XApiPlugin::create()->getXApiStatementClient(); |
||
| 97 | |||
| 98 | //try { |
||
| 99 | // $statementClient->storeStatement($statement); |
||
| 100 | //} catch (ConflictException $e) { |
||
| 101 | // echo Display::return_message($e->getMessage(), 'error'); |
||
| 102 | // |
||
| 103 | // exit; |
||
| 104 | //} catch (XApiException $e) { |
||
| 105 | // echo Display::return_message($e->getMessage(), 'error'); |
||
| 106 | // |
||
| 107 | // exit; |
||
| 108 | //} |
||
| 109 | |||
| 110 | $viewSessionId = (string) Uuid::uuid4(); |
||
| 111 | |||
| 112 | $state = new State( |
||
| 113 | $activity, |
||
| 114 | $actor, |
||
| 115 | 'LMS.LaunchData', |
||
| 116 | (string) $registration |
||
| 117 | ); |
||
| 118 | |||
| 119 | $documentDataData = []; |
||
| 120 | $documentDataData['contentTemplate'] = [ |
||
| 121 | 'extensions' => [ |
||
| 122 | 'https://w3id.org/xapi/cmi5/context/extensions/sessionid' => $viewSessionId, |
||
| 123 | ], |
||
| 124 | ]; |
||
| 125 | $documentDataData['launchMode'] = 'Normal'; |
||
| 126 | $documentDataData['launchMethod'] = $item->getLaunchMethod(); |
||
| 127 | |||
| 128 | if ($item->getLaunchParameters()) { |
||
| 129 | $documentDataData['launchParameteres'] = $item->getLaunchParameters(); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($item->getMasteryScore()) { |
||
| 133 | $documentDataData['masteryScore'] = $item->getMasteryScore(); |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($item->getEntitlementKey()) { |
||
| 137 | $documentDataData['entitlementKey'] = [ |
||
| 138 | 'courseStructure' => $item->getEntitlementKey(), |
||
| 139 | ]; |
||
| 140 | } |
||
| 141 | |||
| 142 | $documentData = new DocumentData($documentDataData); |
||
| 143 | |||
| 144 | try { |
||
| 145 | $plugin |
||
| 146 | ->getXApiStateClient() |
||
| 147 | ->createOrReplaceDocument( |
||
| 148 | new StateDocument($state, $documentData) |
||
| 149 | ); |
||
| 150 | } catch (Exception $exception) { |
||
| 151 | echo Display::return_message($exception->getMessage(), 'error'); |
||
| 152 | |||
| 153 | exit; |
||
| 154 | } |
||
| 155 | |||
| 156 | $launchUrl = $plugin->generateLaunchUrl( |
||
| 157 | 'cmi5', |
||
| 158 | $item->getUrl(), |
||
| 159 | $customActivityId->getValue(), |
||
| 160 | $actor, |
||
| 161 | $registration, |
||
| 162 | $toolLaunch->getLrsUrl(), |
||
| 163 | $toolLaunch->getLrsAuthUsername(), |
||
| 164 | $toolLaunch->getLrsAuthPassword(), |
||
| 165 | $viewSessionId |
||
| 166 | ); |
||
| 167 | |||
| 168 | if ('OwnWindow' === $item->getLaunchMethod()) { |
||
| 169 | Display::display_reduced_header(); |
||
| 170 | |||
| 171 | echo '<br><p class="text-center">'; |
||
| 172 | echo Display::toolbarButton( |
||
| 173 | $plugin->get_lang('LaunchNewAttempt'), |
||
| 174 | $launchUrl, |
||
| 175 | 'external-link fa-fw', |
||
| 176 | 'success', |
||
| 177 | [ |
||
| 178 | 'target' => '_blank', |
||
| 179 | ] |
||
| 180 | ); |
||
| 181 | echo '</div>'; |
||
| 182 | |||
| 183 | Display::display_reduced_footer(); |
||
| 184 | |||
| 185 | exit; |
||
| 186 | } |
||
| 187 | |||
| 188 | header("Location: $launchUrl"); |
||
| 189 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: