Browse Source

WIP LTI allow edit tools added in course - refs BT#13469

Angel Fernando Quiroz Campos 6 years ago
parent
commit
f3a3181b1c

+ 28 - 12
plugin/ims_lti/ImsLtiPlugin.php

@@ -1,18 +1,17 @@
 <?php
 /* For license terms, see /license.txt */
 
+use Chamilo\CoreBundle\Entity\Course;
 use Chamilo\CoreBundle\Entity\CourseRelUser;
-use Chamilo\CoreBundle\Entity\GradebookEvaluation;
 use Chamilo\CoreBundle\Entity\Session;
 use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
 use Chamilo\CourseBundle\Entity\CTool;
-use Chamilo\CoreBundle\Entity\Course;
+use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
 use Chamilo\UserBundle\Entity\User;
+use Doctrine\DBAL\DBALException;
 use Doctrine\DBAL\Schema\Schema;
 use Doctrine\DBAL\Types\Type;
-use Doctrine\DBAL\DBALException;
 use Symfony\Component\Filesystem\Filesystem;
-use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
 
 /**
  * Description of MsiLti
@@ -196,7 +195,7 @@ class ImsLtiPlugin extends Plugin
     {
         $button = Display::toolbarButton(
             $this->get_lang('ConfigureExternalTool'),
-            api_get_path(WEB_PLUGIN_PATH).'ims_lti/add.php?'.api_get_cidreq(),
+            api_get_path(WEB_PLUGIN_PATH).'ims_lti/configure.php?'.api_get_cidreq(),
             'cog',
             'primary'
         );
@@ -365,36 +364,36 @@ class ImsLtiPlugin extends Plugin
 
     /**
      * @param array      $contentItem
-     * @param ImsLtiTool $ltiTool
+     * @param ImsLtiTool $baseLtiTool
      * @param Course     $course
      *
      * @throws \Doctrine\ORM\OptimisticLockException
      */
-    public function saveItemAsLtiLink(array $contentItem, ImsLtiTool $ltiTool, Course $course)
+    public function saveItemAsLtiLink(array $contentItem, ImsLtiTool $baseLtiTool, Course $course)
     {
         $em = Database::getManager();
         $ltiToolRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
 
-        $url = empty($contentItem['url']) ? $ltiTool->getLaunchUrl() : $contentItem['url'];
+        $url = empty($contentItem['url']) ? $baseLtiTool->getLaunchUrl() : $contentItem['url'];
 
         /** @var ImsLtiTool $newLtiTool */
-        $newLtiTool = $ltiToolRepo->findOneBy(['launchUrl' => $url, 'isGlobal' => false]);
+        $newLtiTool = $ltiToolRepo->findOneBy(['launchUrl' => $url, 'isGlobal' => false, 'course' => $course]);
 
         if (empty($newLtiTool)) {
             $newLtiTool = new ImsLtiTool();
             $newLtiTool
                 ->setLaunchUrl($url)
                 ->setConsumerKey(
-                    $ltiTool->getConsumerKey()
+                    $baseLtiTool->getConsumerKey()
                 )
                 ->setSharedSecret(
-                    $ltiTool->getSharedSecret()
+                    $baseLtiTool->getSharedSecret()
                 );
         }
 
         $newLtiTool
             ->setName(
-                !empty($contentItem['title']) ? $contentItem['title'] : $ltiTool->getName()
+                !empty($contentItem['title']) ? $contentItem['title'] : $baseLtiTool->getName()
             )
             ->setDescription(
                 !empty($contentItem['text']) ? $contentItem['text'] : null
@@ -447,4 +446,21 @@ class ImsLtiPlugin extends Plugin
 
         return $response;
     }
+
+    /**
+     * @param int    $toolId
+     * @param Course $course
+     *
+     * @return bool
+     */
+    public static function existsToolInCourse($toolId, Course $course)
+    {
+        $em = Database::getManager();
+        $toolRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
+
+        /** @var ImsLtiTool $tool */
+        $tool = $toolRepo->findOneBy(['id' => $toolId, 'isGlobal' => false, 'course' => $course]);
+
+        return !empty($tool);
+    }
 }

+ 0 - 125
plugin/ims_lti/add.php

