More efficient querying of courses (from Martin Langhoff)
[moodle.git] / admin / modules.php
blob6cdb3b07b2c16e9af859228ce8f693779abd099f
1 <?PHP // $Id$
2 // Allows the admin to create, delete and rename course categories
4 require_once("../config.php");
5 require_once("../course/lib.php");
7 optional_variable($disable);
8 optional_variable($enable);
9 optional_variable($delete);
10 optional_variable($confirm);
12 require_login();
14 if (!isadmin()) {
15 error("Only administrators can use this page!");
18 if (!$site = get_site()) {
19 error("Site isn't defined!");
23 /// Print headings
25 $stradministration = get_string("administration");
26 $strconfiguration = get_string("configuration");
27 $strmanagemodules = get_string("managemodules");
28 $strdelete = get_string("delete");
29 $strversion = get_string("version");
30 $strhide = get_string("hide");
31 $strshow = get_string("show");
32 $strsettings = get_string("settings");
33 $stractivities = get_string("activities");
34 $stractivitymodule = get_string("activitymodule");
36 print_header("$site->shortname: $strmanagemodules", "$site->fullname",
37 "<a href=\"index.php\">$stradministration</a> -> ".
38 "<a href=\"configure.php\">$strconfiguration</a> -> $strmanagemodules");
40 print_heading($strmanagemodules);
43 /// If data submitted, then process and store.
45 if (!empty($hide) and confirm_sesskey()) {
46 if (!$module = get_record("modules", "name", $hide)) {
47 error("Module doesn't exist!");
49 set_field("modules", "visible", "0", "id", $module->id); // Hide main module
50 set_field("course_modules", "visible", "0", "module", $module->id); // Hide all related activity modules
53 if (!empty($show) and confirm_sesskey()) {
54 if (!$module = get_record("modules", "name", $show)) {
55 error("Module doesn't exist!");
57 set_field("modules", "visible", "1", "id", $module->id); // Show main module
58 set_field("course_modules", "visible", "1", "module", $module->id); // Show all related activity modules
61 if (!empty($delete) and confirm_sesskey()) {
63 $strmodulename = get_string("modulename", "$delete");
65 if (empty($confirm)) {
66 notice_yesno(get_string("moduledeleteconfirm", "", $strmodulename),
67 "modules.php?delete=$delete&confirm=$delete&sesskey=$USER->sesskey",
68 "modules.php");
69 print_footer();
70 exit;
72 } else { // Delete everything!!
74 if ($delete == "forum") {
75 error("You can not delete the forum module!!");
78 if (!$module = get_record("modules", "name", $delete)) {
79 error("Module doesn't exist!");
82 // OK, first delete all the relevant instances from all course sections
83 if ($coursemods = get_records("course_modules", "module", $module->id)) {
84 foreach ($coursemods as $coursemod) {
85 if (! delete_mod_from_section($coursemod->id, $coursemod->section)) {
86 notify("Could not delete the $strmodulename with id = $coursemod->id from section $coursemod->section");
91 // Now delete all the course module records
92 if (!delete_records("course_modules", "module", $module->id)) {
93 notify("Error occurred while deleting all $strmodulename records in course_modules table");
96 // Then delete all the logs
97 if (!delete_records("log", "module", $module->name)) {
98 notify("Error occurred while deleting all $strmodulename records in log table");
101 // And log_display information
102 if (!delete_records("log_display", "module", $module->name)) {
103 notify("Error occurred while deleting all $strmodulename records in log_display table");
106 // And the module entry itself
107 if (!delete_records("modules", "name", $module->name)) {
108 notify("Error occurred while deleting the $strmodulename record from modules table");
111 // Then the tables themselves
113 if ($tables = $db->Metatables()) {
114 $prefix = $CFG->prefix.$module->name;
115 foreach ($tables as $table) {
116 if (strpos($table, $prefix) === 0) {
117 if (!execute_sql("DROP TABLE $table", false)) {
118 notify("ERROR: while trying to drop table $table");
124 rebuild_course_cache(); // Because things have changed
127 $a->module = $strmodulename;
128 $a->directory = "$CFG->dirroot/mod/$delete";
129 notice(get_string("moduledeletefiles", "", $a), "modules.php");
133 /// Get and sort the existing modules
135 if (!$modules = get_records("modules")) {
136 error("No modules found!!"); // Should never happen
139 foreach ($modules as $module) {
140 $strmodulename = get_string("modulename", "$module->name");
141 $modulebyname[$strmodulename] = $module;
143 ksort($modulebyname);
145 /// Print the table of all modules
147 if (empty($THEME->custompix)) {
148 $pixpath = "../pix";
149 $modpixpath = "../mod";
150 } else {
151 $pixpath = "../theme/$CFG->theme/pix";
152 $modpixpath = "../theme/$CFG->theme/pix/mod";
155 $table->head = array ($stractivitymodule, $stractivities, $strversion, "$strhide/$strshow", $strdelete, $strsettings);
156 $table->align = array ("LEFT", "RIGHT", "LEFT", "CENTER", "CENTER", "CENTER");
157 $table->wrap = array ("NOWRAP", "", "", "", "","");
158 $table->size = array ("100%", "10", "10", "10", "10","12");
159 $table->width = "100";
161 foreach ($modulebyname as $modulename => $module) {
163 $icon = "<img src=\"$modpixpath/$module->name/icon.gif\" hspace=10 height=16 width=16 border=0>";
165 $delete = "<a href=\"modules.php?delete=$module->name&sesskey=$USER->sesskey\">$strdelete</a>";
167 if (file_exists("$CFG->dirroot/mod/$module->name/config.html")) {
168 $settings = "<a href=\"module.php?module=$module->name&sesskey=$USER->sesskey\">$strsettings</a>";
169 } else {
170 $settings = "";
173 $count = count_records("$module->name");
175 if ($module->visible) {
176 $visible = "<a href=\"modules.php?hide=$module->name&sesskey=$USER->sesskey\" title=\"$strhide\">".
177 "<img src=\"$pixpath/i/hide.gif\" align=\"absmiddle\" height=16 width=16 border=0></a>";
178 $class = "";
179 } else {
180 $visible = "<a href=\"modules.php?show=$module->name&sesskey=$USER->sesskey\" title=\"$strshow\">".
181 "<img src=\"$pixpath/i/show.gif\" align=\"absmiddle\" height=16 width=16 border=0></a>";
182 $class = "class=\"dimmed_text\"";
184 if ($module->name == "forum") {
185 $delete = "";
186 $visible = "";
187 $class = "";
189 $table->data[] = array ("<p $class>$icon $modulename</p>", $count, $module->version, $visible, $delete, $settings);
191 print_table($table);
193 echo "<br /><br />";
195 print_footer();