Merge branch 'MDL-74996-master-1' of https://github.com/HuongNV13/moodle
[moodle.git] / admin / repositoryinstance.php
blobe7b9c77ed1e2a352fdc6e6c8f5aa90b131db686f
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'));
46 $baseurl = new moodle_url("/$CFG->admin/repositoryinstance.php", array('sesskey'=>sesskey()));
48 $parenturl = new moodle_url("/$CFG->admin/repository.php", array(
49 'sesskey'=>sesskey(),
50 'action'=>'edit',
51 ));
53 if ($new) {
54 $parenturl->param('repos', $new);
55 } else {
56 $parenturl->param('repos', $type);
59 $return = true;
61 if (!empty($edit) || !empty($new)) {
62 if (!empty($edit)) {
63 $instance = repository::get_instance($edit);
64 if (!$instance->can_be_edited_by_user()) {
65 throw new repository_exception('nopermissiontoaccess', 'repository');
67 $instancetype = repository::get_type_by_id($instance->options['typeid']);
68 $classname = 'repository_' . $instancetype->get_typename();
69 $configs = $instance->get_instance_option_names();
70 $plugin = $instancetype->get_typename();
71 $typeid = $instance->options['typeid'];
72 } else {
73 $plugin = $new;
74 $typeid = null;
75 $instance = null;
78 // display the edit form for this instance
79 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid, 'instance' => $instance, 'contextid' => $context->id));
80 // end setup, begin output
82 if ($mform->is_cancelled()){
83 redirect($parenturl);
84 exit;
85 } else if ($fromform = $mform->get_data()){
86 if ($edit) {
87 $settings = array();
88 $settings['name'] = $fromform->name;
89 if (!$instance->readonly) {
90 foreach($configs as $config) {
91 if (isset($fromform->$config)) {
92 $settings[$config] = $fromform->$config;
93 } else {
94 $settings[$config] = null;
98 $success = $instance->set_option($settings);
99 } else {
100 $success = repository::static_function($plugin, 'create', $plugin, 0, $context, $fromform);
101 $data = data_submitted();
103 if ($success) {
104 core_plugin_manager::reset_caches();
105 redirect($parenturl);
106 } else {
107 throw new \moodle_exception('instancenotsaved', 'repository', $parenturl);
109 exit;
110 } else {
111 echo $OUTPUT->header();
112 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
113 echo $OUTPUT->box_start();
114 $mform->display();
115 echo $OUTPUT->box_end();
116 $return = false;
118 } else if (!empty($hide)) {
119 $instance = repository::get_type_by_typename($hide);
120 $instance->hide();
121 core_plugin_manager::reset_caches();
122 $return = true;
123 } else if (!empty($delete)) {
124 $instance = repository::get_instance($delete);
125 if ($instance->readonly) {
126 // If you try to delete an instance set as readonly, display an error message.
127 throw new repository_exception('readonlyinstance', 'repository');
128 } else if (!$instance->can_be_edited_by_user()) {
129 throw new repository_exception('nopermissiontoaccess', 'repository');
131 if ($sure) {
132 if ($instance->delete($downloadcontents)) {
133 $deletedstr = get_string('instancedeleted', 'repository');
134 core_plugin_manager::reset_caches();
135 redirect($parenturl, $deletedstr, 3);
136 } else {
137 throw new \moodle_exception('instancenotdeleted', 'repository', $parenturl);
139 exit;
142 echo $OUTPUT->header();
143 echo $OUTPUT->box_start('generalbox', 'notice');
144 $continueurl = new moodle_url($baseurl, array(
145 'type' => $type,
146 'delete' => $delete,
147 'sure' => 'yes',
149 $continueanddownloadurl = new moodle_url($continueurl, array(
150 'downloadcontents' => 1
152 $message = get_string('confirmdelete', 'repository', $instance->name);
153 echo html_writer::tag('p', $message);
155 echo $OUTPUT->single_button($continueurl, get_string('continueuninstall', 'repository'));
156 echo $OUTPUT->single_button($continueanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
157 echo $OUTPUT->single_button($parenturl, get_string('cancel'));
159 echo $OUTPUT->box_end();
161 $return = false;
164 if (!empty($return)) {
165 redirect($parenturl);
167 echo $OUTPUT->footer();