@@ -1,125 +0,0 @@
-<?php
-/* For license terms, see /license.txt */
-
-use Chamilo\CoreBundle\Entity\Course;
-use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
-
-require_once __DIR__.'/../../main/inc/global.inc.php';
-
-api_protect_course_script();
-api_protect_teacher_script();
-
-$plugin = ImsLtiPlugin::create();
-$em = Database::getManager();
-$toolsRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
-
-/** @var ImsLtiTool $baseTool */
-$baseTool = isset($_REQUEST['type']) ? $toolsRepo->find(intval($_REQUEST['type'])) : null;
-
-/** @var Course $course */
-$course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
-$globalTools = $toolsRepo->findBy(['isGlobal' => true]);
-
-if ($baseTool && !$baseTool->isGlobal()) {
-    Display::addFlash(
-        Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning')
-    );
-
-    header('Location: '.api_get_self().'?'.api_get_cidreq());
-    exit;
-}
-
-$form = new FormValidator('ims_lti_add_tool');
-$form->addHeader($plugin->get_lang('ToolSettings'));
-
-if ($baseTool) {
-    $form->addHtml('<p class="lead">'.Security::remove_XSS($baseTool->getDescription()).'</p>');
-}
-
-$form->addText('name', get_lang('Title'));
-
-if (!$baseTool) {
-    $form->addElement('url', 'url', $plugin->get_lang('LaunchUrl'));
-    $form->addText('consumer_key', $plugin->get_lang('ConsumerKey'), true);
-    $form->addText('shared_secret', $plugin->get_lang('SharedSecret'), true);
-    $form->addRule('url', get_lang('Required'), 'required');
-}
-
-$form->addButtonAdvancedSettings('lti_adv');
-$form->addHtml('<div id="lti_adv_options" style="display:none;">');
-$form->addTextarea('description', get_lang('Description'), ['rows' => 3]);
-
-if (!$baseTool) {
-    $form->addTextarea('custom_params', [$plugin->get_lang('CustomParams'), $plugin->get_lang('CustomParamsHelp')]);
-    $form->addCheckBox('deep_linking', $plugin->get_lang('SupportDeepLinking'), get_lang('Yes'));
-}
-
-if ($baseTool) {
-    $form->addHidden('type', $baseTool->getId());
-}
-
-$form->addHtml('</div>');
-
-$form->addButtonCreate($plugin->get_lang('AddExternalTool'));
-
-if ($form->validate()) {
-    $formValues = $form->getSubmitValues();
-    $tool = null;
-
-    if ($baseTool) {
-        $tool = clone $baseTool;
-    } else {
-        $tool = new ImsLtiTool();
-        $tool
-            ->setLaunchUrl($formValues['url'])
-            ->setConsumerKey($formValues['consumer_key'])
-            ->setSharedSecret($formValues['shared_secret'])
-            ->setCustomParams(
-                empty($formValues['custom_params']) ? null : $formValues['custom_params']
-            );
-    }
-
-    $tool
-        ->setName($formValues['name'])
-        ->setDescription(
-            empty($formValues['description']) ? null : $formValues['description']
-        )
-        ->setIsGlobal(false)
-        ->setCourse($course);
-    $em->persist($tool);
-    $em->flush();
-
-    $plugin->addCourseTool($course, $tool);
-
-    Display::addFlash(
-        Display::return_message($plugin->get_lang('ToolAdded'), 'success')
-    );
-
-    header('Location: '.api_get_course_url());
-    exit;
-}
-
-$categories = Category::load(null, null, $course->getCode());
-
-$template = new Template($plugin->get_lang('AddExternalTool'));
-$template->assign('type', $baseTool ? $baseTool->getId() : null);
-$template->assign('tools', $globalTools);
-$template->assign('form', $form->returnForm());
-
-$content = $template->fetch('ims_lti/view/add.tpl');
-
-$actions = Display::url(
-    Display::return_icon('add.png', $plugin->get_lang('AddExternalTool'), [], ICON_SIZE_MEDIUM),
-    api_get_self().'?'.api_get_cidreq()
-);
-
-if (!empty($categories)) {
-    $actions .= Display::url(
-        Display::return_icon('gradebook.png', $plugin->get_lang('AddToolToGradebook'), [], ICON_SIZE_MEDIUM),
-        './gradebook/add_eval.php?selectcat='.$categories[0]->get_id().'&'.api_get_cidreq()
-    );
-}
-
-$template->assign('actions', Display::toolbarAction('lti_toolbar', [$actions]));
-$template->assign('content', $content);
-$template->display_one_col_template();

