MDL-57007 behat: assume all capabilities during installation
[moodle.git] / admin / repository.php
blobbf10f510c4ec8c2f1d3a169d943d569a3cdb6188
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 require_once(__DIR__ . '/../config.php');
18 require_once($CFG->dirroot . '/repository/lib.php');
19 require_once($CFG->libdir . '/adminlib.php');
21 $repository = optional_param('repos', '', PARAM_ALPHANUMEXT);
22 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
23 $sure = optional_param('sure', '', PARAM_ALPHA);
24 $downloadcontents = optional_param('downloadcontents', false, PARAM_BOOL);
26 $display = true; // fall through to normal display
28 $pagename = 'managerepositories';
30 if ($action == 'edit') {
31 $pagename = 'repositorysettings' . $repository;
32 } else if ($action == 'delete') {
33 $pagename = 'repositorydelete';
34 } else if (($action == 'newon') || ($action == 'newoff')) {
35 $pagename = 'repositorynew';
38 // Need to remember this for form
39 $formaction = $action;
41 // Check what visibility to show the new repository
42 if ($action == 'newon') {
43 $action = 'new';
44 $visible = true;
45 } else if ($action == 'newoff') {
46 $action = 'new';
47 $visible = false;
50 require_capability('moodle/site:config', context_system::instance());
51 admin_externalpage_setup($pagename);
53 $sesskeyurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php?sesskey=' . sesskey();
54 $baseurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php';
56 $configstr = get_string('manage', 'repository');
58 $return = true;
60 if (!empty($action)) {
61 require_sesskey();
64 /**
65 * Helper function that generates a moodle_url object
66 * relevant to the repository
68 function repository_action_url($repository) {
69 global $baseurl;
70 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'repos'=>$repository));
73 if (($action == 'edit') || ($action == 'new')) {
74 $pluginname = '';
75 if ($action == 'edit') {
76 $repositorytype = repository::get_type_by_typename($repository);
77 $classname = 'repository_' . $repositorytype->get_typename();
78 $configs = call_user_func(array($classname, 'get_type_option_names'));
79 $plugin = $repositorytype->get_typename();
80 // looking for instance to edit plugin name
81 $instanceoptions = call_user_func(array($classname, 'get_instance_option_names'));
82 if (empty($instanceoptions)) {
83 $params = array();
84 $params['type'] = $plugin;
85 $instances = repository::get_instances($params);
86 if ($instance = array_pop($instances)) {
87 // use the one form db record
88 $pluginname = $instance->instance->name;
92 } else {
93 $repositorytype = null;
94 $plugin = $repository;
95 $typeid = $repository;
97 $PAGE->set_pagetype('admin-repository-' . $plugin);
98 // display the edit form for this instance
99 $mform = new repository_type_form('', array('pluginname'=>$pluginname, 'plugin' => $plugin, 'instance' => $repositorytype, 'action' => $formaction));
100 $fromform = $mform->get_data();
102 //detect if we create a new type without config (in this case if don't want to display a setting page during creation)
103 $nosettings = false;
104 if ($action == 'new') {
105 $adminconfignames = repository::static_function($repository, 'get_type_option_names');
106 $nosettings = empty($adminconfignames);
108 // end setup, begin output
110 if ($mform->is_cancelled()){
111 redirect($baseurl);
112 } else if (!empty($fromform) || $nosettings) {
113 require_sesskey();
114 if ($action == 'edit') {
115 $settings = array();
116 foreach($configs as $config) {
117 if (!empty($fromform->$config)) {
118 $settings[$config] = $fromform->$config;
119 } else {
120 // if the config name is not appear in $fromform
121 // empty this config value
122 $settings[$config] = '';
125 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
126 if (!empty($instanceoptionnames)) {
127 if (array_key_exists('enablecourseinstances', $fromform)) {
128 $settings['enablecourseinstances'] = $fromform->enablecourseinstances;
130 else {
131 $settings['enablecourseinstances'] = 0;
133 if (array_key_exists('enableuserinstances', $fromform)) {
134 $settings['enableuserinstances'] = $fromform->enableuserinstances;
136 else {
137 $settings['enableuserinstances'] = 0;
140 $success = $repositorytype->update_options($settings);
141 } else {
142 $type = new repository_type($plugin, (array)$fromform, $visible);
143 $success = true;
144 if (!$repoid = $type->create()) {
145 $success = false;
147 $data = data_submitted();
149 if ($success) {
150 // configs saved
151 core_plugin_manager::reset_caches();
152 redirect($baseurl);
153 } else {
154 print_error('instancenotsaved', 'repository', $baseurl);
156 exit;
157 } else {
158 echo $OUTPUT->header();
159 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
160 $displaysettingform = true;
161 if ($action == 'edit') {
162 $typeoptionnames = repository::static_function($repository, 'get_type_option_names');
163 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
164 if (empty($typeoptionnames) && empty($instanceoptionnames)) {
165 $displaysettingform = false;
168 if ($displaysettingform){
169 $OUTPUT->box_start();
170 $mform->display();
171 $OUTPUT->box_end();
173 $return = false;
175 // Display instances list and creation form
176 if ($action == 'edit') {
177 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
178 if (!empty($instanceoptionnames)) {
179 repository::display_instances_list(context_system::instance(), $repository);
183 } else if ($action == 'show') {
184 if (!confirm_sesskey()) {
185 print_error('confirmsesskeybad', '', $baseurl);
187 $repositorytype = repository::get_type_by_typename($repository);
188 if (empty($repositorytype)) {
189 print_error('invalidplugin', 'repository', '', $repository);
191 $repositorytype->update_visibility(true);
192 core_plugin_manager::reset_caches();
193 $return = true;
194 } else if ($action == 'hide') {
195 if (!confirm_sesskey()) {
196 print_error('confirmsesskeybad', '', $baseurl);
198 $repositorytype = repository::get_type_by_typename($repository);
199 if (empty($repositorytype)) {
200 print_error('invalidplugin', 'repository', '', $repository);
202 $repositorytype->update_visibility(false);
203 core_plugin_manager::reset_caches();
204 $return = true;
205 } else if ($action == 'delete') {
206 $repositorytype = repository::get_type_by_typename($repository);
207 if ($sure) {
208 $PAGE->set_pagetype('admin-repository-' . $repository);
209 if (!confirm_sesskey()) {
210 print_error('confirmsesskeybad', '', $baseurl);
213 if ($repositorytype->delete($downloadcontents)) {
214 core_plugin_manager::reset_caches();
215 redirect($baseurl);
216 } else {
217 print_error('instancenotdeleted', 'repository', $baseurl);
219 exit;
220 } else {
221 echo $OUTPUT->header();
223 $message = get_string('confirmremove', 'repository', $repositorytype->get_readablename());
225 $output = $OUTPUT->box_start('generalbox', 'notice');
226 $output .= html_writer::tag('p', $message);
228 $removeurl = new moodle_url($sesskeyurl);
229 $removeurl->params(array(
230 'action' =>'delete',
231 'repos' => $repository,
232 'sure' => 'yes',
235 $removeanddownloadurl = new moodle_url($sesskeyurl);
236 $removeanddownloadurl->params(array(
237 'action' =>'delete',
238 'repos'=> $repository,
239 'sure' => 'yes',
240 'downloadcontents' => 1,
243 $output .= $OUTPUT->single_button($removeurl, get_string('continueuninstall', 'repository'));
244 $output .= $OUTPUT->single_button($removeanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
245 $output .= $OUTPUT->single_button($baseurl, get_string('cancel'));
246 $output .= $OUTPUT->box_end();
248 echo $output;
250 $return = false;
252 } else if ($action == 'moveup') {
253 $repositorytype = repository::get_type_by_typename($repository);
254 $repositorytype->move_order('up');
255 } else if ($action == 'movedown') {
256 $repositorytype = repository::get_type_by_typename($repository);
257 $repositorytype->move_order('down');
258 } else {
259 // If page is loaded directly
260 echo $OUTPUT->header();
261 echo $OUTPUT->heading(get_string('manage', 'repository'));
263 // Get strings that are used
264 $strshow = get_string('on', 'repository');
265 $strhide = get_string('off', 'repository');
266 $strdelete = get_string('disabled', 'repository');
267 $struninstall = get_string('uninstallplugin', 'core_admin');
269 $actionchoicesforexisting = array(
270 'show' => $strshow,
271 'hide' => $strhide,
272 'delete' => $strdelete
275 $actionchoicesfornew = array(
276 'newon' => $strshow,
277 'newoff' => $strhide,
278 'delete' => $strdelete
281 $output = '';
282 $output .= $OUTPUT->box_start('generalbox');
284 // Set strings that are used multiple times
285 $settingsstr = get_string('settings');
286 $disablestr = get_string('disable');
288 // Table to list plug-ins
289 $table = new html_table();
290 $table->head = array(get_string('name'), get_string('isactive', 'repository'), get_string('order'), $settingsstr, $struninstall);
292 $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign');
293 $table->id = 'repositoriessetting';
294 $table->data = array();
295 $table->attributes['class'] = 'admintable generaltable';
297 // Get list of used plug-ins
298 $repositorytypes = repository::get_types();
299 // Array to store plugins being used
300 $alreadyplugins = array();
301 if (!empty($repositorytypes)) {
302 $totalrepositorytypes = count($repositorytypes);
303 $updowncount = 1;
304 foreach ($repositorytypes as $i) {
305 $settings = '';
306 $typename = $i->get_typename();
307 // Display edit link only if you can config the type or if it has multiple instances (e.g. has instance config)
308 $typeoptionnames = repository::static_function($typename, 'get_type_option_names');
309 $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
311 if (!empty($typeoptionnames) || !empty($instanceoptionnames)) {
312 // Calculate number of instances in order to display them for the Moodle administrator
313 if (!empty($instanceoptionnames)) {
314 $params = array();
315 $params['context'] = array(context_system::instance());
316 $params['onlyvisible'] = false;
317 $params['type'] = $typename;
318 $admininstancenumber = count(repository::static_function($typename, 'get_instances', $params));
319 // site instances
320 $admininstancenumbertext = get_string('instancesforsite', 'repository', $admininstancenumber);
321 $params['context'] = array();
322 $instances = repository::static_function($typename, 'get_instances', $params);
323 $courseinstances = array();
324 $userinstances = array();
326 foreach ($instances as $instance) {
327 $repocontext = context::instance_by_id($instance->instance->contextid);
328 if ($repocontext->contextlevel == CONTEXT_COURSE) {
329 $courseinstances[] = $instance;
330 } else if ($repocontext->contextlevel == CONTEXT_USER) {
331 $userinstances[] = $instance;
334 // course instances
335 $instancenumber = count($courseinstances);
336 $courseinstancenumbertext = get_string('instancesforcourses', 'repository', $instancenumber);
338 // user private instances
339 $instancenumber = count($userinstances);
340 $userinstancenumbertext = get_string('instancesforusers', 'repository', $instancenumber);
341 } else {
342 $admininstancenumbertext = "";
343 $courseinstancenumbertext = "";
344 $userinstancenumbertext = "";
347 $settings .= '<a href="' . $sesskeyurl . '&amp;action=edit&amp;repos=' . $typename . '">' . $settingsstr .'</a>';
349 $settings .= $OUTPUT->container_start('mdl-left');
350 $settings .= '<br/>';
351 $settings .= $admininstancenumbertext;
352 $settings .= '<br/>';
353 $settings .= $courseinstancenumbertext;
354 $settings .= '<br/>';
355 $settings .= $userinstancenumbertext;
356 $settings .= $OUTPUT->container_end();
358 // Get the current visibility
359 if ($i->get_visible()) {
360 $currentaction = 'show';
361 } else {
362 $currentaction = 'hide';
365 $select = new single_select(repository_action_url($typename, 'repos'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . basename($typename));
366 $select->set_label(get_string('action'), array('class' => 'accesshide'));
367 // Display up/down link
368 $updown = '';
369 $spacer = $OUTPUT->spacer(array('height'=>15, 'width'=>15)); // should be done with CSS instead
371 if ($updowncount > 1) {
372 $updown .= "<a href=\"$sesskeyurl&amp;action=moveup&amp;repos=".$typename."\">";
373 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/up') . "\" alt=\"up\" /></a>&nbsp;";
375 else {
376 $updown .= $spacer;
378 if ($updowncount < $totalrepositorytypes) {
379 $updown .= "<a href=\"$sesskeyurl&amp;action=movedown&amp;repos=".$typename."\">";
380 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/down') . "\" alt=\"down\" /></a>";
382 else {
383 $updown .= $spacer;
386 $updowncount++;
388 $uninstall = '';
389 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('repository_' . $typename, 'manage')) {
390 $uninstall = html_writer::link($uninstallurl, $struninstall);
393 $table->data[] = array($i->get_readablename(), $OUTPUT->render($select), $updown, $settings, $uninstall);
395 if (!in_array($typename, $alreadyplugins)) {
396 $alreadyplugins[] = $typename;
401 // Get all the plugins that exist on disk
402 $plugins = core_component::get_plugin_list('repository');
403 if (!empty($plugins)) {
404 foreach ($plugins as $plugin => $dir) {
405 // Check that it has not already been listed
406 if (!in_array($plugin, $alreadyplugins)) {
407 $select = new single_select(repository_action_url($plugin, 'repos'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . basename($plugin));
408 $select->set_label(get_string('action'), array('class' => 'accesshide'));
409 $uninstall = '';
410 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('repository_' . $plugin, 'manage')) {
411 $uninstall = html_writer::link($uninstallurl, $struninstall);
413 $table->data[] = array(get_string('pluginname', 'repository_'.$plugin), $OUTPUT->render($select), '', '', $uninstall);
418 $output .= html_writer::table($table);
419 $output .= $OUTPUT->box_end();
420 print $output;
421 $return = false;
424 if ($return) {
425 redirect($baseurl);
427 echo $OUTPUT->footer();