MDL-21695 adding help strings
[moodle.git] / repository / manage_instances.php
blob45ebc6c6f298e911839bac87350f582b99d6bcf3
1 <?php
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.
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/>.
19 /**
20 * This file is used to manage repositories
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
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());
44 if ($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';
57 if ($sure !== '') {
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 $PAGE->set_url($url);
70 require_login(SITEID, false);
72 $context = get_context_instance_by_id($contextid);
74 /// Security: make sure we're allowed to do this operation
75 if ($context->contextlevel == CONTEXT_COURSE) {
76 $pagename = get_string("repositorycourse",'repository');
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);
81 if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
82 print_error('invalidcourseid');
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');
91 $user = $USER;
93 } else {
94 print_error('invalidcontext');
99 /// Security: we cannot perform any action if the type is not visible or if the context has been disabled
100 if (!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']);
110 if (isset($type) && !$type->get_visible()) {
111 print_error('typenotvisible', 'repository', $baseurl);
114 if (isset($type) && !$type->get_contextvisibility($context)) {
115 print_error('usercontextrepositorydisabled', 'repository', $baseurl);
118 /// Create navigation links
119 if (!empty($course)) {
120 $PAGE->navbar->add($pagename);
121 $fullname = $course->fullname;
122 } else {
123 $fullname = fullname($user);
124 $strrepos = get_string('repositories', 'repository');
125 $PAGE->navbar->add($fullname, new moodle_url('/user/view.php', array('id'=>$user->id)));
126 $PAGE->navbar->add($strrepos);
129 $title = $pagename;
131 /// Display page header
132 $PAGE->set_title($title);
133 $PAGE->set_heading($fullname);
134 echo $OUTPUT->header();
136 if ($context->contextlevel == CONTEXT_USER) {
137 if ( !$course = $DB->get_record('course', array('id'=>$usercourseid))) {
138 print_error('invalidcourseid');
140 $currenttab = 'repositories';
141 include($CFG->dirroot.'/user/tabs.php');
144 echo $OUTPUT->heading($pagename);
146 $return = true;
148 if (!empty($edit) || !empty($new)) {
149 if (!empty($edit)) {
150 $instance = repository::get_instance($edit);
151 //if you try to edit an instance set as readonly, display an error message
152 if ($instance->readonly) {
153 throw new repository_exception('readonlyinstance', 'repository');
155 $instancetype = repository::get_type_by_id($instance->options['typeid']);
156 $classname = 'repository_' . $instancetype->get_typename();
157 $configs = $instance->get_instance_option_names();
158 $plugin = $instancetype->get_typename();
159 $typeid = $instance->options['typeid'];
160 } else {
161 $plugin = $new;
162 $typeid = $new;
163 $instance = null;
166 /// Create edit form for this instance
167 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
169 /// Process the form data if any, or display
170 if ($mform->is_cancelled()){
171 redirect($baseurl);
172 exit;
174 } else if ($fromform = $mform->get_data()){
175 if (!confirm_sesskey()) {
176 print_error('confirmsesskeybad', '', $baseurl);
178 if ($edit) {
179 $settings = array();
180 $settings['name'] = $fromform->name;
181 foreach($configs as $config) {
182 $settings[$config] = $fromform->$config;
184 $success = $instance->set_option($settings);
185 } else {
186 $success = repository::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
187 $data = data_submitted();
189 if ($success) {
190 $savedstr = get_string('configsaved', 'repository');
191 //echo $OUTPUT->header();
192 echo $OUTPUT->heading($savedstr);
193 redirect($baseurl, $savedstr, 3);
194 } else {
195 print_error('instancenotsaved', 'repository', $baseurl);
197 exit;
198 } else { // Display the form
199 // echo $OUTPUT->header();
200 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
201 $OUTPUT->box_start();
202 $mform->display();
203 $OUTPUT->box_end();
204 $return = false;
206 } else if (!empty($delete)) {
207 // echo $OUTPUT->header();
208 $instance = repository::get_instance($delete);
209 //if you try to delete an instance set as readonly, display an error message
210 if ($instance->readonly) {
211 throw new repository_exception('readonlyinstance', 'repository');
213 if ($sure) {
214 if (!confirm_sesskey()) {
215 print_error('confirmsesskeybad', '', $baseurl);
217 if ($instance->delete()) {
218 $deletedstr = get_string('instancedeleted', 'repository');
219 echo $OUTPUT->heading($deletedstr);
220 redirect($baseurl, $deletedstr, 3);
221 } else {
222 print_error('instancenotdeleted', 'repository', $baseurl);
224 exit;
226 $formcontinue = new single_button(new moodle_url($baseurl, array('delete' => $delete, 'sure' => 'yes')), get_string('yes'));
227 $formcancel = new single_button($baseurl, get_string('no'));
228 echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name), $formcontinue, $formcancel);
229 $return = false;
230 } else {
231 repository::display_instances_list($context);
232 $return = false;
235 if (!empty($return)) {
236 redirect($baseurl);
239 echo $OUTPUT->footer();