MDL-40250 phpunit: Add unit tests for core_files_external::upload()
[moodle.git] / admin / courseformats.php
bloba8377dec7a7af7dd519e0245fa21b2dcbf24bed2
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 /**
18 * Allows the admin to enable, disable and uninstall course formats
20 * @package core_admin
21 * @copyright 2012 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once($CFG->libdir.'/adminlib.php');
27 require_once($CFG->libdir.'/pluginlib.php');
29 $action = required_param('action', PARAM_ALPHANUMEXT);
30 $formatname = required_param('format', PARAM_PLUGIN);
31 $confirm = optional_param('confirm', 0, PARAM_BOOL);
33 $syscontext = context_system::instance();
34 $PAGE->set_url('/admin/courseformats.php');
35 $PAGE->set_context($syscontext);
37 require_login();
38 require_capability('moodle/site:config', $syscontext);
39 require_sesskey();
41 $return = new moodle_url('/admin/settings.php', array('section' => 'manageformats'));
43 $allplugins = plugin_manager::instance()->get_plugins();
44 $formatplugins = $allplugins['format'];
45 $sortorder = array_flip(array_keys($formatplugins));
47 if (!isset($formatplugins[$formatname])) {
48 print_error('courseformatnotfound', 'error', $return, $formatname);
51 switch ($action) {
52 case 'disable':
53 if ($formatplugins[$formatname]->is_enabled()) {
54 if (get_config('moodlecourse', 'format') === $formatname) {
55 print_error('cannotdisableformat', 'error', $return);
57 set_config('disabled', 1, 'format_'. $formatname);
59 break;
60 case 'enable':
61 if (!$formatplugins[$formatname]->is_enabled()) {
62 unset_config('disabled', 'format_'. $formatname);
64 break;
65 case 'up':
66 if ($sortorder[$formatname]) {
67 $currentindex = $sortorder[$formatname];
68 $seq = array_keys($formatplugins);
69 $seq[$currentindex] = $seq[$currentindex-1];
70 $seq[$currentindex-1] = $formatname;
71 set_config('format_plugins_sortorder', implode(',', $seq));
73 break;
74 case 'down':
75 if ($sortorder[$formatname] < count($sortorder)-1) {
76 $currentindex = $sortorder[$formatname];
77 $seq = array_keys($formatplugins);
78 $seq[$currentindex] = $seq[$currentindex+1];
79 $seq[$currentindex+1] = $formatname;
80 set_config('format_plugins_sortorder', implode(',', $seq));
82 break;
83 case 'uninstall':
84 echo $OUTPUT->header();
85 echo $OUTPUT->heading(get_string('courseformats', 'moodle'));
87 $coursecount = $DB->count_records('course', array('format' => $formatname));
88 if ($coursecount) {
89 // Check that default format is set. It will be used to convert courses
90 // using this format
91 $defaultformat = get_config('moodlecourse', 'format');
92 $defaultformat = $formatplugins[get_config('moodlecourse', 'format')];
93 if (!$defaultformat) {
94 echo $OUTPUT->error_text(get_string('defaultformatnotset', 'admin'));
95 echo $OUTPUT->footer();
96 exit;
100 $format = $formatplugins[$formatname];
101 $deleteurl = $format->get_uninstall_url();
102 if (!$deleteurl) {
103 // somebody was trying to cheat and type non-existing link
104 echo $OUTPUT->error_text(get_string('cannotuninstall', 'admin', $format->displayname));
105 echo $OUTPUT->footer();
106 exit;
109 if (!$confirm) {
110 if ($coursecount) {
111 $message = get_string('formatuninstallwithcourses', 'admin',
112 (object)array('count' => $coursecount, 'format' => $format->displayname,
113 'defaultformat' => $defaultformat->displayname));
114 } else {
115 $message = get_string('formatuninstallconfirm', 'admin', $format->displayname);
117 $deleteurl->param('confirm', 1);
118 echo $OUTPUT->confirm($message, $deleteurl, $return);
119 } else {
120 $a = new stdClass();
121 $a->plugin = $format->displayname;
122 $a->directory = $format->rootdir;
123 uninstall_plugin('format', $formatname);
124 echo $OUTPUT->notification(get_string('formatuninstalled', 'admin', $a), 'notifysuccess');
125 echo $OUTPUT->continue_button($return);
128 echo $OUTPUT->footer();
129 exit;
131 redirect($return);