3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * This file is used to manage repositories
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
29 require_once(dirname(dirname(__FILE__
)) . '/config.php');
30 require_once($CFG->dirroot
. '/repository/lib.php');
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
39 $url = new moodle_url('/repository/manage_instances.php');
41 $baseurl = new moodle_url('/repository/manage_instances.php');
42 $baseurl->param('sesskey', sesskey());
45 $url->param('edit', $edit);
46 $pagename = 'repositoryinstanceedit';
48 $url->param('delete', $delete);
49 $pagename = 'repositorydelete';
51 $url->param('new', $new);
52 $pagename = 'repositoryinstancenew';
54 $pagename = 'repositorylist';
58 $url->param('sure', $sure);
60 if ($contextid !== 0) {
61 $url->param('contextid', $contextid);
62 $baseurl->param('contextid', $contextid);
64 if ($usercourseid != SITEID
) {
65 $url->param('usercourseid', $usercourseid);
68 $context = get_context_instance_by_id($contextid);
71 $PAGE->set_context($context);
73 /// Security: make sure we're allowed to do this operation
74 if ($context->contextlevel
== CONTEXT_COURSE
) {
75 $pagename = get_string("repositorycourse",'repository');
77 if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid
))) {
78 print_error('invalidcourseid');
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);
85 } else if ($context->contextlevel
== CONTEXT_USER
) {
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');
93 $PAGE->set_pagelayout('mydashboard');
95 print_error('invalidcontext');
98 /// Security: we cannot perform any action if the type is not visible or if the context has been disabled
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']);
109 // The context passed MUST match the context of the repository. And as both have to be
110 // similar, this also ensures that the context is either a user one, or a course one.
111 if (!empty($instance)) {
112 if ($instance->instance
->contextid
!= $context->id
) {
113 print_error('invalidcontext');
118 if (!$type->get_visible()) {
119 print_error('typenotvisible', 'repository', $baseurl);
121 // Prevents the user from creating/editing an instance if the repository is not visible in
122 // this context OR if the user does not have the capability to view this repository in this context.
123 $canviewrepository = has_capability('repository/'.$type->get_typename().':view', $context);
124 if (!$type->get_contextvisibility($context) ||
!$canviewrepository) {
125 print_error('usercontextrepositorydisabled', 'repository', $baseurl);
129 /// Create navigation links
130 if (!empty($course)) {
131 $PAGE->navbar
->add($pagename);
132 $fullname = $course->fullname
;
134 $fullname = fullname($user);
135 $strrepos = get_string('repositories', 'repository');
136 $PAGE->navbar
->add($fullname, new moodle_url('/user/view.php', array('id'=>$user->id
)));
137 $PAGE->navbar
->add($strrepos);
142 /// Display page header
143 $PAGE->set_title($title);
144 $PAGE->set_heading($fullname);
146 if ($context->contextlevel
== CONTEXT_USER
) {
147 if ( !$course = $DB->get_record('course', array('id'=>$usercourseid))) {
148 print_error('invalidcourseid');
153 if (!empty($edit) ||
!empty($new)) {
155 $instance = repository
::get_instance($edit);
156 //if you try to edit an instance set as readonly, display an error message
157 if ($instance->readonly
) {
158 throw new repository_exception('readonlyinstance', 'repository');
160 // Check if we can read the content of the repository, if not exception is thrown.
161 $instance->check_capability();
162 $instancetype = repository
::get_type_by_id($instance->options
['typeid']);
163 $classname = 'repository_' . $instancetype->get_typename();
164 $configs = $instance->get_instance_option_names();
165 $plugin = $instancetype->get_typename();
166 $typeid = $instance->options
['typeid'];
173 /// Create edit form for this instance
174 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
176 /// Process the form data if any, or display
177 if ($mform->is_cancelled()){
181 } else if ($fromform = $mform->get_data()){
182 if (!confirm_sesskey()) {
183 print_error('confirmsesskeybad', '', $baseurl);
187 $settings['name'] = $fromform->name
;
188 foreach($configs as $config) {
189 $settings[$config] = isset($fromform->$config) ?
$fromform->$config : null;
191 $success = $instance->set_option($settings);
193 $success = repository
::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
194 $data = data_submitted();
197 $savedstr = get_string('configsaved', 'repository');
200 print_error('instancenotsaved', 'repository', $baseurl);
203 } else { // Display the form
204 echo $OUTPUT->header();
205 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
206 $OUTPUT->box_start();
211 } else if (!empty($delete)) {
212 // echo $OUTPUT->header();
213 $instance = repository
::get_instance($delete);
214 //if you try to delete an instance set as readonly, display an error message
215 if ($instance->readonly
) {
216 throw new repository_exception('readonlyinstance', 'repository');
218 // Check if we can read the content of the repository, if not exception is thrown.
219 $instance->check_capability();
221 if (!confirm_sesskey()) {
222 print_error('confirmsesskeybad', '', $baseurl);
224 if ($instance->delete()) {
225 $deletedstr = get_string('instancedeleted', 'repository');
226 redirect($baseurl, $deletedstr, 3);
228 print_error('instancenotdeleted', 'repository', $baseurl);
232 echo $OUTPUT->header();
233 $formcontinue = new single_button(new moodle_url($baseurl, array('delete' => $delete, 'sure' => 'yes')), get_string('yes'));
234 $formcancel = new single_button($baseurl, get_string('no'));
235 echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name
), $formcontinue, $formcancel);
238 echo $OUTPUT->header();
239 repository
::display_instances_list($context);
243 if (!empty($return)) {
247 echo $OUTPUT->footer();