Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / modules.php
blob5012a4be9aceac8fc91d0d2740015e71cffd27fe
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");
127 // Then delete all the logs
128 if (!delete_records("log", "module", $module->name)) {
129 notify("Error occurred while deleting all $strmodulename records in log table");
132 // And log_display information
133 if (!delete_records("log_display", "module", $module->name)) {
134 notify("Error occurred while deleting all $strmodulename records in log_display table");
137 // And the module entry itself
138 if (!delete_records("modules", "name", $module->name)) {
139 notify("Error occurred while deleting the $strmodulename record from modules table");
142 // And the module configuration records
143 if (!execute_sql("DELETE FROM {$CFG->prefix}config WHERE name LIKE '{$module->name}_%'")) {
144 notify("Error occurred while deleting the $strmodulename records from the config table");
147 // cleanup the gradebook
148 require_once($CFG->libdir.'/gradelib.php');
149 grade_uninstalled_module($module->name);
151 // Then the tables themselves
152 drop_plugin_tables($module->name, "$CFG->dirroot/mod/$module->name/db/install.xml", false);
154 // Delete the capabilities that were defined by this module
155 capabilities_cleanup('mod/'.$module->name);
157 // remove entent handlers and dequeue pending events
158 events_uninstall('mod/'.$module->name);
160 // Perform any custom uninstall tasks
161 if (file_exists($CFG->dirroot . '/mod/' . $module->name . '/lib.php')) {
162 require_once($CFG->dirroot . '/mod/' . $module->name . '/lib.php');
163 $uninstallfunction = $module->name . '_uninstall';
164 if (function_exists($uninstallfunction)) {
165 if (! $uninstallfunction() ) {
166 notify('Encountered a problem running uninstall function for '. $module->name.'!');
171 $a->module = $strmodulename;
172 $a->directory = "$CFG->dirroot/mod/$delete";
173 notice(get_string("moduledeletefiles", "", $a), "modules.php");
177 admin_externalpage_print_header();
178 print_heading($stractivities);
180 /// Get and sort the existing modules
182 if (!$modules = get_records("modules")) {
183 error("No modules found!!"); // Should never happen
186 foreach ($modules as $module) {
187 $strmodulename = get_string("modulename", "$module->name");
188 // Deal with modules which are lacking the language string
189 if ($strmodulename == '[[modulename]]') {
190 $strmodulename = $module->name;
192 $modulebyname[$strmodulename] = $module;
194 ksort($modulebyname, SORT_LOCALE_STRING);
196 /// Print the table of all modules
197 // construct the flexible table ready to display
198 $table = new flexible_table(MODULE_TABLE);
199 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
200 $table->define_headers(array($stractivitymodule, $stractivities, $strversion, "$strhide/$strshow", $strdelete, $strsettings));
201 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/modules.php');
202 $table->set_attribute('id', 'modules');
203 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
204 $table->setup();
206 foreach ($modulebyname as $modulename => $module) {
208 // took out hspace="\10\", because it does not validate. don't know what to replace with.
209 $icon = "<img src=\"$CFG->modpixpath/$module->name/icon.gif\" class=\"icon\" alt=\"\" />";
211 $delete = "<a href=\"modules.php?delete=$module->name&amp;sesskey=$USER->sesskey\">$strdelete</a>";
213 if (file_exists("$CFG->dirroot/mod/$module->name/settings.php")) {
214 $settings = "<a href=\"settings.php?section=modsetting$module->name\">$strsettings</a>";
215 } else if (file_exists("$CFG->dirroot/mod/$module->name/config.html")) {
216 $settings = "<a href=\"module.php?module=$module->name\">$strsettings</a>";
217 } else {
218 $settings = "";
221 $count = count_records_select("$module->name",'course<>0');
222 if ($count>0) {
223 $countlink = "<a href=\"{$CFG->wwwroot}/course/search.php?modulelist=$module->name" .
224 "&amp;sesskey={$USER->sesskey}\" title=\"$strshowmodulecourse\">$count</a>";
226 else {
227 $countlink = "$count";
230 if ($module->visible) {
231 $visible = "<a href=\"modules.php?hide=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strhide\">".
232 "<img src=\"$CFG->pixpath/i/hide.gif\" class=\"icon\" alt=\"$strhide\" /></a>";
233 $class = "";
234 } else {
235 $visible = "<a href=\"modules.php?show=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strshow\">".
236 "<img src=\"$CFG->pixpath/i/show.gif\" class=\"icon\" alt=\"$strshow\" /></a>";
237 $class = " class=\"dimmed_text\"";
239 if ($module->name == "forum") {
240 $delete = "";
241 $visible = "";
242 $class = "";
245 $extra = '';
246 if (!file_exists("$CFG->dirroot/mod/$module->name/lib.php")) {
247 $extra = ' <span class="notifyproblem">('.get_string('missingfromdisk').')</span>';
250 $table->add_data(array(
251 '<span'.$class.'>'.$icon.' '.$modulename.$extra.'</span>',
252 $countlink,
253 '<span'.$class.'>'.$module->version.'</span>',
254 $visible,
255 $delete,
256 $settings
260 $table->print_html();
262 admin_externalpage_print_footer();