+ 210 - 0
plugin/ims_lti/configure.php

@@ -0,0 +1,210 @@
+<?php
+/* For license terms, see /license.txt */
+
+use Chamilo\CoreBundle\Entity\Course;
+use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
+
+require_once __DIR__.'/../../main/inc/global.inc.php';
+
+api_protect_course_script();
+api_protect_teacher_script();
+
+$plugin = ImsLtiPlugin::create();
+$em = Database::getManager();
+$toolsRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
+
+/** @var ImsLtiTool $baseTool */
+$baseTool = isset($_REQUEST['type']) ? $toolsRepo->find(intval($_REQUEST['type'])) : null;
+$action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : 'add';
+
+/** @var Course $course */
+$course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
+$addedTools = $toolsRepo->findBy(['course' => $course]);
+$globalTools = $toolsRepo->findBy(['isGlobal' => true]);
+
+if ($baseTool && !$baseTool->isGlobal()) {
+    Display::addFlash(
+        Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning')
+    );
+
+    header('Location: '.api_get_self().'?'.api_get_cidreq());
+    exit;
+}
+
+switch ($action) {
+    case 'add':
+        $form = new FormValidator('ims_lti_add_tool');
+        $form->addHeader($plugin->get_lang('ToolSettings'));
+
+        if ($baseTool) {
+            $form->addHtml('<p class="lead">'.Security::remove_XSS($baseTool->getDescription()).'</p>');
+        }
+
+        $form->addText('name', get_lang('Name'));
+
+        if (!$baseTool) {
+            $form->addElement('url', 'url', $plugin->get_lang('LaunchUrl'));
+            $form->addText('consumer_key', $plugin->get_lang('ConsumerKey'), true);
+            $form->addText('shared_secret', $plugin->get_lang('SharedSecret'), true);
+            $form->addRule('url', get_lang('Required'), 'required');
+        }
+
+        $form->addButtonAdvancedSettings('lti_adv');
+        $form->addHtml('<div id="lti_adv_options" style="display:none;">');
+        $form->addTextarea('description', get_lang('Description'), ['rows' => 3]);
+
+        if (!$baseTool) {
+            $form->addTextarea(
+                'custom_params',
+                [$plugin->get_lang('CustomParams'), $plugin->get_lang('CustomParamsHelp')]
+            );
+            $form->addCheckBox('deep_linking', $plugin->get_lang('SupportDeepLinking'), get_lang('Yes'));
+        }
+
+        if ($baseTool) {
+            $form->addHidden('type', $baseTool->getId());
+        }
+
+        $form->addHtml('</div>');
+
+        $form->addButtonCreate($plugin->get_lang('AddExternalTool'));
+
+        if ($form->validate()) {
+            $formValues = $form->getSubmitValues();
+            $tool = null;
+
+            if ($baseTool) {
+                $tool = clone $baseTool;
+            } else {
+                $tool = new ImsLtiTool();
+                $tool
+                    ->setLaunchUrl($formValues['url'])
+                    ->setConsumerKey($formValues['consumer_key'])
+                    ->setSharedSecret($formValues['shared_secret'])
+                    ->setCustomParams(
+                        empty($formValues['custom_params']) ? null : $formValues['custom_params']
+                    );
+            }
+
+            $tool
+                ->setName($formValues['name'])
+                ->setDescription(
+                    empty($formValues['description']) ? null : $formValues['description']
+                )
+                ->setIsGlobal(false)
+                ->setCourse($course);
+
+            $em->persist($tool);
+            $em->flush();
+
+            $plugin->addCourseTool($course, $tool);
+
+            Display::addFlash(
+                Display::return_message($plugin->get_lang('ToolAdded'), 'success')
+            );
+
+            header('Location: '.api_get_self().'?'.api_get_cidreq());
+            exit;
+        }
+        break;
+    case 'edit':
+        $form = new FormValidator('ims_lti_edit_tool');
+        $form->addHeader($plugin->get_lang('ToolSettings'));
+
+        /** @var ImsLtiTool|null $tool */
+        $tool = null;
+
+        if (!empty($_REQUEST['id'])) {
+            $tool = $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', (int) $_REQUEST['id']);
+        }
+
+        if (empty($tool)) {
+            Display::addFlash(
+                Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning')
+            );
+
+            break;
+        }
+        
+        if (!ImsLtiPlugin::existsToolInCourse($tool->getId(), $course)) {
+            Display::addFlash(
+                Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning')
+            );
+
+            break;
+        }
+
+        $form->addText('name', get_lang('Title'));
+        $form->addButtonAdvancedSettings('lti_adv');
+        $form->addHtml('<div id="lti_adv_options" style="display:none;">');
+        $form->addTextarea('description', get_lang('Description'), ['rows' => 3]);
+        $form->addTextarea(
+            'custom_params',
+            [$plugin->get_lang('CustomParams'), $plugin->get_lang('CustomParamsHelp')]
+        );
+        $form->addHtml('</div>');
+        $form->addButtonUpdate($plugin->get_lang('EditExternalTool'));
+        $form->addHidden('id', $tool->getId());
+        $form->addHidden('action', 'edit');
+        $form->applyFilter('__ALL__', 'Security::remove_XSS');
+
+        if ($form->validate()) {
+            $formValues = $form->getSubmitValues();
+
+            $tool
+                ->setName($formValues['name'])
+                ->setDescription($formValues['description'])
+                ->setCustomParams(
+                    empty($formValues['custom_params']) ? null : $formValues['custom_params']
+                );
+
+            $em->persist($tool);
+            $em->flush();
+
+            $courseTool = $plugin->findCourseToolByLink($course, $tool);
+
+            if ($courseTool) {
+                $plugin->updateCourseTool($courseTool, $tool);
+            }
+
+            Display::addFlash(
+                Display::return_message($plugin->get_lang('ToolEdited'), 'success')
+            );
+
+            header('Location: '.api_get_self().'?'.api_get_cidreq());
+            exit;
+        }
+
+        $form->setDefaults([
+            'name' => $tool->getName(),
+            'description' => $tool->getDescription(),
+            'custom_params' => $tool->getCustomParams(),
+        ]);
+        break;
+}
+
+$categories = Category::load(null, null, $course->getCode());
+
+$template = new Template($plugin->get_lang('AddExternalTool'));
+$template->assign('type', $baseTool ? $baseTool->getId() : null);
+$template->assign('added_tools', $addedTools);
+$template->assign('global_tools', $globalTools);
+$template->assign('form', $form->returnForm());
+
+$content = $template->fetch('ims_lti/view/add.tpl');
+
+$actions = Display::url(
+    Display::return_icon('add.png', $plugin->get_lang('AddExternalTool'), [], ICON_SIZE_MEDIUM),
+    api_get_self().'?'.api_get_cidreq()
+);
+
+if (!empty($categories)) {
+    $actions .= Display::url(
+        Display::return_icon('gradebook.png', $plugin->get_lang('AddToolToGradebook'), [], ICON_SIZE_MEDIUM),
+        './gradebook/add_eval.php?selectcat='.$categories[0]->get_id().'&'.api_get_cidreq()
+    );
+}
+
+$template->assign('actions', Display::toolbarAction('lti_toolbar', [$actions]));
+$template->assign('content', $content);
+$template->display_one_col_template();

