Activity completion MDL-24746 If you turn on autocompletion for an activity after...
[moodle.git] / course / modedit.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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.
14 //
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/>.
18 /**
19 * Adds or updates modules in a course using new formslib
20 *
21 * @package    moodlecore
22 * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
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);
42 }
44 if (!empty($add)) {
45     $url->param('add', $add);
46     $PAGE->set_url($url);
48     $section = required_param('section', PARAM_INT);
49     $course  = required_param('course', PARAM_INT);
51     $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
52     $module = $DB->get_record('modules', array('name'=>$add), '*', MUST_EXIST);
54     require_login($course);
55     $context = get_context_instance(CONTEXT_COURSE, $course->id);
56     require_capability('moodle/course:manageactivities', $context);
58     $cw = get_course_section($section, $course->id);
60     if (!course_allowed_module($course, $module->id)) {
61         print_error('moduledisable');
62     }
64     $cm = null;
66     $data = new stdClass();
67     $data->section          = $section;  // The section number itself - relative!!! (section column in course_sections)
68     $data->visible          = $cw->visible;
69     $data->course           = $course->id;
70     $data->module           = $module->id;
71     $data->modulename       = $module->name;
72     $data->groupmode        = $course->groupmode;
73     $data->groupingid       = $course->defaultgroupingid;
74     $data->groupmembersonly = 0;
75     $data->id               = '';
76     $data->instance         = '';
77     $data->coursemodule     = '';
78     $data->add              = $add;
79     $data->return           = 0; //must be false if this is an add, go back to course view on cancel
81     if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
82         $draftid_editor = file_get_submitted_draft_itemid('introeditor');
83         file_prepare_draft_area($draftid_editor, null, null, null, null);
84         $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
85     }
87     if (!empty($type)) { //TODO: hopefully will be removed in 2.0
88         $data->type = $type;
89     }
91     $sectionname = get_section_name($course, $cw);
92     $fullmodulename = get_string('modulename', $module->name);
94     if ($data->section && $course->format != 'site') {
95         $heading = new stdClass();
96         $heading->what = $fullmodulename;
97         $heading->to   = $sectionname;
98         $pageheading = get_string('addinganewto', 'moodle', $heading);
99     } else {
100         $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
101     }
103 } else if (!empty($update)) {
105     $url->param('update', $update);
106     $PAGE->set_url($url);
108     $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
109     $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
111     require_login($course, false, $cm); // needed to setup proper $COURSE
112     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
113     require_capability('moodle/course:manageactivities', $context);
115     $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
116     $data = $data = $DB->get_record($module->name, array('id'=>$cm->instance), '*', MUST_EXIST);
117     $cw = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
119     $data->coursemodule       = $cm->id;
120     $data->section            = $cw->section;  // The section number itself - relative!!! (section column in course_sections)
121     $data->visible            = $cm->visible; //??  $cw->visible ? $cm->visible : 0; // section hiding overrides
122     $data->cmidnumber         = $cm->idnumber;          // The cm IDnumber
123     $data->groupmode          = groups_get_activity_groupmode($cm); // locked later if forced
124     $data->groupingid         = $cm->groupingid;
125     $data->groupmembersonly   = $cm->groupmembersonly;
126     $data->course             = $course->id;
127     $data->module             = $module->id;
128     $data->modulename         = $module->name;
129     $data->instance           = $cm->instance;
130     $data->return             = $return;
131     $data->update             = $update;
132     $data->completion         = $cm->completion;
133     $data->completionview     = $cm->completionview;
134     $data->completionexpected = $cm->completionexpected;
135     $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
136     if (!empty($CFG->enableavailability)) {
137         $data->availablefrom      = $cm->availablefrom;
138         $data->availableuntil     = $cm->availableuntil;
139         $data->showavailability   = $cm->showavailability;
140     }
142     if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
143         $draftid_editor = file_get_submitted_draft_itemid('introeditor');
144         $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
145         $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
146     }
148     if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
149                                              'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
150         // add existing outcomes
151         foreach ($items as $item) {
152             if (!empty($item->outcomeid)) {
153                 $data->{'outcome_'.$item->outcomeid} = 1;
154             }
155         }
157         // set category if present
158         $gradecat = false;
159         foreach ($items as $item) {
160             if ($gradecat === false) {
161                 $gradecat = $item->categoryid;
162                 continue;
163             }
164             if ($gradecat != $item->categoryid) {
165                 //mixed categories
166                 $gradecat = false;
167                 break;
168             }
169         }
170         if ($gradecat !== false) {
171             // do not set if mixed categories present
172             $data->gradecat = $gradecat;
173         }
174     }
176     $sectionname = get_section_name($course, $cw);
177     $fullmodulename = get_string('modulename', $module->name);
179     if ($data->section && $course->format != 'site') {
180         $heading = new stdClass();
181         $heading->what = $fullmodulename;
182         $heading->in   = $sectionname;
183         $pageheading = get_string('updatingain', 'moodle', $heading);
184     } else {
185         $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
186     }
188 } else {
189     require_login();
190     print_error('invalidaction');
193 $pagepath = 'mod-' . $module->name . '-';
194 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
195     $pagepath .= $type;
196 } else {
197     $pagepath .= 'mod';
199 $PAGE->set_pagetype($pagepath);
200 $PAGE->set_pagelayout('admin');
202 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
203 if (file_exists($modmoodleform)) {
204     require_once($modmoodleform);
205 } else {
206     print_error('noformdesc');
209 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
210 if (file_exists($modlib)) {
211     include_once($modlib);
212 } else {
213     print_error('modulemissingcode', '', '', $modlib);
216 $mformclassname = 'mod_'.$module->name.'_mod_form';
217 $mform = new $mformclassname($data, $cw->section, $cm, $course);
218 $mform->set_data($data);
220 if ($mform->is_cancelled()) {
221     if ($return && !empty($cm->id)) {
222         redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
223     } else {
224         redirect("$CFG->wwwroot/course/view.php?id=$course->id#section-".$cw->section);
225     }
226 } else if ($fromform = $mform->get_data()) {
227     if (empty($fromform->coursemodule)) {
228         // Add
229         $cm = null;
230         $course = $DB->get_record('course', array('id'=>$fromform->course), '*', MUST_EXIST);
231         $fromform->instance     = '';
232         $fromform->coursemodule = '';
233     } else {
234         // Update
235         $cm = get_coursemodule_from_id('', $fromform->coursemodule, 0, false, MUST_EXIST);
236         $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
237         $fromform->instance     = $cm->instance;
238         $fromform->coursemodule = $cm->id;
239     }
241     if (!empty($fromform->coursemodule)) {
242         $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
243     } else {
244         $context = get_context_instance(CONTEXT_COURSE, $course->id);
245     }
247     $fromform->course = $course->id;
248     $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR);  // For safety
250     $addinstancefunction    = $fromform->modulename."_add_instance";
251     $updateinstancefunction = $fromform->modulename."_update_instance";
253     if (!isset($fromform->groupingid)) {
254         $fromform->groupingid = 0;
255     }
257     if (!isset($fromform->groupmembersonly)) {
258         $fromform->groupmembersonly = 0;
259     }
261     if (!isset($fromform->name)) { //label
262         $fromform->name = $fromform->modulename;
263     }
265     if (!isset($fromform->completion)) {
266         $fromform->completion = COMPLETION_DISABLED;
267     }
268     if (!isset($fromform->completionview)) {
269         $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
270     }
272     // Convert the 'use grade' checkbox into a grade-item number: 0 if
273     // checked, null if not
274     if (isset($fromform->completionusegrade) && $fromform->completionusegrade) {
275         $fromform->completiongradeitemnumber = 0;
276     } else {
277         $fromform->completiongradeitemnumber = null;
278     }
280     if (!empty($fromform->update)) {
282         if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
283             $fromform->groupmode = $cm->groupmode; // keep original
284         }
286         // update course module first
287         $cm->groupmode        = $fromform->groupmode;
288         $cm->groupingid       = $fromform->groupingid;
289         $cm->groupmembersonly = $fromform->groupmembersonly;
291         $completion = new completion_info($course);
292         if ($completion->is_enabled()) {
293             // Update completion settings
294             $cm->completion                = $fromform->completion;
295             $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
296             $cm->completionview            = $fromform->completionview;
297             $cm->completionexpected        = $fromform->completionexpected;
298         }
299         if (!empty($CFG->enableavailability)) {
300             $cm->availablefrom             = $fromform->availablefrom;
301             $cm->availableuntil            = $fromform->availableuntil;
302             // The form time is midnight, but because we want it to be
303             // inclusive, set it to 23:59:59 on that day.
304             if ($cm->availableuntil) {
305                 $cm->availableuntil = strtotime('23:59:59',
306                     $cm->availableuntil);
307             }
308             $cm->showavailability          = $fromform->showavailability;
309             condition_info::update_cm_from_form($cm,$fromform,true);
310         }
312         $DB->update_record('course_modules', $cm);
314         $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
316         // update embedded links and save files
317         if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
318             $fromform->intro = file_save_draft_area_files($fromform->introeditor['itemid'], $modcontext->id,
319                                                           'mod_'.$fromform->modulename, 'intro', 0,
320                                                           array('subdirs'=>true), $fromform->introeditor['text']);
321             $fromform->introformat = $fromform->introeditor['format'];
322             unset($fromform->introeditor);
323         }
325         if (!$updateinstancefunction($fromform, $mform)) {
326             print_error('cannotupdatemod', '', 'view.php?id=$course->id', $fromform->modulename);
327         }
329         // make sure visibility is set correctly (in particular in calendar)
330         set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
332         if (isset($fromform->cmidnumber)) { //label
333             // set cm idnumber - uniqueness is already verified by form validation
334             set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
335         }
337         // Now that module is fully updated, also update completion data if 
338         // required (this will wipe all user completion data and recalculate it)
339         if ($completion->is_enabled() && !empty($fromform->completionunlocked)) {
340             $completion->reset_all_state($cm);
341         }
343         // Trigger mod_updated event with information about this module.
344         $eventdata = new stdClass();
345         $eventdata->modulename = $fromform->modulename;
346         $eventdata->name       = $fromform->name;
347         $eventdata->cmid       = $fromform->coursemodule;
348         $eventdata->courseid   = $course->id;
349         $eventdata->userid     = $USER->id;
350         events_trigger('mod_updated', $eventdata);
352         add_to_log($course->id, "course", "update mod",
353                    "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
354                    "$fromform->modulename $fromform->instance");
355         add_to_log($course->id, $fromform->modulename, "update",
356                    "view.php?id=$fromform->coursemodule",
357                    "$fromform->instance", $fromform->coursemodule);
359     } else if (!empty($fromform->add)) {
361         if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
362             $fromform->groupmode = 0; // do not set groupmode
363         }
365         if (!course_allowed_module($course, $fromform->modulename)) {
366             print_error('moduledisable', '', '', $fromform->modulename);
367         }
369         // first add course_module record because we need the context
370         $newcm = new stdClass();
371         $newcm->course           = $course->id;
372         $newcm->module           = $fromform->module;
373         $newcm->instance         = 0; // not known yet, will be updated later (this is similar to restore code)
374         $newcm->visible          = $fromform->visible;
375         $newcm->groupmode        = $fromform->groupmode;
376         $newcm->groupingid       = $fromform->groupingid;
377         $newcm->groupmembersonly = $fromform->groupmembersonly;
378         $completion = new completion_info($course);
379         if ($completion->is_enabled()) {
380             $newcm->completion                = $fromform->completion;
381             $newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
382             $newcm->completionview            = $fromform->completionview;
383             $newcm->completionexpected        = $fromform->completionexpected;
384         }
385         if(!empty($CFG->enableavailability)) {
386             $newcm->availablefrom             = $fromform->availablefrom;
387             $newcm->availableuntil            = $fromform->availableuntil;
388             // The form time is midnight, but because we want it to be
389             // inclusive, set it to 23:59:59 on that day.
390             if ($newcm->availableuntil) {
391                 $newcm->availableuntil = strtotime('23:59:59',
392                     $newcm->availableuntil);
393             }
394             $newcm->showavailability          = $fromform->showavailability;
395         }
397         if (!$fromform->coursemodule = add_course_module($newcm)) {
398             print_error('cannotaddcoursemodule');
399         }
401         if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, false)) {
402             $introeditor = $fromform->introeditor;
403             unset($fromform->introeditor);
404             $fromform->intro       = $introeditor['text'];
405             $fromform->introformat = $introeditor['format'];
406         }
408         $returnfromfunc = $addinstancefunction($fromform, $mform);
410         if (!$returnfromfunc or !is_number($returnfromfunc)) {
411             // undo everything we can
412             $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
413             delete_context(CONTEXT_MODULE, $fromform->coursemodule);
414             $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule));
416             if (!is_number($returnfromfunc)) {
417                 print_error('invalidfunction', '', 'view.php?id=$course->id');
418             } else {
419                 print_error('cannotaddnewmodule', '', "view.php?id=$course->id", $fromform->modulename);
420             }
421         }
423         $fromform->instance = $returnfromfunc;
425         $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule));
427         // update embedded links and save files
428         $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
429         if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
430             $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
431                                                           'mod_'.$fromform->modulename, 'intro', 0,
432                                                           array('subdirs'=>true), $introeditor['text']);
433             $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id'=>$fromform->instance));
434         }
436         // course_modules and course_sections each contain a reference
437         // to each other, so we have to update one of them twice.
438         $sectionid = add_mod_to_section($fromform);
440         $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule));
442         // make sure visibility is set correctly (in particular in calendar)
443         set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
445         if (isset($fromform->cmidnumber)) { //label
446             // set cm idnumber - uniqueness is already verified by form validation
447             set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
448         }
450         // Set up conditions
451         if ($CFG->enableavailability) {
452             condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
453         }
455         // Trigger mod_created event with information about this module.
456         $eventdata = new stdClass();
457         $eventdata->modulename = $fromform->modulename;
458         $eventdata->name       = $fromform->name;
459         $eventdata->cmid       = $fromform->coursemodule;
460         $eventdata->courseid   = $course->id;
461         $eventdata->userid     = $USER->id;
462         events_trigger('mod_created', $eventdata);
464         add_to_log($course->id, "course", "add mod",
465                    "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
466                    "$fromform->modulename $fromform->instance");
467         add_to_log($course->id, $fromform->modulename, "add",
468                    "view.php?id=$fromform->coursemodule",
469                    "$fromform->instance", $fromform->coursemodule);
470     } else {
471         print_error('invaliddata');
472     }
474     // sync idnumber with grade_item
475     if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
476                  'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
477         if ($grade_item->idnumber != $fromform->cmidnumber) {
478             $grade_item->idnumber = $fromform->cmidnumber;
479             $grade_item->update();
480         }
481     }
483     $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
484                                          'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
486     // create parent category if requested and move to correct parent category
487     if ($items and isset($fromform->gradecat)) {
488         if ($fromform->gradecat == -1) {
489             $grade_category = new grade_category();
490             $grade_category->courseid = $course->id;
491             $grade_category->fullname = $fromform->name;
492             $grade_category->insert();
493             if ($grade_item) {
494                 $parent = $grade_item->get_parent_category();
495                 $grade_category->set_parent($parent->id);
496             }
497             $fromform->gradecat = $grade_category->id;
498         }
499         foreach ($items as $itemid=>$unused) {
500             $items[$itemid]->set_parent($fromform->gradecat);
501             if ($itemid == $grade_item->id) {
502                 // use updated grade_item
503                 $grade_item = $items[$itemid];
504             }
505         }
506     }
508     // add outcomes if requested
509     if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
510         $grade_items = array();
512         // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
513         $max_itemnumber = 999;
514         if ($items) {
515             foreach($items as $item) {
516                 if ($item->itemnumber > $max_itemnumber) {
517                     $max_itemnumber = $item->itemnumber;
518                 }
519             }
520         }
522         foreach($outcomes as $outcome) {
523             $elname = 'outcome_'.$outcome->id;
525             if (property_exists($fromform, $elname) and $fromform->$elname) {
526                 // so we have a request for new outcome grade item?
527                 if ($items) {
528                     foreach($items as $item) {
529                         if ($item->outcomeid == $outcome->id) {
530                             //outcome aready exists
531                             continue 2;
532                         }
533                     }
534                 }
536                 $max_itemnumber++;
538                 $outcome_item = new grade_item();
539                 $outcome_item->courseid     = $course->id;
540                 $outcome_item->itemtype     = 'mod';
541                 $outcome_item->itemmodule   = $fromform->modulename;
542                 $outcome_item->iteminstance = $fromform->instance;
543                 $outcome_item->itemnumber   = $max_itemnumber;
544                 $outcome_item->itemname     = $outcome->fullname;
545                 $outcome_item->outcomeid    = $outcome->id;
546                 $outcome_item->gradetype    = GRADE_TYPE_SCALE;
547                 $outcome_item->scaleid      = $outcome->scaleid;
548                 $outcome_item->insert();
550                 // move the new outcome into correct category and fix sortorder if needed
551                 if ($grade_item) {
552                     $outcome_item->set_parent($grade_item->categoryid);
553                     $outcome_item->move_after_sortorder($grade_item->sortorder);
555                 } else if (isset($fromform->gradecat)) {
556                     $outcome_item->set_parent($fromform->gradecat);
557                 }
558             }
559         }
560     }
562     rebuild_course_cache($course->id);
563     grade_regrade_final_grades($course->id);
564     plagiarism_save_form_elements($fromform); //save plagiarism settings
566     if (isset($fromform->submitbutton)) {
567         redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
568     } else {
569         redirect("$CFG->wwwroot/course/view.php?id=$course->id");
570     }
571     exit;
573 } else {
575     $streditinga = get_string('editinga', 'moodle', $fullmodulename);
576     $strmodulenameplural = get_string('modulenameplural', $module->name);
578     if (!empty($cm->id)) {
579         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
580     } else {
581         $context = get_context_instance(CONTEXT_COURSE, $course->id);
582     }
584     $PAGE->set_heading($course->fullname);
585     $PAGE->set_title($streditinga);
586     $PAGE->set_cacheable(false);
587     echo $OUTPUT->header();
589     if (get_string_manager()->string_exists('modulename_help', $module->name)) {
590         echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
591     } else {
592         echo $OUTPUT->heading_with_help($pageheading, '', '', 'icon');
593     }
595     $mform->display();
597     echo $OUTPUT->footer();