Should be $COURSE not $course
[moodle-linuxchix.git] / admin / modules.php
blob08120caf2738ab8e8c68489725785b158f4f7e9c
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 $coursesaffected = false;
39 /// If data submitted, then process and store.
41 if (!empty($hide) and confirm_sesskey()) {
42 if (!$module = get_record("modules", "name", $hide)) {
43 error("Module doesn't exist!");
45 set_field("modules", "visible", "0", "id", $module->id); // Hide main module
46 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.
47 set_field('course_modules', 'visibleold', '0', 'visible' ,'0', 'module', $module->id);
48 set_field("course_modules", "visible", "0", "module", $module->id); // Hide all related activity modules
49 $coursesaffected = true;
52 if (!empty($show) and confirm_sesskey()) {
53 if (!$module = get_record("modules", "name", $show)) {
54 error("Module doesn't exist!");
56 set_field("modules", "visible", "1", "id", $module->id); // Show main module
57 set_field('course_modules', 'visible', '1', 'visibleold', '1', 'module', $module->id); // Get the previous saved visible state for the course module.
58 $coursesaffected = true;
61 if (!empty($delete) and confirm_sesskey()) {
63 $strmodulename = get_string("modulename", "$delete");
65 if (!$confirm) {
66 notice_yesno(get_string("moduledeleteconfirm", "", $strmodulename),
67 "modules.php?delete=$delete&amp;confirm=1&amp;sesskey=$USER->sesskey",
68 "modules.php");
69 admin_externalpage_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");
123 // Delete the capabilities that were defined by this module
124 capabilities_cleanup('mod/'.$module->name);
126 // remove entent handlers and dequeue pending events
127 events_uninstall('mod/'.$module->name);
129 // rebuild_course_cache(); // Because things have changed
130 $coursesaffected = true;
132 $a->module = $strmodulename;
133 $a->directory = "$CFG->dirroot/mod/$delete";
134 notice(get_string("moduledeletefiles", "", $a), "modules.php");
138 if ($coursesaffected) {
139 rebuild_course_cache(); // Because things have changed
143 /// Get and sort the existing modules
145 if (!$modules = get_records("modules")) {
146 error("No modules found!!"); // Should never happen
149 foreach ($modules as $module) {
150 $strmodulename = get_string("modulename", "$module->name");
151 // Deal with modules which are lacking the language string
152 if ($strmodulename == '[[modulename]]') {
153 $strmodulename = $module->name;
155 $modulebyname[$strmodulename] = $module;
157 ksort($modulebyname);
159 /// Print the table of all modules
160 // construct the flexible table ready to display
161 $table = new flexible_table(MODULE_TABLE);
162 $table->define_columns(array('name', 'instances', 'version', 'hideshow', 'delete', 'settings'));
163 $table->define_headers(array($stractivitymodule, $stractivities, $strversion, "$strhide/$strshow", $strdelete, $strsettings));
164 $table->define_baseurl($CFG->wwwroot.'/'.$CFG->admin.'/modules.php');
165 $table->set_attribute('id', 'modules');
166 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
167 $table->setup();
169 foreach ($modulebyname as $modulename => $module) {
171 // took out hspace="\10\", because it does not validate. don't know what to replace with.
172 $icon = "<img src=\"$CFG->modpixpath/$module->name/icon.gif\" class=\"icon\" alt=\"\" />";
174 $delete = "<a href=\"modules.php?delete=$module->name&amp;sesskey=$USER->sesskey\">$strdelete</a>";
176 if (file_exists("$CFG->dirroot/mod/$module->name/config.html")) {
177 $settings = "<a href=\"module.php?module=$module->name\">$strsettings</a>";
178 } else {
179 $settings = "";
182 $count = count_records_select("$module->name",'course<>0');
183 if ($count>0) {
184 $countlink = "<a href=\"{$CFG->wwwroot}/course/search.php?modulelist=$module->name" .
185 "&amp;sesskey={$USER->sesskey}\" title=\"$strshowmodulecourse\">$count</a>";
187 else {
188 $countlink = "$count";
191 if ($module->visible) {
192 $visible = "<a href=\"modules.php?hide=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strhide\">".
193 "<img src=\"$CFG->pixpath/i/hide.gif\" class=\"icon\" alt=\"$strhide\" /></a>";
194 $class = "";
195 } else {
196 $visible = "<a href=\"modules.php?show=$module->name&amp;sesskey=$USER->sesskey\" title=\"$strshow\">".
197 "<img src=\"$CFG->pixpath/i/show.gif\" class=\"icon\" alt=\"$strshow\" /></a>";
198 $class = " class=\"dimmed_text\"";
200 if ($module->name == "forum") {
201 $delete = "";
202 $visible = "";
203 $class = "";
206 $table->add_data(array(
207 '<span'.$class.'>'.$icon.' '.$modulename.'</span>',
208 $countlink,
209 '<span'.$class.'>'.$module->version.'</span>',
210 $visible,
211 $delete,
212 $settings
216 $table->print_html();
218 admin_externalpage_print_footer();