3 // Moves, adds, updates, duplicates or deletes modules in a course
5 require("../config.php");
10 $sectionreturn = optional_param('sr', '', PARAM_INT
);
12 if (isset($SESSION->modform
)) { // Variables are stored in the session
13 $mod = $SESSION->modform
;
14 unset($SESSION->modform
);
16 $mod = (object)$_POST;
20 if (!empty($SESSION->returnpage
)) {
21 $return = $SESSION->returnpage
;
22 unset($SESSION->returnpage
);
25 redirect("view.php?id=$mod->course#$sectionreturn");
30 if (isset($_POST["course"]) and confirm_sesskey()) { // add or update form submitted
32 if (!$course = get_record("course", "id", $mod->course
)) {
33 error("This course doesn't exist");
36 if (!isteacheredit($course->id
)) {
37 error("You can't modify this course!");
40 $mod->modulename
= clean_filename($mod->modulename
); // For safety
41 $modlib = "$CFG->dirroot/mod/$mod->modulename/lib.php";
43 if (file_exists($modlib)) {
44 include_once($modlib);
46 error("This module is missing important code! ($modlib)");
48 $addinstancefunction = $mod->modulename
."_add_instance";
49 $updateinstancefunction = $mod->modulename
."_update_instance";
50 $deleteinstancefunction = $mod->modulename
."_delete_instance";
51 $moderr = "$CFG->dirroot/mod/$mod->modulename/moderr.html";
56 if (isset($mod->name
)) {
57 if (trim($mod->name
) == '') {
62 $return = $updateinstancefunction($mod);
64 if (file_exists($moderr)) {
66 include_once($moderr);
69 error("Could not update the $mod->modulename", "view.php?id=$course->id");
72 if (isset($mod->visible
)) {
73 set_coursemodule_visible($mod->coursemodule
, $mod->visible
);
76 if (isset($mod->groupmode
)) {
77 set_coursemodule_groupmode($mod->coursemodule
, $mod->groupmode
);
80 if (is_string($return)) {
81 error($return, "view.php?id=$course->id");
84 if (isset($mod->redirect
)) {
85 $SESSION->returnpage
= $mod->redirecturl
;
87 $SESSION->returnpage
= "$CFG->wwwroot/mod/$mod->modulename/view.php?id=$mod->coursemodule";
90 add_to_log($course->id
, "course", "update mod",
91 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
92 "$mod->modulename $mod->instance");
93 add_to_log($course->id
, $mod->modulename
, "update",
94 "view.php?id=$mod->coursemodule",
95 "$mod->instance", $mod->coursemodule
);
100 if (trim($mod->name
) == '') {
101 $mod->name
= get_string("modulename", $mod->modulename
);
104 $return = $addinstancefunction($mod);
106 if (file_exists($moderr)) {
108 include_once($moderr);
111 error("Could not add a new instance of $mod->modulename", "view.php?id=$course->id");
113 if (is_string($return)) {
114 error($return, "view.php?id=$course->id");
117 if (!isset($mod->groupmode
)) { // to deal with pre-1.5 modules
118 $mod->groupmode
= $course->groupmode
; /// Default groupmode the same as course
121 $mod->instance
= $return;
123 // course_modules and course_sections each contain a reference
124 // to each other, so we have to update one of them twice.
126 if (! $mod->coursemodule
= add_course_module($mod) ) {
127 error("Could not add a new course module");
129 if (! $sectionid = add_mod_to_section($mod) ) {
130 error("Could not add the new course module to that section");
133 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule
)) {
134 error("Could not update the course module with the correct section");
137 if (!isset($mod->visible
)) { // We get the section's visible field status
138 $mod->visible
= get_field("course_sections","visible","id",$sectionid);
140 // make sure visibility is set correctly (in particular in calendar)
141 set_coursemodule_visible($mod->coursemodule
, $mod->visible
);
143 if (isset($mod->redirect
)) {
144 $SESSION->returnpage
= $mod->redirecturl
;
146 $SESSION->returnpage
= "$CFG->wwwroot/mod/$mod->modulename/view.php?id=$mod->coursemodule";
149 add_to_log($course->id
, "course", "add mod",
150 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
151 "$mod->modulename $mod->instance");
152 add_to_log($course->id
, $mod->modulename
, "add",
153 "view.php?id=$mod->coursemodule",
154 "$mod->instance", $mod->coursemodule
);
158 if (! $deleteinstancefunction($mod->instance
)) {
159 notify("Could not delete the $mod->modulename (instance)");
161 if (! delete_course_module($mod->coursemodule
)) {
162 notify("Could not delete the $mod->modulename (coursemodule)");
164 if (! delete_mod_from_section($mod->coursemodule
, "$mod->section")) {
165 notify("Could not delete the $mod->modulename from that section");
168 unset($SESSION->returnpage
);
170 add_to_log($course->id
, "course", "delete mod",
171 "view.php?id=$mod->course",
172 "$mod->modulename $mod->instance", $mod->coursemodule
);
175 error("No mode defined");
179 rebuild_course_cache($course->id
);
181 if (!empty($SESSION->returnpage
)) {
182 $return = $SESSION->returnpage
;
183 unset($SESSION->returnpage
);
186 redirect("view.php?id=$course->id#$sectionreturn");
191 if ((isset($_GET['movetosection']) or isset($_GET['moveto'])) and confirm_sesskey()) {
193 if (! $cm = get_record("course_modules", "id", $USER->activitycopy
)) {
194 error("The copied course module doesn't exist!");
197 if (isset($_GET['movetosection'])) {
198 if (! $section = get_record("course_sections", "id", $_GET['movetosection'])) {
199 error("This section doesn't exist");
203 } else { // normal moveto
204 if (! $beforecm = get_record("course_modules", "id", $_GET['moveto'])) {
205 error("The destination course module doesn't exist");
207 if (! $section = get_record("course_sections", "id", $beforecm->section
)) {
208 error("This section doesn't exist");
212 if (!isteacheredit($section->course
)) {
213 error("You can't modify this course!");
216 if (!ismoving($section->course
)) {
217 error("You need to copy something first!");
220 moveto_module($cm, $section, $beforecm);
222 unset($USER->activitycopy
);
223 unset($USER->activitycopycourse
);
224 unset($USER->activitycopyname
);
226 rebuild_course_cache($section->course
);
228 if (SITEID
== $section->course
) {
229 redirect($CFG->wwwroot
);
231 redirect("view.php?id=$section->course#$sectionreturn");
234 } else if (isset($_GET['indent']) and confirm_sesskey()) {
236 require_variable($id);
238 if (! $cm = get_record("course_modules", "id", $id)) {
239 error("This course module doesn't exist");
242 $cm->indent +
= $_GET['indent'];
244 if ($cm->indent
< 0) {
248 if (!set_field("course_modules", "indent", $cm->indent
, "id", $cm->id
)) {
249 error("Could not update the indent level on that course module");
252 if (SITEID
== $cm->course
) {
253 redirect($CFG->wwwroot
);
255 redirect("view.php?id=$cm->course#$sectionreturn");
259 } else if (isset($_GET['hide']) and confirm_sesskey()) {
261 if (! $cm = get_record("course_modules", "id", $_GET['hide'])) {
262 error("This course module doesn't exist");
265 if (!isteacheredit($cm->course
)) {
266 error("You can't modify this course!");
269 set_coursemodule_visible($cm->id
, 0);
271 rebuild_course_cache($cm->course
);
273 if (SITEID
== $cm->course
) {
274 redirect($CFG->wwwroot
);
276 redirect("view.php?id=$cm->course#$sectionreturn");
280 } else if (isset($_GET['show']) and confirm_sesskey()) {
282 if (! $cm = get_record("course_modules", "id", $_GET['show'])) {
283 error("This course module doesn't exist");
286 if (!isteacheredit($cm->course
)) {
287 error("You can't modify this course!");
290 if (! $section = get_record("course_sections", "id", $cm->section
)) {
291 error("This module doesn't exist");
294 if (! $module = get_record("modules", "id", $cm->module
)) {
295 error("This module doesn't exist");
298 if ($module->visible
and ($section->visible
or (SITEID
== $cm->course
))) {
299 set_coursemodule_visible($cm->id
, 1);
300 rebuild_course_cache($cm->course
);
303 if (SITEID
== $cm->course
) {
304 redirect($CFG->wwwroot
);
306 redirect("view.php?id=$cm->course#$sectionreturn");
310 } else if (isset($_GET['groupmode']) and confirm_sesskey()) {
312 if (! $cm = get_record("course_modules", "id", $_GET['id'])) {
313 error("This course module doesn't exist");
316 if (!isteacheredit($cm->course
)) {
317 error("You can't modify this course!");
320 set_coursemodule_groupmode($cm->id
, $_GET['groupmode']);
322 rebuild_course_cache($cm->course
);
324 if (SITEID
== $cm->course
) {
325 redirect($CFG->wwwroot
);
327 redirect("view.php?id=$cm->course#$sectionreturn");
331 } else if (isset($_GET['copy']) and confirm_sesskey()) { // value = course module
333 if (! $cm = get_record("course_modules", "id", $_GET['copy'])) {
334 error("This course module doesn't exist");
337 if (!isteacheredit($cm->course
)) {
338 error("You can't modify this course!");
341 if (! $section = get_record("course_sections", "id", $cm->section
)) {
342 error("This module doesn't exist");
345 if (! $module = get_record("modules", "id", $cm->module
)) {
346 error("This module doesn't exist");
349 if (! $instance = get_record($module->name
, "id", $cm->instance
)) {
350 error("Could not find the instance of this module");
353 $USER->activitycopy
= $copy;
354 $USER->activitycopycourse
= $cm->course
;
355 $USER->activitycopyname
= $instance->name
;
357 redirect("view.php?id=$cm->course#$sectionreturn");
359 } else if (isset($_GET['cancelcopy']) and confirm_sesskey()) { // value = course module
361 $courseid = $USER->activitycopycourse
;
363 unset($USER->activitycopy
);
364 unset($USER->activitycopycourse
);
365 unset($USER->activitycopyname
);
367 redirect("view.php?id=$courseid#$sectionreturn");
369 } else if (isset($_GET['delete']) and confirm_sesskey()) { // value = course module
371 if (! $cm = get_record("course_modules", "id", $_GET['delete'])) {
372 error("This course module doesn't exist");
375 if (! $course = get_record("course", "id", $cm->course
)) {
376 error("This course doesn't exist");
379 if (!isteacheredit($course->id
)) {
380 error("You can't modify this course!");
383 if (! $module = get_record("modules", "id", $cm->module
)) {
384 error("This module doesn't exist");
387 if (! $instance = get_record($module->name
, "id", $cm->instance
)) {
388 // Delete this module from the course right away
389 if (! delete_mod_from_section($cm->id
, $cm->section
)) {
390 notify("Could not delete the $module->name from that section");
392 if (! delete_course_module($cm->id
)) {
393 notify("Could not delete the $module->name (coursemodule)");
395 error("The required instance of this module didn't exist. Module deleted.",
396 "$CFG->wwwroot/course/view.php?id=$course->id");
399 $fullmodulename = get_string("modulename", $module->name
);
401 $form->coursemodule
= $cm->id
;
402 $form->section
= $cm->section
;
403 $form->course
= $cm->course
;
404 $form->instance
= $cm->instance
;
405 $form->modulename
= $module->name
;
406 $form->fullmodulename
= $fullmodulename;
407 $form->instancename
= $instance->name
;
408 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
410 $strdeletecheck = get_string("deletecheck", "", "$form->fullmodulename");
411 $strdeletecheckfull = get_string("deletecheckfull", "", "$form->fullmodulename '$form->instancename'");
413 print_header_simple("$strdeletecheck", "",
416 print_simple_box_start("center", "60%", "#FFAAAA", 20, "noticebox");
417 print_heading($strdeletecheckfull);
418 include_once("mod_delete.html");
419 print_simple_box_end();
420 print_footer($course);
425 } else if (isset($_GET['update']) and confirm_sesskey()) { // value = course module
427 if (! $cm = get_record("course_modules", "id", $_GET['update'])) {
428 error("This course module doesn't exist");
431 if (! $course = get_record("course", "id", $cm->course
)) {
432 error("This course doesn't exist");
435 if (!isteacheredit($course->id
)) {
436 error("You can't modify this course!");
439 if (! $module = get_record("modules", "id", $cm->module
)) {
440 error("This module doesn't exist");
443 if (! $form = get_record($module->name
, "id", $cm->instance
)) {
444 error("The required instance of this module doesn't exist");
447 if (! $cw = get_record("course_sections", "id", $cm->section
)) {
448 error("This course section doesn't exist");
451 if (isset($return)) {
452 $SESSION->returnpage
= "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
455 $form->coursemodule
= $cm->id
;
456 $form->section
= $cm->section
; // The section ID
457 $form->course
= $course->id
;
458 $form->module
= $module->id
;
459 $form->modulename
= $module->name
;
460 $form->instance
= $cm->instance
;
461 $form->mode
= "update";
462 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
464 $sectionname = get_string("name$course->format");
465 $fullmodulename = strtolower(get_string("modulename", $module->name
));
467 if ($form->section
) {
468 $heading->what
= $fullmodulename;
469 $heading->in
= "$sectionname $cw->section";
470 $pageheading = get_string("updatingain", "moodle", $heading);
472 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
474 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">".format_string($form->name
,true)."</a> ->";
476 } else if (isset($_GET['duplicate']) and confirm_sesskey()) { // value = course module
478 if (! $cm = get_record("course_modules", "id", $_GET['duplicate'])) {
479 error("This course module doesn't exist");
482 if (! $course = get_record("course", "id", $cm->course
)) {
483 error("This course doesn't exist");
486 if (!isteacheredit($course->id
)) {
487 error("You can't modify this course!");
490 if (! $module = get_record("modules", "id", $cm->module
)) {
491 error("This module doesn't exist");
494 if (! $form = get_record($module->name
, "id", $cm->instance
)) {
495 error("The required instance of this module doesn't exist");
498 if (! $cw = get_record("course_sections", "id", $cm->section
)) {
499 error("This course section doesn't exist");
502 if (isset($return)) {
503 $SESSION->returnpage
= "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
506 $section = get_field('course_sections', 'section', 'id', $cm->section
);
508 $form->coursemodule
= $cm->id
;
509 $form->section
= $section; // The section ID
510 $form->course
= $course->id
;
511 $form->module
= $module->id
;
512 $form->modulename
= $module->name
;
513 $form->instance
= $cm->instance
;
515 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
517 $sectionname = get_string("name$course->format");
518 $fullmodulename = strtolower(get_string("modulename", $module->name
));
520 if ($form->section
) {
521 $heading->what
= $fullmodulename;
522 $heading->in
= "$sectionname $cw->section";
523 $pageheading = get_string("duplicatingain", "moodle", $heading);
525 $pageheading = get_string("duplicatinga", "moodle", $fullmodulename);
527 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">$form->name</a> ->";
530 } else if (isset($_GET['add']) and confirm_sesskey()) {
532 if (empty($_GET['add'])) {
533 redirect($_SERVER["HTTP_REFERER"]);
537 require_variable($id);
538 require_variable($section);
540 if (! $course = get_record("course", "id", $id)) {
541 error("This course doesn't exist");
544 if (! $module = get_record("modules", "name", $_GET['add'])) {
545 error("This module type doesn't exist");
548 $form->section
= $section; // The section number itself
549 $form->course
= $course->id
;
550 $form->module
= $module->id
;
551 $form->modulename
= $module->name
;
552 $form->instance
= "";
553 $form->coursemodule
= "";
555 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
556 if (isset($_GET['type'])) {
557 $form->type
= $_GET['type'];
560 $sectionname = get_string("name$course->format");
561 $fullmodulename = get_string("modulename", $module->name
);
563 if ($form->section
) {
564 $heading->what
= $fullmodulename;
565 $heading->to
= "$sectionname $form->section";
566 $pageheading = get_string("addinganewto", "moodle", $heading);
568 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
573 error("No action was specfied");
576 if (!isteacheredit($course->id
)) {
577 error("You can't modify this course!");
580 $streditinga = get_string("editinga", "moodle", $fullmodulename);
581 $strmodulenameplural = get_string("modulenameplural", $module->name
);
583 if ($module->name
== "label") {
586 $focuscursor = "form.name";
589 print_header_simple($streditinga, '',
590 "<a href=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</a> ->
591 $strnav $streditinga", $focuscursor, "", false);
593 unset($SESSION->modform
); // Clear any old ones that may be hanging around.
595 $modform = "../mod/$module->name/mod.html";
597 if (file_exists($modform)) {
599 if ($usehtmleditor = can_use_html_editor()) {
600 $defaultformat = FORMAT_HTML
;
603 $defaultformat = FORMAT_MOODLE
;
606 $icon = "<img align=\"middle\" height=\"16\" width=\"16\" src=\"$CFG->modpixpath/$module->name/icon.gif\" alt=\"\" /> ";
608 print_heading_with_help($pageheading, "mods", $module->name
, $icon);
609 print_simple_box_start("center");
610 include_once($modform);
611 print_simple_box_end();
613 if ($usehtmleditor and empty($nohtmleditorneeded)) {
614 use_html_editor($editorfields);
618 notice("This module cannot be added to this course yet! (No file found at: $modform)", "$CFG->wwwroot/course/view.php?id=$course->id");
621 print_footer($course);