+ 5 - 2
plugin/ims_lti/create.php

@@ -13,6 +13,7 @@ $plugin = ImsLtiPlugin::create();
 $em = Database::getManager();
 
 $form = new FormValidator('ism_lti_create_tool');
+$form->addHeader($plugin->get_lang('ToolSettings'));
 $form->addText('name', get_lang('Name'));
 $form->addText('base_url', $plugin->get_lang('LaunchUrl'));
 $form->addText('consumer_key', $plugin->get_lang('ConsumerKey'));
@@ -52,11 +53,13 @@ if ($form->validate()) {
     exit;
 }
 
-$template = new Template($plugin->get_lang('AddExternalTool'));
+$pageTitle = $plugin->get_lang('AddExternalTool');
+
+$template = new Template($pageTitle);
 $template->assign('form', $form->returnForm());
 
 $content = $template->fetch('ims_lti/view/add.tpl');
 
-$template->assign('header', $plugin->get_title());
+$template->assign('header', $pageTitle);
 $template->assign('content', $content);
 $template->display_one_col_template();

+ 2 - 0
plugin/ims_lti/lang/english.php

@@ -29,3 +29,5 @@ $strings['ConfigureExternalTool'] = 'Configure external tools';
 $strings['SupportDeepLinking'] = 'Support Deep-Linking';
 $strings['ScoreNotSet'] = 'Score not set';
 $strings['ScoreForXUserIsYScore'] = 'Score for %s is %s';
