Merge branch 'MDL-59847-master' of https://github.com/snake/moodle
[moodle.git] / admin / repositoryinstance.php
blob960d0ff28c0ce310f037ce8c8f5d69da3ea1c03e
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 require_sesskey();
23 // id of repository
24 $edit = optional_param('edit', 0, PARAM_INT);
25 $new = optional_param('new', '', PARAM_PLUGIN);
26 $hide = optional_param('hide', 0, PARAM_INT);
27 $delete = optional_param('delete', 0, PARAM_INT);
28 $sure = optional_param('sure', '', PARAM_ALPHA);
29 $type = optional_param('type', '', PARAM_PLUGIN);
30 $downloadcontents = optional_param('downloadcontents', false, PARAM_BOOL);
32 $context = context_system::instance();
34 $pagename = 'repositorycontroller';
36 if ($edit){
37 $pagename = 'repositoryinstanceedit';
38 } else if ($delete) {
39 $pagename = 'repositorydelete';
40 } else if ($new) {
41 $pagename = 'repositoryinstancenew';
44 admin_externalpage_setup($pagename, '', null, new moodle_url('/admin/repositoryinstance.php'));
45 require_capability('moodle/site:config', $context);
47 $baseurl = new moodle_url("/$CFG->admin/repositoryinstance.php", array('sesskey'=>sesskey()));
49 $parenturl = new moodle_url("/$CFG->admin/repository.php", array(
50 'sesskey'=>sesskey(),
51 'action'=>'edit',
52 ));
54 if ($new) {
55 $parenturl->param('repos', $new);
56 } else {
57 $parenturl->param('repos', $type);
60 $return = true;
62 if (!empty($edit) || !empty($new)) {
63 if (!empty($edit)) {
64 $instance = repository::get_instance($edit);
65 if (!$instance->can_be_edited_by_user()) {
66 throw new repository_exception('nopermissiontoaccess', 'repository');
68 $instancetype = repository::get_type_by_id($instance->options['typeid']);
69 $classname = 'repository_' . $instancetype->get_typename();
70 $configs = $instance->get_instance_option_names();
71 $plugin = $instancetype->get_typename();
72 $typeid = $instance->options['typeid'];
73 } else {
74 $plugin = $new;
75 $typeid = null;
76 $instance = null;
79 // display the edit form for this instance
80 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid, 'instance' => $instance, 'contextid' => $context->id));
81 // end setup, begin output
83 if ($mform->is_cancelled()){
84 redirect($parenturl);
85 exit;
86 } else if ($fromform = $mform->get_data()){
87 if ($edit) {
88 $settings = array();
89 $settings['name'] = $fromform->name;
90 if (!$instance->readonly) {
91 foreach($configs as $config) {
92 if (isset($fromform->$config)) {
93 $settings[$config] = $fromform->$config;
94 } else {
95 $settings[$config] = null;
99 $success = $instance->set_option($settings);
100 } else {
101 $success = repository::static_function($plugin, 'create', $plugin, 0, $context, $fromform);
102 $data = data_submitted();
104 if ($success) {
105 core_plugin_manager::reset_caches();
106 redirect($parenturl);
107 } else {
108 print_error('instancenotsaved', 'repository', $parenturl);
110 exit;
111 } else {
112 echo $OUTPUT->header();
113 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
114 echo $OUTPUT->box_start();
115 $mform->display();
116 echo $OUTPUT->box_end();
117 $return = false;
119 } else if (!empty($hide)) {
120 $instance = repository::get_type_by_typename($hide);
121 $instance->hide();
122 core_plugin_manager::reset_caches();
123 $return = true;
124 } else if (!empty($delete)) {
125 $instance = repository::get_instance($delete);
126 if ($instance->readonly) {
127 // If you try to delete an instance set as readonly, display an error message.
128 throw new repository_exception('readonlyinstance', 'repository');
129 } else if (!$instance->can_be_edited_by_user()) {
130 throw new repository_exception('nopermissiontoaccess', 'repository');
132 if ($sure) {
133 if ($instance->delete($downloadcontents)) {
134 $deletedstr = get_string('instancedeleted', 'repository');
135 core_plugin_manager::reset_caches();
136 redirect($parenturl, $deletedstr, 3);
137 } else {
138 print_error('instancenotdeleted', 'repository', $parenturl);
140 exit;
143 echo $OUTPUT->header();
144 echo $OUTPUT->box_start('generalbox', 'notice');
145 $continueurl = new moodle_url($baseurl, array(
146 'type' => $type,
147 'delete' => $delete,
148 'sure' => 'yes',
150 $continueanddownloadurl = new moodle_url($continueurl, array(
151 'downloadcontents' => 1
153 $message = get_string('confirmdelete', 'repository', $instance->name);
154 echo html_writer::tag('p', $message);
156 echo $OUTPUT->single_button($continueurl, get_string('continueuninstall', 'repository'));
157 echo $OUTPUT->single_button($continueanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
158 echo $OUTPUT->single_button($parenturl, get_string('cancel'));
160 echo $OUTPUT->box_end();
162 $return = false;
165 if (!empty($return)) {
166 redirect($parenturl);
168 echo $OUTPUT->footer();