Browse Source

Ims LTI avoid duplicate course tool - refs BT#13469

Angel Fernando Quiroz Campos 6 years ago
parent
commit
21fa358970
2 changed files with 82 additions and 34 deletions
  1. 54 22
      plugin/ims_lti/ImsLtiPlugin.php
  2. 28 12
      plugin/ims_lti/item_return.php

+ 54 - 22
plugin/ims_lti/ImsLtiPlugin.php

@@ -190,37 +190,69 @@ class ImsLtiPlugin extends Plugin
     }
 
     /**
-     * Add the course tool
-     * @param Course $course
-     * @param ImsLtiTool $tool
+     * @param Course     $course
+     * @param ImsLtiTool $ltiTool
+     *
+     * @return CTool
+     */
+    public function findCourseToolByLink(Course $course, ImsLtiTool $ltiTool)
+    {
+        $em = Database::getManager();
+        $toolRepo = $em->getRepository('ChamiloCourseBundle:CTool');
+
+        /** @var CTool $cTool */
+        $cTool = $toolRepo->findOneBy(
+            [
+                'cId' => $course,
+                'link' => self::generateToolLink($ltiTool)
+            ]
+        );
+
+        return $cTool;
+    }
+
+    /**
+     * @param CTool      $courseTool
+     * @param ImsLtiTool $ltiTool
+     *
      * @throws \Doctrine\ORM\OptimisticLockException
      */
-    public function addCourseTool(Course $course, ImsLtiTool $tool)
+    public function updateCourseTool(CTool $courseTool, ImsLtiTool $ltiTool)
     {
         $em = Database::getManager();
-        $cTool = new CTool();
-        $cTool
-            ->setCId($course->getId())
-            ->setName($tool->getName())
-            ->setLink($this->get_name().'/start.php?'.http_build_query(['id' => $tool->getId()]))
-            ->setImage($this->get_name().'.png')
-            ->setVisibility(1)
-            ->setAdmin(0)
-            ->setAddress('squaregray.gif')
-            ->setAddedTool('NO')
-            ->setTarget('_self')
-            ->setCategory('plugin')
-            ->setSessionId(0);
-
-        $em->persist($cTool);
-        $em->flush();
 
-        $cTool->setId($cTool->getIid());
+        $courseTool->setName($ltiTool->getName());
 
-        $em->persist($cTool);
+        $em->persist($courseTool);
         $em->flush();
     }
 
+    /**
+     * @param ImsLtiTool $tool
+     *
+     * @return string
+     */
+    private static function generateToolLink(ImsLtiTool $tool)
+    {
+        return  'ims_lti/start.php?id='.$tool->getId();
+    }
+
+    /**
+     * Add the course tool
+     *
+     * @param Course     $course
+     * @param ImsLtiTool $tool
+     */
+    public function addCourseTool(Course $course, ImsLtiTool $tool)
+    {
+        $this->createLinkToCourseTool(
+            $tool->getName(),
+            $course->getId(),
+            null,
+            self::generateToolLink($tool)
+        );
+    }
+
     /**
      * @return string
      */

+ 28 - 12
plugin/ims_lti/item_return.php

@@ -18,6 +18,7 @@ $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 */
@@ -33,28 +34,43 @@ $contentItems = json_decode($_POST['content_items'], true);
 $contentItems = $contentItems['@graph'];
 
 foreach ($contentItems as $contentItem) {
-    /** @var ImsLtiTool $newTool */
-    $newTool = clone $ltiTool;
-
     switch ($contentItem['@type']) {
         case 'LtiLinkItem':
-            $newTool
+            $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
-                )
-                ->setLaunchUrl(
-                    !empty($contentItem['url']) ? $contentItem['url'] : $ltiTool->getLaunchUrl()
-                )
-                ->setIsGlobal(false)
-                ->setActiveDeepLinking(false);
+                );
 
-            $em->persist($newTool);
+            $em->persist($newLtiTool);
             $em->flush();
 
-            $plugin->addCourseTool($course, $newTool);
+            $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;