Browse Source

WIP LTI using same form to add tool - refs BT#13469

Angel Fernando Quiroz Campos 6 years ago
parent
commit
da51a01084

+ 4 - 3
plugin/ims_lti/Entity/ImsLtiTool.php

@@ -102,7 +102,7 @@ class ImsLtiTool
     {
         $this->description = null;
         $this->customParams = null;
-        $this->isGlobal = false;
+        $this->isGlobal = true;
         $this->activeDeepLinking = false;
         $this->course = null;
         $this->gradebookEval =null;
@@ -236,7 +236,7 @@ class ImsLtiTool
      */
     public function isGlobal()
     {
-        return $this->isGlobal;
+        return $this->course === null;
     }
 
     /**
@@ -299,7 +299,7 @@ class ImsLtiTool
     }
 
     /**
-     * Set course.
+     * Set course. And set isGlobal according to the course or not.
      *
      * @param Course|null $course
      *
@@ -308,6 +308,7 @@ class ImsLtiTool
     public function setCourse(Course $course = null)
     {
         $this->course = $course;
+        $this->isGlobal = $course === null;
 
         return $this;
     }

+ 2 - 1
plugin/ims_lti/ImsLtiPlugin.php

@@ -105,6 +105,7 @@ class ImsLtiPlugin extends Plugin
      * Creates the plugin tables on database
      *
      * @return boolean
+     * @throws DBALException
      */
     private function createPluginTables()
     {
@@ -129,6 +130,7 @@ class ImsLtiPlugin extends Plugin
             $toolTable->addColumn('active_deep_linking', Type::BOOLEAN)->setDefault(false);
             $toolTable->addColumn('c_id', Type::INTEGER)->setNotnull(false);
             $toolTable->addColumn('gradebook_eval_id', Type::INTEGER)->setNotnull(false);
+            $toolTable->addColumn('privacy', Type::TEXT)->setNotnull(false)->setDefault(null);
             $toolTable->addForeignKeyConstraint(
                 'course',
                 ['c_id'],
@@ -154,7 +156,6 @@ class ImsLtiPlugin extends Plugin
             }
         }
 
-
         return true;
     }
 

+ 31 - 47
plugin/ims_lti/configure.php

@@ -33,66 +33,50 @@ if ($baseTool && !$baseTool->isGlobal()) {
 
 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'));
-        }
+        $form = new FrmAdd('ims_lti_add_tool', [], $baseTool);
+        $form->build();
 
         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 = new ImsLtiTool();
             $tool
                 ->setName($formValues['name'])
                 ->setDescription(
                     empty($formValues['description']) ? null : $formValues['description']
                 )
-                ->setIsGlobal(false)
-                ->setCourse($course);
+                ->setLaunchUrl(
+                    $baseTool ? $baseTool->getLaunchUrl() : $formValues['launch_url']
+                )
+                ->setConsumerKey(
+                    $baseTool ? $baseTool->getConsumerKey() : $formValues['consumer_key']
+                )
+                ->setSharedSecret(
+                    $baseTool ? $baseTool->getSharedSecret() : $formValues['shared_secret']
+                )
+                ->setCustomParams(
+                    empty($formValues['custom_params']) ? null : $formValues['custom_params']
+                )
+                ->setCourse($course)
+                ->setActiveDeepLinking(false)
+                ->setPrivacy(
+                    !empty($formValues['share_name']),
+                    !empty($formValues['share_email']),
+                    !empty($formValues['share_picture'])
+                );
+
+            if (null === $baseTool ||
+                ($baseTool && !$baseTool->isActiveDeepLinking())
+            ) {
+                $tool
+                    ->setActiveDeepLinking(
+                        !empty($formValues['deep_linking'])
+                    );
+            }
 
             $em->persist($tool);
             $em->flush();

+ 2 - 2
plugin/ims_lti/create.php

