on-demand release 3.9dev+
[moodle.git] / mod / lti / instructor_edit_tool_type.php
blob8925ce10de1b7449c82da890a018fd313fbfe0bc
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.
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/>.
17 /**
18 * This page allows instructors to configure course level tool providers.
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
26 require_once('../../config.php');
27 require_once($CFG->dirroot.'/mod/lti/edit_form.php');
28 require_once($CFG->dirroot.'/mod/lti/lib.php');
30 $courseid = required_param('course', PARAM_INT);
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'));
38 $action = optional_param('action', null, PARAM_TEXT);
39 $typeid = optional_param('typeid', null, PARAM_INT);
41 require_sesskey();
43 require_capability('mod/lti:addcoursetool', context_course::instance($courseid));
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;
53 // Delete action is called via ajax.
54 if ($action == 'delete') {
55 lti_delete_type($typeid);
56 die;
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;
65 echo $OUTPUT->header();
67 if ($action == 'edit') {
68 $type = lti_get_type_type_config($typeid);
69 } else {
70 $type = new stdClass();
71 $type->lti_clientid = null;
74 $form = new mod_lti_edit_types_form($url, (object)array('id' => $typeid, 'clientid' => $type->lti_clientid));
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);
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;
91 if ($data = $form->get_data()) {
92 $type = new stdClass();
94 if (!empty($typeid)) {
95 $type->id = $typeid;
97 lti_load_type_if_cartridge($data);
99 lti_update_type($type, $data);
101 $fromdb = lti_get_type($typeid);
102 $json = json_encode($fromdb);
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;
112 lti_load_type_if_cartridge($data);
114 $id = lti_add_type($type, $data);
116 $fromdb = lti_get_type($id);
117 $json = json_encode($fromdb);
119 // Output script to update the calling window.
120 $script = <<<EOF
121 window.opener.M.mod_lti.editor.addToolType({$json});
122 EOF;
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();
133 echo $OUTPUT->footer();