MDL-13766 Tidying
[moodle.git] / repository / manage_instances.php
CommitLineData
faaa613d 1<?php
2// $Id$
3require_once(dirname(dirname(__FILE__)) . '/config.php');
4require_once($CFG->dirroot . '/repository/lib.php');
5//require_once($CFG->libdir . '/adminlib.php');
6
7// id of repository
8$edit = optional_param('edit', 0, PARAM_INT);
9$new = optional_param('new', '', PARAM_FORMAT);
10$delete = optional_param('delete', 0, PARAM_INT);
11$sure = optional_param('sure', '', PARAM_ALPHA);
12$contextid = optional_param('contextid', 0, PARAM_INT);
12c79bfd 13//$userid = optional_param('userid', 0, PARAM_INT);
faaa613d 14$display = true; // fall through to normal display
15
16if ($edit){
17 $pagename = 'repositoryinstanceedit';
18}else if ($delete) {
19 $pagename = 'repositorydelete';
20} else if ($new) {
21 $pagename = 'repositoryinstancenew';
22}
23else {
24 $pagename = 'repositorylist';
25}
26
27require_login(SITEID, false);
28
29$context = get_context_instance_by_id($contextid);
30
31//security: detect if we are in a course context
32if ($context->contextlevel == CONTEXT_COURSE) {
33 $pagename = get_string("repositorycourse",'repository');
34
35 //is the user is allowed to edit this course, he's allowed to edit list of repository instances
36 require_capability('moodle/course:update', $context);
37 //retrieve course
38 //Retrieve the course object
39 if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
40 print_error('invalidcourseid');
41 }
42}
12c79bfd 43else {
44 $pagename = get_string("personalrepositories",'repository');
45 //is the user looking at its own repository instances
46 if ($USER->id != $context->instanceid){
47 print_error('notyourinstances', 'repository');
6c91ce49 48 }
49 if ($context->contextlevel != CONTEXT_USER){
50 print_error('wrongcontext', 'repository');
12c79bfd 51 }
52 $user = $USER;
53}
faaa613d 54
55$baseurl = $CFG->wwwroot . '/repository/manage_instances.php?contextid=' . $contextid . '&amp;sesskey='. sesskey();
56
57//security: we cannot perform any action if the type is not visible
58if (!empty($new)){
59 $type = repository_get_type_by_typename($new);
60} else if (!empty($edit)){
61 $instance = repository_get_instance($edit);
62 $type = repository_get_type_by_id($instance->typeid);
63} else if (!empty($delete)){
64 $instance = repository_get_instance($delete);
65 $type = repository_get_type_by_id($instance->typeid);
66}
67if (isset($type) && !$type->get_visible()) {
68 print_error('typenotvisible', 'repository', $baseurl);
69}
70
71
72
73//Create header crumbtrail
74//$streditrepositoryaccount = get_string("editrepositoryinstance",'repository');
75$navlinks = array();
76if (!empty($course)) {
77 $navlinks[] = array('name' => $course->shortname,
78 'link' => "$CFG->wwwroot/course/view.php?id=$course->id",
79 'type' => 'misc');
80 $navlinks[] = array('name' => $pagename,
81 'link' => null,
12c79bfd 82 'type' => 'misc');
faaa613d 83 $fullname = $course->fullname;
84}
12c79bfd 85else {
86 $fullname = fullname($user);
87 $strrepos = get_string('repositories', 'repository');
88 $navlinks[] = array('name' => $fullname, 'link' => $CFG->wwwroot . '/user/view.php?id=' . $user->id, 'type' => 'misc');
89 $navlinks[] = array('name' => $strrepos, 'link' => null, 'type' => 'misc');
90}
91
92$title = $pagename;
faaa613d 93$navigation = build_navigation($navlinks);
94
95//display page header
96print_header($title, $fullname, $navigation);
97print_heading($pagename);
98
99$return = true;
100
101if (!empty($edit) || !empty($new)) {
102 if (!empty($edit)) {
103 $instance = repository_get_instance($edit);
104 $instancetype = repository_get_type_by_id($instance->typeid);
105 $classname = 'repository_' . $instancetype->get_typename();
106 $configs = $instance->get_instance_option_names();
107 $plugin = $instancetype->get_typename();
108 $typeid = $instance->typeid;
109 } else {
110 $plugin = $new;
111 $typeid = $new;
112 $instance = null;
113 }
114
115 // create edit form for this instance
116 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
117 // end setup, begin output
118
119 if ($mform->is_cancelled()){
120 redirect($baseurl);
121 exit;
122 } else if ($fromform = $mform->get_data()){
123 if (!confirm_sesskey()) {
124 print_error('confirmsesskeybad', '', $baseurl);
125 }
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 {
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;
153 }
154} else if (!empty($delete)) {
155 // admin_externalpage_print_header();
156 $instance = repository_get_instance($delete);
157 if ($sure) {
158 if (!confirm_sesskey()) {
159 print_error('confirmsesskeybad', '', $baseurl);
160 }
161 if ($instance->delete()) {
162 $deletedstr = get_string('instancedeleted', 'repository');
163 print_heading($deletedstr);
164 redirect($baseurl, $deletedstr, 3);
165 } else {
166 print_error('instancenotdeleted', 'repository', $baseurl);
167 }
168 exit;
169 }
170 notice_yesno(get_string('confirmdelete', 'repository', $instance->name), $baseurl . '&amp;delete=' . $delete . '&amp;sure=yes', $baseurl);
171 $return = false;
172} else {
173 repository_display_instances_list($context);
174 $return = false;
175}
176
177if (!empty($return)) {
178 redirect($baseurl);
179}
180
181//display page footer
182print_footer($course);