configure.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : 'add';
  14. /** @var Course $course */
  15. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  16. $addedTools = $toolsRepo->findBy(['course' => $course]);
  17. $globalTools = $toolsRepo->findBy(['parent' => null, 'course' => null]);
  18. if ($baseTool && !$baseTool->isGlobal()) {
  19. Display::addFlash(
  20. Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning')
  21. );
  22. header('Location: '.api_get_self().'?'.api_get_cidreq());
  23. exit;
  24. }
  25. switch ($action) {
  26. case 'add':
  27. $form = new FrmAdd('ims_lti_add_tool', [], $baseTool);
  28. $form->build();
  29. if ($baseTool) {
  30. $form->addHidden('type', $baseTool->getId());
  31. }
  32. if ($form->validate()) {
  33. $formValues = $form->getSubmitValues();
  34. $tool = new ImsLtiTool();
  35. $tool
  36. ->setName($formValues['name'])
  37. ->setDescription(
  38. empty($formValues['description']) ? null : $formValues['description']
  39. )
  40. ->setCustomParams(
  41. empty($formValues['custom_params']) ? null : $formValues['custom_params']
  42. )
  43. ->setCourse($course)
  44. ->setActiveDeepLinking(!empty($formValues['deep_linking']))
  45. ->setPrivacy(
  46. !empty($formValues['share_name']),
  47. !empty($formValues['share_email']),
  48. !empty($formValues['share_picture'])
  49. );
  50. if ($baseTool) {
  51. $tool
  52. ->setLaunchUrl($baseTool->getLaunchUrl())
  53. ->setConsumerKey($baseTool->getConsumerKey())
  54. ->setSharedSecret($baseTool->getSharedSecret());
  55. } else {
  56. if (empty($formValues['consumer_key']) && empty($formValues['shared_secret'])) {
  57. try {
  58. $launchUrl = $plugin->getLaunchUrlFromCartridge($formValues['launch_url']);
  59. } catch (Exception $e) {
  60. Display::addFlash(
  61. Display::return_message($e->getMessage(), 'error')
  62. );
  63. header('Location: '.api_get_self().'?'.api_get_cidreq());
  64. exit;
  65. }
  66. $tool->setLaunchUrl($launchUrl);
  67. } else {
  68. $tool
  69. ->setLaunchUrl($formValues['launch_url'])
  70. ->setConsumerKey($formValues['consumer_key'])
  71. ->setSharedSecret($formValues['shared_secret']);
  72. }
  73. }
  74. if (null === $baseTool ||
  75. ($baseTool && !$baseTool->isActiveDeepLinking())
  76. ) {
  77. $tool
  78. ->setActiveDeepLinking(
  79. !empty($formValues['deep_linking'])
  80. );
  81. }
  82. if ($baseTool) {
  83. $tool->setParent($baseTool);
  84. }
  85. $em->persist($tool);
  86. $em->flush();
  87. if (!$tool->isActiveDeepLinking()) {
  88. $plugin->addCourseTool($course, $tool);
  89. }
  90. Display::addFlash(
  91. Display::return_message($plugin->get_lang('ToolAdded'), 'success')
  92. );
  93. header('Location: '.api_get_self().'?'.api_get_cidreq());
  94. exit;
  95. }
  96. $form->setDefaultValues();
  97. break;
  98. case 'edit':
  99. /** @var ImsLtiTool|null $tool */
  100. $tool = null;
  101. if (!empty($_REQUEST['id'])) {
  102. $tool = $em->find('ChamiloPluginBundle:ImsLti\ImsLtiTool', (int) $_REQUEST['id']);
  103. }
  104. if (empty($tool) ||
  105. !ImsLtiPlugin::existsToolInCourse($tool->getId(), $course)
  106. ) {
  107. api_not_allowed(
  108. true,
  109. Display::return_message($plugin->get_lang('ToolNotAvailable'), 'error')
  110. );
  111. break;
  112. }
  113. $form = new FrmEdit('ims_lti_edit_tool', [], $tool);
  114. $form->build(false);
  115. if ($form->validate()) {
  116. $formValues = $form->getSubmitValues();
  117. $tool
  118. ->setName($formValues['name'])
  119. ->setDescription(
  120. empty($formValues['description']) ? null : $formValues['description']
  121. )
  122. ->setActiveDeepLinking(
  123. !empty($formValues['deep_linking'])
  124. )
  125. ->setCustomParams(
  126. empty($formValues['custom_params']) ? null : $formValues['custom_params']
  127. )
  128. ->setPrivacy(
  129. !empty($formValues['share_name']),
  130. !empty($formValues['share_email']),
  131. !empty($formValues['share_picture'])
  132. );
  133. if (null === $tool->getParent()) {
  134. $tool
  135. ->setLaunchUrl($formValues['launch_url'])
  136. ->setConsumerKey($formValues['consumer_key'])
  137. ->setSharedSecret($formValues['shared_secret']);
  138. }
  139. $em->persist($tool);
  140. $em->flush();
  141. $courseTool = $plugin->findCourseToolByLink($course, $tool);
  142. if ($courseTool) {
  143. $plugin->updateCourseTool($courseTool, $tool);
  144. }
  145. Display::addFlash(
  146. Display::return_message($plugin->get_lang('ToolEdited'), 'success')
  147. );
  148. header('Location: '.api_get_self().'?'.api_get_cidreq());
  149. exit;
  150. }
  151. $form->setDefaultValues();
  152. break;
  153. }
  154. $categories = Category::load(null, null, $course->getCode());
  155. $template = new Template($plugin->get_lang('AddExternalTool'));
  156. $template->assign('type', $baseTool ? $baseTool->getId() : null);
  157. $template->assign('added_tools', $addedTools);
  158. $template->assign('global_tools', $globalTools);
  159. $template->assign('form', $form->returnForm());
  160. $content = $template->fetch('ims_lti/view/add.tpl');
  161. $actions = Display::url(
  162. Display::return_icon('add.png', $plugin->get_lang('AddExternalTool'), [], ICON_SIZE_MEDIUM),
  163. api_get_self().'?'.api_get_cidreq()
  164. );
  165. if (!empty($categories)) {
  166. $actions .= Display::url(
  167. Display::return_icon('gradebook.png', get_lang('MakeQualifiable'), [], ICON_SIZE_MEDIUM),
  168. './gradebook/add_eval.php?selectcat='.$categories[0]->get_id().'&'.api_get_cidreq()
  169. );
  170. }
  171. $template->assign('actions', Display::toolbarAction('lti_toolbar', [$actions]));
  172. $template->assign('content', $content);
  173. $template->display_one_col_template();