form.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /* For license terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Course;
  4. use Chamilo\CoreBundle\Entity\Session;
  5. use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
  6. use Chamilo\UserBundle\Entity\User;
  7. require_once __DIR__.'/../../main/inc/global.inc.php';
  8. api_protect_course_script(false);
  9. api_block_anonymous_users(false);
  10. $em = Database::getManager();
  11. /** @var ImsLtiTool $tool */
  12. $tool = isset($_GET['id'])
  13. ? $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', (int) $_GET['id'])
  14. : null;
  15. if (!$tool) {
  16. api_not_allowed(true);
  17. }
  18. /** @var ImsLtiPlugin $imsLtiPlugin */
  19. $imsLtiPlugin = ImsLtiPlugin::create();
  20. /** @var Session $session */
  21. $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
  22. /** @var Course $course */
  23. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  24. /** @var User $user */
  25. $user = $em->find('ChamiloUserBundle:User', api_get_user_id());
  26. $pluginPath = api_get_path(WEB_PLUGIN_PATH).'ims_lti/';
  27. $toolUserId = ImsLtiPlugin::generateToolUserId($user->getId());
  28. $platformDomain = str_replace(['https://', 'http://'], '', api_get_setting('InstitutionUrl'));
  29. $params = [];
  30. $params['lti_version'] = 'LTI-1p0';
  31. if ($tool->isActiveDeepLinking()) {
  32. $params['lti_message_type'] = 'ContentItemSelectionRequest';
  33. $params['content_item_return_url'] = $pluginPath.'item_return.php';
  34. $params['accept_media_types'] = '*/*';
  35. $params['accept_presentation_document_targets'] = 'iframe';
  36. //$params['accept_unsigned'];
  37. //$params['accept_multiple'];
  38. //$params['accept_copy_advice'];
  39. //$params['auto_create']';
  40. $params['title'] = $tool->getName();
  41. $params['text'] = $tool->getDescription();
  42. $params['data'] = 'tool:'.$tool->getId();
  43. } else {
  44. $params['lti_message_type'] = 'basic-lti-launch-request';
  45. $params['resource_link_id'] = $tool->getId();
  46. $params['resource_link_title'] = $tool->getName();
  47. $params['resource_link_description'] = $tool->getDescription();
  48. $toolEval = $tool->getGradebookEval();
  49. if (!empty($toolEval)) {
  50. $params['lis_result_sourcedid'] = json_encode(
  51. ['e' => $toolEval->getId(), 'u' => $user->getId(), 'l' => uniqid(), 'lt' => time()]
  52. );
  53. $params['lis_outcome_service_url'] = api_get_path(WEB_PATH).'lti/os';
  54. $params['lis_person_sourcedid'] = "$platformDomain:$toolUserId";
  55. $params['lis_course_section_sourcedid'] = "$platformDomain:".$course->getId();
  56. if ($session) {
  57. $params['lis_course_section_sourcedid'] .= ':'.$session->getId();
  58. }
  59. }
  60. }
  61. $params['user_id'] = $toolUserId;
  62. if ($tool->isSharingPicture()) {
  63. $params['user_image'] = UserManager::getUserPicture($user->getId());
  64. }
  65. $params['roles'] = ImsLtiPlugin::getUserRoles($user);
  66. if ($tool->isSharingName()) {
  67. $params['lis_person_name_given'] = $user->getFirstname();
  68. $params['lis_person_name_family'] = $user->getLastname();
  69. $params['lis_person_name_full'] = $user->getFirstname().' '.$user->getLastname();
  70. }
  71. if ($tool->isSharingEmail()) {
  72. $params['lis_person_contact_email_primary'] = $user->getEmail();
  73. }
  74. if (DRH === $user->getStatus()) {
  75. $scopeMentor = ImsLtiPlugin::getRoleScopeMentor($user);
  76. if (!empty($scopeMentor)) {
  77. $params['role_scope_mentor'] = $scopeMentor;
  78. }
  79. }
  80. $params['context_id'] = $course->getId();
  81. $params['context_type'] = 'CourseSection';
  82. $params['context_label'] = $course->getCode();
  83. $params['context_title'] = $course->getTitle();
  84. $params['launch_presentation_locale'] = api_get_language_isocode();
  85. $params['launch_presentation_document_target'] = 'iframe';
  86. $params['tool_consumer_info_product_family_code'] = 'Chamilo LMS';
  87. $params['tool_consumer_info_version'] = api_get_version();
  88. $params['tool_consumer_instance_guid'] = $platformDomain;
  89. $params['tool_consumer_instance_name'] = api_get_setting('siteName');
  90. $params['tool_consumer_instance_url'] = api_get_path(WEB_PATH);
  91. $params['tool_consumer_instance_contact_email'] = api_get_setting('emailAdministrator');
  92. $params['oauth_callback'] = 'about:blank';
  93. $customParams = $tool->parseCustomParams();
  94. $imsLtiPlugin->trimParams($customParams);
  95. $substitutables = ImsLti::getSubstitutableParams($user, $course, $session);
  96. $variables = array_keys($substitutables);
  97. foreach ($customParams as $customKey => $customValue) {
  98. if (in_array($customValue, $variables)) {
  99. $val = $substitutables[$customValue];
  100. if (is_array($val)) {
  101. $val = current($val);
  102. if (array_key_exists($val, $params)) {
  103. $customParams[$customKey] = $params[$val];
  104. continue;
  105. } else {
  106. $val = false;
  107. }
  108. }
  109. if (false === $val) {
  110. $customParams[$customKey] = $customValue;
  111. continue;
  112. }
  113. $customParams[$customKey] = $substitutables[$customValue];
  114. }
  115. }
  116. $params += $customParams;
  117. $imsLtiPlugin->trimParams($params);
  118. if (!empty($tool->getConsumerKey()) && !empty($tool->getSharedSecret())) {
  119. $consumer = new OAuthConsumer(
  120. $tool->getConsumerKey(),
  121. $tool->getSharedSecret(),
  122. null
  123. );
  124. $hmacMethod = new OAuthSignatureMethod_HMAC_SHA1();
  125. $request = OAuthRequest::from_consumer_and_token(
  126. $consumer,
  127. '',
  128. 'POST',
  129. $tool->getLaunchUrl(),
  130. $params
  131. );
  132. $request->sign_request($hmacMethod, $consumer, '');
  133. $params = $request->get_parameters();
  134. }
  135. $imsLtiPlugin->removeUrlParamsFromLaunchParams($tool, $params);
  136. ?>
  137. <!DOCTYPE html>
  138. <html>
  139. <head>
  140. <title>title</title>
  141. </head>
  142. <body>
  143. <form action="<?php echo $tool->getLaunchUrl() ?>" name="ltiLaunchForm" method="post"
  144. encType="application/x-www-form-urlencoded">
  145. <?php foreach ($params as $key => $value) { ?>
  146. <input type="hidden" name="<?php echo $key ?>" value="<?php echo htmlspecialchars($value) ?>">
  147. <?php } ?>
  148. </form>
  149. <script>document.ltiLaunchForm.submit();</script>
  150. </body>
  151. </html>