chamilo /
chamilo-lms
| 1 | <?php |
||||
| 2 | /* For license terms, see /license.txt */ |
||||
| 3 | |||||
| 4 | use Chamilo\LtiBundle\Entity\ExternalTool; |
||||
| 5 | |||||
| 6 | require_once __DIR__.'/../../main/inc/global.inc.php'; |
||||
| 7 | |||||
| 8 | api_protect_course_script(); |
||||
| 9 | api_protect_teacher_script(); |
||||
| 10 | |||||
| 11 | $plugin = ImsLtiPlugin::create(); |
||||
| 12 | $em = Database::getManager(); |
||||
| 13 | $toolsRepo = $em->getRepository(ExternalTool::class); |
||||
| 14 | |||||
| 15 | /** @var ExternalTool|null $baseTool */ |
||||
| 16 | $baseTool = isset($_REQUEST['type']) ? $toolsRepo->find(intval($_REQUEST['type'])) : null; |
||||
| 17 | $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : 'add'; |
||||
| 18 | |||||
| 19 | $course = api_get_course_entity(api_get_course_int_id()); |
||||
| 20 | $addedTools = $toolsRepo->findBy(['course' => $course]); |
||||
| 21 | $globalTools = $toolsRepo->findBy(['parent' => null, 'course' => null]); |
||||
| 22 | |||||
| 23 | if ($baseTool && !$baseTool->isGlobal()) { |
||||
| 24 | Display::addFlash( |
||||
| 25 | Display::return_message($plugin->get_lang('ToolNotAvailable'), 'warning') |
||||
| 26 | ); |
||||
| 27 | |||||
| 28 | header('Location: '.api_get_self().'?'.api_get_cidreq()); |
||||
| 29 | exit; |
||||
| 30 | } |
||||
| 31 | |||||
| 32 | $categories = Category::load(null, null, $course->getCode()); |
||||
| 33 | |||||
| 34 | switch ($action) { |
||||
| 35 | case 'add': |
||||
| 36 | $form = new \Chamilo\PluginBundle\ImsLti\Form\FrmAdd('ims_lti_add_tool', [], $baseTool); |
||||
| 37 | $form->build(); |
||||
| 38 | |||||
| 39 | if ($baseTool) { |
||||
| 40 | $form->addHidden('type', $baseTool->getId()); |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | if ($form->validate()) { |
||||
| 44 | $formValues = $form->getSubmitValues(); |
||||
| 45 | |||||
| 46 | $tool = new ExternalTool(); |
||||
| 47 | |||||
| 48 | if ($baseTool) { |
||||
| 49 | $tool = clone $baseTool; |
||||
| 50 | $tool->setParent($baseTool); |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | $tool |
||||
| 54 | ->setName($formValues['name']) |
||||
|
0 ignored issues
–
show
|
|||||
| 55 | ->setDescription( |
||||
| 56 | empty($formValues['description']) ? null : $formValues['description'] |
||||
| 57 | ) |
||||
| 58 | ->setCustomParams( |
||||
| 59 | empty($formValues['custom_params']) ? null : $formValues['custom_params'] |
||||
| 60 | ) |
||||
| 61 | ->setDocumenTarget($formValues['document_target']) |
||||
| 62 | ->setCourse($course) |
||||
| 63 | ->setPrivacy( |
||||
| 64 | !empty($formValues['share_name']), |
||||
| 65 | !empty($formValues['share_email']), |
||||
| 66 | !empty($formValues['share_picture']) |
||||
| 67 | ); |
||||
| 68 | |||||
| 69 | if (!empty($formValues['replacement_user_id'])) { |
||||
| 70 | $tool->setReplacementForUserId($formValues['replacement_user_id']); |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | if (!$baseTool) { |
||||
| 74 | if (ImsLti::V_1P3 === $formValues['version']) { |
||||
| 75 | $tool |
||||
| 76 | ->setVersion(ImsLti::V_1P3) |
||||
| 77 | ->setLaunchUrl($formValues['launch_url']) |
||||
| 78 | ->setClientId( |
||||
| 79 | ImsLti::generateClientId() |
||||
| 80 | ) |
||||
| 81 | ->setLoginUrl($formValues['login_url']) |
||||
| 82 | ->setRedirectUrl($formValues['redirect_url']) |
||||
| 83 | ->setAdvantageServices( |
||||
| 84 | [ |
||||
| 85 | 'ags' => $formValues['1p3_ags'] ?? LtiAssignmentGradesService::AGS_NONE, |
||||
| 86 | 'nrps' => $formValues['1p3_nrps'], |
||||
| 87 | ] |
||||
| 88 | ) |
||||
| 89 | ->setJwksUrl($formValues['jwks_url']) |
||||
| 90 | ->publicKey = $formValues['public_key']; |
||||
| 91 | } elseif (ImsLti::V_1P1 === $formValues['version']) { |
||||
| 92 | if (empty($formValues['consumer_key']) && empty($formValues['shared_secret'])) { |
||||
| 93 | try { |
||||
| 94 | $launchUrl = $plugin->getLaunchUrlFromCartridge($formValues['launch_url']); |
||||
| 95 | } catch (Exception $e) { |
||||
| 96 | Display::addFlash( |
||||
| 97 | Display::return_message($e->getMessage(), 'error') |
||||
| 98 | ); |
||||
| 99 | |||||
| 100 | header('Location: '.api_get_self().'?'.api_get_cidreq()); |
||||
| 101 | exit; |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | $tool->setLaunchUrl($launchUrl); |
||||
| 105 | } else { |
||||
| 106 | $tool |
||||
| 107 | ->setLaunchUrl($formValues['launch_url']) |
||||
| 108 | ->setConsumerKey($formValues['consumer_key']) |
||||
| 109 | ->setSharedSecret($formValues['shared_secret']); |
||||
| 110 | } |
||||
| 111 | } |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | if (null === $baseTool || |
||||
| 115 | ($baseTool && !$baseTool->isActiveDeepLinking()) |
||||
| 116 | ) { |
||||
| 117 | $tool |
||||
| 118 | ->setActiveDeepLinking( |
||||
| 119 | !empty($formValues['deep_linking']) |
||||
| 120 | ); |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | $em->persist($tool); |
||||
| 124 | $em->flush(); |
||||
| 125 | |||||
| 126 | if ($tool->getVersion() === ImsLti::V_1P3) { |
||||
| 127 | $advServices = $tool->getAdvantageServices(); |
||||
| 128 | |||||
| 129 | if (LtiAssignmentGradesService::AGS_NONE !== $advServices['ags']) { |
||||
| 130 | $lineItemResource = new LtiLineItemsResource( |
||||
| 131 | $tool->getId(), |
||||
| 132 | $course->getId() |
||||
| 133 | ); |
||||
| 134 | $lineItemResource->createLineItem( |
||||
| 135 | ['label' => $tool->getName(), 'scoreMaximum' => 100] |
||||
|
0 ignored issues
–
show
The method
getName() does not exist on Chamilo\LtiBundle\Entity\ExternalTool.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 136 | ); |
||||
| 137 | |||||
| 138 | Display::addFlash( |
||||
| 139 | Display::return_message($plugin->get_lang('GradebookEvaluationCreated'), 'success') |
||||
| 140 | ); |
||||
| 141 | } |
||||
| 142 | } |
||||
| 143 | |||||
| 144 | if (!$tool->isActiveDeepLinking()) { |
||||
| 145 | $plugin->addCourseTool($course, $tool); |
||||
| 146 | } |
||||
| 147 | |||||
| 148 | Display::addFlash( |
||||
| 149 | Display::return_message($plugin->get_lang('ToolAdded'), 'success') |
||||
| 150 | ); |
||||
| 151 | |||||
| 152 | header('Location: '.api_get_self().'?'.api_get_cidreq()); |
||||
| 153 | exit; |
||||
| 154 | } |
||||
| 155 | |||||
| 156 | $form->setDefaultValues(); |
||||
| 157 | break; |
||||
| 158 | case 'edit': |
||||
| 159 | /** @var ExternalTool|null $tool */ |
||||
| 160 | $tool = null; |
||||
| 161 | |||||
| 162 | if (!empty($_REQUEST['id'])) { |
||||
| 163 | $tool = $em->find(ExternalTool::class, (int) $_REQUEST['id']); |
||||
| 164 | } |
||||
| 165 | |||||
| 166 | if (empty($tool) || |
||||
| 167 | !ImsLtiPlugin::existsToolInCourse($tool->getId(), $course) |
||||
| 168 | ) { |
||||
| 169 | api_not_allowed( |
||||
| 170 | true, |
||||
| 171 | Display::return_message($plugin->get_lang('ToolNotAvailable'), 'error') |
||||
| 172 | ); |
||||
| 173 | |||||
| 174 | break; |
||||
| 175 | } |
||||
| 176 | |||||
| 177 | $form = new \Chamilo\PluginBundle\Form\FrmEdit('ims_lti_edit_tool', [], $tool); |
||||
| 178 | $form->build(false); |
||||
| 179 | |||||
| 180 | if ($form->validate()) { |
||||
| 181 | $formValues = $form->getSubmitValues(); |
||||
| 182 | |||||
| 183 | $tool |
||||
| 184 | ->setName($formValues['name']) |
||||
| 185 | ->setDescription( |
||||
| 186 | empty($formValues['description']) ? null : $formValues['description'] |
||||
| 187 | ) |
||||
| 188 | ->setActiveDeepLinking( |
||||
| 189 | !empty($formValues['deep_linking']) |
||||
| 190 | ) |
||||
| 191 | ->setCustomParams( |
||||
| 192 | empty($formValues['custom_params']) ? null : $formValues['custom_params'] |
||||
| 193 | ) |
||||
| 194 | ->setDocumenTarget($formValues['document_target']) |
||||
| 195 | ->setPrivacy( |
||||
| 196 | !empty($formValues['share_name']), |
||||
| 197 | !empty($formValues['share_email']), |
||||
| 198 | !empty($formValues['share_picture']) |
||||
| 199 | ); |
||||
| 200 | |||||
| 201 | if (!empty($formValues['replacement_user_id'])) { |
||||
| 202 | $tool->setReplacementForUserId($formValues['replacement_user_id']); |
||||
| 203 | } |
||||
| 204 | |||||
| 205 | if (null === $tool->getParent()) { |
||||
| 206 | if ($tool->getVersion() === ImsLti::V_1P3) { |
||||
| 207 | $tool |
||||
| 208 | ->setLaunchUrl($formValues['launch_url']) |
||||
| 209 | ->setLoginUrl($formValues['login_url']) |
||||
| 210 | ->setRedirectUrl($formValues['redirect_url']) |
||||
| 211 | ->setAdvantageServices( |
||||
| 212 | [ |
||||
| 213 | 'ags' => $formValues['1p3_ags'] ?? LtiAssignmentGradesService::AGS_NONE, |
||||
| 214 | 'nrps' => $formValues['1p3_nrps'], |
||||
| 215 | ] |
||||
| 216 | ) |
||||
| 217 | ->setJwksUrl($formValues['jwks_url']) |
||||
| 218 | ->publicKey = $formValues['public_key']; |
||||
| 219 | } elseif ($tool->getVersion() === ImsLti::V_1P1) { |
||||
| 220 | $tool |
||||
| 221 | ->setLaunchUrl($formValues['launch_url']) |
||||
| 222 | ->setConsumerKey($formValues['consumer_key']) |
||||
| 223 | ->setSharedSecret($formValues['shared_secret']); |
||||
| 224 | } |
||||
| 225 | } |
||||
| 226 | |||||
| 227 | $em->persist($tool); |
||||
| 228 | $em->flush(); |
||||
| 229 | |||||
| 230 | $courseTool = $plugin->findCourseToolByLink($course, $tool); |
||||
| 231 | |||||
| 232 | if ($courseTool) { |
||||
|
0 ignored issues
–
show
|
|||||
| 233 | $plugin->updateCourseTool($courseTool, $tool); |
||||
| 234 | } |
||||
| 235 | |||||
| 236 | Display::addFlash( |
||||
| 237 | Display::return_message($plugin->get_lang('ToolEdited'), 'success') |
||||
| 238 | ); |
||||
| 239 | |||||
| 240 | header('Location: '.api_get_self().'?'.api_get_cidreq()); |
||||
| 241 | exit; |
||||
| 242 | } |
||||
| 243 | |||||
| 244 | $form->setDefaultValues(); |
||||
| 245 | break; |
||||
| 246 | default: |
||||
| 247 | api_not_allowed(true); |
||||
| 248 | break; |
||||
| 249 | } |
||||
| 250 | |||||
| 251 | $template = new Template($plugin->get_lang('AddExternalTool')); |
||||
| 252 | $template->assign('type', $baseTool ? $baseTool->getId() : null); |
||||
| 253 | $template->assign('added_tools', $addedTools); |
||||
| 254 | $template->assign('global_tools', $globalTools); |
||||
| 255 | $template->assign('form', $form->returnForm()); |
||||
| 256 | |||||
| 257 | $content = $template->fetch('ImsLti/view/add.tpl'); |
||||
| 258 | |||||
| 259 | $actions = Display::url( |
||||
| 260 | Display::return_icon('add.png', $plugin->get_lang('AddExternalTool'), [], ICON_SIZE_MEDIUM), |
||||
| 261 | api_get_self().'?'.api_get_cidreq() |
||||
| 262 | ); |
||||
| 263 | |||||
| 264 | if (!empty($categories)) { |
||||
| 265 | $actions .= Display::url( |
||||
| 266 | Display::return_icon('gradebook.png', get_lang('MakeQualifiable'), [], ICON_SIZE_MEDIUM), |
||||
| 267 | './gradebook/add_eval.php?selectcat='.$categories[0]->get_id().'&'.api_get_cidreq() |
||||
| 268 | ); |
||||
| 269 | } |
||||
| 270 | |||||
| 271 | $template->assign('actions', Display::toolbarAction('lti_toolbar', [$actions])); |
||||
| 272 | $template->assign('content', $content); |
||||
| 273 | $template->display_one_col_template(); |
||||
| 274 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.