Add link to module editing ... back to module type list
[moodle.git] / course / mod.php
1 <?PHP // $Id$
3 //  Moves, adds, updates or deletes modules in a course
5     require("../config.php");
6     require("lib.php");
8     if (isset($cancel)) {  
9         if ($SESSION->returnpage) {
10             $return = $SESSION->returnpage;
11             unset($SESSION->returnpage);
12             save_session("SESSION");
13             redirect($return);
14         } else {
15             redirect("view.php?id=$mod->course");
16         }
17     } 
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");
26         } else {
27             $mod = (object)$HTTP_POST_VARS;
28         }
30         require_login($mod->course);
32         if (!isteacher($mod->course)) {
33             error("You can't modify this course!");
34         }
36         $modlib = "../mod/$mod->modulename/lib.php";
37         if (file_exists($modlib)) {
38             include($modlib);
39         } else {
40             error("This module is missing important code! ($modlib)");
41         }
42         $addinstancefunction    = $mod->modulename."_add_instance";
43         $updateinstancefunction = $mod->modulename."_update_instance";
44         $deleteinstancefunction = $mod->modulename."_delete_instance";
46         switch ($mod->mode) {
47             case "update":
48                 if (! $updateinstancefunction($mod)) {
49                     error("Could not update the $mod->modulename");
50                 }
51                 add_to_log($mod->course, "course", "update mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance"); 
52                 break;
54             case "add":
55                 if (! $mod->instance = $addinstancefunction($mod)) {
56                     error("Could not add a new instance of $mod->modulename");
57                 }
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");
63                 }
64                 if (! $sectionid = add_mod_to_section($mod) ) {
65                     error("Could not add the new course module to that section");
66                 }
67                 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
68                     error("Could not update the course module with the correct section");
69                 }   
70                 add_to_log($mod->course, "course", "add mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance"); 
71                 break;
72             case "delete":
73                 if (! $deleteinstancefunction($mod->instance)) {
74                     notify("Could not delete the $mod->modulename (instance)");
75                 }
76                 if (! delete_course_module($mod->coursemodule)) {
77                     notify("Could not delete the $mod->modulename (coursemodule)");
78                 }
79                 if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
80                     notify("Could not delete the $mod->modulename from that section");
81                 }
82                 add_to_log($mod->course, "course", "delete mod", "view.php?id=$mod->course", "$mod->modulename $mod->instance"); 
83                 break;
84             default:
85                 error("No mode defined");
87         }
89         if ($SESSION->returnpage) {
90             $return = $SESSION->returnpage;
91             unset($SESSION->returnpage);
92             save_session("SESSION");
93             redirect($return);
94         } else {
95             redirect("view.php?id=$mod->course");
96         }
97         exit;
98     }
101     if (isset($move)) {  
103         require_variable($id);   
105         move_module($id, $move);
107         redirect($HTTP_REFERER);
108         exit;
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");
114         }
116         if (! $course = get_record("course", "id", $cm->course)) {
117             error("This course doesn't exist");
118         }
120         require_login($course->id);
122         if (!isteacher($course->id)) {
123             error("You can't modify this course!");
124         }
126         if (! $module = get_record("modules", "id", $cm->module)) {
127             error("This module doesn't exist");
128         }
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)");
134             }
135             if (! delete_mod_from_section($cm->id, $cm->section)) {
136                 notify("Could not delete the $module->name from that section");
137             }
138             error("The required instance of this module didn't exist.  Module deleted.",
139                   "$CFG->wwwroot/course/view.php?id=$course->id");
140         }
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");
154         exit;
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");
161         }
163         if (! $course = get_record("course", "id", $cm->course)) {
164             error("This course doesn't exist");
165         }
167         if (! $module = get_record("modules", "id", $cm->module)) {
168             error("This module doesn't exist");
169         }
171         if (! $form = get_record($module->name, "id", $cm->instance)) {
172             error("The required instance of this module doesn't exist");
173         }
174         
175         if (! $cw = get_record("course_sections", "id", $cm->section)) {
176             error("This course section doesn't exist");
177         }
179         if (isset($return)) {  
180             $SESSION->returnpage = "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
181             save_session("SESSION");
182         }
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);
199         } else {
200             $pageheading = get_string("updatinga", "moodle", $fullmodulename);
201         }
203         
204     } else if (isset($add)) {
206         if (!$add) {
207             redirect($HTTP_REFERER);
208             die;
209         }
211         require_variable($id);
212         require_variable($section);
214         if (! $course = get_record("course", "id", $id)) {
215             error("This course doesn't exist");
216         }
218         if (! $module = get_record("modules", "name", $add)) {
219             error("This module type doesn't exist");
220         }
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;
227         $form->mode       = "add";
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);
236         } else {
237             $pageheading = get_string("addinganew", "moodle", $fullmodulename);
238         }
240     } else {
241         error("No action was specfied");
242     }
244     require_login($course->id);
246     if (!isteacher($course->id)) {
247         error("You can't modify this course!");
248     }
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);
258     } else {
259         print_header("$course->shortname: $streditinga", "$course->fullname",
260                      "$streditinga", "form.name", "", false);
261     }
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");
272         include($modform);
273         print_simple_box_end();
275     } else {
276         notice("This module cannot be added to this course yet!", "$CFG->wwwroot/course/view.php?id=$course->id");
277     }
279     print_footer($course);
281 ?>