+$strings['AddedTools'] = 'Added tools';
+$strings['ToolEdited'] = 'Tool edited';

+ 29 - 33
plugin/ims_lti/view/add.tpl

@@ -1,41 +1,37 @@
 <div class="row">
-    {% if tools|length %}
+    {% if global_tools|length or added_tools|length %}
         <div class="col-sm-3">
-            <h2>{{ 'AvailableTools'|get_plugin_lang('ImsLtiPlugin') }}</h2>
-            <ul class="nav nav-pills nav-stacked">
-                {% for tool in tools %}
-                    <li class="{{ type == tool.id ? 'active' : '' }}">
-                        {% if tool.isActiveDeepLinking %}
-                            <a href="{{ _p.web_plugin }}ims_lti/start.php?id={{ tool.id }}&{{ _p.web_cid_query }}">{{ tool.name }}</a>
-                        {% else %}
-                            <a href="{{ _p.web_self }}?type={{ tool.id }}&{{ _p.web_cid_query }}">{{ tool.name }}</a>
-                        {% endif %}
-                    </li>
-                {% endfor %}
-            </ul>
+            {% if added_tools|length %}
+                <h2>{{ 'AddedTools'|get_plugin_lang('ImsLtiPlugin') }}</h2>
+                <ul class="nav nav-pills nav-stacked">
+                    {% for tool in added_tools %}
+                        <li class="{{ type == tool.id ? 'active' : '' }}">
+                            <a href="{{ _p.web_plugin }}ims_lti/configure.php?action=edit&id={{ tool.id }}&{{ _p.web_cid_query }}">
+                                {{ tool.name }}
+                            </a>
+                        </li>
+                    {% endfor %}
+                </ul>
+            {% endif %}
+
+            {% if global_tools|length %}
+                <h2>{{ 'AvailableTools'|get_plugin_lang('ImsLtiPlugin') }}</h2>
+                <ul class="nav nav-pills nav-stacked">
+                    {% for tool in global_tools %}
+                        <li class="{{ type == tool.id ? 'active' : '' }}">
+                            {% if tool.isActiveDeepLinking %}
+                                <a href="{{ _p.web_plugin }}ims_lti/start.php?id={{ tool.id }}&{{ _p.web_cid_query }}">{{ tool.name }}</a>
+                            {% else %}
+                                <a href="{{ _p.web_self }}?type={{ tool.id }}&{{ _p.web_cid_query }}">{{ tool.name }}</a>
+                            {% endif %}
+                        </li>
+                    {% endfor %}
+                </ul>
+            {% endif %}
         </div>
     {% endif %}
 
-    <div class="{{ tools|length ? 'col-sm-9' : 'col-sm-12' }}">
-        {% if tools|length == 0 %}
-            <h2>{{ 'ToolSettings'|get_plugin_lang('ImsLtiPlugin') }}</h2>
-        {% endif %}
-
+    <div class="col-sm-9 {{ not global_tools|length or not added_tools|length ? 'col-md-offset-3' : '' }}">
         {{ form }}
     </div>
 </div>
-
-<script>
-    $(document).on('ready', function () {
-        $('select[name="type"]').on('change', function () {
-            var advancedOptionsEl = $('#show_advanced_options');
-            var type = parseInt($(this).val());
-
-            if (type > 0) {
-                advancedOptionsEl.hide();
-            } else {
-                advancedOptionsEl.show();
-            }
-        });
-    });
-</script>