MDL-17457 migrated all modules to new db/install.php; added upgrade.txt file for...
[moodle.git] / repository / manage_instances.php
CommitLineData
c65dd2fe 1<?php // $Id$
2
3 require_once(dirname(dirname(__FILE__)) . '/config.php');
4 require_once($CFG->dirroot . '/repository/lib.php');
5 //require_once($CFG->libdir . '/adminlib.php');
6
7 $edit = optional_param('edit', 0, PARAM_INT); // Repository ID
8 $new = optional_param('new', '', PARAM_FORMAT);
9 $delete = optional_param('delete', 0, PARAM_INT);
10 $sure = optional_param('sure', '', PARAM_ALPHA);
11 $contextid = optional_param('contextid', 0, PARAM_INT);
0ec691c4 12 $usercourseid = optional_param('usercourseid', SITEID, PARAM_INT); // Extra: used for user context only
c65dd2fe 13
c65dd2fe 14 if ($edit){
15 $pagename = 'repositoryinstanceedit';
16 } else if ($delete) {
17 $pagename = 'repositorydelete';
18 } else if ($new) {
19 $pagename = 'repositoryinstancenew';
20 } else {
21 $pagename = 'repositorylist';
6c91ce49 22 }
c65dd2fe 23
24 require_login(SITEID, false);
25
26 $context = get_context_instance_by_id($contextid);
27
28/// Security: make sure we're allowed to do this operation
29 if ($context->contextlevel == CONTEXT_COURSE) {
30 $pagename = get_string("repositorycourse",'repository');
31
32 // If the user is allowed to edit this course, he's allowed to edit list of repository instances
33 require_capability('moodle/course:update', $context);
34
35 if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
36 print_error('invalidcourseid');
37 }
38
39 } else if ($context->contextlevel == CONTEXT_USER) {
40 $pagename = get_string("personalrepositories",'repository');
41 //is the user looking at its own repository instances
42 if ($USER->id != $context->instanceid){
43 print_error('notyourinstances', 'repository');
44 }
45 $user = $USER;
46
47 } else {
48 // throw an error here
f48fb4d6 49 print_error('wrongcontextid');
c65dd2fe 50 exit;
12c79bfd 51 }
c65dd2fe 52
53 $baseurl = $CFG->wwwroot . '/repository/manage_instances.php?contextid=' . $contextid . '&amp;sesskey='. sesskey();
54
55
f48fb4d6 56/// Security: we cannot perform any action if the type is not visible or if the context has been disabled
c65dd2fe 57 if (!empty($new)){
dbc01944 58 $type = repository::get_type_by_typename($new);
c65dd2fe 59 } else if (!empty($edit)){
dbc01944 60 $instance = repository::get_instance($edit);
61 $type = repository::get_type_by_id($instance->typeid);
c65dd2fe 62 } else if (!empty($delete)){
dbc01944 63 $instance = repository::get_instance($delete);
64 $type = repository::get_type_by_id($instance->typeid);
c65dd2fe 65 }
f48fb4d6 66 if (isset($type) && ( !$type->get_visible() || (!$type->get_contextvisibility($context->contextlevel)) ) ) {
c65dd2fe 67 print_error('typenotvisible', 'repository', $baseurl);
68 }
69
70
71/// Create navigation links
72 $navlinks = array();
73 if (!empty($course)) {
74 $navlinks[] = array('name' => $course->shortname,
75 'link' => "$CFG->wwwroot/course/view.php?id=$course->id",
76 'type' => 'misc');
77 $navlinks[] = array('name' => $pagename,
78 'link' => null,
dbc01944 79 'type' => 'misc');
c65dd2fe 80 $fullname = $course->fullname;
faaa613d 81 } else {
c65dd2fe 82 $fullname = fullname($user);
83 $strrepos = get_string('repositories', 'repository');
84 $navlinks[] = array('name' => $fullname, 'link' => $CFG->wwwroot . '/user/view.php?id=' . $user->id, 'type' => 'misc');
85 $navlinks[] = array('name' => $strrepos, 'link' => null, 'type' => 'misc');
faaa613d 86 }
87
c65dd2fe 88 $title = $pagename;
89 $navigation = build_navigation($navlinks);
faaa613d 90
c65dd2fe 91
92/// Display page header
93 print_header($title, $fullname, $navigation);
0ec691c4 94
95 if ($context->contextlevel == CONTEXT_USER) {
96 if ( !$course = $DB->get_record('course', array('id'=>$usercourseid))) {
97 print_error('invalidcourseid');
98 }
99 $currenttab = 'repositories';
100 include($CFG->dirroot.'/user/tabs.php');
101 }
102
c65dd2fe 103 print_heading($pagename);
104
105 $return = true;
106
107 if (!empty($edit) || !empty($new)) {
108 if (!empty($edit)) {
dbc01944 109 $instance = repository::get_instance($edit);
948c2860 110 //if you try to edit an instance set as readonly, display an error message
111 if ($instance->readonly) {
112 throw new repository_exception('readonlyinstance', 'repository');
113 }
dbc01944 114 $instancetype = repository::get_type_by_id($instance->typeid);
c65dd2fe 115 $classname = 'repository_' . $instancetype->get_typename();
116 $configs = $instance->get_instance_option_names();
117 $plugin = $instancetype->get_typename();
118 $typeid = $instance->typeid;
119 } else {
120 $plugin = $new;
121 $typeid = $new;
122 $instance = null;
faaa613d 123 }
c65dd2fe 124
125 /// Create edit form for this instance
126 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
127
128 /// Process the form data if any, or display
129 if ($mform->is_cancelled()){
130 redirect($baseurl);
131 exit;
132
133 } else if ($fromform = $mform->get_data()){
134 if (!confirm_sesskey()) {
135 print_error('confirmsesskeybad', '', $baseurl);
faaa613d 136 }
c65dd2fe 137 if ($edit) {
138 $settings = array();
139 $settings['name'] = $fromform->name;
140 foreach($configs as $config) {
141 $settings[$config] = $fromform->$config;
142 }
143 $success = $instance->set_option($settings);
144 } else {
dbc01944 145 $success = repository::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
c65dd2fe 146 $data = data_submitted();
147 }
148 if ($success) {
149 $savedstr = get_string('configsaved', 'repository');
150 //admin_externalpage_print_header();
151 print_heading($savedstr);
152 redirect($baseurl, $savedstr, 3);
153 } else {
154 print_error('instancenotsaved', 'repository', $baseurl);
155 }
156 exit;
157 } else { // Display the form
158 // admin_externalpage_print_header();
159 print_heading(get_string('configplugin', 'repository_'.$plugin));
160 print_simple_box_start();
161 $mform->display();
162 print_simple_box_end();
163 $return = false;
faaa613d 164 }
c65dd2fe 165 } else if (!empty($delete)) {
166 // admin_externalpage_print_header();
dbc01944 167 $instance = repository::get_instance($delete);
948c2860 168 //if you try to delete an instance set as readonly, display an error message
169 if ($instance->readonly) {
170 throw new repository_exception('readonlyinstance', 'repository');
171 }
c65dd2fe 172 if ($sure) {
173 if (!confirm_sesskey()) {
174 print_error('confirmsesskeybad', '', $baseurl);
175 }
176 if ($instance->delete()) {
177 $deletedstr = get_string('instancedeleted', 'repository');
178 print_heading($deletedstr);
179 redirect($baseurl, $deletedstr, 3);
180 } else {
181 print_error('instancenotdeleted', 'repository', $baseurl);
182 }
183 exit;
faaa613d 184 }
c65dd2fe 185 notice_yesno(get_string('confirmdelete', 'repository', $instance->name), $baseurl . '&amp;delete=' . $delete . '&amp;sure=yes', $baseurl);
186 $return = false;
faaa613d 187 } else {
dbc01944 188 repository::display_instances_list($context);
faaa613d 189 $return = false;
190 }
c65dd2fe 191
192 if (!empty($return)) {
193 redirect($baseurl);
faaa613d 194 }
c65dd2fe 195
196 print_footer($course);