Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/mod/lti/ -> ajax.php (source)
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * AJAX service used when adding an External Tool.
  19   *
  20   * It is used to provide immediate feedback
  21   * of which tool provider is to be used based on the Launch URL.
  22   *
  23   * @package    mod_lti
  24   * @subpackage xml
  25   * @copyright Copyright (c) 2011 Moodlerooms Inc. (http://www.moodlerooms.com)
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   * @author     Chris Scribner
  28   */
  29  define('AJAX_SCRIPT', true);
  30  
  31  require_once(__DIR__ . "/../../config.php");
  32  require_once($CFG->dirroot . '/mod/lti/locallib.php');
  33  
  34  $courseid = required_param('course', PARAM_INT);
  35  $context = context_course::instance($courseid);
  36  
  37  require_login($courseid, false);
  38  
  39  $action = required_param('action', PARAM_TEXT);
  40  
  41  $response = new stdClass();
  42  
  43  switch ($action) {
  44      case 'find_tool_config':
  45          $toolurl = required_param('toolurl', PARAM_RAW);
  46          $toolid = optional_param('toolid', 0, PARAM_INT);
  47  
  48          require_capability('moodle/course:manageactivities', $context);
  49          require_capability('mod/lti:addinstance', $context);
  50  
  51          if (!empty($toolurl) && lti_is_cartridge($toolurl)) {
  52              $response->cartridge = true;
  53          } else {
  54              if (empty($toolid) && !empty($toolurl)) {
  55                  $tool = lti_get_tool_by_url_match($toolurl, $courseid);
  56  
  57                  if (!empty($tool)) {
  58                      $toolid = $tool->id;
  59  
  60                      $response->toolid = $tool->id;
  61                      $response->toolname = s($tool->name);
  62                      $response->tooldomain = s($tool->tooldomain);
  63                  }
  64              } else {
  65                  $response->toolid = $toolid;
  66              }
  67  
  68              if (!empty($toolid)) {
  69                  // Look up privacy settings.
  70                  $query = '
  71                      SELECT name, value
  72                      FROM {lti_types_config}
  73                      WHERE
  74                          typeid = :typeid
  75                      AND name IN (\'sendname\', \'sendemailaddr\', \'acceptgrades\')
  76                  ';
  77  
  78                  $privacyconfigs = $DB->get_records_sql($query, array('typeid' => $toolid));
  79                  $success = count($privacyconfigs) > 0;
  80                  foreach ($privacyconfigs as $config) {
  81                      $configname = $config->name;
  82                      $response->$configname = $config->value;
  83                  }
  84                  if (!$success) {
  85                      $response->error = s(get_string('tool_config_not_found', 'mod_lti'));
  86                  }
  87              }
  88          }
  89  
  90          break;
  91  }
  92  echo $OUTPUT->header();
  93  echo json_encode($response);
  94  
  95  die;