Ver código fonte

Ims LTI improve redirection when add lti tool - refs BT#13469

Angel Fernando Quiroz Campos 6 anos atrás
pai
commit
0318d42d14
2 arquivos alterados com 77 adições e 44 exclusões
  1. 51 0
      plugin/ims_lti/ImsLtiPlugin.php
  2. 26 44
      plugin/ims_lti/item_return.php

+ 51 - 0
plugin/ims_lti/ImsLtiPlugin.php

@@ -342,4 +342,55 @@ class ImsLtiPlugin extends Plugin
 
         return implode(',', $scope);
     }
+
+    /**
+     * @param array      $contentItem
+     * @param ImsLtiTool $ltiTool
+     * @param Course     $course
+     *
+     * @throws \Doctrine\ORM\OptimisticLockException
+     */
+    public function saveItemAsLtiLink(array $contentItem, ImsLtiTool $ltiTool, Course $course)
+    {
+        $em = Database::getManager();
+        $ltiToolRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
+
+        $url = empty($contentItem['url']) ? $ltiTool->getLaunchUrl() : $contentItem['url'];
+
+        /** @var ImsLtiTool $newLtiTool */
+        $newLtiTool = $ltiToolRepo->findOneBy(['launchUrl' => $url, 'isGlobal' => false]);
+
+        if (empty($newLtiTool)) {
+            $newLtiTool = new ImsLtiTool();
+            $newLtiTool
+                ->setLaunchUrl($url)
+                ->setConsumerKey(
+                    $ltiTool->getConsumerKey()
+                )
+                ->setSharedSecret(
+                    $ltiTool->getSharedSecret()
+                );
+        }
+
+        $newLtiTool
+            ->setName(
+                !empty($contentItem['title']) ? $contentItem['title'] : $ltiTool->getName()
+            )
+            ->setDescription(
+                !empty($contentItem['text']) ? $contentItem['text'] : null
+            );
+
+        $em->persist($newLtiTool);
+        $em->flush();
+
+        $courseTool = $this->findCourseToolByLink($course, $newLtiTool);
+
+        if ($courseTool) {
+            $this->updateCourseTool($courseTool, $newLtiTool);
+
+            return;
+        }
+
+        $this->addCourseTool($course, $newLtiTool);
+    }
 }

+ 26 - 44
plugin/ims_lti/item_return.php

@@ -1,7 +1,6 @@
 <?php
 /* For license terms, see /license.txt */
 
-use Chamilo\CoreBundle\Entity\Session;
 use Chamilo\CoreBundle\Entity\Course;
 use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
 
@@ -18,11 +17,8 @@ $toolId = str_replace('tool:', '', $_POST['data']);
 
 $plugin = ImsLtiPlugin::create();
 $em = Database::getManager();
-$ltiToolRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
 /** @var Course $course */
 $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
-/** @var Session|null $session */
-$session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
 /** @var ImsLtiTool|null $ltiTool */
 $ltiTool = $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', $toolId);
 
@@ -34,45 +30,31 @@ $contentItems = json_decode($_POST['content_items'], true);
 $contentItems = $contentItems['@graph'];
 
 foreach ($contentItems as $contentItem) {
-    switch ($contentItem['@type']) {
-        case 'LtiLinkItem':
-            $url = empty($contentItem['url']) ? $ltiTool->getLaunchUrl() : $contentItem['url'];
-
-            /** @var ImsLtiTool $newLtiTool */
-            $newLtiTool = $ltiToolRepo->findOneBy(['launchUrl' => $url, 'isGlobal' => false]);
-
-            if (empty($newLtiTool)) {
-                $newLtiTool = new ImsLtiTool();
-                $newLtiTool
-                    ->setLaunchUrl($url)
-                    ->setConsumerKey(
-                        $ltiTool->getConsumerKey()
-                    )
-                    ->setSharedSecret(
-                        $ltiTool->getSharedSecret()
-                    );
-            }
-
-            $newLtiTool
-                ->setName(
-                    !empty($contentItem['title']) ? $contentItem['title'] : $ltiTool->getName()
-                )
-                ->setDescription(
-                    !empty($contentItem['text']) ? $contentItem['text'] : null
-                );
-
-            $em->persist($newLtiTool);
-            $em->flush();
-
-            $courseTool = $plugin->findCourseToolByLink($course, $newLtiTool);
-
-            if ($courseTool) {
-                $plugin->updateCourseTool($courseTool, $newLtiTool);
-            } else {
-                $plugin->addCourseTool($course, $newLtiTool);
-            }
-
-            echo Display::return_message($plugin->get_lang('ToolAdded'), 'success');
-            break;
+    if ('LtiLinkItem' === $contentItem['@type']) {
+        if ('application/vnd.ims.lti.v1.ltilink' === $contentItem['mediaType']) {
+            $plugin->saveItemAsLtiLink($contentItem, $ltiTool, $course);
+
+            Display::addFlash(
+                Display::return_message($plugin->get_lang('ToolAdded'), 'success')
+            );
+        }
     }
 }
+
+$currentUrl = api_get_path(WEB_PLUGIN_PATH).'ims_lti/start.php?id='.$ltiTool->getId();
+?>
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport"
+          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
+    <meta http-equiv="X-UA-Compatible" content="ie=edge">
+    <title>Document</title>
+</head>
+<body>
+    <script>
+        window.parent.location.href = '<?php echo $currentUrl ?>';
+    </script>
+</body>
+</html>