repositoryinstance.php 5.8 KB

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