New translationd added.
[moodle.git] / course / mod.php
blob4355ebd3f4539f51592536e1417d3e84904711ab
1 <?PHP // $Id$
3 // Moves, adds, updates or deletes modules in a course
5 require("../config.php");
6 require("lib.php");
8 require_login();
10 if (isset($cancel)) {
11 if (!empty($SESSION->returnpage)) {
12 $return = $SESSION->returnpage;
13 unset($SESSION->returnpage);
14 redirect($return);
15 } else {
16 redirect("view.php?id=$mod->course");
21 if (isset($_POST["course"])) { // add or update form submitted
23 if (isset($SESSION->modform)) { // Variables are stored in the session
24 $mod = $SESSION->modform;
25 unset($SESSION->modform);
26 } else {
27 $mod = (object)$_POST;
30 if (!isteacher($mod->course)) {
31 error("You can't modify this course!");
34 $modlib = "../mod/$mod->modulename/lib.php";
35 if (file_exists($modlib)) {
36 include_once($modlib);
37 } else {
38 error("This module is missing important code! ($modlib)");
40 $addinstancefunction = $mod->modulename."_add_instance";
41 $updateinstancefunction = $mod->modulename."_update_instance";
42 $deleteinstancefunction = $mod->modulename."_delete_instance";
44 switch ($mod->mode) {
45 case "update":
46 if (! $updateinstancefunction($mod)) {
47 error("Could not update the $mod->modulename");
49 add_to_log($mod->course, "course", "update mod",
50 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
51 "$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");
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",
71 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
72 "$mod->modulename $mod->instance");
73 break;
74 case "delete":
75 if (! $deleteinstancefunction($mod->instance)) {
76 notify("Could not delete the $mod->modulename (instance)");
78 if (! delete_course_module($mod->coursemodule)) {
79 notify("Could not delete the $mod->modulename (coursemodule)");
81 if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
82 notify("Could not delete the $mod->modulename from that section");
84 add_to_log($mod->course, "course", "delete mod",
85 "view.php?id=$mod->course",
86 "$mod->modulename $mod->instance");
87 break;
88 default:
89 error("No mode defined");
93 rebuild_course_cache($mod->course);
95 if (!empty($SESSION->returnpage)) {
96 $return = $SESSION->returnpage;
97 unset($SESSION->returnpage);
98 redirect($return);
99 } else {
100 redirect("view.php?id=$mod->course");
102 exit;
106 if (isset($move)) {
108 require_variable($id);
110 if (! $cm = get_record("course_modules", "id", $id)) {
111 error("This course module doesn't exist");
114 if (!isteacher($cm->course)) {
115 error("You can't modify this course!");
118 move_module($cm, $move);
120 rebuild_course_cache($cm->course);
122 $site = get_site();
123 if ($site->id == $cm->course) {
124 redirect($CFG->wwwroot);
125 } else {
126 redirect("view.php?id=$cm->course");
128 exit;
130 } else if (isset($hide)) {
132 if (! $cm = get_record("course_modules", "id", $hide)) {
133 error("This course module doesn't exist");
136 if (!isteacher($cm->course)) {
137 error("You can't modify this course!");
140 hide_course_module($cm->id);
142 rebuild_course_cache($cm->course);
144 $site = get_site();
145 if ($site->id == $cm->course) {
146 redirect($CFG->wwwroot);
147 } else {
148 redirect("view.php?id=$cm->course");
150 exit;
152 } else if (isset($show)) {
154 if (! $cm = get_record("course_modules", "id", $show)) {
155 error("This course module doesn't exist");
158 if (!isteacher($cm->course)) {
159 error("You can't modify this course!");
162 if (! $section = get_record("course_sections", "id", $cm->section)) {
163 error("This module doesn't exist");
166 if (! $module = get_record("modules", "id", $cm->module)) {
167 error("This module doesn't exist");
170 $site = get_site();
172 if ($module->visible and ($section->visible or ($site->id == $cm->course))) {
173 show_course_module($cm->id);
174 rebuild_course_cache($cm->course);
177 if ($site->id == $cm->course) {
178 redirect($CFG->wwwroot);
179 } else {
180 redirect("view.php?id=$cm->course");
182 exit;
184 } else if (isset($delete)) { // value = course module
186 if (! $cm = get_record("course_modules", "id", $delete)) {
187 error("This course module doesn't exist");
190 if (! $course = get_record("course", "id", $cm->course)) {
191 error("This course doesn't exist");
194 if (!isteacher($course->id)) {
195 error("You can't modify this course!");
198 if (! $module = get_record("modules", "id", $cm->module)) {
199 error("This module doesn't exist");
202 if (! $instance = get_record($module->name, "id", $cm->instance)) {
203 // Delete this module from the course right away
204 if (! delete_course_module($cm->id)) {
205 notify("Could not delete the $module->name (coursemodule)");
207 if (! delete_mod_from_section($cm->id, $cm->section)) {
208 notify("Could not delete the $module->name from that section");
210 error("The required instance of this module didn't exist. Module deleted.",
211 "$CFG->wwwroot/course/view.php?id=$course->id");
214 $fullmodulename = strtolower(get_string("modulename", $module->name));
216 $form->coursemodule = $cm->id;
217 $form->section = $cm->section;
218 $form->course = $cm->course;
219 $form->instance = $cm->instance;
220 $form->modulename = $module->name;
221 $form->fullmodulename = $fullmodulename;
222 $form->instancename = $instance->name;
224 $strdeletecheck = get_string("deletecheck", "", "$form->fullmodulename");
225 $strdeletecheckfull = get_string("deletecheckfull", "", "$form->fullmodulename '$form->instancename'");
227 print_header("$course->shortname: $strdeletecheck", "$course->fullname",
228 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->
229 $strdeletecheck");
231 print_simple_box_start("center", "60%", "#FFAAAA", 20, "noticebox");
232 print_heading($strdeletecheckfull);
233 include_once("mod_delete.html");
234 print_simple_box_end();
235 print_footer($course);
237 exit;
240 } else if (isset($update)) { // value = course module
242 if (! $cm = get_record("course_modules", "id", $update)) {
243 error("This course module doesn't exist");
246 if (! $course = get_record("course", "id", $cm->course)) {
247 error("This course doesn't exist");
250 if (!isteacher($course->id)) {
251 error("You can't modify this course!");
254 if (! $module = get_record("modules", "id", $cm->module)) {
255 error("This module doesn't exist");
258 if (! $form = get_record($module->name, "id", $cm->instance)) {
259 error("The required instance of this module doesn't exist");
262 if (! $cw = get_record("course_sections", "id", $cm->section)) {
263 error("This course section doesn't exist");
266 if (isset($return)) {
267 $SESSION->returnpage = "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
270 $form->coursemodule = $cm->id;
271 $form->section = $cm->section; // The section ID
272 $form->course = $course->id;
273 $form->module = $module->id;
274 $form->modulename = $module->name;
275 $form->instance = $cm->instance;
276 $form->mode = "update";
278 $sectionname = get_string("name$course->format");
279 $fullmodulename = strtolower(get_string("modulename", $module->name));
281 if ($form->section) {
282 $heading->what = $fullmodulename;
283 $heading->in = "$sectionname $cw->section";
284 $pageheading = get_string("updatingain", "moodle", $heading);
285 } else {
286 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
290 } else if (isset($add)) {
292 if (!$add) {
293 redirect($_SERVER["HTTP_REFERER"]);
294 die;
297 require_variable($id);
298 require_variable($section);
300 if (! $course = get_record("course", "id", $id)) {
301 error("This course doesn't exist");
304 if (! $module = get_record("modules", "name", $add)) {
305 error("This module type doesn't exist");
308 $form->section = $section; // The section number itself
309 $form->course = $course->id;
310 $form->module = $module->id;
311 $form->modulename = $module->name;
312 $form->instance = "";
313 $form->coursemodule = "";
314 $form->mode = "add";
316 $sectionname = get_string("name$course->format");
317 $fullmodulename = strtolower(get_string("modulename", $module->name));
319 if ($form->section) {
320 $heading->what = $fullmodulename;
321 $heading->to = "$sectionname $form->section";
322 $pageheading = get_string("addinganewto", "moodle", $heading);
323 } else {
324 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
327 } else {
328 error("No action was specfied");
331 if (!isteacher($course->id)) {
332 error("You can't modify this course!");
335 $streditinga = get_string("editinga", "moodle", $fullmodulename);
336 $strmodulenameplural = get_string("modulenameplural", $module->name);
338 if ($course->category) {
339 print_header("$course->shortname: $streditinga", "$course->fullname",
340 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->
341 <A HREF=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</A> ->
342 $streditinga", "form.name", "", false);
343 } else {
344 print_header("$course->shortname: $streditinga", "$course->fullname",
345 "$streditinga", "form.name", "", false);
348 unset($SESSION->modform); // Clear any old ones that may be hanging around.
350 $modform = "../mod/$module->name/mod.html";
352 if (file_exists($modform)) {
354 print_heading($pageheading);
355 print_simple_box_start("center", "", "$THEME->cellheading");
356 include_once($modform);
357 print_simple_box_end();
359 } else {
360 notice("This module cannot be added to this course yet! (No file found at: $modform)", "$CFG->wwwroot/course/view.php?id=$course->id");
363 print_footer($course);