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