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 if (!empty($fromform->update
)) {
284 if (!empty($course->groupmodeforce
) or !isset($fromform->groupmode
)) {
285 $fromform->groupmode
= $cm->groupmode
; // keep original
288 // update course module first
289 $cm->groupmode
= $fromform->groupmode
;
290 $cm->groupingid
= $fromform->groupingid
;
291 $cm->groupmembersonly
= $fromform->groupmembersonly
;
293 $completion = new completion_info($course);
294 if ($completion->is_enabled()) {
295 // Update completion settings
296 $cm->completion
= $fromform->completion
;
297 $cm->completiongradeitemnumber
= $fromform->completiongradeitemnumber
;
298 $cm->completionview
= $fromform->completionview
;
299 $cm->completionexpected
= $fromform->completionexpected
;
301 if (!empty($CFG->enableavailability
)) {
302 $cm->availablefrom
= $fromform->availablefrom
;
303 $cm->availableuntil
= $fromform->availableuntil
;
304 // The form time is midnight, but because we want it to be
305 // inclusive, set it to 23:59:59 on that day.
306 if ($cm->availableuntil
) {
307 $cm->availableuntil
= strtotime('23:59:59',
308 $cm->availableuntil
);
310 $cm->showavailability
= $fromform->showavailability
;
311 condition_info
::update_cm_from_form($cm,$fromform,true);
314 $DB->update_record('course_modules', $cm);
316 $modcontext = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
318 // update embedded links and save files
319 if (plugin_supports('mod', $fromform->modulename
, FEATURE_MOD_INTRO
, true)) {
320 $fromform->intro
= file_save_draft_area_files($fromform->introeditor
['itemid'], $modcontext->id
,
321 'mod_'.$fromform->modulename
, 'intro', 0,
322 array('subdirs'=>true), $fromform->introeditor
['text']);
323 $fromform->introformat
= $fromform->introeditor
['format'];
324 unset($fromform->introeditor
);
327 if (!$updateinstancefunction($fromform, $mform)) {
328 print_error('cannotupdatemod', '', "view.php?id={$course->id}#section-{$cw->section}", $fromform->modulename
);
331 // make sure visibility is set correctly (in particular in calendar)
332 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
334 if (isset($fromform->cmidnumber
)) { //label
335 // set cm idnumber - uniqueness is already verified by form validation
336 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
339 // Now that module is fully updated, also update completion data if
340 // required (this will wipe all user completion data and recalculate it)
341 if ($completion->is_enabled() && !empty($fromform->completionunlocked
)) {
342 $completion->reset_all_state($cm);
345 // Trigger mod_updated event with information about this module.
346 $eventdata = new stdClass();
347 $eventdata->modulename
= $fromform->modulename
;
348 $eventdata->name
= $fromform->name
;
349 $eventdata->cmid
= $fromform->coursemodule
;
350 $eventdata->courseid
= $course->id
;
351 $eventdata->userid
= $USER->id
;
352 events_trigger('mod_updated', $eventdata);
354 add_to_log($course->id
, "course", "update mod",
355 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
356 "$fromform->modulename $fromform->instance");
357 add_to_log($course->id
, $fromform->modulename
, "update",
358 "view.php?id=$fromform->coursemodule",
359 "$fromform->instance", $fromform->coursemodule
);
361 } else if (!empty($fromform->add
)) {
363 if (!empty($course->groupmodeforce
) or !isset($fromform->groupmode
)) {
364 $fromform->groupmode
= 0; // do not set groupmode
367 if (!course_allowed_module($course, $fromform->modulename
)) {
368 print_error('moduledisable', '', '', $fromform->modulename
);
371 // first add course_module record because we need the context
372 $newcm = new stdClass();
373 $newcm->course
= $course->id
;
374 $newcm->module
= $fromform->module
;
375 $newcm->instance
= 0; // not known yet, will be updated later (this is similar to restore code)
376 $newcm->visible
= $fromform->visible
;
377 $newcm->groupmode
= $fromform->groupmode
;
378 $newcm->groupingid
= $fromform->groupingid
;
379 $newcm->groupmembersonly
= $fromform->groupmembersonly
;
380 $completion = new completion_info($course);
381 if ($completion->is_enabled()) {
382 $newcm->completion
= $fromform->completion
;
383 $newcm->completiongradeitemnumber
= $fromform->completiongradeitemnumber
;
384 $newcm->completionview
= $fromform->completionview
;
385 $newcm->completionexpected
= $fromform->completionexpected
;
387 if(!empty($CFG->enableavailability
)) {
388 $newcm->availablefrom
= $fromform->availablefrom
;
389 $newcm->availableuntil
= $fromform->availableuntil
;
390 // The form time is midnight, but because we want it to be
391 // inclusive, set it to 23:59:59 on that day.
392 if ($newcm->availableuntil
) {
393 $newcm->availableuntil
= strtotime('23:59:59',
394 $newcm->availableuntil
);
396 $newcm->showavailability
= $fromform->showavailability
;
399 if (!$fromform->coursemodule
= add_course_module($newcm)) {
400 print_error('cannotaddcoursemodule');
403 if (plugin_supports('mod', $fromform->modulename
, FEATURE_MOD_INTRO
, true)) {
404 $introeditor = $fromform->introeditor
;
405 unset($fromform->introeditor
);
406 $fromform->intro
= $introeditor['text'];
407 $fromform->introformat
= $introeditor['format'];
410 $returnfromfunc = $addinstancefunction($fromform, $mform);
412 if (!$returnfromfunc or !is_number($returnfromfunc)) {
413 // undo everything we can
414 $modcontext = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
415 delete_context(CONTEXT_MODULE
, $fromform->coursemodule
);
416 $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule
));
418 if (!is_number($returnfromfunc)) {
419 print_error('invalidfunction', '', "view.php?id={$course->id}#section-{$cw->section}");
421 print_error('cannotaddnewmodule', '', "view.php?id={$course->id}#section-{$cw->section}", $fromform->modulename
);
425 $fromform->instance
= $returnfromfunc;
427 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule
));
429 // update embedded links and save files
430 $modcontext = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
431 if (!empty($introeditor)) {
432 $fromform->intro
= file_save_draft_area_files($introeditor['itemid'], $modcontext->id
,
433 'mod_'.$fromform->modulename
, 'intro', 0,
434 array('subdirs'=>true), $introeditor['text']);
435 $DB->set_field($fromform->modulename
, 'intro', $fromform->intro
, array('id'=>$fromform->instance
));
438 // course_modules and course_sections each contain a reference
439 // to each other, so we have to update one of them twice.
440 $sectionid = add_mod_to_section($fromform);
442 $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule
));
444 // make sure visibility is set correctly (in particular in calendar)
445 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
447 if (isset($fromform->cmidnumber
)) { //label
448 // set cm idnumber - uniqueness is already verified by form validation
449 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
453 if ($CFG->enableavailability
) {
454 condition_info
::update_cm_from_form((object)array('id'=>$fromform->coursemodule
), $fromform, false);
457 // Trigger mod_created event with information about this module.
458 $eventdata = new stdClass();
459 $eventdata->modulename
= $fromform->modulename
;
460 $eventdata->name
= $fromform->name
;
461 $eventdata->cmid
= $fromform->coursemodule
;
462 $eventdata->courseid
= $course->id
;
463 $eventdata->userid
= $USER->id
;
464 events_trigger('mod_created', $eventdata);
466 add_to_log($course->id
, "course", "add mod",
467 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
468 "$fromform->modulename $fromform->instance");
469 add_to_log($course->id
, $fromform->modulename
, "add",
470 "view.php?id=$fromform->coursemodule",
471 "$fromform->instance", $fromform->coursemodule
);
473 print_error('invaliddata');
476 // sync idnumber with grade_item
477 if ($grade_item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
478 'iteminstance'=>$fromform->instance
, 'itemnumber'=>0, 'courseid'=>$course->id
))) {
479 if ($grade_item->idnumber
!= $fromform->cmidnumber
) {
480 $grade_item->idnumber
= $fromform->cmidnumber
;
481 $grade_item->update();
485 $items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
486 'iteminstance'=>$fromform->instance
, 'courseid'=>$course->id
));
488 // create parent category if requested and move to correct parent category
489 if ($items and isset($fromform->gradecat
)) {
490 if ($fromform->gradecat
== -1) {
491 $grade_category = new grade_category();
492 $grade_category->courseid
= $course->id
;
493 $grade_category->fullname
= $fromform->name
;
494 $grade_category->insert();
496 $parent = $grade_item->get_parent_category();
497 $grade_category->set_parent($parent->id
);
499 $fromform->gradecat
= $grade_category->id
;
501 foreach ($items as $itemid=>$unused) {
502 $items[$itemid]->set_parent($fromform->gradecat
);
503 if ($itemid == $grade_item->id
) {
504 // use updated grade_item
505 $grade_item = $items[$itemid];
510 // add outcomes if requested
511 if ($outcomes = grade_outcome
::fetch_all_available($course->id
)) {
512 $grade_items = array();
514 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
515 $max_itemnumber = 999;
517 foreach($items as $item) {
518 if ($item->itemnumber
> $max_itemnumber) {
519 $max_itemnumber = $item->itemnumber
;
524 foreach($outcomes as $outcome) {
525 $elname = 'outcome_'.$outcome->id
;
527 if (property_exists($fromform, $elname) and $fromform->$elname) {
528 // so we have a request for new outcome grade item?
530 foreach($items as $item) {
531 if ($item->outcomeid
== $outcome->id
) {
532 //outcome aready exists
540 $outcome_item = new grade_item();
541 $outcome_item->courseid
= $course->id
;
542 $outcome_item->itemtype
= 'mod';
543 $outcome_item->itemmodule
= $fromform->modulename
;
544 $outcome_item->iteminstance
= $fromform->instance
;
545 $outcome_item->itemnumber
= $max_itemnumber;
546 $outcome_item->itemname
= $outcome->fullname
;
547 $outcome_item->outcomeid
= $outcome->id
;
548 $outcome_item->gradetype
= GRADE_TYPE_SCALE
;
549 $outcome_item->scaleid
= $outcome->scaleid
;
550 $outcome_item->insert();
552 // move the new outcome into correct category and fix sortorder if needed
554 $outcome_item->set_parent($grade_item->categoryid
);
555 $outcome_item->move_after_sortorder($grade_item->sortorder
);
557 } else if (isset($fromform->gradecat
)) {
558 $outcome_item->set_parent($fromform->gradecat
);
564 rebuild_course_cache($course->id
);
565 grade_regrade_final_grades($course->id
);
566 plagiarism_save_form_elements($fromform); //save plagiarism settings
568 if (isset($fromform->submitbutton
)) {
569 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
571 redirect("$CFG->wwwroot/course/view.php?id={$course->id}#section-{$cw->section}");
577 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
578 $strmodulenameplural = get_string('modulenameplural', $module->name
);
580 if (!empty($cm->id
)) {
581 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
583 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
586 $PAGE->set_heading($course->fullname
);
587 $PAGE->set_title($streditinga);
588 $PAGE->set_cacheable(false);
589 echo $OUTPUT->header();
591 if (get_string_manager()->string_exists('modulename_help', $module->name
)) {
592 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name
, 'icon');
594 echo $OUTPUT->heading_with_help($pageheading, '', $module->name
, 'icon');
599 echo $OUTPUT->footer();