3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Adds or updates modules in a course using new formslib
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../config.php");
27 require_once("lib.php");
28 require_once($CFG->libdir
.'/filelib.php');
29 require_once($CFG->libdir
.'/gradelib.php');
30 require_once($CFG->libdir
.'/completionlib.php');
31 require_once($CFG->libdir
.'/conditionlib.php');
32 require_once($CFG->libdir
.'/plagiarismlib.php');
34 $add = optional_param('add', '', PARAM_ALPHA
); // module name
35 $update = optional_param('update', 0, PARAM_INT
);
36 $return = optional_param('return', 0, PARAM_BOOL
); //return to course/view.php if false or mod/modname/view.php if true
37 $type = optional_param('type', '', PARAM_ALPHANUM
); //TODO: hopefully will be removed in 2.0
39 $url = new moodle_url('/course/modedit.php');
40 if (!empty($return)) {
41 $url->param('return', $return);
45 $section = required_param('section', PARAM_INT
);
46 $course = required_param('course', PARAM_INT
);
48 $url->param('add', $add);
49 $url->param('section', $section);
50 $url->param('course', $course);
53 $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST
);
54 $module = $DB->get_record('modules', array('name'=>$add), '*', MUST_EXIST
);
56 require_login($course);
57 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
58 require_capability('moodle/course:manageactivities', $context);
60 $cw = get_course_section($section, $course->id
);
62 if (!course_allowed_module($course, $module->id
)) {
63 print_error('moduledisable');
68 $data = new stdClass();
69 $data->section
= $section; // The section number itself - relative!!! (section column in course_sections)
70 $data->visible
= $cw->visible
;
71 $data->course
= $course->id
;
72 $data->module
= $module->id
;
73 $data->modulename
= $module->name
;
74 $data->groupmode
= $course->groupmode
;
75 $data->groupingid
= $course->defaultgroupingid
;
76 $data->groupmembersonly
= 0;
79 $data->coursemodule
= '';
81 $data->return = 0; //must be false if this is an add, go back to course view on cancel
83 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
84 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
85 file_prepare_draft_area($draftid_editor, null, null, null, null);
86 $data->introeditor
= array('text'=>'', 'format'=>FORMAT_HTML
, 'itemid'=>$draftid_editor); // TODO: add better default
89 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
93 $sectionname = get_section_name($course, $cw);
94 $fullmodulename = get_string('modulename', $module->name
);
96 if ($data->section
&& $course->format
!= 'site') {
97 $heading = new stdClass();
98 $heading->what
= $fullmodulename;
99 $heading->to
= $sectionname;
100 $pageheading = get_string('addinganewto', 'moodle', $heading);
102 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
105 } else if (!empty($update)) {
107 $url->param('update', $update);
108 $PAGE->set_url($url);
110 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST
);
111 $course = $DB->get_record('course', array('id'=>$cm->course
), '*', MUST_EXIST
);
113 require_login($course, false, $cm); // needed to setup proper $COURSE
114 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
115 require_capability('moodle/course:manageactivities', $context);
117 $module = $DB->get_record('modules', array('id'=>$cm->module
), '*', MUST_EXIST
);
118 $data = $data = $DB->get_record($module->name
, array('id'=>$cm->instance
), '*', MUST_EXIST
);
119 $cw = $DB->get_record('course_sections', array('id'=>$cm->section
), '*', MUST_EXIST
);
121 $data->coursemodule
= $cm->id
;
122 $data->section
= $cw->section
; // The section number itself - relative!!! (section column in course_sections)
123 $data->visible
= $cm->visible
; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
124 $data->cmidnumber
= $cm->idnumber
; // The cm IDnumber
125 $data->groupmode
= groups_get_activity_groupmode($cm); // locked later if forced
126 $data->groupingid
= $cm->groupingid
;
127 $data->groupmembersonly
= $cm->groupmembersonly
;
128 $data->course
= $course->id
;
129 $data->module
= $module->id
;
130 $data->modulename
= $module->name
;
131 $data->instance
= $cm->instance
;
132 $data->return = $return;
133 $data->update
= $update;
134 $data->completion
= $cm->completion
;
135 $data->completionview
= $cm->completionview
;
136 $data->completionexpected
= $cm->completionexpected
;
137 $data->completionusegrade
= is_null($cm->completiongradeitemnumber
) ?
0 : 1;
138 if (!empty($CFG->enableavailability
)) {
139 $data->availablefrom
= $cm->availablefrom
;
140 $data->availableuntil
= $cm->availableuntil
;
141 $data->showavailability
= $cm->showavailability
;
144 if (plugin_supports('mod', $data->modulename
, FEATURE_MOD_INTRO
, true)) {
145 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
146 $currentintro = file_prepare_draft_area($draftid_editor, $context->id
, 'mod_'.$data->modulename
, 'intro', 0, array('subdirs'=>true), $data->intro
);
147 $data->introeditor
= array('text'=>$currentintro, 'format'=>$data->introformat
, 'itemid'=>$draftid_editor);
150 if ($items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename
,
151 'iteminstance'=>$data->instance
, 'courseid'=>$course->id
))) {
152 // add existing outcomes
153 foreach ($items as $item) {
154 if (!empty($item->outcomeid
)) {
155 $data->{'outcome_'.$item->outcomeid
} = 1;
159 // set category if present
161 foreach ($items as $item) {
162 if ($gradecat === false) {
163 $gradecat = $item->categoryid
;
166 if ($gradecat != $item->categoryid
) {
172 if ($gradecat !== false) {
173 // do not set if mixed categories present
174 $data->gradecat
= $gradecat;
178 $sectionname = get_section_name($course, $cw);
179 $fullmodulename = get_string('modulename', $module->name
);
181 if ($data->section
&& $course->format
!= 'site') {
182 $heading = new stdClass();
183 $heading->what
= $fullmodulename;
184 $heading->in
= $sectionname;
185 $pageheading = get_string('updatingain', 'moodle', $heading);
187 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
192 print_error('invalidaction');
195 $pagepath = 'mod-' . $module->name
. '-';
196 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
201 $PAGE->set_pagetype($pagepath);
202 $PAGE->set_pagelayout('admin');
204 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
205 if (file_exists($modmoodleform)) {
206 require_once($modmoodleform);
208 print_error('noformdesc');
211 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
212 if (file_exists($modlib)) {
213 include_once($modlib);
215 print_error('modulemissingcode', '', '', $modlib);
218 $mformclassname = 'mod_'.$module->name
.'_mod_form';
219 $mform = new $mformclassname($data, $cw->section
, $cm, $course);
220 $mform->set_data($data);
222 if ($mform->is_cancelled()) {
223 if ($return && !empty($cm->id
)) {
224 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
226 redirect("$CFG->wwwroot/course/view.php?id=$course->id#section-".$cw->section
);
228 } else if ($fromform = $mform->get_data()) {
229 if (empty($fromform->coursemodule
)) {
232 $course = $DB->get_record('course', array('id'=>$fromform->course
), '*', MUST_EXIST
);
233 $fromform->instance
= '';
234 $fromform->coursemodule
= '';
237 $cm = get_coursemodule_from_id('', $fromform->coursemodule
, 0, false, MUST_EXIST
);
238 $course = $DB->get_record('course', array('id'=>$cm->course
), '*', MUST_EXIST
);
239 $fromform->instance
= $cm->instance
;
240 $fromform->coursemodule
= $cm->id
;
243 if (!empty($fromform->coursemodule
)) {
244 $context = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
246 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
249 $fromform->course
= $course->id
;
250 $fromform->modulename
= clean_param($fromform->modulename
, PARAM_SAFEDIR
); // For safety
252 $addinstancefunction = $fromform->modulename
."_add_instance";
253 $updateinstancefunction = $fromform->modulename
."_update_instance";
255 if (!isset($fromform->groupingid
)) {
256 $fromform->groupingid
= 0;
259 if (!isset($fromform->groupmembersonly
)) {
260 $fromform->groupmembersonly
= 0;
263 if (!isset($fromform->name
)) { //label
264 $fromform->name
= $fromform->modulename
;
267 if (!isset($fromform->completion
)) {
268 $fromform->completion
= COMPLETION_DISABLED
;
270 if (!isset($fromform->completionview
)) {
271 $fromform->completionview
= COMPLETION_VIEW_NOT_REQUIRED
;
274 // Convert the 'use grade' checkbox into a grade-item number: 0 if
275 // checked, null if not
276 if (isset($fromform->completionusegrade
) && $fromform->completionusegrade
) {
277 $fromform->completiongradeitemnumber
= 0;
279 $fromform->completiongradeitemnumber
= null;
282 // the type of event to trigger (mod_created/mod_updated)
285 if (!empty($fromform->update
)) {
287 if (!empty($course->groupmodeforce
) or !isset($fromform->groupmode
)) {
288 $fromform->groupmode
= $cm->groupmode
; // keep original
291 // update course module first
292 $cm->groupmode
= $fromform->groupmode
;
293 $cm->groupingid
= $fromform->groupingid
;
294 $cm->groupmembersonly
= $fromform->groupmembersonly
;
296 $completion = new completion_info($course);
297 if ($completion->is_enabled()) {
298 // Update completion settings
299 $cm->completion
= $fromform->completion
;
300 $cm->completiongradeitemnumber
= $fromform->completiongradeitemnumber
;
301 $cm->completionview
= $fromform->completionview
;
302 $cm->completionexpected
= $fromform->completionexpected
;
304 if (!empty($CFG->enableavailability
)) {
305 $cm->availablefrom
= $fromform->availablefrom
;
306 $cm->availableuntil
= $fromform->availableuntil
;
307 // The form time is midnight, but because we want it to be
308 // inclusive, add 23:59:59 to the time (86,399 seconds).
309 if ($cm->availableuntil
) {
310 $cm->availableuntil +
= 86399;
312 $cm->showavailability
= $fromform->showavailability
;
313 condition_info
::update_cm_from_form($cm,$fromform,true);
316 $DB->update_record('course_modules', $cm);
318 $modcontext = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
320 // update embedded links and save files
321 if (plugin_supports('mod', $fromform->modulename
, FEATURE_MOD_INTRO
, true)) {
322 $fromform->intro
= file_save_draft_area_files($fromform->introeditor
['itemid'], $modcontext->id
,
323 'mod_'.$fromform->modulename
, 'intro', 0,
324 array('subdirs'=>true), $fromform->introeditor
['text']);
325 $fromform->introformat
= $fromform->introeditor
['format'];
326 unset($fromform->introeditor
);
329 if (!$updateinstancefunction($fromform, $mform)) {
330 print_error('cannotupdatemod', '', "view.php?id={$course->id}#section-{$cw->section}", $fromform->modulename
);
333 // make sure visibility is set correctly (in particular in calendar)
334 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
336 if (isset($fromform->cmidnumber
)) { //label
337 // set cm idnumber - uniqueness is already verified by form validation
338 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
341 // Now that module is fully updated, also update completion data if
342 // required (this will wipe all user completion data and recalculate it)
343 if ($completion->is_enabled() && !empty($fromform->completionunlocked
)) {
344 $completion->reset_all_state($cm);
347 $eventname = 'mod_updated';
349 add_to_log($course->id
, "course", "update mod",
350 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
351 "$fromform->modulename $fromform->instance");
352 add_to_log($course->id
, $fromform->modulename
, "update",
353 "view.php?id=$fromform->coursemodule",
354 "$fromform->instance", $fromform->coursemodule
);
356 } else if (!empty($fromform->add
)) {
358 if (!empty($course->groupmodeforce
) or !isset($fromform->groupmode
)) {
359 $fromform->groupmode
= 0; // do not set groupmode
362 if (!course_allowed_module($course, $fromform->modulename
)) {
363 print_error('moduledisable', '', '', $fromform->modulename
);
366 // first add course_module record because we need the context
367 $newcm = new stdClass();
368 $newcm->course
= $course->id
;
369 $newcm->module
= $fromform->module
;
370 $newcm->instance
= 0; // not known yet, will be updated later (this is similar to restore code)
371 $newcm->visible
= $fromform->visible
;
372 $newcm->groupmode
= $fromform->groupmode
;
373 $newcm->groupingid
= $fromform->groupingid
;
374 $newcm->groupmembersonly
= $fromform->groupmembersonly
;
375 $completion = new completion_info($course);
376 if ($completion->is_enabled()) {
377 $newcm->completion
= $fromform->completion
;
378 $newcm->completiongradeitemnumber
= $fromform->completiongradeitemnumber
;
379 $newcm->completionview
= $fromform->completionview
;
380 $newcm->completionexpected
= $fromform->completionexpected
;
382 if(!empty($CFG->enableavailability
)) {
383 $newcm->availablefrom
= $fromform->availablefrom
;
384 $newcm->availableuntil
= $fromform->availableuntil
;
385 // The form time is midnight, but because we want it to be
386 // inclusive, add 23:59:59 to the time (86,399 seconds).
387 if ($newcm->availableuntil
) {
388 $newcm->availableuntil +
= 86399;
390 $newcm->showavailability
= $fromform->showavailability
;
393 if (!$fromform->coursemodule
= add_course_module($newcm)) {
394 print_error('cannotaddcoursemodule');
397 if (plugin_supports('mod', $fromform->modulename
, FEATURE_MOD_INTRO
, true)) {
398 $introeditor = $fromform->introeditor
;
399 unset($fromform->introeditor
);
400 $fromform->intro
= $introeditor['text'];
401 $fromform->introformat
= $introeditor['format'];
404 $returnfromfunc = $addinstancefunction($fromform, $mform);
406 if (!$returnfromfunc or !is_number($returnfromfunc)) {
407 // undo everything we can
408 $modcontext = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
409 delete_context(CONTEXT_MODULE
, $fromform->coursemodule
);
410 $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule
));
412 if (!is_number($returnfromfunc)) {
413 print_error('invalidfunction', '', "view.php?id={$course->id}#section-{$cw->section}");
415 print_error('cannotaddnewmodule', '', "view.php?id={$course->id}#section-{$cw->section}", $fromform->modulename
);
419 $fromform->instance
= $returnfromfunc;
421 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule
));
423 // update embedded links and save files
424 $modcontext = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
425 if (!empty($introeditor)) {
426 $fromform->intro
= file_save_draft_area_files($introeditor['itemid'], $modcontext->id
,
427 'mod_'.$fromform->modulename
, 'intro', 0,
428 array('subdirs'=>true), $introeditor['text']);
429 $DB->set_field($fromform->modulename
, 'intro', $fromform->intro
, array('id'=>$fromform->instance
));
432 // course_modules and course_sections each contain a reference
433 // to each other, so we have to update one of them twice.
434 $sectionid = add_mod_to_section($fromform);
436 $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule
));
438 // make sure visibility is set correctly (in particular in calendar)
439 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
441 if (isset($fromform->cmidnumber
)) { //label
442 // set cm idnumber - uniqueness is already verified by form validation
443 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
447 if ($CFG->enableavailability
) {
448 condition_info
::update_cm_from_form((object)array('id'=>$fromform->coursemodule
), $fromform, false);
451 $eventname = 'mod_created';
453 add_to_log($course->id
, "course", "add mod",
454 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
455 "$fromform->modulename $fromform->instance");
456 add_to_log($course->id
, $fromform->modulename
, "add",
457 "view.php?id=$fromform->coursemodule",
458 "$fromform->instance", $fromform->coursemodule
);
460 print_error('invaliddata');
463 // Trigger mod_created/mod_updated event with information about this module.
464 $eventdata = new stdClass();
465 $eventdata->modulename
= $fromform->modulename
;
466 $eventdata->name
= $fromform->name
;
467 $eventdata->cmid
= $fromform->coursemodule
;
468 $eventdata->courseid
= $course->id
;
469 $eventdata->userid
= $USER->id
;
470 events_trigger($eventname, $eventdata);
472 // sync idnumber with grade_item
473 if ($grade_item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
474 'iteminstance'=>$fromform->instance
, 'itemnumber'=>0, 'courseid'=>$course->id
))) {
475 if ($grade_item->idnumber
!= $fromform->cmidnumber
) {
476 $grade_item->idnumber
= $fromform->cmidnumber
;
477 $grade_item->update();
481 $items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
482 'iteminstance'=>$fromform->instance
, 'courseid'=>$course->id
));
484 // create parent category if requested and move to correct parent category
485 if ($items and isset($fromform->gradecat
)) {
486 if ($fromform->gradecat
== -1) {
487 $grade_category = new grade_category();
488 $grade_category->courseid
= $course->id
;
489 $grade_category->fullname
= $fromform->name
;
490 $grade_category->insert();
492 $parent = $grade_item->get_parent_category();
493 $grade_category->set_parent($parent->id
);
495 $fromform->gradecat
= $grade_category->id
;
497 foreach ($items as $itemid=>$unused) {
498 $items[$itemid]->set_parent($fromform->gradecat
);
499 if ($itemid == $grade_item->id
) {
500 // use updated grade_item
501 $grade_item = $items[$itemid];
506 // add outcomes if requested
507 if ($outcomes = grade_outcome
::fetch_all_available($course->id
)) {
508 $grade_items = array();
510 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
511 $max_itemnumber = 999;
513 foreach($items as $item) {
514 if ($item->itemnumber
> $max_itemnumber) {
515 $max_itemnumber = $item->itemnumber
;
520 foreach($outcomes as $outcome) {
521 $elname = 'outcome_'.$outcome->id
;
523 if (property_exists($fromform, $elname) and $fromform->$elname) {
524 // so we have a request for new outcome grade item?
526 foreach($items as $item) {
527 if ($item->outcomeid
== $outcome->id
) {
528 //outcome aready exists
536 $outcome_item = new grade_item();
537 $outcome_item->courseid
= $course->id
;
538 $outcome_item->itemtype
= 'mod';
539 $outcome_item->itemmodule
= $fromform->modulename
;
540 $outcome_item->iteminstance
= $fromform->instance
;
541 $outcome_item->itemnumber
= $max_itemnumber;
542 $outcome_item->itemname
= $outcome->fullname
;
543 $outcome_item->outcomeid
= $outcome->id
;
544 $outcome_item->gradetype
= GRADE_TYPE_SCALE
;
545 $outcome_item->scaleid
= $outcome->scaleid
;
546 $outcome_item->insert();
548 // move the new outcome into correct category and fix sortorder if needed
550 $outcome_item->set_parent($grade_item->categoryid
);
551 $outcome_item->move_after_sortorder($grade_item->sortorder
);
553 } else if (isset($fromform->gradecat
)) {
554 $outcome_item->set_parent($fromform->gradecat
);
560 rebuild_course_cache($course->id
);
561 grade_regrade_final_grades($course->id
);
562 plagiarism_save_form_elements($fromform); //save plagiarism settings
564 if (isset($fromform->submitbutton
)) {
565 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
567 redirect("$CFG->wwwroot/course/view.php?id={$course->id}#section-{$cw->section}");
573 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
574 $strmodulenameplural = get_string('modulenameplural', $module->name
);
576 if (!empty($cm->id
)) {
577 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
579 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
582 $PAGE->set_heading($course->fullname
);
583 $PAGE->set_title($streditinga);
584 $PAGE->set_cacheable(false);
585 echo $OUTPUT->header();
587 if (get_string_manager()->string_exists('modulename_help', $module->name
)) {
588 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name
, 'icon');
590 echo $OUTPUT->heading_with_help($pageheading, '', $module->name
, 'icon');
595 echo $OUTPUT->footer();