@@ -22,11 +22,11 @@ if ($form->validate()) {
     $externalTool
         ->setName($formValues['name'])
         ->setDescription($formValues['description'])
-        ->setLaunchUrl($formValues['base_url'])
+        ->setLaunchUrl($formValues['launch_url'])
         ->setConsumerKey($formValues['consumer_key'])
         ->setSharedSecret($formValues['shared_secret'])
         ->setCustomParams($formValues['custom_params'])
-        ->setIsGlobal(true)
+        ->setCourse(null)
         ->setActiveDeepLinking(
             isset($formValues['deep_linking'])
         )

+ 0 - 1
plugin/ims_lti/form.php

@@ -119,7 +119,6 @@ $result = $oauth->sign(array(
         'oauth_callback' => 'about:blank'
     )
 ));
-var_dump($params);die;
 ?>
 <!DOCTYPE html>
 <html>

+ 44 - 8
plugin/ims_lti/src/Form/FrmAdd.php

@@ -1,22 +1,33 @@
 <?php
 /* For licensing terms, see /license.txt */
 
+use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
+
 /**
  * Class FrmAdd.
  */
 class FrmAdd extends FormValidator
 {
+    /**
+     * @var ImsLtiTool|null
+     */
+    private $baseTool;
+
     /**
      * FrmAdd constructor.
      *
-     * @param string $name
-     * @param array  $attributes
+     * @param string          $name
+     * @param array           $attributes
+     * @param ImsLtiTool|null $tool
      */
     public function __construct(
         $name,
-        $attributes = []
+        $attributes = [],
+        ImsLtiTool $tool = null
     ) {
         parent::__construct($name, 'POST', '', '', $attributes, self::LAYOUT_HORIZONTAL, true);
+
+        $this->baseTool = $tool;
     }
 
     /**
@@ -28,17 +39,28 @@ class FrmAdd extends FormValidator
 
         $this->addHeader($plugin->get_lang('ToolSettings'));
         $this->addText('name', get_lang('Name'));
-        $this->addText('base_url', $plugin->get_lang('LaunchUrl'));
-        $this->addText('consumer_key', $plugin->get_lang('ConsumerKey'));
-        $this->addText('shared_secret', $plugin->get_lang('SharedSecret'));
-        $this->addTextarea('description', get_lang('Description'), ['rows' => 3]);
+        $this->addTextarea('description', get_lang('Description'));
+
+        if (null === $this->baseTool) {
+            $this->addElement('url', 'launch_url', $plugin->get_lang('LaunchUrl'));
+            $this->addRule('launch_url', get_lang('Required'), 'required');
+            $this->addText('consumer_key', $plugin->get_lang('ConsumerKey'));
+            $this->addText('shared_secret', $plugin->get_lang('SharedSecret'));
+        }
+
         $this->addButtonAdvancedSettings('lti_adv');
         $this->addHtml('<div id="lti_adv_options" style="display:none;">');
         $this->addTextarea(
             'custom_params',
             [$plugin->get_lang('CustomParams'), $plugin->get_lang('CustomParamsHelp')]
         );
-        $this->addCheckBox('deep_linking', null, $plugin->get_lang('SupportDeepLinking'));
+
+        if (null === $this->baseTool ||
+            ($this->baseTool && !$this->baseTool->isActiveDeepLinking())
+        ) {
+            $this->addCheckBox('deep_linking', null, $plugin->get_lang('SupportDeepLinking'));
+        }
+
         $this->addHtml('</div>');
         $this->addButtonAdvancedSettings('lti_privacy', get_lang('Privacy'));
         $this->addHtml('<div id="lti_privacy_options" style="display:none;">');
@@ -47,5 +69,19 @@ class FrmAdd extends FormValidator
         $this->addCheckBox('share_picture', null, $plugin->get_lang('ShareLauncherPicture'));
         $this->addHtml('</div>');
         $this->addButtonCreate($plugin->get_lang('AddExternalTool'));
+        $this->applyFilter('__ALL__', 'Security::remove_XSS');
+
+        if (null !== $this->baseTool) {
+            $this->setDefaults(
+                [
+                    'name' => $this->baseTool->getName(),
+                    'description' => $this->baseTool->getDescription(),
+                    'custom_params' => $this->baseTool->getCustomParams(),
+                    'share_name' => $this->baseTool->isSharingName(),
+                    'share_email' => $this->baseTool->isSharingEmail(),
+                    'share_picture' => $this->baseTool->isSharingPicture(),
+                ]
+            );
+        }
     }
 }