Merge branch 'm20_MDL-22449_wiki_help_buttons' of git://github.com/dongsheng/moodle
[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
5d354ded 23 * @package core
5363905a 24 * @subpackage repository
5d354ded
PS
25 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5363905a
DC
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 63}
7b2e259c 64if ($usercourseid != SITEID) {
5363905a
DC
65 $url->param('usercourseid', $usercourseid);
66}
67
5363905a 68$context = get_context_instance_by_id($contextid);
c65dd2fe 69
49d20def
DC
70$PAGE->set_url($url);
71$PAGE->set_context($context);
72
c65dd2fe 73/// Security: make sure we're allowed to do this operation
5363905a
DC
74if ($context->contextlevel == CONTEXT_COURSE) {
75 $pagename = get_string("repositorycourse",'repository');
c65dd2fe 76
5363905a
DC
77 if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
78 print_error('invalidcourseid');
79 }
49d20def
DC
80 require_login($course, false);
81 // If the user is allowed to edit this course, he's allowed to edit list of repository instances
82 require_capability('moodle/course:update', $context);
83
c65dd2fe 84
5363905a 85} else if ($context->contextlevel == CONTEXT_USER) {
49d20def 86 require_login();
5363905a
DC
87 $pagename = get_string("personalrepositories",'repository');
88 //is the user looking at its own repository instances
89 if ($USER->id != $context->instanceid){
90 print_error('notyourinstances', 'repository');
12c79bfd 91 }
5363905a 92 $user = $USER;
49d20def 93 $PAGE->set_pagelayout('mydashboard');
5363905a
DC
94} else {
95 print_error('invalidcontext');
96}
c65dd2fe 97
5363905a
DC
98/// Security: we cannot perform any action if the type is not visible or if the context has been disabled
99if (!empty($new)){
100 $type = repository::get_type_by_typename($new);
101} else if (!empty($edit)){
102 $instance = repository::get_instance($edit);
103 $type = repository::get_type_by_id($instance->options['typeid']);
104} else if (!empty($delete)){
105 $instance = repository::get_instance($delete);
106 $type = repository::get_type_by_id($instance->options['typeid']);
107}
108
109if (isset($type) && !$type->get_visible()) {
110 print_error('typenotvisible', 'repository', $baseurl);
111}
112
113if (isset($type) && !$type->get_contextvisibility($context)) {
114 print_error('usercontextrepositorydisabled', 'repository', $baseurl);
115}
c65dd2fe 116
117/// Create navigation links
5363905a
DC
118if (!empty($course)) {
119 $PAGE->navbar->add($pagename);
120 $fullname = $course->fullname;
121} else {
122 $fullname = fullname($user);
123 $strrepos = get_string('repositories', 'repository');
a6855934 124 $PAGE->navbar->add($fullname, new moodle_url('/user/view.php', array('id'=>$user->id)));
5363905a
DC
125 $PAGE->navbar->add($strrepos);
126}
127
128$title = $pagename;
c65dd2fe 129
130/// Display page header
5363905a
DC
131$PAGE->set_title($title);
132$PAGE->set_heading($fullname);
133echo $OUTPUT->header();
0ec691c4 134
5363905a
DC
135if ($context->contextlevel == CONTEXT_USER) {
136 if ( !$course = $DB->get_record('course', array('id'=>$usercourseid))) {
137 print_error('invalidcourseid');
0ec691c4 138 }
5363905a 139}
0ec691c4 140
5363905a 141$return = true;
5363905a
DC
142if (!empty($edit) || !empty($new)) {
143 if (!empty($edit)) {
144 $instance = repository::get_instance($edit);
145 //if you try to edit an instance set as readonly, display an error message
146 if ($instance->readonly) {
147 throw new repository_exception('readonlyinstance', 'repository');
faaa613d 148 }
5363905a
DC
149 $instancetype = repository::get_type_by_id($instance->options['typeid']);
150 $classname = 'repository_' . $instancetype->get_typename();
151 $configs = $instance->get_instance_option_names();
152 $plugin = $instancetype->get_typename();
153 $typeid = $instance->options['typeid'];
154 } else {
155 $plugin = $new;
156 $typeid = $new;
157 $instance = null;
158 }
c65dd2fe 159
5363905a
DC
160/// Create edit form for this instance
161 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
c65dd2fe 162
5363905a
DC
163/// Process the form data if any, or display
164 if ($mform->is_cancelled()){
165 redirect($baseurl);
166 exit;
c65dd2fe 167
5363905a
DC
168 } else if ($fromform = $mform->get_data()){
169 if (!confirm_sesskey()) {
170 print_error('confirmsesskeybad', '', $baseurl);
faaa613d 171 }
5363905a
DC
172 if ($edit) {
173 $settings = array();
174 $settings['name'] = $fromform->name;
175 foreach($configs as $config) {
176 $settings[$config] = $fromform->$config;
c65dd2fe 177 }
5363905a
DC
178 $success = $instance->set_option($settings);
179 } else {
180 $success = repository::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
181 $data = data_submitted();
faaa613d 182 }
5363905a
DC
183 if ($success) {
184 $savedstr = get_string('configsaved', 'repository');
5363905a 185 echo $OUTPUT->heading($savedstr);
49d20def 186 redirect($baseurl);
5363905a
DC
187 } else {
188 print_error('instancenotsaved', 'repository', $baseurl);
189 }
190 exit;
191 } else { // Display the form
5363905a
DC
192 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
193 $OUTPUT->box_start();
194 $mform->display();
195 $OUTPUT->box_end();
faaa613d 196 $return = false;
197 }
5363905a 198} else if (!empty($delete)) {
61ef8f9f 199 // echo $OUTPUT->header();
5363905a
DC
200 $instance = repository::get_instance($delete);
201 //if you try to delete an instance set as readonly, display an error message
202 if ($instance->readonly) {
203 throw new repository_exception('readonlyinstance', 'repository');
faaa613d 204 }
5363905a
DC
205 if ($sure) {
206 if (!confirm_sesskey()) {
207 print_error('confirmsesskeybad', '', $baseurl);
208 }
209 if ($instance->delete()) {
210 $deletedstr = get_string('instancedeleted', 'repository');
211 echo $OUTPUT->heading($deletedstr);
212 redirect($baseurl, $deletedstr, 3);
213 } else {
214 print_error('instancenotdeleted', 'repository', $baseurl);
215 }
216 exit;
217 }
dc6896ef
PS
218 $formcontinue = new single_button(new moodle_url($baseurl, array('delete' => $delete, 'sure' => 'yes')), get_string('yes'));
219 $formcancel = new single_button($baseurl, get_string('no'));
5363905a
DC
220 echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name), $formcontinue, $formcancel);
221 $return = false;
222} else {
223 repository::display_instances_list($context);
224 $return = false;
225}
226
227if (!empty($return)) {
228 redirect($baseurl);
229}
230
231echo $OUTPUT->footer();