MDL-15404: fix few bugs in repository administration, add a beta version of the cours...
[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);
13
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}
43
44$baseurl = $CFG->wwwroot . '/repository/manage_instances.php?contextid=' . $contextid . '&amp;sesskey='. sesskey();
45
46//security: we cannot perform any action if the type is not visible
47if (!empty($new)){
48 $type = repository_get_type_by_typename($new);
49} else if (!empty($edit)){
50 $instance = repository_get_instance($edit);
51 $type = repository_get_type_by_id($instance->typeid);
52} else if (!empty($delete)){
53 $instance = repository_get_instance($delete);
54 $type = repository_get_type_by_id($instance->typeid);
55}
56if (isset($type) && !$type->get_visible()) {
57 print_error('typenotvisible', 'repository', $baseurl);
58}
59
60
61
62//Create header crumbtrail
63//$streditrepositoryaccount = get_string("editrepositoryinstance",'repository');
64$navlinks = array();
65if (!empty($course)) {
66 $navlinks[] = array('name' => $course->shortname,
67 'link' => "$CFG->wwwroot/course/view.php?id=$course->id",
68 'type' => 'misc');
69 $navlinks[] = array('name' => $pagename,
70 'link' => null,
71 'type' => 'misc');
72 $title = $pagename;
73 $fullname = $course->fullname;
74}
75$navigation = build_navigation($navlinks);
76
77//display page header
78print_header($title, $fullname, $navigation);
79print_heading($pagename);
80
81$return = true;
82
83if (!empty($edit) || !empty($new)) {
84 if (!empty($edit)) {
85 $instance = repository_get_instance($edit);
86 $instancetype = repository_get_type_by_id($instance->typeid);
87 $classname = 'repository_' . $instancetype->get_typename();
88 $configs = $instance->get_instance_option_names();
89 $plugin = $instancetype->get_typename();
90 $typeid = $instance->typeid;
91 } else {
92 $plugin = $new;
93 $typeid = $new;
94 $instance = null;
95 }
96
97 // create edit form for this instance
98 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
99 // end setup, begin output
100
101 if ($mform->is_cancelled()){
102 redirect($baseurl);
103 exit;
104 } else if ($fromform = $mform->get_data()){
105 if (!confirm_sesskey()) {
106 print_error('confirmsesskeybad', '', $baseurl);
107 }
108 if ($edit) {
109 $settings = array();
110 $settings['name'] = $fromform->name;
111 foreach($configs as $config) {
112 $settings[$config] = $fromform->$config;
113 }
114 $success = $instance->set_option($settings);
115 } else {
116 $success = repository_static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
117 $data = data_submitted();
118 }
119 if ($success) {
120 $savedstr = get_string('configsaved', 'repository');
121 //admin_externalpage_print_header();
122 print_heading($savedstr);
123 redirect($baseurl, $savedstr, 3);
124 } else {
125 print_error('instancenotsaved', 'repository', $baseurl);
126 }
127 exit;
128 } else {
129 // admin_externalpage_print_header();
130 print_heading(get_string('configplugin', 'repository_'.$plugin));
131 print_simple_box_start();
132 $mform->display();
133 print_simple_box_end();
134 $return = false;
135 }
136} else if (!empty($delete)) {
137 // admin_externalpage_print_header();
138 $instance = repository_get_instance($delete);
139 if ($sure) {
140 if (!confirm_sesskey()) {
141 print_error('confirmsesskeybad', '', $baseurl);
142 }
143 if ($instance->delete()) {
144 $deletedstr = get_string('instancedeleted', 'repository');
145 print_heading($deletedstr);
146 redirect($baseurl, $deletedstr, 3);
147 } else {
148 print_error('instancenotdeleted', 'repository', $baseurl);
149 }
150 exit;
151 }
152 notice_yesno(get_string('confirmdelete', 'repository', $instance->name), $baseurl . '&amp;delete=' . $delete . '&amp;sure=yes', $baseurl);
153 $return = false;
154} else {
155 repository_display_instances_list($context);
156 $return = false;
157}
158
159if (!empty($return)) {
160 redirect($baseurl);
161}
162
163//display page footer
164print_footer($course);