From 1cd530edaba1405e6ef8954251825397d5e78059 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Fri, 12 Oct 2018 13:54:38 -0500 Subject: [PATCH] Ims LTI avoid duplicate course tool - refs BT#13469 --- plugin/ims_lti/ImsLtiPlugin.php | 76 +++++++++++++++++++++++---------- plugin/ims_lti/item_return.php | 40 +++++++++++------ 2 files changed, 82 insertions(+), 34 deletions(-) diff --git a/plugin/ims_lti/ImsLtiPlugin.php b/plugin/ims_lti/ImsLtiPlugin.php index 0a3591e982..8adc68de9f 100644 --- a/plugin/ims_lti/ImsLtiPlugin.php +++ b/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 - ->setCourse($course) - ->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 */ diff --git a/plugin/ims_lti/item_return.php b/plugin/ims_lti/item_return.php index 27005f9d50..d778d240e9 100644 --- a/plugin/ims_lti/item_return.php +++ b/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;