Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / course / mod.php
blob828da8efcbbc5746b585d39904777284855e3c25
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 (! $deleteinstancefunction($mod->instance)) {
218 notify("Could not delete the $mod->modulename (instance)");
220 if (! delete_course_module($mod->coursemodule)) {
221 notify("Could not delete the $mod->modulename (coursemodule)");
223 if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) {
224 notify("Could not delete the $mod->modulename from that section");
227 unset($SESSION->returnpage);
229 add_to_log($course->id, "course", "delete mod",
230 "view.php?id=$mod->course",
231 "$mod->modulename $mod->instance", $mod->coursemodule);
232 break;
233 default:
234 error("No mode defined");
238 rebuild_course_cache($course->id);
240 if (!empty($SESSION->returnpage)) {
241 $return = $SESSION->returnpage;
242 unset($SESSION->returnpage);
243 redirect($return);
244 } else {
245 redirect("view.php?id=$course->id#section-$sectionreturn");
247 exit;
250 if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
252 if (! $cm = get_record("course_modules", "id", $USER->activitycopy)) {
253 error("The copied course module doesn't exist!");
256 if (!empty($movetosection)) {
257 if (! $section = get_record("course_sections", "id", $movetosection)) {
258 error("This section doesn't exist");
260 $beforecm = NULL;
262 } else { // normal moveto
263 if (! $beforecm = get_record("course_modules", "id", $moveto)) {
264 error("The destination course module doesn't exist");
266 if (! $section = get_record("course_sections", "id", $beforecm->section)) {
267 error("This section doesn't exist");
271 require_login($section->course); // needed to setup proper $COURSE
272 $context = get_context_instance(CONTEXT_COURSE, $section->course);
273 require_capability('moodle/course:manageactivities', $context);
275 if (!ismoving($section->course)) {
276 error("You need to copy something first!");
279 moveto_module($cm, $section, $beforecm);
281 unset($USER->activitycopy);
282 unset($USER->activitycopycourse);
283 unset($USER->activitycopyname);
285 rebuild_course_cache($section->course);
287 if (SITEID == $section->course) {
288 redirect($CFG->wwwroot);
289 } else {
290 redirect("view.php?id=$section->course#section-$sectionreturn");
293 } else if (!empty($indent) and confirm_sesskey()) {
295 $id = required_param('id',PARAM_INT);
297 if (! $cm = get_record("course_modules", "id", $id)) {
298 error("This course module doesn't exist");
301 require_login($cm->course); // needed to setup proper $COURSE
302 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
303 require_capability('moodle/course:manageactivities', $context);
305 $cm->indent += $indent;
307 if ($cm->indent < 0) {
308 $cm->indent = 0;
311 if (!set_field("course_modules", "indent", $cm->indent, "id", $cm->id)) {
312 error("Could not update the indent level on that course module");
315 if (SITEID == $cm->course) {
316 redirect($CFG->wwwroot);
317 } else {
318 redirect("view.php?id=$cm->course#section-$sectionreturn");
320 exit;
322 } else if (!empty($hide) and confirm_sesskey()) {
324 if (! $cm = get_record("course_modules", "id", $hide)) {
325 error("This course module doesn't exist");
328 require_login($cm->course); // needed to setup proper $COURSE
329 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
330 require_capability('moodle/course:activityvisibility', $context);
332 set_coursemodule_visible($cm->id, 0);
334 rebuild_course_cache($cm->course);
336 if (SITEID == $cm->course) {
337 redirect($CFG->wwwroot);
338 } else {
339 redirect("view.php?id=$cm->course#section-$sectionreturn");
341 exit;
343 } else if (!empty($show) and confirm_sesskey()) {
345 if (! $cm = get_record("course_modules", "id", $show)) {
346 error("This course module doesn't exist");
349 require_login($cm->course); // needed to setup proper $COURSE
350 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
351 require_capability('moodle/course:activityvisibility', $context);
353 if (! $section = get_record("course_sections", "id", $cm->section)) {
354 error("This module doesn't exist");
357 if (! $module = get_record("modules", "id", $cm->module)) {
358 error("This module doesn't exist");
361 if ($module->visible and ($section->visible or (SITEID == $cm->course))) {
362 set_coursemodule_visible($cm->id, 1);
363 rebuild_course_cache($cm->course);
366 if (SITEID == $cm->course) {
367 redirect($CFG->wwwroot);
368 } else {
369 redirect("view.php?id=$cm->course#section-$sectionreturn");
371 exit;
373 } else if ($groupmode > -1 and confirm_sesskey()) {
375 $id = required_param( 'id', PARAM_INT );
377 if (! $cm = get_record("course_modules", "id", $id)) {
378 error("This course module doesn't exist");
381 require_login($cm->course); // needed to setup proper $COURSE
382 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
383 require_capability('moodle/course:manageactivities', $context);
385 set_coursemodule_groupmode($cm->id, $groupmode);
387 rebuild_course_cache($cm->course);
389 if (SITEID == $cm->course) {
390 redirect($CFG->wwwroot);
391 } else {
392 redirect("view.php?id=$cm->course#section-$sectionreturn");
394 exit;
396 } else if (!empty($copy) and confirm_sesskey()) { // value = course module
398 if (! $cm = get_record("course_modules", "id", $copy)) {
399 error("This course module doesn't exist");
402 require_login($cm->course); // needed to setup proper $COURSE
403 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
404 require_capability('moodle/course:manageactivities', $context);
406 if (! $section = get_record("course_sections", "id", $cm->section)) {
407 error("This module doesn't exist");
410 if (! $module = get_record("modules", "id", $cm->module)) {
411 error("This module doesn't exist");
414 if (! $instance = get_record($module->name, "id", $cm->instance)) {
415 error("Could not find the instance of this module");
418 $USER->activitycopy = $copy;
419 $USER->activitycopycourse = $cm->course;
420 $USER->activitycopyname = $instance->name;
422 redirect("view.php?id=$cm->course#section-$sectionreturn");
424 } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
426 $courseid = $USER->activitycopycourse;
428 unset($USER->activitycopy);
429 unset($USER->activitycopycourse);
430 unset($USER->activitycopyname);
432 redirect("view.php?id=$courseid#section-$sectionreturn");
434 } else if (!empty($delete) and confirm_sesskey()) { // value = course module
436 if (! $cm = get_record("course_modules", "id", $delete)) {
437 error("This course module doesn't exist");
440 if (! $course = get_record("course", "id", $cm->course)) {
441 error("This course doesn't exist");
444 require_login($cm->course); // needed to setup proper $COURSE
445 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
446 require_capability('moodle/course:manageactivities', $context);
448 if (! $module = get_record("modules", "id", $cm->module)) {
449 error("This module doesn't exist");
452 if (! $instance = get_record($module->name, "id", $cm->instance)) {
453 // Delete this module from the course right away
454 if (! delete_mod_from_section($cm->id, $cm->section)) {
455 notify("Could not delete the $module->name from that section");
457 if (! delete_course_module($cm->id)) {
458 notify("Could not delete the $module->name (coursemodule)");
460 error("The required instance of this module didn't exist. Module deleted.",
461 "$CFG->wwwroot/course/view.php?id=$course->id");
464 $fullmodulename = get_string("modulename", $module->name);
466 $form->coursemodule = $cm->id;
467 $form->section = $cm->section;
468 $form->course = $cm->course;
469 $form->instance = $cm->instance;
470 $form->modulename = $module->name;
471 $form->fullmodulename = $fullmodulename;
472 $form->instancename = $instance->name;
473 $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
475 $strdeletecheck = get_string('deletecheck', '', $form->fullmodulename);
476 $strdeletecheckfull = get_string('deletecheckfull', '', "$form->fullmodulename '$form->instancename'");
478 $CFG->pagepath = 'mod/'.$module->name.'/delete';
480 print_header_simple($strdeletecheck, '', $strdeletecheck);
482 print_simple_box_start('center', '60%', '#FFAAAA', 20, 'noticebox');
483 print_heading($strdeletecheckfull);
484 include_once('mod_delete.html');
485 print_simple_box_end();
486 print_footer($course);
488 exit;
491 } else if (!empty($update) and confirm_sesskey()) { // value = course module
493 if (! $cm = get_record("course_modules", "id", $update)) {
494 error("This course module doesn't exist");
497 if (! $course = get_record("course", "id", $cm->course)) {
498 error("This course doesn't exist");
501 require_login($course->id); // needed to setup proper $COURSE
502 $context = get_context_instance(CONTEXT_COURSE, $course->id);
503 require_capability('moodle/course:manageactivities', $context);
505 if (! $module = get_record("modules", "id", $cm->module)) {
506 error("This module doesn't exist");
509 if (! $form = get_record($module->name, "id", $cm->instance)) {
510 error("The required instance of this module doesn't exist");
513 if (! $cw = get_record("course_sections", "id", $cm->section)) {
514 error("This course section doesn't exist");
517 if (isset($return)) {
518 $SESSION->returnpage = "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
521 $form->coursemodule = $cm->id;
522 $form->section = $cm->section; // The section ID
523 $form->course = $course->id;
524 $form->module = $module->id;
525 $form->modulename = $module->name;
526 $form->instance = $cm->instance;
527 $form->mode = "update";
528 $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
530 $sectionname = get_section_name($course->format);
531 $fullmodulename = get_string("modulename", $module->name);
533 if ($form->section && $course->format != 'site') {
534 $heading->what = $fullmodulename;
535 $heading->in = "$sectionname $cw->section";
536 $pageheading = get_string("updatingain", "moodle", $heading);
537 } else {
538 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
540 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">".format_string($form->name,true)."</a> ->";
542 if ($module->name == 'resource') {
543 $CFG->pagepath = 'mod/'.$module->name.'/'.$form->type;
544 } else {
545 $CFG->pagepath = 'mod/'.$module->name.'/mod';
548 } else if (!empty($duplicate) and confirm_sesskey()) { // value = course module
551 if (! $cm = get_record("course_modules", "id", $duplicate)) {
552 error("This course module doesn't exist");
555 if (! $course = get_record("course", "id", $cm->course)) {
556 error("This course doesn't exist");
559 require_login($course->id); // needed to setup proper $COURSE
560 $context = get_context_instance(CONTEXT_COURSE, $course->id);
561 require_capability('moodle/course:manageactivities', $context);
563 if (! $module = get_record("modules", "id", $cm->module)) {
564 error("This module doesn't exist");
567 if (! $form = get_record($module->name, "id", $cm->instance)) {
568 error("The required instance of this module doesn't exist");
571 if (! $cw = get_record("course_sections", "id", $cm->section)) {
572 error("This course section doesn't exist");
575 if (isset($return)) {
576 $SESSION->returnpage = "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id";
579 $section = get_field('course_sections', 'section', 'id', $cm->section);
581 $form->coursemodule = $cm->id;
582 $form->section = $section; // The section ID
583 $form->course = $course->id;
584 $form->module = $module->id;
585 $form->modulename = $module->name;
586 $form->instance = $cm->instance;
587 $form->mode = "add";
588 $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
590 $sectionname = get_string("name$course->format");
591 $fullmodulename = get_string("modulename", $module->name);
593 if ($form->section) {
594 $heading->what = $fullmodulename;
595 $heading->in = "$sectionname $cw->section";
596 $pageheading = get_string("duplicatingain", "moodle", $heading);
597 } else {
598 $pageheading = get_string("duplicatinga", "moodle", $fullmodulename);
600 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">$form->name</a> ->";
602 $CFG->pagepath = 'mod/'.$module->name.'/mod';
605 } else if (!empty($add) and confirm_sesskey()) {
607 $id = required_param('id',PARAM_INT);
608 $section = required_param('section',PARAM_INT);
610 if (! $course = get_record("course", "id", $id)) {
611 error("This course doesn't exist");
614 if (! $module = get_record("modules", "name", $add)) {
615 error("This module type doesn't exist");
618 $context = get_context_instance(CONTEXT_COURSE, $course->id);
619 require_capability('moodle/course:manageactivities', $context);
621 if (!course_allowed_module($course,$module->id)) {
622 error("This module has been disabled for this particular course");
625 require_login($course->id); // needed to setup proper $COURSE
627 $form->section = $section; // The section number itself
628 $form->course = $course->id;
629 $form->module = $module->id;
630 $form->modulename = $module->name;
631 $form->instance = "";
632 $form->coursemodule = "";
633 $form->mode = "add";
634 $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
635 if (!empty($type)) {
636 $form->type = $type;
639 $sectionname = get_string("name$course->format");
640 $fullmodulename = get_string("modulename", $module->name);
642 if ($form->section && $course->format != 'site') {
643 $heading->what = $fullmodulename;
644 $heading->to = "$sectionname $form->section";
645 $pageheading = get_string("addinganewto", "moodle", $heading);
646 } else {
647 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
650 $CFG->pagepath = 'mod/'.$module->name;
651 if (!empty($type)) {
652 $CFG->pagepath .= '/' . $type;
654 else {
655 $CFG->pagepath .= '/mod';
658 } else {
659 error("No action was specfied");
662 require_login($course->id); // needed to setup proper $COURSE
663 $context = get_context_instance(CONTEXT_COURSE, $course->id);
664 require_capability('moodle/course:manageactivities', $context);
666 $streditinga = get_string("editinga", "moodle", $fullmodulename);
667 $strmodulenameplural = get_string("modulenameplural", $module->name);
669 if ($module->name == "label") {
670 $focuscursor = "form.content";
671 } else {
672 $focuscursor = "form.name";
675 $navlinks = array();
676 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
677 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'action');
678 $navigation = build_navigation($navlinks);
680 print_header_simple($streditinga, '', $navigation, $focuscursor, "", false);
682 if (!empty($cm->id)) {
683 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
684 $currenttab = 'update';
685 include_once($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
688 unset($SESSION->modform); // Clear any old ones that may be hanging around.
690 $modform = "../mod/$module->name/mod.html";
692 if (file_exists($modform)) {
694 if ($usehtmleditor = can_use_html_editor()) {
695 $defaultformat = FORMAT_HTML;
696 $editorfields = '';
697 } else {
698 $defaultformat = FORMAT_MOODLE;
701 $icon = '<img class="icon" src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt="'.get_string('modulename',$module->name).'"/>';
703 print_heading_with_help($pageheading, "mods", $module->name, $icon);
704 print_simple_box_start('center', '', '', 5, 'generalbox', $module->name);
705 include_once($modform);
706 print_simple_box_end();
708 if ($usehtmleditor and empty($nohtmleditorneeded)) {
709 use_html_editor($editorfields);
712 } else {
713 notice("This module cannot be added to this course yet! (No file found at: $modform)", "$CFG->wwwroot/course/view.php?id=$course->id");
716 print_footer($course);