MDL-12563 Fixed RSS dates so that they are always GMT
[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);
12
13 $display = true; // fall through to normal display
14
15 if ($edit){
16 $pagename = 'repositoryinstanceedit';
17 } else if ($delete) {
18 $pagename = 'repositorydelete';
19 } else if ($new) {
20 $pagename = 'repositoryinstancenew';
21 } else {
22 $pagename = 'repositorylist';
6c91ce49 23 }
c65dd2fe 24
25 require_login(SITEID, false);
26
27 $context = get_context_instance_by_id($contextid);
28
29/// Security: make sure we're allowed to do this operation
30 if ($context->contextlevel == CONTEXT_COURSE) {
31 $pagename = get_string("repositorycourse",'repository');
32
33 // If the user is allowed to edit this course, he's allowed to edit list of repository instances
34 require_capability('moodle/course:update', $context);
35
36 if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
37 print_error('invalidcourseid');
38 }
39
40 } else if ($context->contextlevel == CONTEXT_USER) {
41 $pagename = get_string("personalrepositories",'repository');
42 //is the user looking at its own repository instances
43 if ($USER->id != $context->instanceid){
44 print_error('notyourinstances', 'repository');
45 }
46 $user = $USER;
47
48 } else {
49 // throw an error here
50 exit;
12c79bfd 51 }
c65dd2fe 52
53 $baseurl = $CFG->wwwroot . '/repository/manage_instances.php?contextid=' . $contextid . '&amp;sesskey='. sesskey();
54
55
56/// Security: we cannot perform any action if the type is not visible
57 if (!empty($new)){
58 $type = repository_get_type_by_typename($new);
59 } else if (!empty($edit)){
faaa613d 60 $instance = repository_get_instance($edit);
c65dd2fe 61 $type = repository_get_type_by_id($instance->typeid);
62 } else if (!empty($delete)){
63 $instance = repository_get_instance($delete);
64 $type = repository_get_type_by_id($instance->typeid);
65 }
66 if (isset($type) && !$type->get_visible()) {
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,
79 'type' => 'misc');
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);
94 print_heading($pagename);
95
96 $return = true;
97
98 if (!empty($edit) || !empty($new)) {
99 if (!empty($edit)) {
100 $instance = repository_get_instance($edit);
101 $instancetype = repository_get_type_by_id($instance->typeid);
102 $classname = 'repository_' . $instancetype->get_typename();
103 $configs = $instance->get_instance_option_names();
104 $plugin = $instancetype->get_typename();
105 $typeid = $instance->typeid;
106 } else {
107 $plugin = $new;
108 $typeid = $new;
109 $instance = null;
faaa613d 110 }
c65dd2fe 111
112 /// Create edit form for this instance
113 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
114
115 /// Process the form data if any, or display
116 if ($mform->is_cancelled()){
117 redirect($baseurl);
118 exit;
119
120 } else if ($fromform = $mform->get_data()){
121 if (!confirm_sesskey()) {
122 print_error('confirmsesskeybad', '', $baseurl);
faaa613d 123 }
c65dd2fe 124 if ($edit) {
125 $settings = array();
126 $settings['name'] = $fromform->name;
127 foreach($configs as $config) {
128 $settings[$config] = $fromform->$config;
129 }
130 $success = $instance->set_option($settings);
131 } else {
132 $success = repository_static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
133 $data = data_submitted();
134 }
135 if ($success) {
136 $savedstr = get_string('configsaved', 'repository');
137 //admin_externalpage_print_header();
138 print_heading($savedstr);
139 redirect($baseurl, $savedstr, 3);
140 } else {
141 print_error('instancenotsaved', 'repository', $baseurl);
142 }
143 exit;
144 } else { // Display the form
145 // admin_externalpage_print_header();
146 print_heading(get_string('configplugin', 'repository_'.$plugin));
147 print_simple_box_start();
148 $mform->display();
149 print_simple_box_end();
150 $return = false;
faaa613d 151 }
c65dd2fe 152 } else if (!empty($delete)) {
153 // admin_externalpage_print_header();
154 $instance = repository_get_instance($delete);
155 if ($sure) {
156 if (!confirm_sesskey()) {
157 print_error('confirmsesskeybad', '', $baseurl);
158 }
159 if ($instance->delete()) {
160 $deletedstr = get_string('instancedeleted', 'repository');
161 print_heading($deletedstr);
162 redirect($baseurl, $deletedstr, 3);
163 } else {
164 print_error('instancenotdeleted', 'repository', $baseurl);
165 }
166 exit;
faaa613d 167 }
c65dd2fe 168 notice_yesno(get_string('confirmdelete', 'repository', $instance->name), $baseurl . '&amp;delete=' . $delete . '&amp;sure=yes', $baseurl);
169 $return = false;
faaa613d 170 } else {
c65dd2fe 171 repository_display_instances_list($context);
faaa613d 172 $return = false;
173 }
c65dd2fe 174
175 if (!empty($return)) {
176 redirect($baseurl);
faaa613d 177 }
c65dd2fe 178
179 print_footer($course);