3 // Moves, adds, updates or deletes modules in a course
5 require("../config.php");
8 if (isset($course) && isset($HTTP_POST_VARS)) { // add or update form submitted
9 $mod = (object)$HTTP_POST_VARS;
11 require_login($mod->course);
13 if (!isteacher($mod->course)) {
14 error("You can't modify this course!");
17 $modcode = "../mod/$mod->modulename/mod.php";
18 if (file_exists($modcode)) {
21 error("This module is missing important code! (mod.php)");
26 if (! update_instance($mod)) {
27 error("Could not update the $mod->modulename");
29 add_to_log($mod->course, "course", "update mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance");
33 if (! $mod->instance = add_instance($mod)) {
34 error("Could not add a new instance of $mod->modulename");
36 // course_modules and course_sections each contain a reference
37 // to each other, so we have to update one of them twice.
39 if (! $mod->coursemodule = add_course_module($mod) ) {
40 error("Could not add a new course module");
42 if (! $sectionid = add_mod_to_section($mod) ) {
43 error("Could not add the new course module to that section");
45 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
46 error("Could not update the course module with the correct section");
48 add_to_log($mod->course, "course", "add mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance");
51 if (! delete_instance($mod->instance)) {
52 notify("Could not delete the $mod->modulename (instance)");
54 if (! delete_course_module($mod->coursemodule)) {
55 notify("Could not delete the $mod->modulename (coursemodule)");
57 if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
58 notify("Could not delete the $mod->modulename from that section");
60 add_to_log($mod->course, "course", "delete mod", "view.php?id=$mod->course", "$mod->modulename $mod->instance");
63 error("No mode defined");
67 if ($SESSION->returnpage) {
68 $return = $SESSION->returnpage;
69 unset($SESSION->returnpage);
72 redirect("view.php?id=$mod->course");
78 $SESSION->returnpage = $HTTP_REFERER;
83 require_variable($id);
85 move_module($id, $move);
87 redirect($HTTP_REFERER);
90 } else if (isset($delete)) { // value = course module
92 if (! $cm = get_record("course_modules", "id", $delete)) {
93 error("This course module doesn't exist");
96 if (! $course = get_record("course", "id", $cm->course)) {
97 error("This course doesn't exist");
100 require_login($course->id);
102 if (!isteacher($course->id)) {
103 error("You can't modify this course!");
106 if (! $module = get_record("modules", "id", $cm->module)) {
107 error("This module doesn't exist");
110 if (! $instance = get_record($module->name, "id", $cm->instance)) {
111 // Delete this module from the course right away
112 if (! delete_course_module($cm->id)) {
113 notify("Could not delete the $module->name (coursemodule)");
115 if (! delete_mod_from_section($cm->id, $cm->section)) {
116 notify("Could not delete the $module->name from that section");
118 error("The required instance of this module didn't exist. Module deleted.",
119 "$CFG->wwwroot/course/view.php?id=$course->id");
122 $fullmodulename = strtolower(get_string("modulename", $module->name));
124 $form->coursemodule = $cm->id;
125 $form->section = $cm->section;
126 $form->course = $cm->course;
127 $form->instance = $cm->instance;
128 $form->modulename = $module->name;
129 $form->fullmodulename = $fullmodulename;
130 $form->instancename = $instance->name;
132 include("mod_delete.html");
137 } else if (isset($update)) { // value = course module
139 if (! $cm = get_record("course_modules", "id", $update)) {
140 error("This course module doesn't exist");
143 if (! $course = get_record("course", "id", $cm->course)) {
144 error("This course doesn't exist");
147 if (! $module = get_record("modules", "id", $cm->module)) {
148 error("This module doesn't exist");
151 $sectionname = $COURSE_SECTION[$course->format];
153 if (! $form = get_record($module->name, "id", $cm->instance)) {
154 error("The required instance of this module doesn't exist");
157 if (! $cw = get_record("course_sections", "id", $cm->section)) {
158 error("This course section doesn't exist");
161 $form->coursemodule = $cm->id;
162 $form->section = $cm->section; // The section ID
163 $form->course = $course->id;
164 $form->module = $module->id;
165 $form->modulename = $module->name;
166 $form->instance = $cm->instance;
167 $form->mode = "update";
169 $fullmodulename = strtolower(get_string("modulename", $module->name));
170 $pageheading = "Updating a $fullmodulename in $sectionname $cw->section";
173 } else if (isset($add)) {
176 redirect($HTTP_REFERER);
180 require_variable($id);
181 require_variable($section);
183 if (! $course = get_record("course", "id", $id)) {
184 error("This course doesn't exist");
187 $sectionname = $COURSE_SECTION[$course->format];
189 if (! $module = get_record("modules", "name", $add)) {
190 error("This module type doesn't exist");
193 $form->section = $section; // The section number itself
194 $form->course = $course->id;
195 $form->module = $module->id;
196 $form->modulename = $module->name;
197 $form->instance = $cm->instance;
200 $fullmodulename = strtolower(get_string("modulename", $module->name));
202 $pageheading = "Adding a new $fullmodulename";
204 if ($form->section) {
205 $pageheading .= " to $sectionname $form->section";
209 error("No action was specfied");
212 require_login($course->id);
214 if (!isteacher($course->id)) {
215 error("You can't modify this course!");
218 if ($course->category) {
219 print_header("$course->shortname: Editing a $fullmodulename",
220 "$course->shortname: Editing a $fullmodulename",
221 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->
222 Editing a $fullmodulename", "form.name", "", false);
224 print_header("$course->shortname: Editing a $fullmodulename",
225 "$course->shortname: Editing a $fullmodulename",
226 "Editing a $fullmodulename", "form.name", "", false);
229 $modform = "../mod/$module->name/mod.html";
231 if (file_exists($modform)) {
233 print_heading($pageheading);
234 print_simple_box_start("center", "", "$THEME->cellheading");
236 print_simple_box_end();
239 notice("This module cannot be added to this course yet!", "$CFG->wwwroot/course/view.php?id=$course->id");
242 print_footer($course);