MDL-21782 removing oboslete tabs
[moodle.git] / repository / manage_instances.php
CommitLineData
4317f92f 1<?php
c65dd2fe 2
5363905a
DC
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * This file is used to manage repositories
21 *
22 * @since 2.0
23 * @package moodlecore
24 * @subpackage repository
25 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29require_once(dirname(dirname(__FILE__)) . '/config.php');
30require_once($CFG->dirroot . '/repository/lib.php');
31
32$edit = optional_param('edit', 0, PARAM_INT);
33$new = optional_param('new', '', PARAM_FORMAT);
34$delete = optional_param('delete', 0, PARAM_INT);
35$sure = optional_param('sure', '', PARAM_ALPHA);
36$contextid = optional_param('contextid', 0, PARAM_INT);
37$usercourseid = optional_param('usercourseid', SITEID, PARAM_INT); // Extra: used for user context only
38
a6855934 39$url = new moodle_url('/repository/manage_instances.php');
fe879bf3
DC
40
41$baseurl = new moodle_url('/repository/manage_instances.php');
42$baseurl->param('sesskey', sesskey());
5363905a
DC
43
44if ($edit){
45 $url->param('edit', $edit);
46 $pagename = 'repositoryinstanceedit';
47} else if ($delete) {
48 $url->param('delete', $delete);
49 $pagename = 'repositorydelete';
50} else if ($new) {
51 $url->param('new', $new);
52 $pagename = 'repositoryinstancenew';
53} else {
54 $pagename = 'repositorylist';
55}
56
57if ($sure !== '') {
58 $url->param('sure', $sure);
59}
60if ($contextid !== 0) {
61 $url->param('contextid', $contextid);
fe879bf3 62 $baseurl->param('contextid', $contextid);
5363905a
DC
63}
64if ($usercourseid !== SITEID) {
65 $url->param('usercourseid', $usercourseid);
66}
67
68$PAGE->set_url($url);
69
70require_login(SITEID, false);
71
72$context = get_context_instance_by_id($contextid);
c65dd2fe 73
74/// Security: make sure we're allowed to do this operation
5363905a
DC
75if ($context->contextlevel == CONTEXT_COURSE) {
76 $pagename = get_string("repositorycourse",'repository');
c65dd2fe 77
5363905a
DC
78 // If the user is allowed to edit this course, he's allowed to edit list of repository instances
79 require_capability('moodle/course:update', $context);
c65dd2fe 80
5363905a
DC
81 if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
82 print_error('invalidcourseid');
83 }
c65dd2fe 84
5363905a
DC
85} else if ($context->contextlevel == CONTEXT_USER) {
86 $pagename = get_string("personalrepositories",'repository');
87 //is the user looking at its own repository instances
88 if ($USER->id != $context->instanceid){
89 print_error('notyourinstances', 'repository');
12c79bfd 90 }
5363905a 91 $user = $USER;
c65dd2fe 92
5363905a
DC
93} else {
94 print_error('invalidcontext');
95}
c65dd2fe 96
97
c65dd2fe 98
5363905a
DC
99/// Security: we cannot perform any action if the type is not visible or if the context has been disabled
100if (!empty($new)){
101 $type = repository::get_type_by_typename($new);
102} else if (!empty($edit)){
103 $instance = repository::get_instance($edit);
104 $type = repository::get_type_by_id($instance->options['typeid']);
105} else if (!empty($delete)){
106 $instance = repository::get_instance($delete);
107 $type = repository::get_type_by_id($instance->options['typeid']);
108}
109
110if (isset($type) && !$type->get_visible()) {
111 print_error('typenotvisible', 'repository', $baseurl);
112}
113
114if (isset($type) && !$type->get_contextvisibility($context)) {
115 print_error('usercontextrepositorydisabled', 'repository', $baseurl);
116}
c65dd2fe 117
118/// Create navigation links
5363905a
DC
119if (!empty($course)) {
120 $PAGE->navbar->add($pagename);
121 $fullname = $course->fullname;
122} else {
123 $fullname = fullname($user);
124 $strrepos = get_string('repositories', 'repository');
a6855934 125 $PAGE->navbar->add($fullname, new moodle_url('/user/view.php', array('id'=>$user->id)));
5363905a
DC
126 $PAGE->navbar->add($strrepos);
127}
128
129$title = $pagename;
c65dd2fe 130
131/// Display page header
5363905a
DC
132$PAGE->set_title($title);
133$PAGE->set_heading($fullname);
134echo $OUTPUT->header();
0ec691c4 135
5363905a
DC
136if ($context->contextlevel == CONTEXT_USER) {
137 if ( !$course = $DB->get_record('course', array('id'=>$usercourseid))) {
138 print_error('invalidcourseid');
0ec691c4 139 }
5363905a 140}
0ec691c4 141
5363905a 142echo $OUTPUT->heading($pagename);
c65dd2fe 143
5363905a 144$return = true;
c65dd2fe 145
5363905a
DC
146if (!empty($edit) || !empty($new)) {
147 if (!empty($edit)) {
148 $instance = repository::get_instance($edit);
149 //if you try to edit an instance set as readonly, display an error message
150 if ($instance->readonly) {
151 throw new repository_exception('readonlyinstance', 'repository');
faaa613d 152 }
5363905a
DC
153 $instancetype = repository::get_type_by_id($instance->options['typeid']);
154 $classname = 'repository_' . $instancetype->get_typename();
155 $configs = $instance->get_instance_option_names();
156 $plugin = $instancetype->get_typename();
157 $typeid = $instance->options['typeid'];
158 } else {
159 $plugin = $new;
160 $typeid = $new;
161 $instance = null;
162 }
c65dd2fe 163
5363905a
DC
164/// Create edit form for this instance
165 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
c65dd2fe 166
5363905a
DC
167/// Process the form data if any, or display
168 if ($mform->is_cancelled()){
169 redirect($baseurl);
170 exit;
c65dd2fe 171
5363905a
DC
172 } else if ($fromform = $mform->get_data()){
173 if (!confirm_sesskey()) {
174 print_error('confirmsesskeybad', '', $baseurl);
faaa613d 175 }
5363905a
DC
176 if ($edit) {
177 $settings = array();
178 $settings['name'] = $fromform->name;
179 foreach($configs as $config) {
180 $settings[$config] = $fromform->$config;
c65dd2fe 181 }
5363905a
DC
182 $success = $instance->set_option($settings);
183 } else {
184 $success = repository::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
185 $data = data_submitted();
faaa613d 186 }
5363905a
DC
187 if ($success) {
188 $savedstr = get_string('configsaved', 'repository');
61ef8f9f 189 //echo $OUTPUT->header();
5363905a
DC
190 echo $OUTPUT->heading($savedstr);
191 redirect($baseurl, $savedstr, 3);
192 } else {
193 print_error('instancenotsaved', 'repository', $baseurl);
194 }
195 exit;
196 } else { // Display the form
61ef8f9f 197 // echo $OUTPUT->header();
5363905a
DC
198 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
199 $OUTPUT->box_start();
200 $mform->display();
201 $OUTPUT->box_end();
faaa613d 202 $return = false;
203 }
5363905a 204} else if (!empty($delete)) {
61ef8f9f 205 // echo $OUTPUT->header();
5363905a
DC
206 $instance = repository::get_instance($delete);
207 //if you try to delete an instance set as readonly, display an error message
208 if ($instance->readonly) {
209 throw new repository_exception('readonlyinstance', 'repository');
faaa613d 210 }
5363905a
DC
211 if ($sure) {
212 if (!confirm_sesskey()) {
213 print_error('confirmsesskeybad', '', $baseurl);
214 }
215 if ($instance->delete()) {
216 $deletedstr = get_string('instancedeleted', 'repository');
217 echo $OUTPUT->heading($deletedstr);
218 redirect($baseurl, $deletedstr, 3);
219 } else {
220 print_error('instancenotdeleted', 'repository', $baseurl);
221 }
222 exit;
223 }
dc6896ef
PS
224 $formcontinue = new single_button(new moodle_url($baseurl, array('delete' => $delete, 'sure' => 'yes')), get_string('yes'));
225 $formcancel = new single_button($baseurl, get_string('no'));
5363905a
DC
226 echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name), $formcontinue, $formcancel);
227 $return = false;
228} else {
229 repository::display_instances_list($context);
230 $return = false;
231}
232
233if (!empty($return)) {
234 redirect($baseurl);
235}
236
237echo $OUTPUT->footer();