From 6004db18e29de30db349b6d512fb1f47a31fb29c Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Tue, 7 Jan 2020 18:11:03 -0500 Subject: [PATCH] LTI: Fix forms to add/edit tool --- plugin/ims_lti/configure.php | 48 +++++++++++++++++++---------- plugin/ims_lti/create.php | 1 + plugin/ims_lti/edit.php | 41 ++++++++++++++++++------ plugin/ims_lti/lang/english.php | 1 + plugin/ims_lti/src/Form/FrmEdit.php | 46 +++++++++++++++++++++++++-- 5 files changed, 109 insertions(+), 28 deletions(-) diff --git a/plugin/ims_lti/configure.php b/plugin/ims_lti/configure.php index 57c82dcec7..7752fa7bab 100644 --- a/plugin/ims_lti/configure.php +++ b/plugin/ims_lti/configure.php @@ -68,24 +68,40 @@ switch ($action) { ); if (!$baseTool) { - if (empty($formValues['consumer_key']) && empty($formValues['shared_secret'])) { - try { - $launchUrl = $plugin->getLaunchUrlFromCartridge($formValues['launch_url']); - } catch (Exception $e) { - Display::addFlash( - Display::return_message($e->getMessage(), 'error') - ); - - header('Location: '.api_get_self().'?'.api_get_cidreq()); - exit; - } - - $tool->setLaunchUrl($launchUrl); - } else { + if (ImsLti::V_1P3 === $formValues['version']) { $tool + ->setVersion(ImsLti::V_1P3) ->setLaunchUrl($formValues['launch_url']) - ->setConsumerKey($formValues['consumer_key']) - ->setSharedSecret($formValues['shared_secret']); + ->setClientId( + ImsLti::generateClientId() + ) + ->setLoginUrl($formValues['login_url']) + ->setRedirectUrl($formValues['redirect_url']) + ->setAdvantageServices( + [ + 'ags' => $formValues['1p3_ags'], + ] + ); + } elseif (ImsLti::V_1P1 === $formValues['version']) { + if (empty($formValues['consumer_key']) && empty($formValues['shared_secret'])) { + try { + $launchUrl = $plugin->getLaunchUrlFromCartridge($formValues['launch_url']); + } catch (Exception $e) { + Display::addFlash( + Display::return_message($e->getMessage(), 'error') + ); + + header('Location: '.api_get_self().'?'.api_get_cidreq()); + exit; + } + + $tool->setLaunchUrl($launchUrl); + } else { + $tool + ->setLaunchUrl($formValues['launch_url']) + ->setConsumerKey($formValues['consumer_key']) + ->setSharedSecret($formValues['shared_secret']); + } } } diff --git a/plugin/ims_lti/create.php b/plugin/ims_lti/create.php index eca9454a98..9f59dedae4 100644 --- a/plugin/ims_lti/create.php +++ b/plugin/ims_lti/create.php @@ -39,6 +39,7 @@ if ($form->validate()) { if (ImsLti::V_1P3 === $formValues['version']) { $externalTool + ->setVersion(ImsLti::V_1P3) ->setLaunchUrl($formValues['launch_url']) ->setClientId( ImsLti::generateClientId() diff --git a/plugin/ims_lti/edit.php b/plugin/ims_lti/edit.php index 47b279b36b..77f0caf40b 100644 --- a/plugin/ims_lti/edit.php +++ b/plugin/ims_lti/edit.php @@ -50,14 +50,22 @@ if ($form->validate()) { ); if (null === $tool->getParent()) { - $tool - ->setLaunchUrl($formValues['launch_url']) - ->setConsumerKey( - empty($formValues['consumer_key']) ? null : $formValues['consumer_key'] - ) - ->setSharedSecret( - empty($formValues['shared_secret']) ? null : $formValues['shared_secret'] - ); + $tool->setLaunchUrl($formValues['launch_url']); + + if ($tool->getVersion() === ImsLti::V_1P1) { + $tool + ->setConsumerKey( + empty($formValues['consumer_key']) ? null : $formValues['consumer_key'] + ) + ->setSharedSecret( + empty($formValues['shared_secret']) ? null : $formValues['shared_secret'] + ); + } elseif ($tool->getVersion() === ImsLti::V_1P3) { + $tool + ->setLoginUrl($formValues['login_url']) + ->setRedirectUrl($formValues['redirect_url']) + ->publicKey = $formValues['public_key']; + } } if (null === $tool->getParent() || @@ -66,6 +74,19 @@ if ($form->validate()) { $tool->setActiveDeepLinking(!empty($formValues['deep_linking'])); } + if (null == $tool->getParent()) { + /** @var ImsLtiTool $child */ + foreach ($tool->getChildren() as $child) { + $child + ->setLaunchUrl($tool->getLaunchUrl()) + ->setLoginUrl($tool->getLoginUrl()) + ->setRedirectUrl($tool->getRedirectUrl()) + ->publicKey = $tool->publicKey; + + $em->persist($child); + } + } + $em->persist($tool); $em->flush(); @@ -75,10 +96,10 @@ if ($form->validate()) { header('Location: '.api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php'); exit; +} else { + $form->setDefaultValues(); } -$form->setDefaultValues(); - $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin/index.php', 'name' => get_lang('PlatformAdmin')]; $interbreadcrumb[] = ['url' => api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php', 'name' => $plugin->get_title()]; diff --git a/plugin/ims_lti/lang/english.php b/plugin/ims_lti/lang/english.php index d733cfd0eb..32fdc7aefe 100644 --- a/plugin/ims_lti/lang/english.php +++ b/plugin/ims_lti/lang/english.php @@ -42,6 +42,7 @@ $strings['NoAccessToUrl'] = 'No access to URL'; $strings['LaunchUrlNotFound'] = 'Launch URL not found'; $strings['GenerateKeyPairInfo'] = 'A new private and public key pair will be created when enabling.'; $strings['PlatformKeys'] = 'Platform keys'; +$strings['ClientId'] = 'Client ID'; $strings['Keys'] = 'Keys'; $strings['KeyId'] = 'Key ID'; $strings['PublicKey'] = 'Public key'; diff --git a/plugin/ims_lti/src/Form/FrmEdit.php b/plugin/ims_lti/src/Form/FrmEdit.php index 13dec2525f..fc3341f423 100644 --- a/plugin/ims_lti/src/Form/FrmEdit.php +++ b/plugin/ims_lti/src/Form/FrmEdit.php @@ -57,11 +57,33 @@ class FrmEdit extends FormValidator $this->addText('name', get_lang('Name')); $this->addTextarea('description', get_lang('Description')); + $this->addRadio( + 'version', + $plugin->get_lang('LtiVersion'), + [ + ImsLti::V_1P1 => 'LTI 1.0 / 1.1', + ImsLti::V_1P3 => 'LTI 1.3.0', + ] + ); + $this->freeze(['version']); if (null === $parent) { $this->addUrl('launch_url', $plugin->get_lang('LaunchUrl'), true); - $this->addText('consumer_key', $plugin->get_lang('ConsumerKey'), false); - $this->addText('shared_secret', $plugin->get_lang('SharedSecret'), false); + if ($this->tool->getVersion() === ImsLti::V_1P1) { + $this->addText('consumer_key', $plugin->get_lang('ConsumerKey'), false); + $this->addText('shared_secret', $plugin->get_lang('SharedSecret'), false); + } elseif ($this->tool->getVersion() === ImsLti::V_1P3) { + $this->addText('client_id', $plugin->get_lang('ClientId'), true); + $this->freeze(['client_id']); + $this->addTextarea( + 'public_key', + $plugin->get_lang('PublicKey'), + ['style' => 'font-family: monospace;', 'rows' => 5], + true + ); + $this->addUrl('login_url', $plugin->get_lang('LoginUrl')); + $this->addUrl('redirect_url', $plugin->get_lang('RedirectUrl')); + } } $this->addButtonAdvancedSettings('lti_adv'); @@ -81,6 +103,18 @@ class FrmEdit extends FormValidator ); } + if (null === $parent && $this->tool->getVersion() === ImsLti::V_1P3) { + $this->addRadio( + '1p3_ags', + $plugin->get_lang('AssigmentAndGradesService'), + [ + LtiAssignmentGradesService::AGS_NONE => $plugin->get_lang('DontUseService'), + LtiAssignmentGradesService::AGS_SIMPLE => $plugin->get_lang('AGServiceSimple'), + LtiAssignmentGradesService::AGS_FULL => $plugin->get_lang('AGServiceFull'), + ] + ); + } + $this->addHtml(''); $this->addButtonAdvancedSettings('lti_privacy', get_lang('Privacy')); $this->addHtml('