MDL-77891 core_calendar: support display of mod iconurl
[moodle.git] / admin / repository.php
blob735cbdbca267be72549691c275e83ceb9173a128
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 admin_externalpage_setup($pagename);
52 $sesskeyurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php?sesskey=' . sesskey();
53 $baseurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php';
55 $configstr = get_string('manage', 'repository');
57 $return = true;
59 if (!empty($action)) {
60 require_sesskey();
63 /**
64 * Helper function that generates a moodle_url object
65 * relevant to the repository
67 function repository_action_url($repository) {
68 global $baseurl;
69 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'repos'=>$repository));
72 if (($action == 'edit') || ($action == 'new')) {
73 $pluginname = '';
74 if ($action == 'edit') {
75 $repositorytype = repository::get_type_by_typename($repository);
76 $classname = 'repository_' . $repositorytype->get_typename();
77 $configs = call_user_func(array($classname, 'get_type_option_names'));
78 $plugin = $repositorytype->get_typename();
79 // looking for instance to edit plugin name
80 $instanceoptions = call_user_func(array($classname, 'get_instance_option_names'));
81 if (empty($instanceoptions)) {
82 $params = array();
83 $params['type'] = $plugin;
84 $instances = repository::get_instances($params);
85 if ($instance = array_pop($instances)) {
86 // use the one form db record
87 $pluginname = $instance->instance->name;
91 } else {
92 $repositorytype = null;
93 $plugin = $repository;
94 $typeid = $repository;
96 $PAGE->set_pagetype('admin-repository-' . $plugin);
97 // display the edit form for this instance
98 $mform = new repository_type_form('', array('pluginname'=>$pluginname, 'plugin' => $plugin, 'instance' => $repositorytype, 'action' => $formaction));
99 $fromform = $mform->get_data();
101 //detect if we create a new type without config (in this case if don't want to display a setting page during creation)
102 $nosettings = false;
103 if ($action == 'new') {
104 $adminconfignames = repository::static_function($repository, 'get_type_option_names');
105 $nosettings = empty($adminconfignames);
107 // end setup, begin output
109 if ($mform->is_cancelled()){
110 redirect($baseurl);
111 } else if (!empty($fromform) || $nosettings) {
112 require_sesskey();
113 if ($action == 'edit') {
114 $settings = array();
115 foreach($configs as $config) {
116 if (!empty($fromform->$config)) {
117 $settings[$config] = $fromform->$config;
118 } else {
119 // if the config name is not appear in $fromform
120 // empty this config value
121 $settings[$config] = '';
124 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
125 if (!empty($instanceoptionnames)) {
126 if (property_exists($fromform, 'enablecourseinstances')) {
127 $settings['enablecourseinstances'] = $fromform->enablecourseinstances;
129 else {
130 $settings['enablecourseinstances'] = 0;
132 if (property_exists($fromform, 'enableuserinstances')) {
133 $settings['enableuserinstances'] = $fromform->enableuserinstances;
135 else {
136 $settings['enableuserinstances'] = 0;
139 $success = $repositorytype->update_options($settings);
140 } else {
141 $type = new repository_type($plugin, (array)$fromform, $visible);
142 $success = true;
143 if (!$repoid = $type->create()) {
144 $success = false;
145 } else {
146 add_to_config_log('repository_visibility', '', (int)$visible, $plugin);
148 $data = data_submitted();
150 if ($success) {
151 // configs saved
152 core_plugin_manager::reset_caches();
153 redirect($baseurl);
154 } else {
155 throw new \moodle_exception('instancenotsaved', 'repository', $baseurl);
157 exit;
158 } else {
159 echo $OUTPUT->header();
160 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
161 $displaysettingform = true;
162 if ($action == 'edit') {
163 $typeoptionnames = repository::static_function($repository, 'get_type_option_names');
164 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
165 if (empty($typeoptionnames) && empty($instanceoptionnames)) {
166 $displaysettingform = false;
169 if ($displaysettingform){
170 $OUTPUT->box_start();
171 $mform->display();
172 $OUTPUT->box_end();
174 $return = false;
176 // Display instances list and creation form
177 if ($action == 'edit') {
178 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
179 if (!empty($instanceoptionnames)) {
180 repository::display_instances_list(context_system::instance(), $repository);
184 } else if ($action == 'show') {
185 if (!confirm_sesskey()) {
186 throw new \moodle_exception('confirmsesskeybad', '', $baseurl);
188 $class = \core_plugin_manager::resolve_plugininfo_class('repository');
189 $class::enable_plugin($repository, 1);
190 $return = true;
191 } else if ($action == 'hide') {
192 if (!confirm_sesskey()) {
193 throw new \moodle_exception('confirmsesskeybad', '', $baseurl);
195 $class = \core_plugin_manager::resolve_plugininfo_class('repository');
196 $class::enable_plugin($repository, 0);
197 $return = true;
198 } else if ($action == 'delete') {
199 $repositorytype = repository::get_type_by_typename($repository);
200 if ($sure) {
201 $PAGE->set_pagetype('admin-repository-' . $repository);
202 if (!confirm_sesskey()) {
203 throw new \moodle_exception('confirmsesskeybad', '', $baseurl);
206 if ($repositorytype->delete($downloadcontents)) {
207 // Include this information into config changes table.
208 add_to_config_log('repository_visibility', $repositorytype->get_visible(), '', $repository);
209 core_plugin_manager::reset_caches();
210 redirect($baseurl);
211 } else {
212 throw new \moodle_exception('instancenotdeleted', 'repository', $baseurl);
214 exit;
215 } else {
216 echo $OUTPUT->header();
218 $message = get_string('confirmremove', 'repository', $repositorytype->get_readablename());
220 $output = $OUTPUT->box_start('generalbox', 'notice');
221 $output .= html_writer::tag('p', $message);
223 $removeurl = new moodle_url($sesskeyurl);
224 $removeurl->params(array(
225 'action' =>'delete',
226 'repos' => $repository,
227 'sure' => 'yes',
230 $removeanddownloadurl = new moodle_url($sesskeyurl);
231 $removeanddownloadurl->params(array(
232 'action' =>'delete',
233 'repos'=> $repository,
234 'sure' => 'yes',
235 'downloadcontents' => 1,
238 $output .= $OUTPUT->single_button($removeurl, get_string('continueuninstall', 'repository'));
239 $output .= $OUTPUT->single_button($removeanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
240 $output .= $OUTPUT->single_button($baseurl, get_string('cancel'));
241 $output .= $OUTPUT->box_end();
243 echo $output;
245 $return = false;
247 } else if ($action == 'moveup') {
248 $repositorytype = repository::get_type_by_typename($repository);
249 $repositorytype->move_order('up');
250 } else if ($action == 'movedown') {
251 $repositorytype = repository::get_type_by_typename($repository);
252 $repositorytype->move_order('down');
253 } else {
254 // If page is loaded directly
255 echo $OUTPUT->header();
256 echo $OUTPUT->heading(get_string('manage', 'repository'));
258 // Get strings that are used
259 $strshow = get_string('on', 'repository');
260 $strhide = get_string('off', 'repository');
261 $strdelete = get_string('disabled', 'repository');
262 $struninstall = get_string('uninstallplugin', 'core_admin');
264 $actionchoicesforexisting = array(
265 'show' => $strshow,
266 'hide' => $strhide,
267 'delete' => $strdelete
270 $actionchoicesfornew = array(
271 'newon' => $strshow,
272 'newoff' => $strhide,
273 'delete' => $strdelete
276 $output = '';
277 $output .= $OUTPUT->box_start('generalbox');
279 // Set strings that are used multiple times
280 $settingsstr = get_string('settings');
281 $disablestr = get_string('disable');
283 // Table to list plug-ins
284 $table = new html_table();
285 $table->head = array(get_string('name'), get_string('isactive', 'repository'), get_string('order'), $settingsstr, $struninstall);
287 $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign');
288 $table->id = 'repositoriessetting';
289 $table->data = array();
290 $table->attributes['class'] = 'admintable generaltable';
292 // Get list of used plug-ins
293 $repositorytypes = repository::get_types();
294 // Array to store plugins being used
295 $alreadyplugins = array();
296 if (!empty($repositorytypes)) {
297 $totalrepositorytypes = count($repositorytypes);
298 $updowncount = 1;
299 foreach ($repositorytypes as $i) {
300 $settings = '';
301 $typename = $i->get_typename();
302 // Display edit link only if you can config the type or if it has multiple instances (e.g. has instance config)
303 $typeoptionnames = repository::static_function($typename, 'get_type_option_names');
304 $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
306 if (!empty($typeoptionnames) || !empty($instanceoptionnames)) {
307 // Calculate number of instances in order to display them for the Moodle administrator
308 if (!empty($instanceoptionnames)) {
309 $params = array();
310 $params['context'] = array(context_system::instance());
311 $params['onlyvisible'] = false;
312 $params['type'] = $typename;
313 $admininstancenumber = count(repository::static_function($typename, 'get_instances', $params));
314 // site instances
315 $admininstancenumbertext = get_string('instancesforsite', 'repository', $admininstancenumber);
316 $params['context'] = array();
317 $instances = repository::static_function($typename, 'get_instances', $params);
318 $courseinstances = array();
319 $userinstances = array();
321 foreach ($instances as $instance) {
322 $repocontext = context::instance_by_id($instance->instance->contextid);
323 if ($repocontext->contextlevel == CONTEXT_COURSE) {
324 $courseinstances[] = $instance;
325 } else if ($repocontext->contextlevel == CONTEXT_USER) {
326 $userinstances[] = $instance;
329 // course instances
330 $instancenumber = count($courseinstances);
331 $courseinstancenumbertext = get_string('instancesforcourses', 'repository', $instancenumber);
333 // user private instances
334 $instancenumber = count($userinstances);
335 $userinstancenumbertext = get_string('instancesforusers', 'repository', $instancenumber);
336 } else {
337 $admininstancenumbertext = "";
338 $courseinstancenumbertext = "";
339 $userinstancenumbertext = "";
342 $settings .= '<a href="' . $sesskeyurl . '&amp;action=edit&amp;repos=' . $typename . '">' . $settingsstr .'</a>';
344 $settings .= $OUTPUT->container_start('mdl-left');
345 $settings .= '<br/>';
346 $settings .= $admininstancenumbertext;
347 $settings .= '<br/>';
348 $settings .= $courseinstancenumbertext;
349 $settings .= '<br/>';
350 $settings .= $userinstancenumbertext;
351 $settings .= $OUTPUT->container_end();
353 // Get the current visibility
354 if ($i->get_visible()) {
355 $currentaction = 'show';
356 } else {
357 $currentaction = 'hide';
360 $select = new single_select(repository_action_url($typename, 'repos'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . basename($typename));
361 $select->set_label(get_string('action'), array('class' => 'accesshide'));
362 // Display up/down link
363 $updown = '';
364 $spacer = $OUTPUT->spacer(array('height'=>15, 'width'=>15)); // should be done with CSS instead
366 if ($updowncount > 1) {
367 $updown .= "<a href=\"$sesskeyurl&amp;action=moveup&amp;repos=".$typename."\">";
368 $updown .= $OUTPUT->pix_icon('t/up', get_string('moveup')) . "</a>&nbsp;";
370 else {
371 $updown .= $spacer;
373 if ($updowncount < $totalrepositorytypes) {
374 $updown .= "<a href=\"$sesskeyurl&amp;action=movedown&amp;repos=".$typename."\">";
375 $updown .= $OUTPUT->pix_icon('t/down', get_string('movedown')) . "</a>&nbsp;";
377 else {
378 $updown .= $spacer;
381 $updowncount++;
383 $uninstall = '';
384 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('repository_' . $typename, 'manage')) {
385 $uninstall = html_writer::link($uninstallurl, $struninstall);
388 $table->data[] = array($i->get_readablename(), $OUTPUT->render($select), $updown, $settings, $uninstall);
389 $table->rowclasses[] = '';
391 if (!in_array($typename, $alreadyplugins)) {
392 $alreadyplugins[] = $typename;
397 // Get all the plugins that exist on disk
398 $plugins = core_component::get_plugin_list('repository');
399 if (!empty($plugins)) {
400 foreach ($plugins as $plugin => $dir) {
401 // Check that it has not already been listed
402 if (!in_array($plugin, $alreadyplugins)) {
403 $select = new single_select(repository_action_url($plugin, 'repos'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . basename($plugin));
404 $select->set_label(get_string('action'), array('class' => 'accesshide'));
405 $uninstall = '';
406 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('repository_' . $plugin, 'manage')) {
407 $uninstall = html_writer::link($uninstallurl, $struninstall);
409 $table->data[] = array(get_string('pluginname', 'repository_'.$plugin), $OUTPUT->render($select), '', '', $uninstall);
410 $table->rowclasses[] = 'dimmed_text';
415 $output .= html_writer::table($table);
416 $output .= $OUTPUT->box_end();
417 print $output;
418 $return = false;
421 if ($return) {
422 redirect($baseurl);
424 echo $OUTPUT->footer();