MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / admin / modules.php
blob38454e56c39f67760dcb4c4e46c2078a11829264
1 <?PHP // $Id$
2 // Allows the admin to manage activity modules
4 require_once('../config.php');
5 require_once('../course/lib.php');
6 require_once($CFG->libdir.'/adminlib.php');
7 require_once($CFG->libdir.'/tablelib.php');
9 // defines
10 define('MODULE_TABLE','module_administration_table');
12 admin_externalpage_setup('managemodules');
14 $show = optional_param('show', '', PARAM_SAFEDIR);
15 $hide = optional_param('hide', '', PARAM_SAFEDIR);
16 $delete = optional_param('delete', '', PARAM_SAFEDIR);
17 $confirm = optional_param('confirm', '', PARAM_BOOL);
20 /// Print headings
22 $stractivities = get_string("activities");
23 $strdelete = get_string("delete");
24 $strversion = get_string("version");
25 $strhide = get_string("hide");
26 $strshow = get_string("show");
27 $strsettings = get_string("settings");
28 $stractivities = get_string("activities");
29 $stractivitymodule = get_string("activitymodule");
30 $strshowmodulecourse = get_string('showmodulecourse');
32 admin_externalpage_print_header();
34 print_heading($stractivities);
36 /// If data submitted, then process and store.
38 if (!empty($hide) and confirm_sesskey()) {
39 if (!$module = get_record("modules", "name", $hide)) {
40 error("Module doesn't exist!");
42 set_field("modules", "visible", "0", "id", $module->id); // Hide main module
43 // Remember the visibility status in visibleold
44 // and hide...
45 $sql = "UPDATE {$CFG->prefix}course_modules
46 SET visibleold=visible,
47 visible=0
48 WHERE module={$module->id}";
49 execute_sql($sql, false);
50 // clear the course modinfo cache for courses
51 // where we just deleted something
52 $sql = "UPDATE {$CFG->prefix}course
53 SET modinfo=''
54 WHERE id IN (SELECT DISTINCT course
55 FROM {$CFG->prefix}course_modules
56 WHERE visibleold=1 AND module={$module->id})";
57 execute_sql($sql, false);
60 if (!empty($show) and confirm_sesskey()) {
61 if (!$module = get_record("modules", "name", $show)) {
62 error("Module doesn't exist!");
64 set_field("modules", "visible", "1", "id", $module->id); // Show main module
65 set_field('course_modules', 'visible', '1', 'visibleold',
66 '1', 'module', $module->id); // Get the previous saved visible state for the course module.
67 // clear the course modinfo cache for courses
68 // where we just made something visible
69 $sql = "UPDATE {$CFG->prefix}course
70 SET modinfo=''
71 WHERE id IN (SELECT DISTINCT course
72 FROM {$CFG->prefix}course_modules
73 WHERE visible=1 AND module={$module->id})";
74 execute_sql($sql, false);
77 if (!empty($delete) and confirm_sesskey()) {
79 $strmodulename = get_string("modulename", "$delete");
81 if (!$confirm) {
82 notice_yesno(get_string("moduledeleteconfirm", "", $strmodulename),
83 "modules.php?delete=$delete&amp;confirm=1&amp;sesskey=$USER->sesskey",
84 "modules.php");
85 admin_externalpage_print_footer();
86 exit;
88 } else { // Delete everything!!
90 if ($delete == "forum") {
91 error("You can not delete the forum module!!");
94 if (!$module = get_record("modules", "name", $delete)) {
95 error("Module doesn't exist!");
98 // OK, first delete all the relevant instances from all course sections
99 if ($coursemods = get_records("course_modules", "module", $module->id)) {
100 foreach ($coursemods as $coursemod) {
101 if (! delete_mod_from_section($coursemod->id, $coursemod->section)) {
102 notify("Could not delete the $strmodulename with id = $coursemod->id from section $coursemod->section");
107 // clear course.modinfo for courses
108 // that used this module...
109 $sql = "UPDATE {$CFG->prefix}course
110 SET modinfo=''
111 WHERE id IN (SELECT DISTINCT course
112 FROM {$CFG->prefix}course_modules
113 WHERE module={$module->id})";
114 execute_sql($sql, false);
116 // Now delete all the course module records
117 if (!delete_records("course_modules", "module", $module->id)) {
118 notify("Error occurred while deleting all $strmodulename records in course_modules table");
121 // Then delete all the logs
122 if (!delete_records("log", "module", $module->name)) {
123 notify("Error occurred while deleting all $strmodulename records in log table");
126 // And log_display information
127 if (!delete_records("log_display", "module", $module->name)) {
128 notify("Error occurred while deleting all $strmodulename records in log_display table");
131 // And the module entry itself
132 if (!delete_records("modules", "name", $module->name)) {
133 notify("Error occurred while deleting the $strmodulename record from modules table");
136 // And the module configuration records
137 if (!execute_sql("DELETE FROM {$CFG->prefix}config WHERE name LIKE '{$module->name}_%'")) {
138 notify("Error occurred while deleting the $strmodulename records from the config table");
141 // Then the tables themselves
143 if ($tables = $db->Metatables()) {
144 $prefix = $CFG->prefix.$module->name;
145 foreach ($tables as $table) {
146 if (strpos($table, $prefix) === 0) {
147 if (!execute_sql("DROP TABLE $table", false)) {
148 notify("ERROR: while trying to drop table $table");
153 // Delete the capabilities that were defined by this module
154 capabilities_cleanup('mod/'.$module->name);
156 // remove entent handlers and dequeue pending events
157 events_uninstall('mod/'.$module->name);
159 $a->module = $strmodulename;
160 $a->directory = "$CFG->dirroot/mod/$delete";
161 notice(get_string("moduledeletefiles", "", $a), "modules.php");
165 /// Get and sort the existing modules
167 if (!$modules = get_records("modules")) {
168 error("No modules found!!"); // Should never happen
171 foreach ($modules as $module) {
172 $strmodulename = get_string("modulename", "$module->name");
173 // Deal with modules which are lacking the language string
174 if ($strmodulename == '[[modulename]]') {
175 $strmodulename = $module->name;
177 $modulebyname[$strmodulename] = $module;
179 ksort($modulebyname);
181 /// Print the table of all modules
182 // construct the flexible table ready to display
183 $table = new flexible_table(MODULE_TABLE);
184 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
185 $table->define_headers(array($stractivitymodule, $stractivities, $strversion, "$strhide/$strshow", $strdelete, $strsettings));
186 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/modules.php');
187 $table->set_attribute('id', 'modules');
188 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
189 $table->setup();
191 foreach ($modulebyname as $modulename => $module) {
193 // took out hspace="\10\", because it does not validate. don't know what to replace with.
194 $icon = "<img src=\"$CFG->modpixpath/$module->name/icon.gif\" class=\"icon\" alt=\"\" />";
196 $delete = "<a href=\"modules.php?delete=$module->name&amp;sesskey=$USER->sesskey\">$strdelete</a>";
198 if (file_exists("$CFG->dirroot/mod/$module->name/config.html")) {
199 $settings = "<a href=\"module.php?module=$module->name\">$strsettings</a>";
200 } else {
201 $settings = "";
204 $count = count_records_select("$module->name",'course<>0');
205 if ($count>0) {
206 $countlink = "<a href=\"{$CFG->wwwroot}/course/search.php?modulelist=$module->name" .
207 "&amp;sesskey={$USER->sesskey}\" title=\"$strshowmodulecourse\">$count</a>";
209 else {
210 $countlink = "$count";
213 if ($module->visible) {
214 $visible = "<a href=\"modules.php?hide=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strhide\">".
215 "<img src=\"$CFG->pixpath/i/hide.gif\" class=\"icon\" alt=\"$strhide\" /></a>";
216 $class = "";
217 } else {
218 $visible = "<a href=\"modules.php?show=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strshow\">".
219 "<img src=\"$CFG->pixpath/i/show.gif\" class=\"icon\" alt=\"$strshow\" /></a>";
220 $class = " class=\"dimmed_text\"";
222 if ($module->name == "forum") {
223 $delete = "";
224 $visible = "";
225 $class = "";
228 $table->add_data(array(
229 '<span'.$class.'>'.$icon.' '.$modulename.'</span>',
230 $countlink,
231 '<span'.$class.'>'.$module->version.'</span>',
232 $visible,
233 $delete,
234 $settings
238 $table->print_html();
240 admin_externalpage_print_footer();