3 // Moves, adds, updates or deletes modules in a course
5 require("../config.php");
9 if ($SESSION->returnpage) {
10 $return = $SESSION->returnpage;
11 unset($SESSION->returnpage);
12 save_session("SESSION");
15 redirect("view.php?id=$mod->course");
20 if (isset($course) && isset($HTTP_POST_VARS)) { // add or update form submitted
22 if (isset($SESSION->modform)) { // Variables are stored in the session
23 $mod = $SESSION->modform;
24 unset($SESSION->modform);
25 save_session("SESSION");
27 $mod = (object)$HTTP_POST_VARS;
30 require_login($mod->course);
32 if (!isteacher($mod->course)) {
33 error("You can't modify this course!");
36 $modlib = "../mod/$mod->modulename/lib.php";
37 if (file_exists($modlib)) {
40 error("This module is missing important code! ($modlib)");
42 $addinstancefunction = $mod->modulename."_add_instance";
43 $updateinstancefunction = $mod->modulename."_update_instance";
44 $deleteinstancefunction = $mod->modulename."_delete_instance";
48 if (! $updateinstancefunction($mod)) {
49 error("Could not update the $mod->modulename");
51 add_to_log($mod->course, "course", "update mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance");
55 if (! $mod->instance = $addinstancefunction($mod)) {
56 error("Could not add a new instance of $mod->modulename");
58 // course_modules and course_sections each contain a reference
59 // to each other, so we have to update one of them twice.
61 if (! $mod->coursemodule = add_course_module($mod) ) {
62 error("Could not add a new course module");
64 if (! $sectionid = add_mod_to_section($mod) ) {
65 error("Could not add the new course module to that section");
67 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
68 error("Could not update the course module with the correct section");
70 add_to_log($mod->course, "course", "add mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance");
73 if (! $deleteinstancefunction($mod->instance)) {
74 notify("Could not delete the $mod->modulename (instance)");
76 if (! delete_course_module($mod->coursemodule)) {
77 notify("Could not delete the $mod->modulename (coursemodule)");
79 if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
80 notify("Could not delete the $mod->modulename from that section");
82 add_to_log($mod->course, "course", "delete mod", "view.php?id=$mod->course", "$mod->modulename $mod->instance");
85 error("No mode defined");
89 if ($SESSION->returnpage) {
90 $return = $SESSION->returnpage;
91 unset($SESSION->returnpage);
92 save_session("SESSION");
95 redirect("view.php?id=$mod->course");
103 require_variable($id);
105 move_module($id, $move);
107 redirect($HTTP_REFERER);
110 } else if (isset($delete)) { // value = course module
112 if (! $cm = get_record("course_modules", "id", $delete)) {
113 error("This course module doesn't exist");
116 if (! $course = get_record("course", "id", $cm->course)) {
117 error("This course doesn't exist");
120 require_login($course->id);
122 if (!isteacher($course->id)) {
123 error("You can't modify this course!");
126 if (! $module = get_record("modules", "id", $cm->module)) {
127 error("This module doesn't exist");
130 if (! $instance = get_record($module->name, "id", $cm->instance)) {
131 // Delete this module from the course right away
132 if (! delete_course_module($cm->id)) {
133 notify("Could not delete the $module->name (coursemodule)");
135 if (! delete_mod_from_section($cm->id, $cm->section)) {
136 notify("Could not delete the $module->name from that section");
138 error("The required instance of this module didn't exist. Module deleted.",
139 "$CFG->wwwroot/course/view.php?id=$course->id");
142 $fullmodulename = strtolower(get_string("modulename", $module->name));
144 $form->coursemodule = $cm->id;
145 $form->section = $cm->section;
146 $form->course = $cm->course;
147 $form->instance = $cm->instance;
148 $form->modulename = $module->name;
149 $form->fullmodulename = $fullmodulename;
150 $form->instancename = $instance->name;
152 include("mod_delete.html");
157 } else if (isset($update)) { // value = course module
159 if (! $cm = get_record("course_modules", "id", $update)) {
160 error("This course module doesn't exist");
163 if (! $course = get_record("course", "id", $cm->course)) {
164 error("This course doesn't exist");
167 if (! $module = get_record("modules", "id", $cm->module)) {
168 error("This module doesn't exist");
171 if (! $form = get_record($module->name, "id", $cm->instance)) {
172 error("The required instance of this module doesn't exist");
175 if (! $cw = get_record("course_sections", "id", $cm->section)) {
176 error("This course section doesn't exist");
179 if (isset($return)) {
180 $SESSION->returnpage = "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
181 save_session("SESSION");
184 $form->coursemodule = $cm->id;
185 $form->section = $cm->section; // The section ID
186 $form->course = $course->id;
187 $form->module = $module->id;
188 $form->modulename = $module->name;
189 $form->instance = $cm->instance;
190 $form->mode = "update";
192 $sectionname = get_string("name$course->format");
193 $fullmodulename = strtolower(get_string("modulename", $module->name));
195 if ($form->section) {
196 $heading->what = $fullmodulename;
197 $heading->in = "$sectionname $cw->section";
198 $pageheading = get_string("updatingain", "moodle", $heading);
200 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
204 } else if (isset($add)) {
207 redirect($HTTP_REFERER);
211 require_variable($id);
212 require_variable($section);
214 if (! $course = get_record("course", "id", $id)) {
215 error("This course doesn't exist");
218 if (! $module = get_record("modules", "name", $add)) {
219 error("This module type doesn't exist");
222 $form->section = $section; // The section number itself
223 $form->course = $course->id;
224 $form->module = $module->id;
225 $form->modulename = $module->name;
226 $form->instance = $cm->instance;
229 $sectionname = get_string("name$course->format");
230 $fullmodulename = strtolower(get_string("modulename", $module->name));
232 if ($form->section) {
233 $heading->what = $fullmodulename;
234 $heading->to = "$sectionname $form->section";
235 $pageheading = get_string("addinganewto", "moodle", $heading);
237 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
241 error("No action was specfied");
244 require_login($course->id);
246 if (!isteacher($course->id)) {
247 error("You can't modify this course!");
250 $streditinga = get_string("editinga", "moodle", $fullmodulename);
251 $strmodulenameplural = get_string("modulenameplural", $module->name);
253 if ($course->category) {
254 print_header("$course->shortname: $streditinga", "$course->fullname",
255 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->
256 <A HREF=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</A> ->
257 $streditinga", "form.name", "", false);
259 print_header("$course->shortname: $streditinga", "$course->fullname",
260 "$streditinga", "form.name", "", false);
263 unset($SESSION->modform); // Clear any old ones that may be hanging around.
264 save_session("SESSION");
266 $modform = "../mod/$module->name/mod.html";
268 if (file_exists($modform)) {
270 print_heading($pageheading);
271 print_simple_box_start("center", "", "$THEME->cellheading");
273 print_simple_box_end();
276 notice("This module cannot be added to this course yet!", "$CFG->wwwroot/course/view.php?id=$course->id");
279 print_footer($course);