Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / admin / repositoryinstance.php
blobcd8f2096b2d8596e2aedc1c64ca540b1a1670062
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 require_once(__DIR__ . '/../config.php');
18 require_once($CFG->dirroot . '/repository/lib.php');
19 require_once($CFG->libdir . '/adminlib.php');
21 // id of repository
22 $edit = optional_param('edit', 0, PARAM_INT);
23 $new = optional_param('new', '', PARAM_PLUGIN);
24 $hide = optional_param('hide', 0, PARAM_INT);
25 $delete = optional_param('delete', 0, PARAM_INT);
26 $sure = optional_param('sure', '', PARAM_ALPHA);
27 $type = optional_param('type', '', PARAM_PLUGIN);
28 $downloadcontents = optional_param('downloadcontents', false, PARAM_BOOL);
30 $context = context_system::instance();
32 $pagename = 'repositorycontroller';
34 if ($edit){
35 $pagename = 'repositoryinstanceedit';
36 } else if ($delete) {
37 $pagename = 'repositorydelete';
38 } else if ($new) {
39 $pagename = 'repositoryinstancenew';
42 admin_externalpage_setup($pagename, '', null, new moodle_url('/admin/repositoryinstance.php'));
44 // The URL used for redirection, and that all edit related URLs will be based off.
45 $parenturl = new moodle_url('/admin/repository.php', ['action' => 'edit']);
47 if ($new) {
48 $parenturl->param('repos', $new);
49 } else {
50 $parenturl->param('repos', $type);
53 $return = true;
55 if (!empty($edit) || !empty($new)) {
56 if (!empty($edit)) {
57 $instance = repository::get_instance($edit);
58 if (!$instance->can_be_edited_by_user()) {
59 throw new repository_exception('nopermissiontoaccess', 'repository');
61 $instancetype = repository::get_type_by_id($instance->options['typeid']);
62 $classname = 'repository_' . $instancetype->get_typename();
63 $configs = $instance->get_instance_option_names();
64 $plugin = $instancetype->get_typename();
65 $typeid = $instance->options['typeid'];
66 } else {
67 $plugin = $new;
68 $typeid = null;
69 $instance = null;
72 // display the edit form for this instance
73 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid, 'instance' => $instance, 'contextid' => $context->id));
74 // end setup, begin output
76 if ($mform->is_cancelled()){
77 redirect($parenturl);
78 exit;
79 } else if ($fromform = $mform->get_data()){
80 if ($edit) {
81 $settings = array();
82 $settings['name'] = $fromform->name;
83 if (!$instance->readonly) {
84 foreach($configs as $config) {
85 if (isset($fromform->$config)) {
86 $settings[$config] = $fromform->$config;
87 } else {
88 $settings[$config] = null;
92 $success = $instance->set_option($settings);
93 } else {
94 $success = repository::static_function($plugin, 'create', $plugin, 0, $context, $fromform);
95 $data = data_submitted();
97 if ($success) {
98 core_plugin_manager::reset_caches();
99 redirect($parenturl);
100 } else {
101 throw new \moodle_exception('instancenotsaved', 'repository', $parenturl);
103 exit;
104 } else {
105 echo $OUTPUT->header();
106 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
107 echo $OUTPUT->box_start();
108 $mform->display();
109 echo $OUTPUT->box_end();
110 $return = false;
112 } else if (!empty($hide)) {
113 require_sesskey();
114 $instance = repository::get_type_by_typename($hide);
115 $instance->hide();
116 core_plugin_manager::reset_caches();
117 $return = true;
118 } else if (!empty($delete)) {
119 $instance = repository::get_instance($delete);
120 if ($instance->readonly) {
121 // If you try to delete an instance set as readonly, display an error message.
122 throw new repository_exception('readonlyinstance', 'repository');
123 } else if (!$instance->can_be_edited_by_user()) {
124 throw new repository_exception('nopermissiontoaccess', 'repository');
126 if ($sure) {
127 require_sesskey();
128 if ($instance->delete($downloadcontents)) {
129 $deletedstr = get_string('instancedeleted', 'repository');
130 core_plugin_manager::reset_caches();
131 redirect($parenturl, $deletedstr, 3);
132 } else {
133 throw new \moodle_exception('instancenotdeleted', 'repository', $parenturl);
135 exit;
138 echo $OUTPUT->header();
139 echo $OUTPUT->box_start('generalbox', 'notice');
141 $continueurl = new moodle_url($PAGE->url, [
142 'type' => $type,
143 'delete' => $delete,
144 'sure' => 'yes',
147 $continueanddownloadurl = new moodle_url($continueurl, array(
148 'downloadcontents' => 1
151 $message = get_string('confirmdelete', 'repository', $instance->name);
152 echo html_writer::tag('p', $message);
154 echo $OUTPUT->single_button($continueurl, get_string('continueuninstall', 'repository'));
155 echo $OUTPUT->single_button($continueanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
156 echo $OUTPUT->single_button($parenturl, get_string('cancel'));
158 echo $OUTPUT->box_end();
160 $return = false;
163 if (!empty($return)) {
164 redirect($parenturl);
166 echo $OUTPUT->footer();