MDL-10723 Removed all uses of print_navigation() throughout moodle, the function...
[moodle-pu.git] / course / mod.php
bloba6029d9114efdae705090116fa737cbd6cfd41e2
1 <?php // $Id$
3 // Moves, adds, updates, duplicates or deletes modules in a course
5 require("../config.php");
6 require_once("lib.php");
8 require_login();
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);
30 } else {
31 $mod = (object)$_POST;
34 if ($cancel) {
35 if (!empty($SESSION->returnpage)) {
36 $return = $SESSION->returnpage;
37 unset($SESSION->returnpage);
38 redirect($return);
39 } else {
40 redirect("view.php?id=$mod->course#section-$sectionreturn");
44 //check if we are adding / editing a module that has new forms using formslib
45 if (!empty($add)){
46 $modname=$add;
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&section=$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
60 md.id = cm.module")){
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
69 //carry on
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");
77 $mod->instance = '';
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);
101 } else {
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) {
110 case "update":
112 if (isset($mod->name)) {
113 if (trim($mod->name) == '') {
114 unset($mod->name);
118 $return = $updateinstancefunction($mod);
119 if (!$return) {
120 if (file_exists($moderr)) {
121 $form = $mod;
122 include_once($moderr);
123 die;
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->redirect)) {
140 $SESSION->returnpage = $mod->redirecturl;
141 } else {
142 $SESSION->returnpage = "$CFG->wwwroot/mod/$mod->modulename/view.php?id=$mod->coursemodule";
145 add_to_log($course->id, "course", "update mod",
146 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
147 "$mod->modulename $mod->instance");
148 add_to_log($course->id, $mod->modulename, "update",
149 "view.php?id=$mod->coursemodule",
150 "$mod->instance", $mod->coursemodule);
151 break;
153 case "add":
155 if (!course_allowed_module($course,$mod->modulename)) {
156 error("This module ($mod->modulename) has been disabled for this particular course");
159 if (!isset($mod->name) || trim($mod->name) == '') {
160 $mod->name = get_string("modulename", $mod->modulename);
163 $return = $addinstancefunction($mod);
164 if (!$return) {
165 if (file_exists($moderr)) {
166 $form = $mod;
167 include_once($moderr);
168 die;
170 error("Could not add a new instance of $mod->modulename", "view.php?id=$course->id");
172 if (is_string($return)) {
173 error($return, "view.php?id=$course->id");
176 if (!isset($mod->groupmode)) { // to deal with pre-1.5 modules
177 $mod->groupmode = $course->groupmode; /// Default groupmode the same as course
180 $mod->instance = $return;
182 // course_modules and course_sections each contain a reference
183 // to each other, so we have to update one of them twice.
185 if (! $mod->coursemodule = add_course_module($mod) ) {
186 error("Could not add a new course module");
188 if (! $sectionid = add_mod_to_section($mod) ) {
189 error("Could not add the new course module to that section");
192 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
193 error("Could not update the course module with the correct section");
196 if (!isset($mod->visible)) { // We get the section's visible field status
197 $mod->visible = get_field("course_sections","visible","id",$sectionid);
199 // make sure visibility is set correctly (in particular in calendar)
200 set_coursemodule_visible($mod->coursemodule, $mod->visible);
202 if (isset($mod->redirect)) {
203 $SESSION->returnpage = $mod->redirecturl;
204 } else {
205 $SESSION->returnpage = "$CFG->wwwroot/mod/$mod->modulename/view.php?id=$mod->coursemodule";
208 add_to_log($course->id, "course", "add mod",
209 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
210 "$mod->modulename $mod->instance");
211 add_to_log($course->id, $mod->modulename, "add",
212 "view.php?id=$mod->coursemodule",
213 "$mod->instance", $mod->coursemodule);
214 break;
216 case "delete":
217 if ($cm and $cw = get_record("course_sections", "id", $cm->section)) {
218 $sectionreturn = $cw->section;
221 if (! $deleteinstancefunction($mod->instance)) {
222 notify("Could not delete the $mod->modulename (instance)");
224 if (! delete_course_module($mod->coursemodule)) {
225 notify("Could not delete the $mod->modulename (coursemodule)");
227 if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
228 notify("Could not delete the $mod->modulename from that section");
231 unset($SESSION->returnpage);
233 add_to_log($course->id, "course", "delete mod",
234 "view.php?id=$mod->course",
235 "$mod->modulename $mod->instance", $mod->coursemodule);
236 break;
237 default:
238 error("No mode defined");
242 rebuild_course_cache($course->id);
244 if (!empty($SESSION->returnpage)) {
245 $return = $SESSION->returnpage;
246 unset($SESSION->returnpage);
247 redirect($return);
248 } else {
249 redirect("view.php?id=$course->id#section-$sectionreturn");
251 exit;
254 if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
256 if (! $cm = get_record("course_modules", "id", $USER->activitycopy)) {
257 error("The copied course module doesn't exist!");
260 if (!empty($movetosection)) {
261 if (! $section = get_record("course_sections", "id", $movetosection)) {
262 error("This section doesn't exist");
264 $beforecm = NULL;
266 } else { // normal moveto
267 if (! $beforecm = get_record("course_modules", "id", $moveto)) {
268 error("The destination course module doesn't exist");
270 if (! $section = get_record("course_sections", "id", $beforecm->section)) {
271 error("This section doesn't exist");
275 require_login($section->course); // needed to setup proper $COURSE
276 $context = get_context_instance(CONTEXT_COURSE, $section->course);
277 require_capability('moodle/course:manageactivities', $context);
279 if (!ismoving($section->course)) {
280 error("You need to copy something first!");
283 moveto_module($cm, $section, $beforecm);
285 unset($USER->activitycopy);
286 unset($USER->activitycopycourse);
287 unset($USER->activitycopyname);
289 rebuild_course_cache($section->course);
291 if (SITEID == $section->course) {
292 redirect($CFG->wwwroot);
293 } else {
294 redirect("view.php?id=$section->course#section-$sectionreturn");
297 } else if (!empty($indent) and confirm_sesskey()) {
299 $id = required_param('id',PARAM_INT);
301 if (! $cm = get_record("course_modules", "id", $id)) {
302 error("This course module doesn't exist");
305 require_login($cm->course); // needed to setup proper $COURSE
306 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
307 require_capability('moodle/course:manageactivities', $context);
309 $cm->indent += $indent;
311 if ($cm->indent < 0) {
312 $cm->indent = 0;
315 if (!set_field("course_modules", "indent", $cm->indent, "id", $cm->id)) {
316 error("Could not update the indent level on that course module");
319 if (SITEID == $cm->course) {
320 redirect($CFG->wwwroot);
321 } else {
322 redirect("view.php?id=$cm->course#section-$sectionreturn");
324 exit;
326 } else if (!empty($hide) and confirm_sesskey()) {
328 if (! $cm = get_record("course_modules", "id", $hide)) {
329 error("This course module doesn't exist");
332 require_login($cm->course); // needed to setup proper $COURSE
333 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
334 require_capability('moodle/course:activityvisibility', $context);
336 set_coursemodule_visible($cm->id, 0);
338 rebuild_course_cache($cm->course);
340 if (SITEID == $cm->course) {
341 redirect($CFG->wwwroot);
342 } else {
343 redirect("view.php?id=$cm->course#section-$sectionreturn");
345 exit;
347 } else if (!empty($show) and confirm_sesskey()) {
349 if (! $cm = get_record("course_modules", "id", $show)) {
350 error("This course module doesn't exist");
353 require_login($cm->course); // needed to setup proper $COURSE
354 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
355 require_capability('moodle/course:activityvisibility', $context);
357 if (! $section = get_record("course_sections", "id", $cm->section)) {
358 error("This module doesn't exist");
361 if (! $module = get_record("modules", "id", $cm->module)) {
362 error("This module doesn't exist");
365 if ($module->visible and ($section->visible or (SITEID == $cm->course))) {
366 set_coursemodule_visible($cm->id, 1);
367 rebuild_course_cache($cm->course);
370 if (SITEID == $cm->course) {
371 redirect($CFG->wwwroot);
372 } else {
373 redirect("view.php?id=$cm->course#section-$sectionreturn");
375 exit;
377 } else if ($groupmode > -1 and confirm_sesskey()) {
379 $id = required_param( 'id', PARAM_INT );
381 if (! $cm = get_record("course_modules", "id", $id)) {
382 error("This course module doesn't exist");
385 require_login($cm->course); // needed to setup proper $COURSE
386 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
387 require_capability('moodle/course:manageactivities', $context);
389 set_coursemodule_groupmode($cm->id, $groupmode);
391 rebuild_course_cache($cm->course);
393 if (SITEID == $cm->course) {
394 redirect($CFG->wwwroot);
395 } else {
396 redirect("view.php?id=$cm->course#section-$sectionreturn");
398 exit;
400 } else if (!empty($copy) and confirm_sesskey()) { // value = course module
402 if (! $cm = get_record("course_modules", "id", $copy)) {
403 error("This course module doesn't exist");
406 require_login($cm->course); // needed to setup proper $COURSE
407 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
408 require_capability('moodle/course:manageactivities', $context);
410 if (! $section = get_record("course_sections", "id", $cm->section)) {
411 error("This module doesn't exist");
414 if (! $module = get_record("modules", "id", $cm->module)) {
415 error("This module doesn't exist");
418 if (! $instance = get_record($module->name, "id", $cm->instance)) {
419 error("Could not find the instance of this module");
422 $USER->activitycopy = $copy;
423 $USER->activitycopycourse = $cm->course;
424 $USER->activitycopyname = $instance->name;
426 redirect("view.php?id=$cm->course#section-$sectionreturn");
428 } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
430 $courseid = $USER->activitycopycourse;
432 unset($USER->activitycopy);
433 unset($USER->activitycopycourse);
434 unset($USER->activitycopyname);
436 redirect("view.php?id=$courseid#section-$sectionreturn");
438 } else if (!empty($delete) and confirm_sesskey()) { // value = course module
440 if (! $cm = get_record("course_modules", "id", $delete)) {
441 error("This course module doesn't exist");
444 if (! $course = get_record("course", "id", $cm->course)) {
445 error("This course doesn't exist");
448 require_login($cm->course); // needed to setup proper $COURSE
449 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
450 require_capability('moodle/course:manageactivities', $context);
452 if (! $module = get_record("modules", "id", $cm->module)) {
453 error("This module doesn't exist");
456 if (! $instance = get_record($module->name, "id", $cm->instance)) {
457 // Delete this module from the course right away
458 if (! delete_mod_from_section($cm->id, $cm->section)) {
459 notify("Could not delete the $module->name from that section");
461 if (! delete_course_module($cm->id)) {
462 notify("Could not delete the $module->name (coursemodule)");
464 error("The required instance of this module didn't exist. Module deleted.",
465 "$CFG->wwwroot/course/view.php?id=$course->id");
468 $fullmodulename = get_string("modulename", $module->name);
470 $form->coursemodule = $cm->id;
471 $form->section = $cm->section;
472 $form->course = $cm->course;
473 $form->instance = $cm->instance;
474 $form->modulename = $module->name;
475 $form->fullmodulename = $fullmodulename;
476 $form->instancename = $instance->name;
477 $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
479 $strdeletecheck = get_string('deletecheck', '', $form->fullmodulename);
480 $strdeletecheckfull = get_string('deletecheckfull', '', "$form->fullmodulename '$form->instancename'");
482 $CFG->pagepath = 'mod/'.$module->name.'/delete';
484 print_header_simple($strdeletecheck, '', build_navigation(array(array('name'=>$strdeletecheck,'link'=>'','type'=>'misc'))));
486 print_simple_box_start('center', '60%', '#FFAAAA', 20, 'noticebox');
487 print_heading($strdeletecheckfull);
488 include_once('mod_delete.html');
489 print_simple_box_end();
490 print_footer($course);
492 exit;
495 } else if (!empty($update) and confirm_sesskey()) { // value = course module
497 if (! $cm = get_record("course_modules", "id", $update)) {
498 error("This course module doesn't exist");
501 if (! $course = get_record("course", "id", $cm->course)) {
502 error("This course doesn't exist");
505 require_login($course->id); // needed to setup proper $COURSE
506 $context = get_context_instance(CONTEXT_COURSE, $course->id);
507 require_capability('moodle/course:manageactivities', $context);
509 if (! $module = get_record("modules", "id", $cm->module)) {
510 error("This module doesn't exist");
513 if (! $form = get_record($module->name, "id", $cm->instance)) {
514 error("The required instance of this module doesn't exist");
517 if (! $cw = get_record("course_sections", "id", $cm->section)) {
518 error("This course section doesn't exist");
521 if (isset($return)) {
522 $SESSION->returnpage = "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
525 $form->coursemodule = $cm->id;
526 $form->section = $cm->section; // The section ID
527 $form->course = $course->id;
528 $form->module = $module->id;
529 $form->modulename = $module->name;
530 $form->instance = $cm->instance;
531 $form->mode = "update";
532 $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
534 $sectionname = get_section_name($course->format);
535 $fullmodulename = get_string("modulename", $module->name);
537 if ($form->section && $course->format != 'site') {
538 $heading->what = $fullmodulename;
539 $heading->in = "$sectionname $cw->section";
540 $pageheading = get_string("updatingain", "moodle", $heading);
541 } else {
542 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
544 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">".format_string($form->name,true)."</a> ->";
546 if ($module->name == 'resource') {
547 $CFG->pagepath = 'mod/'.$module->name.'/'.$form->type;
548 } else {
549 $CFG->pagepath = 'mod/'.$module->name.'/mod';
552 } else if (!empty($duplicate) and confirm_sesskey()) { // value = course module
555 if (! $cm = get_record("course_modules", "id", $duplicate)) {
556 error("This course module doesn't exist");
559 if (! $course = get_record("course", "id", $cm->course)) {
560 error("This course doesn't exist");
563 require_login($course->id); // needed to setup proper $COURSE
564 $context = get_context_instance(CONTEXT_COURSE, $course->id);
565 require_capability('moodle/course:manageactivities', $context);
567 if (! $module = get_record("modules", "id", $cm->module)) {
568 error("This module doesn't exist");
571 if (! $form = get_record($module->name, "id", $cm->instance)) {
572 error("The required instance of this module doesn't exist");
575 if (! $cw = get_record("course_sections", "id", $cm->section)) {
576 error("This course section doesn't exist");
579 if (isset($return)) {
580 $SESSION->returnpage = "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
583 $section = get_field('course_sections', 'section', 'id', $cm->section);
585 $form->coursemodule = $cm->id;
586 $form->section = $section; // The section ID
587 $form->course = $course->id;
588 $form->module = $module->id;
589 $form->modulename = $module->name;
590 $form->instance = $cm->instance;
591 $form->mode = "add";
592 $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
594 $sectionname = get_string("name$course->format");
595 $fullmodulename = get_string("modulename", $module->name);
597 if ($form->section) {
598 $heading->what = $fullmodulename;
599 $heading->in = "$sectionname $cw->section";
600 $pageheading = get_string("duplicatingain", "moodle", $heading);
601 } else {
602 $pageheading = get_string("duplicatinga", "moodle", $fullmodulename);
604 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">$form->name</a> ->";
606 $CFG->pagepath = 'mod/'.$module->name.'/mod';
609 } else if (!empty($add) and confirm_sesskey()) {
611 $id = required_param('id',PARAM_INT);
612 $section = required_param('section',PARAM_INT);
614 if (! $course = get_record("course", "id", $id)) {
615 error("This course doesn't exist");
618 if (! $module = get_record("modules", "name", $add)) {
619 error("This module type doesn't exist");
622 $context = get_context_instance(CONTEXT_COURSE, $course->id);
623 require_capability('moodle/course:manageactivities', $context);
625 if (!course_allowed_module($course,$module->id)) {
626 error("This module has been disabled for this particular course");
629 require_login($course->id); // needed to setup proper $COURSE
631 $form->section = $section; // The section number itself
632 $form->course = $course->id;
633 $form->module = $module->id;
634 $form->modulename = $module->name;
635 $form->instance = "";
636 $form->coursemodule = "";
637 $form->mode = "add";
638 $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
639 if (!empty($type)) {
640 $form->type = $type;
643 $sectionname = get_string("name$course->format");
644 $fullmodulename = get_string("modulename", $module->name);
646 if ($form->section && $course->format != 'site') {
647 $heading->what = $fullmodulename;
648 $heading->to = "$sectionname $form->section";
649 $pageheading = get_string("addinganewto", "moodle", $heading);
650 } else {
651 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
654 $CFG->pagepath = 'mod/'.$module->name;
655 if (!empty($type)) {
656 $CFG->pagepath .= '/' . $type;
658 else {
659 $CFG->pagepath .= '/mod';
662 } else {
663 error("No action was specfied");
666 require_login($course->id); // needed to setup proper $COURSE
667 $context = get_context_instance(CONTEXT_COURSE, $course->id);
668 require_capability('moodle/course:manageactivities', $context);
670 $streditinga = get_string("editinga", "moodle", $fullmodulename);
671 $strmodulenameplural = get_string("modulenameplural", $module->name);
673 if ($module->name == "label") {
674 $focuscursor = "form.content";
675 } else {
676 $focuscursor = "form.name";
679 $navlinks = array();
680 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
681 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'action');
682 $navigation = build_navigation($navlinks);
684 print_header_simple($streditinga, '', $navigation, $focuscursor, "", false);
686 if (!empty($cm->id)) {
687 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
688 $currenttab = 'update';
689 include_once($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
692 unset($SESSION->modform); // Clear any old ones that may be hanging around.
694 $modform = "../mod/$module->name/mod.html";
696 if (file_exists($modform)) {
698 if ($usehtmleditor = can_use_html_editor()) {
699 $defaultformat = FORMAT_HTML;
700 $editorfields = '';
701 } else {
702 $defaultformat = FORMAT_MOODLE;
705 $icon = '<img class="icon" src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt="'.get_string('modulename',$module->name).'"/>';
707 print_heading_with_help($pageheading, "mods", $module->name, $icon);
708 print_simple_box_start('center', '', '', 5, 'generalbox', $module->name);
709 include_once($modform);
710 print_simple_box_end();
712 if ($usehtmleditor and empty($nohtmleditorneeded)) {
713 use_html_editor($editorfields);
716 } else {
717 notice("This module cannot be added to this course yet! (No file found at: $modform)", "$CFG->wwwroot/course/view.php?id=$course->id");
720 print_footer($course);