add.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /* For license terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Course;
  4. use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
  5. require_once __DIR__.'/../../main/inc/global.inc.php';
  6. api_protect_course_script();
  7. api_protect_teacher_script();
  8. $plugin = ImsLtiPlugin::create();
  9. $em = Database::getManager();
  10. $toolsRepo = $em->getRepository('ChamiloPluginBundle:ImsLti\ImsLtiTool');
  11. /** @var ImsLtiTool $baseTool */
  12. $baseTool = isset($_REQUEST['type']) ? $toolsRepo->find(intval($_REQUEST['type'])) : null;
  13. /** @var Course $course */
  14. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  15. $globalTools = $toolsRepo->findBy(['isGlobal' => true]);
  16. if ($baseTool && !$baseTool->isGlobal()) {
  17. Display::addFlash(
  18. Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning')
  19. );
  20. header('Location: '.api_get_self().'?'.api_get_cidreq());
  21. exit;
  22. }
  23. $form = new FormValidator('ims_lti_add_tool');
  24. if ($baseTool) {
  25. $form->addHtml('<p class="lead">'.Security::remove_XSS($baseTool->getDescription()).'</p>');
  26. }
  27. $form->addText('name', get_lang('Title'));
  28. $form->addTextarea('description', get_lang('Description'), ['rows' => 10]);
  29. if (!$baseTool) {
  30. $form->addElement('url', 'url', $plugin->get_lang('LaunchUrl'));
  31. $form->addText('consumer_key', $plugin->get_lang('ConsumerKey'), true);
  32. $form->addText('shared_secret', $plugin->get_lang('SharedSecret'), true);
  33. $form->addTextarea('custom_params', $plugin->get_lang('CustomParams'));
  34. $form->addRule('url', get_lang('Required'), 'required');
  35. } else {
  36. $form->addHidden('type', $baseTool->getId());
  37. }
  38. $form->addButtonCreate($plugin->get_lang('AddExternalTool'));
  39. if ($form->validate()) {
  40. $formValues = $form->getSubmitValues();
  41. $tool = null;
  42. if ($baseTool) {
  43. $tool = clone $baseTool;
  44. } else {
  45. $tool = new ImsLtiTool();
  46. $tool
  47. ->setLaunchUrl($formValues['url'])
  48. ->setConsumerKey($formValues['consumer_key'])
  49. ->setSharedSecret($formValues['shared_secret'])
  50. ->setCustomParams(
  51. empty($formValues['custom_params']) ? null : $formValues['custom_params']
  52. );
  53. }
  54. $tool
  55. ->setName($formValues['name'])
  56. ->setDescription(
  57. empty($formValues['description']) ? null : $formValues['description']
  58. )
  59. ->setIsGlobal(false);
  60. $em->persist($tool);
  61. $em->flush();
  62. $plugin->addCourseTool($course, $tool);
  63. Display::addFlash(
  64. Display::return_message($plugin->get_lang('ToolAdded'), 'success')
  65. );
  66. header('Location: '.api_get_course_url());
  67. exit;
  68. }
  69. $template = new Template($plugin->get_lang('AddExternalTool'));
  70. $template->assign('type', $baseTool ? $baseTool->getId() : null);
  71. $template->assign('tools', $globalTools);
  72. $template->assign('form', $form->returnForm());
  73. $content = $template->fetch('ims_lti/view/add.tpl');
  74. $template->assign('content', $content);
  75. $template->display_one_col_template();