Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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   * This page allows instructors to configure course level tool providers.
  19   *
  20   * @package mod_lti
  21   * @copyright  Copyright (c) 2011 Moodlerooms Inc. (http://www.moodlerooms.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @author     Chris Scribner
  24   */
  25  
  26  require_once('../../config.php');
  27  require_once($CFG->dirroot.'/mod/lti/edit_form.php');
  28  require_once($CFG->dirroot.'/mod/lti/lib.php');
  29  
  30  $courseid = required_param('course', PARAM_INT);
  31  
  32  require_login($courseid, false);
  33  $url = new moodle_url('/mod/lti/instructor_edit_tool_type.php');
  34  $PAGE->set_url($url);
  35  $PAGE->set_pagelayout('popup');
  36  $PAGE->set_title(get_string('edittype', 'mod_lti'));
  37  
  38  $action = optional_param('action', null, PARAM_TEXT);
  39  $typeid = optional_param('typeid', null, PARAM_INT);
  40  
  41  require_sesskey();
  42  
  43  require_capability('mod/lti:addcoursetool', context_course::instance($courseid));
  44  
  45  if (!empty($typeid)) {
  46      $type = lti_get_type($typeid);
  47      if ($type->course != $courseid) {
  48          throw new Exception('You do not have permissions to edit this tool type.');
  49          die;
  50      }
  51  }
  52  
  53  // Delete action is called via ajax.
  54  if ($action == 'delete') {
  55      lti_delete_type($typeid);
  56      die;
  57  }
  58  
  59  // Add a timeout for closing for behat so it can check for errors before switching back to the main window.
  60  $timeout = 0;
  61  if (defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING) {
  62      $timeout = 2000;
  63  }
  64  
  65  echo $OUTPUT->header();
  66  
  67  if ($action == 'edit') {
  68      $type = lti_get_type_type_config($typeid);
  69  } else {
  70      $type = new stdClass();
  71      $type->lti_clientid = null;
  72  }
  73  
  74  $form = new mod_lti_edit_types_form($url, (object)array('id' => $typeid, 'clientid' => $type->lti_clientid));
  75  
  76  // If the user just opened an add or edit form.
  77  if ($action == 'add' || $action == 'edit') {
  78      if ($action == 'edit') {
  79          $form->set_data($type);
  80      }
  81      echo $OUTPUT->heading(get_string('toolsetup', 'lti'));
  82      $form->display();
  83  } else {
  84      $script = '';
  85      $closewindow = <<<EOF
  86          setTimeout(function() {
  87              window.close();
  88          }, $timeout);
  89  EOF;
  90  
  91      if ($data = $form->get_data()) {
  92          $type = new stdClass();
  93  
  94          if (!empty($typeid)) {
  95              $type->id = $typeid;
  96  
  97              lti_load_type_if_cartridge($data);
  98  
  99              lti_update_type($type, $data);
 100  
 101              $fromdb = lti_get_type($typeid);
 102              $json = json_encode($fromdb);
 103  
 104              // Output script to update the calling window.
 105              $script = <<<EOF
 106                  window.opener.M.mod_lti.editor.updateToolType({$json});
 107  EOF;
 108          } else {
 109              $type->state = LTI_TOOL_STATE_CONFIGURED;
 110              $type->course = $COURSE->id;
 111  
 112              lti_load_type_if_cartridge($data);
 113  
 114              $id = lti_add_type($type, $data);
 115  
 116              $fromdb = lti_get_type($id);
 117              $json = json_encode($fromdb);
 118  
 119              // Output script to update the calling window.
 120              $script = <<<EOF
 121                  window.opener.M.mod_lti.editor.addToolType({$json});
 122  EOF;
 123          }
 124          echo html_writer::script($script . $closewindow);
 125      } else if ($form->is_cancelled()) {
 126          echo html_writer::script($closewindow);
 127      } else {
 128          echo $OUTPUT->heading(get_string('toolsetup', 'lti'));
 129          $form->display();
 130      }
 131  }
 132  
 133  echo $OUTPUT->footer();