mnet MDL-21019 Removed hard coded strings
[moodle.git] / repository / manage_instances.php
CommitLineData
4317f92f 1<?php
c65dd2fe 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
f3a6cd67 14 $url = new moodle_url($CFG->wwwroot.'/repository/manage_instances.php');
15
c65dd2fe 16 if ($edit){
f3a6cd67 17 $url->param('edit', $edit);
c65dd2fe 18 $pagename = 'repositoryinstanceedit';
19 } else if ($delete) {
f3a6cd67 20 $url->param('delete', $delete);
c65dd2fe 21 $pagename = 'repositorydelete';
22 } else if ($new) {
f3a6cd67 23 $url->param('new', $new);
c65dd2fe 24 $pagename = 'repositoryinstancenew';
25 } else {
26 $pagename = 'repositorylist';
6c91ce49 27 }
c65dd2fe 28
f3a6cd67 29 if ($sure !== '') {
30 $url->param('sure', $sure);
31 }
32 if ($contextid !== 0) {
33 $url->param('contextid', $contextid);
34 }
35 if ($usercourseid !== SITEID) {
36 $url->param('usercourseid', $usercourseid);
37 }
38
39 $PAGE->set_url($url);
40
c65dd2fe 41 require_login(SITEID, false);
42
43 $context = get_context_instance_by_id($contextid);
44
45/// Security: make sure we're allowed to do this operation
46 if ($context->contextlevel == CONTEXT_COURSE) {
47 $pagename = get_string("repositorycourse",'repository');
48
49 // If the user is allowed to edit this course, he's allowed to edit list of repository instances
50 require_capability('moodle/course:update', $context);
51
52 if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
53 print_error('invalidcourseid');
54 }
55
56 } else if ($context->contextlevel == CONTEXT_USER) {
57 $pagename = get_string("personalrepositories",'repository');
58 //is the user looking at its own repository instances
59 if ($USER->id != $context->instanceid){
60 print_error('notyourinstances', 'repository');
61 }
62 $user = $USER;
63
64 } else {
65 // throw an error here
f48fb4d6 66 print_error('wrongcontextid');
c65dd2fe 67 exit;
12c79bfd 68 }
c65dd2fe 69
70 $baseurl = $CFG->wwwroot . '/repository/manage_instances.php?contextid=' . $contextid . '&amp;sesskey='. sesskey();
71
72
f48fb4d6 73/// Security: we cannot perform any action if the type is not visible or if the context has been disabled
c65dd2fe 74 if (!empty($new)){
dbc01944 75 $type = repository::get_type_by_typename($new);
c65dd2fe 76 } else if (!empty($edit)){
dbc01944 77 $instance = repository::get_instance($edit);
b317f67a 78 $type = repository::get_type_by_id($instance->options['typeid']);
c65dd2fe 79 } else if (!empty($delete)){
dbc01944 80 $instance = repository::get_instance($delete);
b317f67a 81 $type = repository::get_type_by_id($instance->options['typeid']);
c65dd2fe 82 }
f48fb4d6 83 if (isset($type) && ( !$type->get_visible() || (!$type->get_contextvisibility($context->contextlevel)) ) ) {
c65dd2fe 84 print_error('typenotvisible', 'repository', $baseurl);
85 }
86
87
88/// Create navigation links
c65dd2fe 89 if (!empty($course)) {
d9d8dbaa 90 $PAGE->navbar->add($pagename);
c65dd2fe 91 $fullname = $course->fullname;
faaa613d 92 } else {
c65dd2fe 93 $fullname = fullname($user);
94 $strrepos = get_string('repositories', 'repository');
d9d8dbaa 95 $PAGE->navbar->add($fullname, new moodle_url($CFG->wwwroot.'/user/view.php', array('id'=>$user->id)));
96 $PAGE->navbar->add($strrepos);
faaa613d 97 }
98
c65dd2fe 99 $title = $pagename;
c65dd2fe 100
101/// Display page header
d9d8dbaa 102 $PAGE->set_title();
103 $PAGE->set_heading($fullname);
104 echo $OUTPUT->header();
0ec691c4 105
106 if ($context->contextlevel == CONTEXT_USER) {
107 if ( !$course = $DB->get_record('course', array('id'=>$usercourseid))) {
108 print_error('invalidcourseid');
109 }
110 $currenttab = 'repositories';
111 include($CFG->dirroot.'/user/tabs.php');
112 }
113
88ed16ec 114 echo $OUTPUT->heading($pagename);
c65dd2fe 115
116 $return = true;
117
118 if (!empty($edit) || !empty($new)) {
119 if (!empty($edit)) {
dbc01944 120 $instance = repository::get_instance($edit);
948c2860 121 //if you try to edit an instance set as readonly, display an error message
122 if ($instance->readonly) {
123 throw new repository_exception('readonlyinstance', 'repository');
124 }
b317f67a 125 $instancetype = repository::get_type_by_id($instance->options['typeid']);
c65dd2fe 126 $classname = 'repository_' . $instancetype->get_typename();
127 $configs = $instance->get_instance_option_names();
128 $plugin = $instancetype->get_typename();
b317f67a 129 $typeid = $instance->options['typeid'];
c65dd2fe 130 } else {
131 $plugin = $new;
132 $typeid = $new;
133 $instance = null;
faaa613d 134 }
c65dd2fe 135
136 /// Create edit form for this instance
137 $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
138
139 /// Process the form data if any, or display
140 if ($mform->is_cancelled()){
141 redirect($baseurl);
142 exit;
143
144 } else if ($fromform = $mform->get_data()){
145 if (!confirm_sesskey()) {
146 print_error('confirmsesskeybad', '', $baseurl);
faaa613d 147 }
c65dd2fe 148 if ($edit) {
149 $settings = array();
150 $settings['name'] = $fromform->name;
151 foreach($configs as $config) {
152 $settings[$config] = $fromform->$config;
153 }
154 $success = $instance->set_option($settings);
155 } else {
dbc01944 156 $success = repository::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
c65dd2fe 157 $data = data_submitted();
158 }
159 if ($success) {
160 $savedstr = get_string('configsaved', 'repository');
161 //admin_externalpage_print_header();
88ed16ec 162 echo $OUTPUT->heading($savedstr);
c65dd2fe 163 redirect($baseurl, $savedstr, 3);
164 } else {
165 print_error('instancenotsaved', 'repository', $baseurl);
166 }
167 exit;
168 } else { // Display the form
169 // admin_externalpage_print_header();
88ed16ec 170 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
abdcded6 171 $OUTPUT->box_start();
c65dd2fe 172 $mform->display();
abdcded6 173 $OUTPUT->box_end();
c65dd2fe 174 $return = false;
faaa613d 175 }
c65dd2fe 176 } else if (!empty($delete)) {
177 // admin_externalpage_print_header();
dbc01944 178 $instance = repository::get_instance($delete);
948c2860 179 //if you try to delete an instance set as readonly, display an error message
180 if ($instance->readonly) {
181 throw new repository_exception('readonlyinstance', 'repository');
182 }
c65dd2fe 183 if ($sure) {
184 if (!confirm_sesskey()) {
185 print_error('confirmsesskeybad', '', $baseurl);
186 }
187 if ($instance->delete()) {
188 $deletedstr = get_string('instancedeleted', 'repository');
88ed16ec 189 echo $OUTPUT->heading($deletedstr);
c65dd2fe 190 redirect($baseurl, $deletedstr, 3);
191 } else {
192 print_error('instancenotdeleted', 'repository', $baseurl);
193 }
194 exit;
faaa613d 195 }
55400db0 196 $formcontinue = html_form::make_button($baseurl, array('delete' => $delete, 'sure' => 'yes'), get_string('yes'));
197 $formcancel = html_form::make_button($baseurl, array(), get_string('no'));
198 echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name), $formcontinue, $formcancel);
c65dd2fe 199 $return = false;
faaa613d 200 } else {
dbc01944 201 repository::display_instances_list($context);
faaa613d 202 $return = false;
203 }
c65dd2fe 204
205 if (!empty($return)) {
206 redirect($baseurl);
faaa613d 207 }
c65dd2fe 208
6fd42112 209 echo $OUTPUT->footer();