MDL-35510 theme_mymobile: RTL fixes for course and category names in style/jmobile11_...
[moodle.git] / admin / repository.php
blob3eb48b0da4dc5c2c7ad73a8e6db6a1527ea0dbe5
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 /**
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 $type->create();
144 $success = true;
145 $data = data_submitted();
147 if ($success) {
148 // configs saved
149 redirect($baseurl);
150 } else {
151 print_error('instancenotsaved', 'repository', $baseurl);
153 exit;
154 } else {
155 echo $OUTPUT->header();
156 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
157 $displaysettingform = true;
158 if ($action == 'edit') {
159 $typeoptionnames = repository::static_function($repository, 'get_type_option_names');
160 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
161 if (empty($typeoptionnames) && empty($instanceoptionnames)) {
162 $displaysettingform = false;
165 if ($displaysettingform){
166 $OUTPUT->box_start();
167 $mform->display();
168 $OUTPUT->box_end();
170 $return = false;
172 // Display instances list and creation form
173 if ($action == 'edit') {
174 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
175 if (!empty($instanceoptionnames)) {
176 repository::display_instances_list(context_system::instance(), $repository);
180 } else if ($action == 'show') {
181 if (!confirm_sesskey()) {
182 print_error('confirmsesskeybad', '', $baseurl);
184 $repositorytype = repository::get_type_by_typename($repository);
185 if (empty($repositorytype)) {
186 print_error('invalidplugin', 'repository', '', $repository);
188 $repositorytype->update_visibility(true);
189 $return = true;
190 } else if ($action == 'hide') {
191 if (!confirm_sesskey()) {
192 print_error('confirmsesskeybad', '', $baseurl);
194 $repositorytype = repository::get_type_by_typename($repository);
195 if (empty($repositorytype)) {
196 print_error('invalidplugin', 'repository', '', $repository);
198 $repositorytype->update_visibility(false);
199 $return = true;
200 } else if ($action == 'delete') {
201 $repositorytype = repository::get_type_by_typename($repository);
202 if ($sure) {
203 $PAGE->set_pagetype('admin-repository-' . $repository);
204 if (!confirm_sesskey()) {
205 print_error('confirmsesskeybad', '', $baseurl);
208 if ($repositorytype->delete($downloadcontents)) {
209 redirect($baseurl);
210 } else {
211 print_error('instancenotdeleted', 'repository', $baseurl);
213 exit;
214 } else {
215 echo $OUTPUT->header();
217 $message = get_string('confirmremove', 'repository', $repositorytype->get_readablename());
219 $output = $OUTPUT->box_start('generalbox', 'notice');
220 $output .= html_writer::tag('p', $message);
222 $removeurl = new moodle_url($sesskeyurl);
223 $removeurl->params(array(
224 'action' =>'delete',
225 'repos' => $repository,
226 'sure' => 'yes',
229 $removeanddownloadurl = new moodle_url($sesskeyurl);
230 $removeanddownloadurl->params(array(
231 'action' =>'delete',
232 'repos'=> $repository,
233 'sure' => 'yes',
234 'downloadcontents' => 1,
237 $output .= $OUTPUT->single_button($removeurl, get_string('continueuninstall', 'repository'));
238 $output .= $OUTPUT->single_button($removeanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
239 $output .= $OUTPUT->single_button($baseurl, get_string('cancel'));
240 $output .= $OUTPUT->box_end();
242 echo $output;
244 $return = false;
246 } else if ($action == 'moveup') {
247 $repositorytype = repository::get_type_by_typename($repository);
248 $repositorytype->move_order('up');
249 } else if ($action == 'movedown') {
250 $repositorytype = repository::get_type_by_typename($repository);
251 $repositorytype->move_order('down');
252 } else {
253 // If page is loaded directly
254 echo $OUTPUT->header();
255 echo $OUTPUT->heading(get_string('manage', 'repository'));
257 // Get strings that are used
258 $strshow = get_string('on', 'repository');
259 $strhide = get_string('off', 'repository');
260 $strdelete = get_string('disabled', 'repository');
262 $actionchoicesforexisting = array(
263 'show' => $strshow,
264 'hide' => $strhide,
265 'delete' => $strdelete
268 $actionchoicesfornew = array(
269 'newon' => $strshow,
270 'newoff' => $strhide,
271 'delete' => $strdelete
274 $output = '';
275 $output .= $OUTPUT->box_start('generalbox');
277 // Set strings that are used multiple times
278 $settingsstr = get_string('settings');
279 $disablestr = get_string('disable');
281 // Table to list plug-ins
282 $table = new html_table();
283 $table->head = array(get_string('name'), get_string('isactive', 'repository'), get_string('order'), $settingsstr);
284 $table->align = array('left', 'center', 'center', 'center', 'center');
285 $table->data = array();
287 // Get list of used plug-ins
288 $instances = repository::get_types();
289 if (!empty($instances)) {
290 // Array to store plugins being used
291 $alreadyplugins = array();
292 $totalinstances = count($instances);
293 $updowncount = 1;
294 foreach ($instances as $i) {
295 $settings = '';
296 $typename = $i->get_typename();
297 // Display edit link only if you can config the type or if it has multiple instances (e.g. has instance config)
298 $typeoptionnames = repository::static_function($typename, 'get_type_option_names');
299 $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
301 if (!empty($typeoptionnames) || !empty($instanceoptionnames)) {
302 // Calculate number of instances in order to display them for the Moodle administrator
303 if (!empty($instanceoptionnames)) {
304 $params = array();
305 $params['context'] = array(context_system::instance());
306 $params['onlyvisible'] = false;
307 $params['type'] = $typename;
308 $admininstancenumber = count(repository::static_function($typename, 'get_instances', $params));
309 // site instances
310 $admininstancenumbertext = get_string('instancesforsite', 'repository', $admininstancenumber);
311 $params['context'] = array();
312 $instances = repository::static_function($typename, 'get_instances', $params);
313 $courseinstances = array();
314 $userinstances = array();
316 foreach ($instances as $instance) {
317 if ($instance->context->contextlevel == CONTEXT_COURSE) {
318 $courseinstances[] = $instance;
319 } else if ($instance->context->contextlevel == CONTEXT_USER) {
320 $userinstances[] = $instance;
323 // course instances
324 $instancenumber = count($courseinstances);
325 $courseinstancenumbertext = get_string('instancesforcourses', 'repository', $instancenumber);
327 // user private instances
328 $instancenumber = count($userinstances);
329 $userinstancenumbertext = get_string('instancesforusers', 'repository', $instancenumber);
330 } else {
331 $admininstancenumbertext = "";
332 $courseinstancenumbertext = "";
333 $userinstancenumbertext = "";
336 $settings .= '<a href="' . $sesskeyurl . '&amp;action=edit&amp;repos=' . $typename . '">' . $settingsstr .'</a>';
338 $settings .= $OUTPUT->container_start('mdl-left');
339 $settings .= '<br/>';
340 $settings .= $admininstancenumbertext;
341 $settings .= '<br/>';
342 $settings .= $courseinstancenumbertext;
343 $settings .= '<br/>';
344 $settings .= $userinstancenumbertext;
345 $settings .= $OUTPUT->container_end();
347 // Get the current visibility
348 if ($i->get_visible()) {
349 $currentaction = 'show';
350 } else {
351 $currentaction = 'hide';
354 $select = new single_select(repository_action_url($typename, 'repos'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . basename($typename));
355 $select->set_label(get_string('action'), array('class' => 'accesshide'));
356 // Display up/down link
357 $updown = '';
358 $spacer = $OUTPUT->spacer(array('height'=>15, 'width'=>15)); // should be done with CSS instead
360 if ($updowncount > 1) {
361 $updown .= "<a href=\"$sesskeyurl&amp;action=moveup&amp;repos=".$typename."\">";
362 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/up') . "\" alt=\"up\" /></a>&nbsp;";
364 else {
365 $updown .= $spacer;
367 if ($updowncount < $totalinstances) {
368 $updown .= "<a href=\"$sesskeyurl&amp;action=movedown&amp;repos=".$typename."\">";
369 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/down') . "\" alt=\"down\" /></a>";
371 else {
372 $updown .= $spacer;
375 $updowncount++;
377 $table->data[] = array($i->get_readablename(), $OUTPUT->render($select), $updown, $settings);
379 if (!in_array($typename, $alreadyplugins)) {
380 $alreadyplugins[] = $typename;
385 // Get all the plugins that exist on disk
386 $plugins = get_plugin_list('repository');
387 if (!empty($plugins)) {
388 foreach ($plugins as $plugin => $dir) {
389 // Check that it has not already been listed
390 if (!in_array($plugin, $alreadyplugins)) {
391 $select = new single_select(repository_action_url($plugin, 'repos'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . basename($plugin));
392 $select->set_label(get_string('action'), array('class' => 'accesshide'));
393 $table->data[] = array(get_string('pluginname', 'repository_'.$plugin), $OUTPUT->render($select), '', '');
398 $output .= html_writer::table($table);
399 $output .= $OUTPUT->box_end();
400 print $output;
401 $return = false;
404 if ($return) {
405 redirect($baseurl);
407 echo $OUTPUT->footer();