Updated the 19 build version to 20101023
[moodle.git] / admin / modules.php
blob965f7f97918f68f8b5737f8c20cc27b47ee91382
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');
8 require_once($CFG->libdir.'/ddllib.php');
10 // defines
11 define('MODULE_TABLE','module_administration_table');
13 admin_externalpage_setup('managemodules');
15 $show = optional_param('show', '', PARAM_SAFEDIR);
16 $hide = optional_param('hide', '', PARAM_SAFEDIR);
17 $delete = optional_param('delete', '', PARAM_SAFEDIR);
18 $confirm = optional_param('confirm', '', PARAM_BOOL);
21 /// Print headings
23 $stractivities = get_string("activities");
24 $strdelete = get_string("delete");
25 $strversion = get_string("version");
26 $strhide = get_string("hide");
27 $strshow = get_string("show");
28 $strsettings = get_string("settings");
29 $stractivities = get_string("activities");
30 $stractivitymodule = get_string("activitymodule");
31 $strshowmodulecourse = get_string('showmodulecourse');
33 /// If data submitted, then process and store.
35 if (!empty($hide) and confirm_sesskey()) {
36 if (!$module = get_record("modules", "name", $hide)) {
37 error("Module doesn't exist!");
39 set_field("modules", "visible", "0", "id", $module->id); // Hide main module
40 // Remember the visibility status in visibleold
41 // and hide...
42 $sql = "UPDATE {$CFG->prefix}course_modules
43 SET visibleold=visible,
44 visible=0
45 WHERE module={$module->id}";
46 execute_sql($sql, false);
47 // clear the course modinfo cache for courses
48 // where we just deleted something
49 $sql = "UPDATE {$CFG->prefix}course
50 SET modinfo=''
51 WHERE id IN (SELECT DISTINCT course
52 FROM {$CFG->prefix}course_modules
53 WHERE visibleold=1 AND module={$module->id})";
54 execute_sql($sql, false);
55 admin_get_root(true, false); // settings not required - only pages
58 if (!empty($show) and confirm_sesskey()) {
59 if (!$module = get_record("modules", "name", $show)) {
60 error("Module doesn't exist!");
62 set_field("modules", "visible", "1", "id", $module->id); // Show main module
63 set_field('course_modules', 'visible', '1', 'visibleold',
64 '1', 'module', $module->id); // Get the previous saved visible state for the course module.
65 // clear the course modinfo cache for courses
66 // where we just made something visible
67 $sql = "UPDATE {$CFG->prefix}course
68 SET modinfo=''
69 WHERE id IN (SELECT DISTINCT course
70 FROM {$CFG->prefix}course_modules
71 WHERE visible=1 AND module={$module->id})";
72 execute_sql($sql, false);
73 admin_get_root(true, false); // settings not required - only pages
76 if (!empty($delete) and confirm_sesskey()) {
77 admin_externalpage_print_header();
78 print_heading($stractivities);
80 $strmodulename = get_string("modulename", "$delete");
82 if (!$confirm) {
83 notice_yesno(get_string("moduledeleteconfirm", "", $strmodulename),
84 "modules.php?delete=$delete&amp;confirm=1&amp;sesskey=$USER->sesskey",
85 "modules.php");
86 admin_externalpage_print_footer();
87 exit;
89 } else { // Delete everything!!
91 if ($delete == "forum") {
92 error("You can not delete the forum module!!");
95 if (!$module = get_record("modules", "name", $delete)) {
96 error("Module doesn't exist!");
99 // OK, first delete all the relevant instances from all course sections
100 if ($coursemods = get_records("course_modules", "module", $module->id)) {
101 foreach ($coursemods as $coursemod) {
102 if (! delete_mod_from_section($coursemod->id, $coursemod->section)) {
103 notify("Could not delete the $strmodulename with id = $coursemod->id from section $coursemod->section");
108 // delete calendar events
109 if (!delete_records("event", "modulename", $delete)) {
110 notify("Error occurred while deleting all $strmodulename records in calendar event table");
113 // clear course.modinfo for courses
114 // that used this module...
115 $sql = "UPDATE {$CFG->prefix}course
116 SET modinfo=''
117 WHERE id IN (SELECT DISTINCT course
118 FROM {$CFG->prefix}course_modules
119 WHERE module={$module->id})";
120 execute_sql($sql, false);
122 // Now delete all the course module records
123 if (!delete_records("course_modules", "module", $module->id)) {
124 notify("Error occurred while deleting all $strmodulename records in course_modules table");
126 if ($coursemods) {
127 foreach ($coursemods as $coursemod) {
128 if (!delete_context(CONTEXT_MODULE, $coursemod->id)) {
129 notify("Could not delete the context for $strmodulename with id = $coursemod->id");
134 // Then delete all the logs
135 if (!delete_records("log", "module", $module->name)) {
136 notify("Error occurred while deleting all $strmodulename records in log table");
139 // And log_display information
140 if (!delete_records("log_display", "module", $module->name)) {
141 notify("Error occurred while deleting all $strmodulename records in log_display table");
144 // And the module entry itself
145 if (!delete_records("modules", "name", $module->name)) {
146 notify("Error occurred while deleting the $strmodulename record from modules table");
149 // And the module configuration records
150 if (!execute_sql("DELETE FROM {$CFG->prefix}config WHERE name LIKE '{$module->name}_%'")) {
151 notify("Error occurred while deleting the $strmodulename records from the config table");
154 // cleanup the gradebook
155 require_once($CFG->libdir.'/gradelib.php');
156 grade_uninstalled_module($module->name);
158 // Then the tables themselves
159 drop_plugin_tables($module->name, "$CFG->dirroot/mod/$module->name/db/install.xml", false);
161 // Delete the capabilities that were defined by this module
162 capabilities_cleanup('mod/'.$module->name);
164 // remove entent handlers and dequeue pending events
165 events_uninstall('mod/'.$module->name);
167 // Perform any custom uninstall tasks
168 if (file_exists($CFG->dirroot . '/mod/' . $module->name . '/lib.php')) {
169 require_once($CFG->dirroot . '/mod/' . $module->name . '/lib.php');
170 $uninstallfunction = $module->name . '_uninstall';
171 if (function_exists($uninstallfunction)) {
172 if (! $uninstallfunction() ) {
173 notify('Encountered a problem running uninstall function for '. $module->name.'!');
178 $a->module = $strmodulename;
179 $a->directory = "$CFG->dirroot/mod/$delete";
180 notice(get_string("moduledeletefiles", "", $a), "modules.php");
184 admin_externalpage_print_header();
185 print_heading($stractivities);
187 /// Get and sort the existing modules
189 if (!$modules = get_records("modules")) {
190 error("No modules found!!"); // Should never happen
193 foreach ($modules as $module) {
194 $strmodulename = get_string("modulename", "$module->name");
195 // Deal with modules which are lacking the language string
196 if ($strmodulename == '[[modulename]]') {
197 $strmodulename = $module->name;
199 $modulebyname[$strmodulename] = $module;
201 ksort($modulebyname, SORT_LOCALE_STRING);
203 /// Print the table of all modules
204 // construct the flexible table ready to display
205 $table = new flexible_table(MODULE_TABLE);
206 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
207 $table->define_headers(array($stractivitymodule, $stractivities, $strversion, "$strhide/$strshow", $strdelete, $strsettings));
208 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/modules.php');
209 $table->set_attribute('id', 'modules');
210 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
211 $table->setup();
213 foreach ($modulebyname as $modulename => $module) {
215 // took out hspace="\10\", because it does not validate. don't know what to replace with.
216 $icon = "<img src=\"$CFG->modpixpath/$module->name/icon.gif\" class=\"icon\" alt=\"\" />";
218 $delete = "<a href=\"modules.php?delete=$module->name&amp;sesskey=$USER->sesskey\">$strdelete</a>";
220 if (file_exists("$CFG->dirroot/mod/$module->name/settings.php")) {
221 $settings = "<a href=\"settings.php?section=modsetting$module->name\">$strsettings</a>";
222 } else if (file_exists("$CFG->dirroot/mod/$module->name/config.html")) {
223 $settings = "<a href=\"module.php?module=$module->name\">$strsettings</a>";
224 } else {
225 $settings = "";
228 $count = count_records_select("$module->name",'course<>0');
229 if ($count>0) {
230 $countlink = "<a href=\"{$CFG->wwwroot}/course/search.php?modulelist=$module->name" .
231 "&amp;sesskey={$USER->sesskey}\" title=\"$strshowmodulecourse\">$count</a>";
233 else {
234 $countlink = "$count";
237 if ($module->visible) {
238 $visible = "<a href=\"modules.php?hide=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strhide\">".
239 "<img src=\"$CFG->pixpath/i/hide.gif\" class=\"icon\" alt=\"$strhide\" /></a>";
240 $class = "";
241 } else {
242 $visible = "<a href=\"modules.php?show=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strshow\">".
243 "<img src=\"$CFG->pixpath/i/show.gif\" class=\"icon\" alt=\"$strshow\" /></a>";
244 $class = " class=\"dimmed_text\"";
246 if ($module->name == "forum") {
247 $delete = "";
248 $visible = "";
249 $class = "";
252 $extra = '';
253 if (!file_exists("$CFG->dirroot/mod/$module->name/lib.php")) {
254 $extra = ' <span class="notifyproblem">('.get_string('missingfromdisk').')</span>';
257 $table->add_data(array(
258 '<span'.$class.'>'.$icon.' '.$modulename.$extra.'</span>',
259 $countlink,
260 '<span'.$class.'>'.$module->version.'</span>',
261 $visible,
262 $delete,
263 $settings
267 $table->print_html();
269 admin_externalpage_print_footer();