Add link to module editing ... back to module type list
[moodle.git] / course / mod.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// Moves, adds, updates or deletes modules in a course
4
5 require("../config.php");
d9d1c35d 6 require("lib.php");
f9903ed0 7
7ce20f09 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 }
18
19
f9903ed0 20 if (isset($course) && isset($HTTP_POST_VARS)) { // add or update form submitted
ff80e012 21
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 }
f9903ed0 29
30 require_login($mod->course);
31
0d97d1e3 32 if (!isteacher($mod->course)) {
f9903ed0 33 error("You can't modify this course!");
34 }
35
04eba58f 36 $modlib = "../mod/$mod->modulename/lib.php";
37 if (file_exists($modlib)) {
38 include($modlib);
f9903ed0 39 } else {
04eba58f 40 error("This module is missing important code! ($modlib)");
f9903ed0 41 }
04eba58f 42 $addinstancefunction = $mod->modulename."_add_instance";
43 $updateinstancefunction = $mod->modulename."_update_instance";
44 $deleteinstancefunction = $mod->modulename."_delete_instance";
f9903ed0 45
46 switch ($mod->mode) {
47 case "update":
04eba58f 48 if (! $updateinstancefunction($mod)) {
f9903ed0 49 error("Could not update the $mod->modulename");
50 }
2a9da9ef 51 add_to_log($mod->course, "course", "update mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance");
f9903ed0 52 break;
53
54 case "add":
04eba58f 55 if (! $mod->instance = $addinstancefunction($mod)) {
f9903ed0 56 error("Could not add a new instance of $mod->modulename");
57 }
b5fe4c93 58 // course_modules and course_sections each contain a reference
f9903ed0 59 // to each other, so we have to update one of them twice.
60
d3ca63f0 61 if (! $mod->coursemodule = add_course_module($mod) ) {
f9903ed0 62 error("Could not add a new course module");
63 }
b5fe4c93 64 if (! $sectionid = add_mod_to_section($mod) ) {
65 error("Could not add the new course module to that section");
f9903ed0 66 }
b5fe4c93 67 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
68 error("Could not update the course module with the correct section");
f9903ed0 69 }
2a9da9ef 70 add_to_log($mod->course, "course", "add mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance");
f9903ed0 71 break;
72 case "delete":
04eba58f 73 if (! $deleteinstancefunction($mod->instance)) {
f3065eeb 74 notify("Could not delete the $mod->modulename (instance)");
f9903ed0 75 }
76 if (! delete_course_module($mod->coursemodule)) {
f3065eeb 77 notify("Could not delete the $mod->modulename (coursemodule)");
f9903ed0 78 }
b5fe4c93 79 if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
80 notify("Could not delete the $mod->modulename from that section");
f9903ed0 81 }
2a9da9ef 82 add_to_log($mod->course, "course", "delete mod", "view.php?id=$mod->course", "$mod->modulename $mod->instance");
f9903ed0 83 break;
84 default:
85 error("No mode defined");
86
87 }
88
e07635f4 89 if ($SESSION->returnpage) {
90 $return = $SESSION->returnpage;
91 unset($SESSION->returnpage);
8223d271 92 save_session("SESSION");
e07635f4 93 redirect($return);
94 } else {
95 redirect("view.php?id=$mod->course");
96 }
f9903ed0 97 exit;
98 }
99
100
101 if (isset($move)) {
102
103 require_variable($id);
104
105 move_module($id, $move);
106
107 redirect($HTTP_REFERER);
108 exit;
109
110 } else if (isset($delete)) { // value = course module
111
112 if (! $cm = get_record("course_modules", "id", $delete)) {
113 error("This course module doesn't exist");
114 }
115
116 if (! $course = get_record("course", "id", $cm->course)) {
117 error("This course doesn't exist");
118 }
119
501cdbd8 120 require_login($course->id);
121
122 if (!isteacher($course->id)) {
123 error("You can't modify this course!");
124 }
125
f9903ed0 126 if (! $module = get_record("modules", "id", $cm->module)) {
127 error("This module doesn't exist");
128 }
129
b5fe4c93 130 if (! $instance = get_record($module->name, "id", $cm->instance)) {
501cdbd8 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");
b5fe4c93 140 }
141
90845098 142 $fullmodulename = strtolower(get_string("modulename", $module->name));
f9903ed0 143
144 $form->coursemodule = $cm->id;
b5fe4c93 145 $form->section = $cm->section;
f9903ed0 146 $form->course = $cm->course;
147 $form->instance = $cm->instance;
148 $form->modulename = $module->name;
90845098 149 $form->fullmodulename = $fullmodulename;
b5fe4c93 150 $form->instancename = $instance->name;
f9903ed0 151
152 include("mod_delete.html");
153
154 exit;
155
156
157 } else if (isset($update)) { // value = course module
158
159 if (! $cm = get_record("course_modules", "id", $update)) {
160 error("This course module doesn't exist");
161 }
162
163 if (! $course = get_record("course", "id", $cm->course)) {
164 error("This course doesn't exist");
165 }
166
167 if (! $module = get_record("modules", "id", $cm->module)) {
168 error("This module doesn't exist");
169 }
170
171 if (! $form = get_record($module->name, "id", $cm->instance)) {
172 error("The required instance of this module doesn't exist");
173 }
174
b5fe4c93 175 if (! $cw = get_record("course_sections", "id", $cm->section)) {
176 error("This course section doesn't exist");
f9903ed0 177 }
178
7ce20f09 179 if (isset($return)) {
180 $SESSION->returnpage = "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
181 save_session("SESSION");
182 }
183
2a9da9ef 184 $form->coursemodule = $cm->id;
b5fe4c93 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";
f9903ed0 191
963301c5 192 $sectionname = get_string("name$course->format");
90845098 193 $fullmodulename = strtolower(get_string("modulename", $module->name));
963301c5 194
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 }
f9903ed0 202
203
204 } else if (isset($add)) {
205
206 if (!$add) {
207 redirect($HTTP_REFERER);
208 die;
209 }
210
211 require_variable($id);
b5fe4c93 212 require_variable($section);
f9903ed0 213
214 if (! $course = get_record("course", "id", $id)) {
215 error("This course doesn't exist");
216 }
217
218 if (! $module = get_record("modules", "name", $add)) {
219 error("This module type doesn't exist");
220 }
221
b5fe4c93 222 $form->section = $section; // The section number itself
f9903ed0 223 $form->course = $course->id;
224 $form->module = $module->id;
225 $form->modulename = $module->name;
226 $form->instance = $cm->instance;
227 $form->mode = "add";
228
963301c5 229 $sectionname = get_string("name$course->format");
90845098 230 $fullmodulename = strtolower(get_string("modulename", $module->name));
231
b5fe4c93 232 if ($form->section) {
963301c5 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);
f9903ed0 238 }
239
240 } else {
241 error("No action was specfied");
242 }
243
244 require_login($course->id);
245
0d97d1e3 246 if (!isteacher($course->id)) {
f9903ed0 247 error("You can't modify this course!");
248 }
249
963301c5 250 $streditinga = get_string("editinga", "moodle", $fullmodulename);
22710216 251 $strmodulenameplural = get_string("modulenameplural", $module->name);
963301c5 252
29769ed4 253 if ($course->category) {
963301c5 254 print_header("$course->shortname: $streditinga", "$course->fullname",
29769ed4 255 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->
22710216 256 <A HREF=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</A> ->
963301c5 257 $streditinga", "form.name", "", false);
29769ed4 258 } else {
963301c5 259 print_header("$course->shortname: $streditinga", "$course->fullname",
260 "$streditinga", "form.name", "", false);
29769ed4 261 }
f9903ed0 262
109613b2 263 unset($SESSION->modform); // Clear any old ones that may be hanging around.
264 save_session("SESSION");
265
f9903ed0 266 $modform = "../mod/$module->name/mod.html";
267
268 if (file_exists($modform)) {
269
270 print_heading($pageheading);
271 print_simple_box_start("center", "", "$THEME->cellheading");
272 include($modform);
273 print_simple_box_end();
274
275 } else {
276 notice("This module cannot be added to this course yet!", "$CFG->wwwroot/course/view.php?id=$course->id");
277 }
278
279 print_footer($course);
280
f9903ed0 281?>