Merge branch 'MDL-40604-master' of git://github.com/danpoltawski/moodle
[moodle.git] / admin / repository.php
blobaa77af99b2075f066b2f1b8bdcc29a9b2530a1c5
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(dirname(dirname(__FILE__)) . '/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 // Purge all caches related to repositories administration.
65 cache::make('core', 'plugininfo_repository')->purge();
67 /**
68 * Helper function that generates a moodle_url object
69 * relevant to the repository
71 function repository_action_url($repository) {
72 global $baseurl;
73 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'repos'=>$repository));
76 if (($action == 'edit') || ($action == 'new')) {
77 $pluginname = '';
78 if ($action == 'edit') {
79 $repositorytype = repository::get_type_by_typename($repository);
80 $classname = 'repository_' . $repositorytype->get_typename();
81 $configs = call_user_func(array($classname, 'get_type_option_names'));
82 $plugin = $repositorytype->get_typename();
83 // looking for instance to edit plugin name
84 $instanceoptions = call_user_func(array($classname, 'get_instance_option_names'));
85 if (empty($instanceoptions)) {
86 $params = array();
87 $params['type'] = $plugin;
88 $instances = repository::get_instances($params);
89 if ($instance = array_pop($instances)) {
90 // use the one form db record
91 $pluginname = $instance->instance->name;
95 } else {
96 $repositorytype = null;
97 $plugin = $repository;
98 $typeid = $repository;
100 $PAGE->set_pagetype('admin-repository-' . $plugin);
101 // display the edit form for this instance
102 $mform = new repository_type_form('', array('pluginname'=>$pluginname, 'plugin' => $plugin, 'instance' => $repositorytype, 'action' => $formaction));
103 $fromform = $mform->get_data();
105 //detect if we create a new type without config (in this case if don't want to display a setting page during creation)
106 $nosettings = false;
107 if ($action == 'new') {
108 $adminconfignames = repository::static_function($repository, 'get_type_option_names');
109 $nosettings = empty($adminconfignames);
111 // end setup, begin output
113 if ($mform->is_cancelled()){
114 redirect($baseurl);
115 } else if (!empty($fromform) || $nosettings) {
116 require_sesskey();
117 if ($action == 'edit') {
118 $settings = array();
119 foreach($configs as $config) {
120 if (!empty($fromform->$config)) {
121 $settings[$config] = $fromform->$config;
122 } else {
123 // if the config name is not appear in $fromform
124 // empty this config value
125 $settings[$config] = '';
128 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
129 if (!empty($instanceoptionnames)) {
130 if (array_key_exists('enablecourseinstances', $fromform)) {
131 $settings['enablecourseinstances'] = $fromform->enablecourseinstances;
133 else {
134 $settings['enablecourseinstances'] = 0;
136 if (array_key_exists('enableuserinstances', $fromform)) {
137 $settings['enableuserinstances'] = $fromform->enableuserinstances;
139 else {
140 $settings['enableuserinstances'] = 0;
143 $success = $repositorytype->update_options($settings);
144 } else {
145 $type = new repository_type($plugin, (array)$fromform, $visible);
146 $type->create();
147 $success = true;
148 $data = data_submitted();
150 if ($success) {
151 // configs saved
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 $return = true;
193 } else if ($action == 'hide') {
194 if (!confirm_sesskey()) {
195 print_error('confirmsesskeybad', '', $baseurl);
197 $repositorytype = repository::get_type_by_typename($repository);
198 if (empty($repositorytype)) {
199 print_error('invalidplugin', 'repository', '', $repository);
201 $repositorytype->update_visibility(false);
202 $return = true;
203 } else if ($action == 'delete') {
204 $repositorytype = repository::get_type_by_typename($repository);
205 if ($sure) {
206 $PAGE->set_pagetype('admin-repository-' . $repository);
207 if (!confirm_sesskey()) {
208 print_error('confirmsesskeybad', '', $baseurl);
211 if ($repositorytype->delete($downloadcontents)) {
212 redirect($baseurl);
213 } else {
214 print_error('instancenotdeleted', 'repository', $baseurl);
216 exit;
217 } else {
218 echo $OUTPUT->header();
220 $message = get_string('confirmremove', 'repository', $repositorytype->get_readablename());
222 $output = $OUTPUT->box_start('generalbox', 'notice');
223 $output .= html_writer::tag('p', $message);
225 $removeurl = new moodle_url($sesskeyurl);
226 $removeurl->params(array(
227 'action' =>'delete',
228 'repos' => $repository,
229 'sure' => 'yes',
232 $removeanddownloadurl = new moodle_url($sesskeyurl);
233 $removeanddownloadurl->params(array(
234 'action' =>'delete',
235 'repos'=> $repository,
236 'sure' => 'yes',
237 'downloadcontents' => 1,
240 $output .= $OUTPUT->single_button($removeurl, get_string('continueuninstall', 'repository'));
241 $output .= $OUTPUT->single_button($removeanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
242 $output .= $OUTPUT->single_button($baseurl, get_string('cancel'));
243 $output .= $OUTPUT->box_end();
245 echo $output;
247 $return = false;
249 } else if ($action == 'moveup') {
250 $repositorytype = repository::get_type_by_typename($repository);
251 $repositorytype->move_order('up');
252 } else if ($action == 'movedown') {
253 $repositorytype = repository::get_type_by_typename($repository);
254 $repositorytype->move_order('down');
255 } else {
256 // If page is loaded directly
257 echo $OUTPUT->header();
258 echo $OUTPUT->heading(get_string('manage', 'repository'));
260 // Get strings that are used
261 $strshow = get_string('on', 'repository');
262 $strhide = get_string('off', 'repository');
263 $strdelete = get_string('disabled', 'repository');
265 $actionchoicesforexisting = array(
266 'show' => $strshow,
267 'hide' => $strhide,
268 'delete' => $strdelete
271 $actionchoicesfornew = array(
272 'newon' => $strshow,
273 'newoff' => $strhide,
274 'delete' => $strdelete
277 $output = '';
278 $output .= $OUTPUT->box_start('generalbox');
280 // Set strings that are used multiple times
281 $settingsstr = get_string('settings');
282 $disablestr = get_string('disable');
284 // Table to list plug-ins
285 $table = new html_table();
286 $table->head = array(get_string('name'), get_string('isactive', 'repository'), get_string('order'), $settingsstr);
288 $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign');
289 $table->id = 'repositoriessetting';
290 $table->data = array();
291 $table->attributes['class'] = 'admintable generaltable';
293 // Get list of used plug-ins
294 $repositorytypes = repository::get_types();
295 if (!empty($repositorytypes)) {
296 // Array to store plugins being used
297 $alreadyplugins = array();
298 $totalrepositorytypes = count($repositorytypes);
299 $updowncount = 1;
300 foreach ($repositorytypes as $i) {
301 $settings = '';
302 $typename = $i->get_typename();
303 // Display edit link only if you can config the type or if it has multiple instances (e.g. has instance config)
304 $typeoptionnames = repository::static_function($typename, 'get_type_option_names');
305 $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
307 if (!empty($typeoptionnames) || !empty($instanceoptionnames)) {
308 // Calculate number of instances in order to display them for the Moodle administrator
309 if (!empty($instanceoptionnames)) {
310 $params = array();
311 $params['context'] = array(context_system::instance());
312 $params['onlyvisible'] = false;
313 $params['type'] = $typename;
314 $admininstancenumber = count(repository::static_function($typename, 'get_instances', $params));
315 // site instances
316 $admininstancenumbertext = get_string('instancesforsite', 'repository', $admininstancenumber);
317 $params['context'] = array();
318 $instances = repository::static_function($typename, 'get_instances', $params);
319 $courseinstances = array();
320 $userinstances = array();
322 foreach ($instances as $instance) {
323 $repocontext = context::instance_by_id($instance->instance->contextid);
324 if ($repocontext->contextlevel == CONTEXT_COURSE) {
325 $courseinstances[] = $instance;
326 } else if ($repocontext->contextlevel == CONTEXT_USER) {
327 $userinstances[] = $instance;
330 // course instances
331 $instancenumber = count($courseinstances);
332 $courseinstancenumbertext = get_string('instancesforcourses', 'repository', $instancenumber);
334 // user private instances
335 $instancenumber = count($userinstances);
336 $userinstancenumbertext = get_string('instancesforusers', 'repository', $instancenumber);
337 } else {
338 $admininstancenumbertext = "";
339 $courseinstancenumbertext = "";
340 $userinstancenumbertext = "";
343 $settings .= '<a href="' . $sesskeyurl . '&amp;action=edit&amp;repos=' . $typename . '">' . $settingsstr .'</a>';
345 $settings .= $OUTPUT->container_start('mdl-left');
346 $settings .= '<br/>';
347 $settings .= $admininstancenumbertext;
348 $settings .= '<br/>';
349 $settings .= $courseinstancenumbertext;
350 $settings .= '<br/>';
351 $settings .= $userinstancenumbertext;
352 $settings .= $OUTPUT->container_end();
354 // Get the current visibility
355 if ($i->get_visible()) {
356 $currentaction = 'show';
357 } else {
358 $currentaction = 'hide';
361 $select = new single_select(repository_action_url($typename, 'repos'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . basename($typename));
362 $select->set_label(get_string('action'), array('class' => 'accesshide'));
363 // Display up/down link
364 $updown = '';
365 $spacer = $OUTPUT->spacer(array('height'=>15, 'width'=>15)); // should be done with CSS instead
367 if ($updowncount > 1) {
368 $updown .= "<a href=\"$sesskeyurl&amp;action=moveup&amp;repos=".$typename."\">";
369 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/up') . "\" alt=\"up\" /></a>&nbsp;";
371 else {
372 $updown .= $spacer;
374 if ($updowncount < $totalrepositorytypes) {
375 $updown .= "<a href=\"$sesskeyurl&amp;action=movedown&amp;repos=".$typename."\">";
376 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/down') . "\" alt=\"down\" /></a>";
378 else {
379 $updown .= $spacer;
382 $updowncount++;
384 $table->data[] = array($i->get_readablename(), $OUTPUT->render($select), $updown, $settings);
386 if (!in_array($typename, $alreadyplugins)) {
387 $alreadyplugins[] = $typename;
392 // Get all the plugins that exist on disk
393 $plugins = get_plugin_list('repository');
394 if (!empty($plugins)) {
395 foreach ($plugins as $plugin => $dir) {
396 // Check that it has not already been listed
397 if (!in_array($plugin, $alreadyplugins)) {
398 $select = new single_select(repository_action_url($plugin, 'repos'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . basename($plugin));
399 $select->set_label(get_string('action'), array('class' => 'accesshide'));
400 $table->data[] = array(get_string('pluginname', 'repository_'.$plugin), $OUTPUT->render($select), '', '');
405 $output .= html_writer::table($table);
406 $output .= $OUTPUT->box_end();
407 print $output;
408 $return = false;
411 if ($return) {
412 redirect($baseurl);
414 echo $OUTPUT->footer();