Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / admin / modules.php
blob43d606cc97599d8c4f5ec3236c2117908b71c47e
1 <?PHP // $Id$
2 // Allows the admin to manage activity modules
4 require_once('../config.php');
5 require_once($CFG->libdir.'/adminlib.php');
6 $adminroot = admin_get_root();
7 admin_externalpage_setup('managemodules', $adminroot);
8 require_once('../course/lib.php');
10 $show = optional_param('show', '', PARAM_SAFEDIR);
11 $hide = optional_param('hide', '', PARAM_SAFEDIR);
12 $delete = optional_param('delete', '', PARAM_SAFEDIR);
13 $confirm = optional_param('confirm', '', PARAM_BOOL);
16 /// Print headings
18 $stractivities = get_string("activities");
19 $strdelete = get_string("delete");
20 $strversion = get_string("version");
21 $strhide = get_string("hide");
22 $strshow = get_string("show");
23 $strsettings = get_string("settings");
24 $stractivities = get_string("activities");
25 $stractivitymodule = get_string("activitymodule");
27 admin_externalpage_print_header($adminroot);
29 print_heading($stractivities);
31 $coursesaffected = false;
34 /// If data submitted, then process and store.
36 if (!empty($hide) and confirm_sesskey()) {
37 if (!$module = get_record("modules", "name", $hide)) {
38 error("Module doesn't exist!");
40 set_field("modules", "visible", "0", "id", $module->id); // Hide main module
41 set_field('course_modules', 'visibleold', '1', 'visible' ,'1', 'module', $module->id); // Remember the previous visible state so we can toggle this back if the module is unhidden.
42 set_field('course_modules', 'visibleold', '0', 'visible' ,'0', 'module', $module->id);
43 set_field("course_modules", "visible", "0", "module", $module->id); // Hide all related activity modules
44 $coursesaffected = true;
47 if (!empty($show) and confirm_sesskey()) {
48 if (!$module = get_record("modules", "name", $show)) {
49 error("Module doesn't exist!");
51 set_field("modules", "visible", "1", "id", $module->id); // Show main module
52 set_field('course_modules', 'visible', '1', 'visibleold', '1', 'module', $module->id); // Get the previous saved visible state for the course module.
53 $coursesaffected = true;
56 if (!empty($delete) and confirm_sesskey()) {
58 $strmodulename = get_string("modulename", "$delete");
60 if (!$confirm) {
61 notice_yesno(get_string("moduledeleteconfirm", "", $strmodulename),
62 "modules.php?delete=$delete&amp;confirm=1&amp;sesskey=$USER->sesskey",
63 "modules.php");
64 admin_externalpage_print_footer($adminroot);
65 exit;
67 } else { // Delete everything!!
69 if ($delete == "forum") {
70 error("You can not delete the forum module!!");
73 if (!$module = get_record("modules", "name", $delete)) {
74 error("Module doesn't exist!");
77 // OK, first delete all the relevant instances from all course sections
78 if ($coursemods = get_records("course_modules", "module", $module->id)) {
79 foreach ($coursemods as $coursemod) {
80 if (! delete_mod_from_section($coursemod->id, $coursemod->section)) {
81 notify("Could not delete the $strmodulename with id = $coursemod->id from section $coursemod->section");
86 // Now delete all the course module records
87 if (!delete_records("course_modules", "module", $module->id)) {
88 notify("Error occurred while deleting all $strmodulename records in course_modules table");
91 // Then delete all the logs
92 if (!delete_records("log", "module", $module->name)) {
93 notify("Error occurred while deleting all $strmodulename records in log table");
96 // And log_display information
97 if (!delete_records("log_display", "module", $module->name)) {
98 notify("Error occurred while deleting all $strmodulename records in log_display table");
101 // And the module entry itself
102 if (!delete_records("modules", "name", $module->name)) {
103 notify("Error occurred while deleting the $strmodulename record from modules table");
106 // Then the tables themselves
108 if ($tables = $db->Metatables()) {
109 $prefix = $CFG->prefix.$module->name;
110 foreach ($tables as $table) {
111 if (strpos($table, $prefix) === 0) {
112 if (!execute_sql("DROP TABLE $table", false)) {
113 notify("ERROR: while trying to drop table $table");
118 // Delete the capabilities that were defined by this module
119 capabilities_cleanup('mod/'.$module->name);
121 // rebuild_course_cache(); // Because things have changed
122 $coursesaffected = true;
124 $a->module = $strmodulename;
125 $a->directory = "$CFG->dirroot/mod/$delete";
126 notice(get_string("moduledeletefiles", "", $a), "modules.php");
130 if ($coursesaffected) {
131 rebuild_course_cache(); // Because things have changed
135 /// Get and sort the existing modules
137 if (!$modules = get_records("modules")) {
138 error("No modules found!!"); // Should never happen
141 foreach ($modules as $module) {
142 $strmodulename = get_string("modulename", "$module->name");
143 // Deal with modules which are lacking the language string
144 if ($strmodulename == '[[modulename]]') {
145 $strmodulename = $module->name;
147 $modulebyname[$strmodulename] = $module;
149 ksort($modulebyname);
151 /// Print the table of all modules
153 $table->head = array ($stractivitymodule, $stractivities, $strversion, "$strhide/$strshow", $strdelete, $strsettings);
154 $table->align = array ("left", "right", "left", "center", "center", "center");
155 $table->wrap = array ("nowrap", "", "", "", "","");
156 $table->size = array ("100%", "10", "10", "10", "10","12");
157 $table->width = "100";
159 foreach ($modulebyname as $modulename => $module) {
161 $icon = "<img src=\"$CFG->modpixpath/$module->name/icon.gif\" hspace=\"10\" class=\"icon\" alt=\"\" />";
163 $delete = "<a href=\"modules.php?delete=$module->name&amp;sesskey=$USER->sesskey\">$strdelete</a>";
165 if (file_exists("$CFG->dirroot/mod/$module->name/config.html")) {
166 $settings = "<a href=\"module.php?module=$module->name\">$strsettings</a>";
167 } else {
168 $settings = "";
171 $count = count_records("$module->name");
173 if ($module->visible) {
174 $visible = "<a href=\"modules.php?hide=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strhide\">".
175 "<img src=\"$CFG->pixpath/i/hide.gif\" class=\"icon\" alt=\"$strhide\" /></a>";
176 $class = "";
177 } else {
178 $visible = "<a href=\"modules.php?show=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strshow\">".
179 "<img src=\"$CFG->pixpath/i/show.gif\" class=\"icon\" alt=\"$strshow\" /></a>";
180 $class = "class=\"dimmed_text\"";
182 if ($module->name == "forum") {
183 $delete = "";
184 $visible = "";
185 $class = "";
187 $table->data[] = array ("<span $class>$icon $modulename</span>", $count, $module->version, $visible, $delete, $settings);
189 print_table($table);
191 echo "<br /><br />";
193 admin_externalpage_print_footer($adminroot);