Merge branch 'wip-mdl-30993' of git://github.com/rajeshtaneja/moodle
[moodle.git] / admin / repository.php
blobc3409a5b5a0e342b733f1637d79a5ce2c2ef7afb
1 <?php
3 require_once(dirname(dirname(__FILE__)) . '/config.php');
4 require_once($CFG->dirroot . '/repository/lib.php');
5 require_once($CFG->libdir . '/adminlib.php');
7 $repository = optional_param('repos', '', PARAM_FORMAT);
8 $action = optional_param('action', '', PARAM_ALPHA);
9 $sure = optional_param('sure', '', PARAM_ALPHA);
11 $display = true; // fall through to normal display
13 $pagename = 'managerepositories';
15 if ($action == 'edit') {
16 $pagename = 'repositorysettings' . $repository;
17 } else if ($action == 'delete') {
18 $pagename = 'repositorydelete';
19 } else if (($action == 'newon') || ($action == 'newoff')) {
20 $pagename = 'repositorynew';
23 // Need to remember this for form
24 $formaction = $action;
26 // Check what visibility to show the new repository
27 if ($action == 'newon') {
28 $action = 'new';
29 $visible = true;
30 } else if ($action == 'newoff') {
31 $action = 'new';
32 $visible = false;
35 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
36 admin_externalpage_setup($pagename);
38 $sesskeyurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php?sesskey=' . sesskey();
39 $baseurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php';
41 $configstr = get_string('manage', 'repository');
43 $return = true;
45 /**
46 * Helper function that generates a moodle_url object
47 * relevant to the repository
49 function repository_action_url($repository) {
50 global $baseurl;
51 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'repos'=>$repository));
54 if (($action == 'edit') || ($action == 'new')) {
55 $pluginname = '';
56 if ($action == 'edit') {
57 $repositorytype = repository::get_type_by_typename($repository);
58 $classname = 'repository_' . $repositorytype->get_typename();
59 $configs = call_user_func(array($classname, 'get_type_option_names'));
60 $plugin = $repositorytype->get_typename();
61 // looking for instance to edit plugin name
62 $instanceoptions = call_user_func(array($classname, 'get_instance_option_names'));
63 if (empty($instanceoptions)) {
64 $params = array();
65 $params['type'] = $plugin;
66 $instances = repository::get_instances($params);
67 if ($instance = array_pop($instances)) {
68 // use the one form db record
69 $pluginname = $instance->instance->name;
73 } else {
74 $repositorytype = null;
75 $plugin = $repository;
76 $typeid = $repository;
78 $PAGE->set_pagetype('admin-repository-' . $plugin);
79 // display the edit form for this instance
80 $mform = new repository_type_form('', array('pluginname'=>$pluginname, 'plugin' => $plugin, 'instance' => $repositorytype, 'action' => $formaction));
81 $fromform = $mform->get_data();
83 //detect if we create a new type without config (in this case if don't want to display a setting page during creation)
84 $nosettings = false;
85 if ($action == 'new') {
86 $adminconfignames = repository::static_function($repository, 'get_type_option_names');
87 $nosettings = empty($adminconfignames);
89 // end setup, begin output
91 if ($mform->is_cancelled()){
92 redirect($baseurl);
93 } else if (!empty($fromform) || $nosettings) {
94 require_sesskey();
95 if ($action == 'edit') {
96 $settings = array();
97 foreach($configs as $config) {
98 if (!empty($fromform->$config)) {
99 $settings[$config] = $fromform->$config;
100 } else {
101 // if the config name is not appear in $fromform
102 // empty this config value
103 $settings[$config] = '';
106 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
107 if (!empty($instanceoptionnames)) {
108 if (array_key_exists('enablecourseinstances', $fromform)) {
109 $settings['enablecourseinstances'] = $fromform->enablecourseinstances;
111 else {
112 $settings['enablecourseinstances'] = 0;
114 if (array_key_exists('enableuserinstances', $fromform)) {
115 $settings['enableuserinstances'] = $fromform->enableuserinstances;
117 else {
118 $settings['enableuserinstances'] = 0;
121 $success = $repositorytype->update_options($settings);
122 } else {
123 $type = new repository_type($plugin, (array)$fromform, $visible);
124 $type->create();
125 $success = true;
126 $data = data_submitted();
128 if ($success) {
129 // configs saved
130 redirect($baseurl);
131 } else {
132 print_error('instancenotsaved', 'repository', $baseurl);
134 exit;
135 } else {
136 echo $OUTPUT->header();
137 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
138 $displaysettingform = true;
139 if ($action == 'edit') {
140 $typeoptionnames = repository::static_function($repository, 'get_type_option_names');
141 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
142 if (empty($typeoptionnames) && empty($instanceoptionnames)) {
143 $displaysettingform = false;
146 if ($displaysettingform){
147 $OUTPUT->box_start();
148 $mform->display();
149 $OUTPUT->box_end();
151 $return = false;
153 // Display instances list and creation form
154 if ($action == 'edit') {
155 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
156 if (!empty($instanceoptionnames)) {
157 repository::display_instances_list(get_context_instance(CONTEXT_SYSTEM), $repository);
161 } else if ($action == 'show') {
162 if (!confirm_sesskey()) {
163 print_error('confirmsesskeybad', '', $baseurl);
165 $repositorytype = repository::get_type_by_typename($repository);
166 if (empty($repositorytype)) {
167 print_error('invalidplugin', 'repository', '', $repository);
169 $repositorytype->update_visibility(true);
170 $return = true;
171 } else if ($action == 'hide') {
172 if (!confirm_sesskey()) {
173 print_error('confirmsesskeybad', '', $baseurl);
175 $repositorytype = repository::get_type_by_typename($repository);
176 if (empty($repositorytype)) {
177 print_error('invalidplugin', 'repository', '', $repository);
179 $repositorytype->update_visibility(false);
180 $return = true;
181 } else if ($action == 'delete') {
182 $repositorytype = repository::get_type_by_typename($repository);
183 if ($sure) {
184 $PAGE->set_pagetype('admin-repository-' . $repository);
185 if (!confirm_sesskey()) {
186 print_error('confirmsesskeybad', '', $baseurl);
188 if ($repositorytype->delete()) {
189 redirect($baseurl);
190 } else {
191 print_error('instancenotdeleted', 'repository', $baseurl);
193 exit;
194 } else {
195 echo $OUTPUT->header();
196 echo $OUTPUT->confirm(get_string('confirmremove', 'repository', $repositorytype->get_readablename()), $sesskeyurl . '&action=delete&repos=' . $repository . '&sure=yes', $baseurl);
197 $return = false;
199 } else if ($action == 'moveup') {
200 $repositorytype = repository::get_type_by_typename($repository);
201 $repositorytype->move_order('up');
202 } else if ($action == 'movedown') {
203 $repositorytype = repository::get_type_by_typename($repository);
204 $repositorytype->move_order('down');
205 } else {
206 // If page is loaded directly
207 echo $OUTPUT->header();
208 echo $OUTPUT->heading(get_string('manage', 'repository'));
210 // Get strings that are used
211 $strshow = get_string('on', 'repository');
212 $strhide = get_string('off', 'repository');
213 $strdelete = get_string('disabled', 'repository');
215 $actionchoicesforexisting = array(
216 'show' => $strshow,
217 'hide' => $strhide,
218 'delete' => $strdelete
221 $actionchoicesfornew = array(
222 'newon' => $strshow,
223 'newoff' => $strhide,
224 'delete' => $strdelete
227 $output = '';
228 $output .= $OUTPUT->box_start('generalbox');
230 // Set strings that are used multiple times
231 $settingsstr = get_string('settings');
232 $disablestr = get_string('disable');
234 // Table to list plug-ins
235 $table = new html_table();
236 $table->head = array(get_string('name'), get_string('isactive', 'repository'), get_string('order'), $settingsstr);
237 $table->align = array('left', 'center', 'center', 'center', 'center');
238 $table->data = array();
240 // Get list of used plug-ins
241 $instances = repository::get_types();
242 if (!empty($instances)) {
243 // Array to store plugins being used
244 $alreadyplugins = array();
245 $totalinstances = count($instances);
246 $updowncount = 1;
247 foreach ($instances as $i) {
248 $settings = '';
249 $typename = $i->get_typename();
250 // Display edit link only if you can config the type or if it has multiple instances (e.g. has instance config)
251 $typeoptionnames = repository::static_function($typename, 'get_type_option_names');
252 $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
254 if (!empty($typeoptionnames) || !empty($instanceoptionnames)) {
255 // Calculate number of instances in order to display them for the Moodle administrator
256 if (!empty($instanceoptionnames)) {
257 $params = array();
258 $params['context'] = array(get_system_context());
259 $params['onlyvisible'] = false;
260 $params['type'] = $typename;
261 $admininstancenumber = count(repository::static_function($typename, 'get_instances', $params));
262 // site instances
263 $admininstancenumbertext = get_string('instancesforsite', 'repository', $admininstancenumber);
264 $params['context'] = array();
265 $instances = repository::static_function($typename, 'get_instances', $params);
266 $courseinstances = array();
267 $userinstances = array();
269 foreach ($instances as $instance) {
270 if ($instance->context->contextlevel == CONTEXT_COURSE) {
271 $courseinstances[] = $instance;
272 } else if ($instance->context->contextlevel == CONTEXT_USER) {
273 $userinstances[] = $instance;
276 // course instances
277 $instancenumber = count($courseinstances);
278 $courseinstancenumbertext = get_string('instancesforcourses', 'repository', $instancenumber);
280 // user private instances
281 $instancenumber = count($userinstances);
282 $userinstancenumbertext = get_string('instancesforusers', 'repository', $instancenumber);
283 } else {
284 $admininstancenumbertext = "";
285 $courseinstancenumbertext = "";
286 $userinstancenumbertext = "";
289 $settings .= '<a href="' . $sesskeyurl . '&amp;action=edit&amp;repos=' . $typename . '">' . $settingsstr .'</a>';
291 $settings .= $OUTPUT->container_start('mdl-left');
292 $settings .= '<br/>';
293 $settings .= $admininstancenumbertext;
294 $settings .= '<br/>';
295 $settings .= $courseinstancenumbertext;
296 $settings .= '<br/>';
297 $settings .= $userinstancenumbertext;
298 $settings .= $OUTPUT->container_end();
300 // Get the current visibility
301 if ($i->get_visible()) {
302 $currentaction = 'show';
303 } else {
304 $currentaction = 'hide';
307 $select = new single_select(repository_action_url($typename, 'repos'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . basename($typename));
309 // Display up/down link
310 $updown = '';
311 $spacer = $OUTPUT->spacer(array('height'=>15, 'width'=>15)); // should be done with CSS instead
313 if ($updowncount > 1) {
314 $updown .= "<a href=\"$sesskeyurl&amp;action=moveup&amp;repos=".$typename."\">";
315 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/up') . "\" alt=\"up\" /></a>&nbsp;";
317 else {
318 $updown .= $spacer;
320 if ($updowncount < $totalinstances) {
321 $updown .= "<a href=\"$sesskeyurl&amp;action=movedown&amp;repos=".$typename."\">";
322 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/down') . "\" alt=\"down\" /></a>";
324 else {
325 $updown .= $spacer;
328 $updowncount++;
330 $table->data[] = array($i->get_readablename(), $OUTPUT->render($select), $updown, $settings);
332 if (!in_array($typename, $alreadyplugins)) {
333 $alreadyplugins[] = $typename;
338 // Get all the plugins that exist on disk
339 $plugins = get_plugin_list('repository');
340 if (!empty($plugins)) {
341 foreach ($plugins as $plugin => $dir) {
342 // Check that it has not already been listed
343 if (!in_array($plugin, $alreadyplugins)) {
344 $select = new single_select(repository_action_url($plugin, 'repos'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . basename($plugin));
345 $table->data[] = array(get_string('pluginname', 'repository_'.$plugin), $OUTPUT->render($select), '', '');
350 $output .= html_writer::table($table);
351 $output .= $OUTPUT->box_end();
352 print $output;
353 $return = false;
356 if ($return) {
357 redirect($baseurl);
359 echo $OUTPUT->footer();