3 // Moves, adds, updates, duplicates or deletes modules in a course
5 require("../config.php");
6 require_once("lib.php");
10 $sectionreturn = optional_param('sr', '', PARAM_INT
);
11 $add = optional_param('add','', PARAM_ALPHA
);
12 $type = optional_param('type', '', PARAM_ALPHA
);
13 $indent = optional_param('indent', 0, PARAM_INT
);
14 $update = optional_param('update', 0, PARAM_INT
);
15 $hide = optional_param('hide', 0, PARAM_INT
);
16 $show = optional_param('show', 0, PARAM_INT
);
17 $copy = optional_param('copy', 0, PARAM_INT
);
18 $moveto = optional_param('moveto', 0, PARAM_INT
);
19 $movetosection = optional_param('movetosection', 0, PARAM_INT
);
20 $delete = optional_param('delete', 0, PARAM_INT
);
21 $course = optional_param('course', 0, PARAM_INT
);
22 $groupmode = optional_param('groupmode', -1, PARAM_INT
);
23 $duplicate = optional_param('duplicate', 0, PARAM_INT
);
24 $cancel = optional_param('cancel', 0, PARAM_BOOL
);
25 $cancelcopy = optional_param('cancelcopy', 0, PARAM_BOOL
);
27 if (isset($SESSION->modform
)) { // Variables are stored in the session
28 $mod = $SESSION->modform
;
29 unset($SESSION->modform
);
31 $mod = (object)$_POST;
35 if (!empty($SESSION->returnpage
)) {
36 $return = $SESSION->returnpage
;
37 unset($SESSION->returnpage
);
40 redirect("view.php?id=$mod->course#section-$sectionreturn");
44 //check if we are adding / editing a module that has new forms using formslib
47 if (file_exists("../mod/$modname/mod_form.php")) {
48 $id = required_param('id', PARAM_INT
);
49 $section = required_param('section', PARAM_INT
);
50 $type = optional_param('type', '', PARAM_ALPHA
);
51 $returntomod = optional_param('return', 0, PARAM_BOOL
);
53 redirect("modedit.php?add=$add&type=$type&course=$id§ion=$section&return=$returntomod");
55 }elseif (!empty($update)){
56 if (!$modname=get_field_sql("SELECT md.name
57 FROM {$CFG->prefix}course_modules cm,
58 {$CFG->prefix}modules md
59 WHERE cm.id = '$update' AND
61 error('Invalid course module id!');
63 $returntomod = optional_param('return', 0, PARAM_BOOL
);
64 if (file_exists("../mod/$modname/mod_form.php")) {
65 redirect("modedit.php?update=$update&return=$returntomod");
68 //not adding / editing a module that has new forms using formslib
71 if (!empty($course) and confirm_sesskey()) { // add, delete or update form submitted
73 if (empty($mod->coursemodule
)) { //add
74 if (! $course = get_record("course", "id", $mod->course
)) {
75 error("This course doesn't exist");
78 $mod->coursemodule
= '';
79 } else { //delete and update
80 if (! $cm = get_record("course_modules", "id", $mod->coursemodule
)) {
81 error("This course module doesn't exist");
84 if (! $course = get_record("course", "id", $cm->course
)) {
85 error("This course doesn't exist");
87 $mod->instance
= $cm->instance
;
88 $mod->coursemodule
= $cm->id
;
91 require_login($course->id
); // needed to setup proper $COURSE
92 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
93 require_capability('moodle/course:manageactivities', $context);
95 $mod->course
= $course->id
;
96 $mod->modulename
= clean_param($mod->modulename
, PARAM_SAFEDIR
); // For safety
97 $modlib = "$CFG->dirroot/mod/$mod->modulename/lib.php";
99 if (file_exists($modlib)) {
100 include_once($modlib);
102 error("This module is missing important code! ($modlib)");
104 $addinstancefunction = $mod->modulename
."_add_instance";
105 $updateinstancefunction = $mod->modulename
."_update_instance";
106 $deleteinstancefunction = $mod->modulename
."_delete_instance";
107 $moderr = "$CFG->dirroot/mod/$mod->modulename/moderr.html";
109 switch ($mod->mode
) {
112 if (isset($mod->name
)) {
113 if (trim($mod->name
) == '') {
118 $return = $updateinstancefunction($mod);
120 if (file_exists($moderr)) {
122 include_once($moderr);
125 error("Could not update the $mod->modulename", "view.php?id=$course->id");
127 if (is_string($return)) {
128 error($return, "view.php?id=$course->id");
131 if (isset($mod->visible
)) {
132 set_coursemodule_visible($mod->coursemodule
, $mod->visible
);
135 if (isset($mod->groupmode
)) {
136 set_coursemodule_groupmode($mod->coursemodule
, $mod->groupmode
);
139 if (isset($mod->groupingid
)) {
140 set_coursemodule_groupingid($mod->coursemodule
, $mod->groupingid
);
143 if (isset($mod->groupmembersonly
)) {
144 set_coursemodule_groupmembersonly($mod->coursemodule
, $mod->groupmembersonly
);
147 if (isset($mod->redirect
)) {
148 $SESSION->returnpage
= $mod->redirecturl
;
150 $SESSION->returnpage
= "$CFG->wwwroot/mod/$mod->modulename/view.php?id=$mod->coursemodule";
153 add_to_log($course->id
, "course", "update mod",
154 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
155 "$mod->modulename $mod->instance");
156 add_to_log($course->id
, $mod->modulename
, "update",
157 "view.php?id=$mod->coursemodule",
158 "$mod->instance", $mod->coursemodule
);
163 if (!course_allowed_module($course,$mod->modulename
)) {
164 error("This module ($mod->modulename) has been disabled for this particular course");
167 if (!isset($mod->name
) ||
trim($mod->name
) == '') {
168 $mod->name
= get_string("modulename", $mod->modulename
);
171 $return = $addinstancefunction($mod);
173 if (file_exists($moderr)) {
175 include_once($moderr);
178 error("Could not add a new instance of $mod->modulename", "view.php?id=$course->id");
180 if (is_string($return)) {
181 error($return, "view.php?id=$course->id");
184 if (!isset($mod->groupmode
)) { // to deal with pre-1.5 modules
185 $mod->groupmode
= $course->groupmode
; /// Default groupmode the same as course
188 if (isset($mod->groupingid
)) {
189 set_coursemodule_groupingid($mod->coursemodule
, $mod->groupingid
);
192 if (isset($mod->groupmembersonly
)) {
193 set_coursemodule_groupmembersonly($mod->coursemodule
, $mod->groupmembersonly
);
195 $mod->instance
= $return;
197 // course_modules and course_sections each contain a reference
198 // to each other, so we have to update one of them twice.
200 if (! $mod->coursemodule
= add_course_module($mod) ) {
201 error("Could not add a new course module");
203 if (! $sectionid = add_mod_to_section($mod) ) {
204 error("Could not add the new course module to that section");
207 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule
)) {
208 error("Could not update the course module with the correct section");
211 if (!isset($mod->visible
)) { // We get the section's visible field status
212 $mod->visible
= get_field("course_sections","visible","id",$sectionid);
214 // make sure visibility is set correctly (in particular in calendar)
215 set_coursemodule_visible($mod->coursemodule
, $mod->visible
);
217 if (isset($mod->redirect
)) {
218 $SESSION->returnpage
= $mod->redirecturl
;
220 $SESSION->returnpage
= "$CFG->wwwroot/mod/$mod->modulename/view.php?id=$mod->coursemodule";
223 add_to_log($course->id
, "course", "add mod",
224 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
225 "$mod->modulename $mod->instance");
226 add_to_log($course->id
, $mod->modulename
, "add",
227 "view.php?id=$mod->coursemodule",
228 "$mod->instance", $mod->coursemodule
);
232 if ($cm and $cw = get_record("course_sections", "id", $cm->section
)) {
233 $sectionreturn = $cw->section
;
236 if (! $deleteinstancefunction($mod->instance
)) {
237 notify("Could not delete the $mod->modulename (instance)");
239 if (! delete_course_module($mod->coursemodule
)) {
240 notify("Could not delete the $mod->modulename (coursemodule)");
242 if (! delete_mod_from_section($mod->coursemodule
, "$mod->section")) {
243 notify("Could not delete the $mod->modulename from that section");
246 unset($SESSION->returnpage
);
248 add_to_log($course->id
, "course", "delete mod",
249 "view.php?id=$mod->course",
250 "$mod->modulename $mod->instance", $mod->coursemodule
);
253 error("No mode defined");
257 rebuild_course_cache($course->id
);
259 if (!empty($SESSION->returnpage
)) {
260 $return = $SESSION->returnpage
;
261 unset($SESSION->returnpage
);
264 redirect("view.php?id=$course->id#section-$sectionreturn");
269 if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
271 if (! $cm = get_record("course_modules", "id", $USER->activitycopy
)) {
272 error("The copied course module doesn't exist!");
275 if (!empty($movetosection)) {
276 if (! $section = get_record("course_sections", "id", $movetosection)) {
277 error("This section doesn't exist");
281 } else { // normal moveto
282 if (! $beforecm = get_record("course_modules", "id", $moveto)) {
283 error("The destination course module doesn't exist");
285 if (! $section = get_record("course_sections", "id", $beforecm->section
)) {
286 error("This section doesn't exist");
290 require_login($section->course
); // needed to setup proper $COURSE
291 $context = get_context_instance(CONTEXT_COURSE
, $section->course
);
292 require_capability('moodle/course:manageactivities', $context);
294 if (!ismoving($section->course
)) {
295 error("You need to copy something first!");
298 moveto_module($cm, $section, $beforecm);
300 unset($USER->activitycopy
);
301 unset($USER->activitycopycourse
);
302 unset($USER->activitycopyname
);
304 rebuild_course_cache($section->course
);
306 if (SITEID
== $section->course
) {
307 redirect($CFG->wwwroot
);
309 redirect("view.php?id=$section->course#section-$sectionreturn");
312 } else if (!empty($indent) and confirm_sesskey()) {
314 $id = required_param('id',PARAM_INT
);
316 if (! $cm = get_record("course_modules", "id", $id)) {
317 error("This course module doesn't exist");
320 require_login($cm->course
); // needed to setup proper $COURSE
321 $context = get_context_instance(CONTEXT_COURSE
, $cm->course
);
322 require_capability('moodle/course:manageactivities', $context);
324 $cm->indent +
= $indent;
326 if ($cm->indent
< 0) {
330 if (!set_field("course_modules", "indent", $cm->indent
, "id", $cm->id
)) {
331 error("Could not update the indent level on that course module");
334 if (SITEID
== $cm->course
) {
335 redirect($CFG->wwwroot
);
337 redirect("view.php?id=$cm->course#section-$sectionreturn");
341 } else if (!empty($hide) and confirm_sesskey()) {
343 if (! $cm = get_record("course_modules", "id", $hide)) {
344 error("This course module doesn't exist");
347 require_login($cm->course
); // needed to setup proper $COURSE
348 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
349 require_capability('moodle/course:activityvisibility', $context);
351 set_coursemodule_visible($cm->id
, 0);
353 rebuild_course_cache($cm->course
);
355 if (SITEID
== $cm->course
) {
356 redirect($CFG->wwwroot
);
358 redirect("view.php?id=$cm->course#section-$sectionreturn");
362 } else if (!empty($show) and confirm_sesskey()) {
364 if (! $cm = get_record("course_modules", "id", $show)) {
365 error("This course module doesn't exist");
368 require_login($cm->course
); // needed to setup proper $COURSE
369 $context = get_context_instance(CONTEXT_COURSE
, $cm->course
);
370 require_capability('moodle/course:activityvisibility', $context);
372 if (! $section = get_record("course_sections", "id", $cm->section
)) {
373 error("This module doesn't exist");
376 if (! $module = get_record("modules", "id", $cm->module
)) {
377 error("This module doesn't exist");
380 if ($module->visible
and ($section->visible
or (SITEID
== $cm->course
))) {
381 set_coursemodule_visible($cm->id
, 1);
382 rebuild_course_cache($cm->course
);
385 if (SITEID
== $cm->course
) {
386 redirect($CFG->wwwroot
);
388 redirect("view.php?id=$cm->course#section-$sectionreturn");
392 } else if ($groupmode > -1 and confirm_sesskey()) {
394 $id = required_param( 'id', PARAM_INT
);
396 if (! $cm = get_record("course_modules", "id", $id)) {
397 error("This course module doesn't exist");
400 require_login($cm->course
); // needed to setup proper $COURSE
401 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
402 require_capability('moodle/course:manageactivities', $context);
404 set_coursemodule_groupmode($cm->id
, $groupmode);
406 rebuild_course_cache($cm->course
);
408 if (SITEID
== $cm->course
) {
409 redirect($CFG->wwwroot
);
411 redirect("view.php?id=$cm->course#section-$sectionreturn");
415 } else if (!empty($copy) and confirm_sesskey()) { // value = course module
417 if (! $cm = get_record("course_modules", "id", $copy)) {
418 error("This course module doesn't exist");
421 require_login($cm->course
); // needed to setup proper $COURSE
422 $context = get_context_instance(CONTEXT_COURSE
, $cm->course
);
423 require_capability('moodle/course:manageactivities', $context);
425 if (! $section = get_record("course_sections", "id", $cm->section
)) {
426 error("This module doesn't exist");
429 if (! $module = get_record("modules", "id", $cm->module
)) {
430 error("This module doesn't exist");
433 if (! $instance = get_record($module->name
, "id", $cm->instance
)) {
434 error("Could not find the instance of this module");
437 $USER->activitycopy
= $copy;
438 $USER->activitycopycourse
= $cm->course
;
439 $USER->activitycopyname
= $instance->name
;
441 redirect("view.php?id=$cm->course#section-$sectionreturn");
443 } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
445 $courseid = $USER->activitycopycourse
;
447 unset($USER->activitycopy
);
448 unset($USER->activitycopycourse
);
449 unset($USER->activitycopyname
);
451 redirect("view.php?id=$courseid#section-$sectionreturn");
453 } else if (!empty($delete) and confirm_sesskey()) { // value = course module
455 if (! $cm = get_record("course_modules", "id", $delete)) {
456 error("This course module doesn't exist");
459 if (! $course = get_record("course", "id", $cm->course
)) {
460 error("This course doesn't exist");
463 require_login($cm->course
); // needed to setup proper $COURSE
464 $context = get_context_instance(CONTEXT_COURSE
, $cm->course
);
465 require_capability('moodle/course:manageactivities', $context);
467 if (! $module = get_record("modules", "id", $cm->module
)) {
468 error("This module doesn't exist");
471 if (! $instance = get_record($module->name
, "id", $cm->instance
)) {
472 // Delete this module from the course right away
473 if (! delete_mod_from_section($cm->id
, $cm->section
)) {
474 notify("Could not delete the $module->name from that section");
476 if (! delete_course_module($cm->id
)) {
477 notify("Could not delete the $module->name (coursemodule)");
479 error("The required instance of this module didn't exist. Module deleted.",
480 "$CFG->wwwroot/course/view.php?id=$course->id");
483 $fullmodulename = get_string("modulename", $module->name
);
485 $form->coursemodule
= $cm->id
;
486 $form->section
= $cm->section
;
487 $form->course
= $cm->course
;
488 $form->instance
= $cm->instance
;
489 $form->modulename
= $module->name
;
490 $form->fullmodulename
= $fullmodulename;
491 $form->instancename
= $instance->name
;
492 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
494 $strdeletecheck = get_string('deletecheck', '', $form->fullmodulename
);
495 $strdeletecheckfull = get_string('deletecheckfull', '', "$form->fullmodulename '$form->instancename'");
497 $CFG->pagepath
= 'mod/'.$module->name
.'/delete';
499 print_header_simple($strdeletecheck, '', build_navigation(array(array('name'=>$strdeletecheck,'link'=>'','type'=>'misc'))));
501 print_simple_box_start('center', '60%', '#FFAAAA', 20, 'noticebox');
502 print_heading($strdeletecheckfull);
503 include_once('mod_delete.html');
504 print_simple_box_end();
505 print_footer($course);
510 } else if (!empty($update) and confirm_sesskey()) { // value = course module
512 if (! $cm = get_record("course_modules", "id", $update)) {
513 error("This course module doesn't exist");
516 if (! $course = get_record("course", "id", $cm->course
)) {
517 error("This course doesn't exist");
520 require_login($course->id
); // needed to setup proper $COURSE
521 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
522 require_capability('moodle/course:manageactivities', $context);
524 if (! $module = get_record("modules", "id", $cm->module
)) {
525 error("This module doesn't exist");
528 if (! $form = get_record($module->name
, "id", $cm->instance
)) {
529 error("The required instance of this module doesn't exist");
532 if (! $cw = get_record("course_sections", "id", $cm->section
)) {
533 error("This course section doesn't exist");
536 if (isset($return)) {
537 $SESSION->returnpage
= "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
540 $form->coursemodule
= $cm->id
;
541 $form->section
= $cm->section
; // The section ID
542 $form->course
= $course->id
;
543 $form->module
= $module->id
;
544 $form->modulename
= $module->name
;
545 $form->instance
= $cm->instance
;
546 $form->mode
= "update";
547 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
549 $sectionname = get_section_name($course->format
);
550 $fullmodulename = get_string("modulename", $module->name
);
552 if ($form->section
&& $course->format
!= 'site') {
553 $heading->what
= $fullmodulename;
554 $heading->in
= "$sectionname $cw->section";
555 $pageheading = get_string("updatingain", "moodle", $heading);
557 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
559 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">".format_string($form->name
,true)."</a> ->";
561 if ($module->name
== 'resource') {
562 $CFG->pagepath
= 'mod/'.$module->name
.'/'.$form->type
;
564 $CFG->pagepath
= 'mod/'.$module->name
.'/mod';
567 } else if (!empty($duplicate) and confirm_sesskey()) { // value = course module
570 if (! $cm = get_record("course_modules", "id", $duplicate)) {
571 error("This course module doesn't exist");
574 if (! $course = get_record("course", "id", $cm->course
)) {
575 error("This course doesn't exist");
578 require_login($course->id
); // needed to setup proper $COURSE
579 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
580 require_capability('moodle/course:manageactivities', $context);
582 if (! $module = get_record("modules", "id", $cm->module
)) {
583 error("This module doesn't exist");
586 if (! $form = get_record($module->name
, "id", $cm->instance
)) {
587 error("The required instance of this module doesn't exist");
590 if (! $cw = get_record("course_sections", "id", $cm->section
)) {
591 error("This course section doesn't exist");
594 if (isset($return)) {
595 $SESSION->returnpage
= "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
598 $section = get_field('course_sections', 'section', 'id', $cm->section
);
600 $form->coursemodule
= $cm->id
;
601 $form->section
= $section; // The section ID
602 $form->course
= $course->id
;
603 $form->module
= $module->id
;
604 $form->modulename
= $module->name
;
605 $form->instance
= $cm->instance
;
607 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
609 $sectionname = get_string("name$course->format");
610 $fullmodulename = get_string("modulename", $module->name
);
612 if ($form->section
) {
613 $heading->what
= $fullmodulename;
614 $heading->in
= "$sectionname $cw->section";
615 $pageheading = get_string("duplicatingain", "moodle", $heading);
617 $pageheading = get_string("duplicatinga", "moodle", $fullmodulename);
619 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">$form->name</a> ->";
621 $CFG->pagepath
= 'mod/'.$module->name
.'/mod';
624 } else if (!empty($add) and confirm_sesskey()) {
626 $id = required_param('id',PARAM_INT
);
627 $section = required_param('section',PARAM_INT
);
629 if (! $course = get_record("course", "id", $id)) {
630 error("This course doesn't exist");
633 if (! $module = get_record("modules", "name", $add)) {
634 error("This module type doesn't exist");
637 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
638 require_capability('moodle/course:manageactivities', $context);
640 if (!course_allowed_module($course,$module->id
)) {
641 error("This module has been disabled for this particular course");
644 require_login($course->id
); // needed to setup proper $COURSE
646 $form->section
= $section; // The section number itself
647 $form->course
= $course->id
;
648 $form->module
= $module->id
;
649 $form->modulename
= $module->name
;
650 $form->instance
= "";
651 $form->coursemodule
= "";
653 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
658 $sectionname = get_string("name$course->format");
659 $fullmodulename = get_string("modulename", $module->name
);
661 if ($form->section
&& $course->format
!= 'site') {
662 $heading->what
= $fullmodulename;
663 $heading->to
= "$sectionname $form->section";
664 $pageheading = get_string("addinganewto", "moodle", $heading);
666 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
669 $CFG->pagepath
= 'mod/'.$module->name
;
671 $CFG->pagepath
.= '/' . $type;
674 $CFG->pagepath
.= '/mod';
678 error("No action was specified");
681 require_login($course->id
); // needed to setup proper $COURSE
682 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
683 require_capability('moodle/course:manageactivities', $context);
685 $streditinga = get_string("editinga", "moodle", $fullmodulename);
686 $strmodulenameplural = get_string("modulenameplural", $module->name
);
688 if ($module->name
== "label") {
689 $focuscursor = "form.content";
691 $focuscursor = "form.name";
695 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
696 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'action');
697 $navigation = build_navigation($navlinks);
699 print_header_simple($streditinga, '', $navigation, $focuscursor, "", false);
701 if (!empty($cm->id
)) {
702 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
703 $currenttab = 'update';
704 $overridableroles = get_overridable_roles($context);
705 $assignableroles = get_assignable_roles($context);
706 include_once($CFG->dirroot
.'/'.$CFG->admin
.'/roles/tabs.php');
709 unset($SESSION->modform
); // Clear any old ones that may be hanging around.
711 $modform = "../mod/$module->name/mod.html";
713 if (file_exists($modform)) {
715 if ($usehtmleditor = can_use_html_editor()) {
716 $defaultformat = FORMAT_HTML
;
719 $defaultformat = FORMAT_MOODLE
;
722 $icon = '<img class="icon" src="'.$CFG->modpixpath
.'/'.$module->name
.'/icon.gif" alt="'.get_string('modulename',$module->name
).'"/>';
724 print_heading_with_help($pageheading, "mods", $module->name
, $icon);
725 print_simple_box_start('center', '', '', 5, 'generalbox', $module->name
);
726 include_once($modform);
727 print_simple_box_end();
729 if ($usehtmleditor and empty($nohtmleditorneeded)) {
730 use_html_editor($editorfields);
734 notice("This module cannot be added to this course yet! (No file found at: $modform)", "$CFG->wwwroot/course/view.php?id=$course->id");
737 print_footer($course);