manage_instances.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /**
  17. * This file is used to manage repositories
  18. *
  19. * @since Moodle 2.0
  20. * @package core
  21. * @subpackage repository
  22. * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. require_once(__DIR__ . '/../config.php');
  26. require_once($CFG->dirroot . '/repository/lib.php');
  27. $edit = optional_param('edit', 0, PARAM_INT);
  28. $new = optional_param('new', '', PARAM_ALPHANUMEXT);
  29. $delete = optional_param('delete', 0, PARAM_INT);
  30. $sure = optional_param('sure', '', PARAM_ALPHA);
  31. $contextid = optional_param('contextid', 0, PARAM_INT);
  32. $usercourseid = optional_param('usercourseid', SITEID, PARAM_INT); // Extra: used for user context only
  33. $url = new moodle_url('/repository/manage_instances.php');
  34. $baseurl = new moodle_url('/repository/manage_instances.php');
  35. $baseurl->param('sesskey', sesskey());
  36. if ($edit){
  37. $url->param('edit', $edit);
  38. $pagename = 'repositoryinstanceedit';
  39. } else if ($delete) {
  40. $url->param('delete', $delete);
  41. $pagename = 'repositorydelete';
  42. } else if ($new) {
  43. $url->param('new', $new);
  44. $pagename = 'repositoryinstancenew';
  45. } else {
  46. $pagename = 'repositorylist';
  47. }
  48. if ($sure !== '') {
  49. $url->param('sure', $sure);
  50. }
  51. if ($contextid !== 0) {
  52. $url->param('contextid', $contextid);
  53. $baseurl->param('contextid', $contextid);
  54. }
  55. if ($usercourseid != SITEID) {
  56. $url->param('usercourseid', $usercourseid);
  57. }
  58. $context = context::instance_by_id($contextid);
  59. $PAGE->set_url($url);
  60. $PAGE->set_context($context);
  61. $PAGE->set_pagelayout('standard');
  62. /// Security: make sure we're allowed to do this operation
  63. if ($context->contextlevel == CONTEXT_COURSE) {
  64. $pagename = get_string("repositorycourse",'repository');
  65. if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
  66. print_error('invalidcourseid');
  67. }
  68. require_login($course, false);
  69. // If the user is allowed to edit this course, he's allowed to edit list of repository instances
  70. require_capability('moodle/course:update', $context);
  71. } else if ($context->contextlevel == CONTEXT_USER) {
  72. require_login();
  73. $pagename = get_string('manageinstances', 'repository');
  74. //is the user looking at its own repository instances
  75. if ($USER->id != $context->instanceid){
  76. print_error('notyourinstances', 'repository');
  77. }
  78. $user = $USER;
  79. $PAGE->set_pagelayout('mydashboard');
  80. } else {
  81. print_error('invalidcontext');
  82. }
  83. /// Security: we cannot perform any action if the type is not visible or if the context has been disabled
  84. if (!empty($new) && empty($edit)){
  85. $type = repository::get_type_by_typename($new);
  86. } else if (!empty($edit)){
  87. $instance = repository::get_instance($edit);
  88. $type = repository::get_type_by_id($instance->options['typeid']);
  89. } else if (!empty($delete)){
  90. $instance = repository::get_instance($delete);
  91. $type = repository::get_type_by_id($instance->options['typeid']);
  92. }
  93. if (isset($type)) {
  94. if (!$type->get_visible()) {
  95. print_error('typenotvisible', 'repository', $baseurl);
  96. }
  97. // Prevents the user from creating/editing an instance if the repository is not visible in
  98. // this context OR if the user does not have the capability to view this repository in this context.
  99. $canviewrepository = has_capability('repository/'.$type->get_typename().':view', $context);
  100. if (!$type->get_contextvisibility($context) || !$canviewrepository) {
  101. print_error('usercontextrepositorydisabled', 'repository', $baseurl);
  102. }
  103. }
  104. // We have an instance when we are going to edit, or delete. Several checks need to be done!
  105. if (!empty($instance)) {
  106. // The context passed MUST match the context of the repository. And as both have to be
  107. // similar, this also ensures that the context is either a user one, or a course one.
  108. if ($instance->instance->contextid != $context->id) {
  109. print_error('invalidcontext');
  110. }
  111. if ($instance->readonly) {
  112. // Cannot edit, or delete a readonly instance.
  113. throw new repository_exception('readonlyinstance', 'repository');
  114. } else if (!$instance->can_be_edited_by_user()) {
  115. // The user has to have the right to edit the instance.
  116. throw new repository_exception('nopermissiontoaccess', 'repository');
  117. }
  118. }
  119. // Create navigation links.
  120. if (!empty($course)) {
  121. $pageheading = $course->fullname;
  122. } else {
  123. $pageheading = $pagename;
  124. }
  125. // Display page header.
  126. $PAGE->set_title($pagename);
  127. $PAGE->set_heading($pageheading);
  128. $return = true;
  129. if (!empty($edit) || !empty($new)) {
  130. if (!empty($edit)) {
  131. $instancetype = repository::get_type_by_id($instance->options['typeid']);
  132. $classname = 'repository_' . $instancetype->get_typename();
  133. $configs = $instance->get_instance_option_names();
  134. $plugin = $instancetype->get_typename();
  135. $typeid = $instance->options['typeid'];
  136. } else {
  137. $plugin = $new;
  138. $typeid = $new;
  139. $instance = null;
  140. }
  141. /// Create edit form for this instance
  142. $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
  143. /// Process the form data if any, or display
  144. if ($mform->is_cancelled()){
  145. redirect($baseurl);
  146. exit;
  147. } else if ($fromform = $mform->get_data()){
  148. if (!confirm_sesskey()) {
  149. print_error('confirmsesskeybad', '', $baseurl);
  150. }
  151. if ($edit) {
  152. $settings = array();
  153. $settings['name'] = $fromform->name;
  154. foreach($configs as $config) {
  155. $settings[$config] = isset($fromform->$config) ? $fromform->$config : null;
  156. }
  157. $success = $instance->set_option($settings);
  158. } else {
  159. $success = repository::static_function($plugin, 'create', $plugin, 0, context::instance_by_id($contextid), $fromform);
  160. $data = data_submitted();
  161. }
  162. if ($success) {
  163. $savedstr = get_string('configsaved', 'repository');
  164. redirect($baseurl);
  165. } else {
  166. print_error('instancenotsaved', 'repository', $baseurl);
  167. }
  168. exit;
  169. } else { // Display the form
  170. echo $OUTPUT->header();
  171. echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
  172. $OUTPUT->box_start();
  173. $mform->display();
  174. $OUTPUT->box_end();
  175. $return = false;
  176. }
  177. } else if (!empty($delete)) {
  178. if ($sure) {
  179. if (!confirm_sesskey()) {
  180. print_error('confirmsesskeybad', '', $baseurl);
  181. }
  182. if ($instance->delete()) {
  183. $deletedstr = get_string('instancedeleted', 'repository');
  184. redirect($baseurl, $deletedstr, 3);
  185. } else {
  186. print_error('instancenotdeleted', 'repository', $baseurl);
  187. }
  188. exit;
  189. }
  190. echo $OUTPUT->header();
  191. $formcontinue = new single_button(new moodle_url($baseurl, array('delete' => $delete, 'sure' => 'yes')), get_string('yes'));
  192. $formcancel = new single_button($baseurl, get_string('no'));
  193. echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name), $formcontinue, $formcancel);
  194. $return = false;
  195. } else {
  196. echo $OUTPUT->header();
  197. repository::display_instances_list($context);
  198. $return = false;
  199. }
  200. if (!empty($return)) {
  201. redirect($baseurl);
  202. }
  203. echo $OUTPUT->footer();