MDL-14589 file api improvements - converting more params to general $options array
[moodle.git] / course / modedit.php
1 <?php // $Id$
3 //  adds or updates modules in a course using new formslib
5     require_once("../config.php");
6     require_once("lib.php");
7     require_once($CFG->libdir.'/filelib.php');
8     require_once($CFG->libdir.'/gradelib.php');
9     require_once($CFG->libdir.'/completionlib.php');
10     require_once($CFG->libdir.'/conditionlib.php');
12     $add    = optional_param('add', 0, PARAM_ALPHA);
13     $update = optional_param('update', 0, PARAM_INT);
14     $return = optional_param('return', 0, PARAM_BOOL); //return to course/view.php if false or mod/modname/view.php if true
15     $type   = optional_param('type', '', PARAM_ALPHANUM);
17     require_login();
19     if (!empty($add)) {
20         $section = required_param('section', PARAM_INT);
21         $course  = required_param('course', PARAM_INT);
23         if (!$course = $DB->get_record('course', array('id'=>$course))) {
24             print_error('invalidcourseid');
25         }
27         require_login($course);
28         $context = get_context_instance(CONTEXT_COURSE, $course->id);
29         require_capability('moodle/course:manageactivities', $context);
31         if (!$module = $DB->get_record('modules', array('name'=>$add))) {
32             print_error('moduledoesnotexist');
33         }
35         $cw = get_course_section($section, $course->id);
37         if (!course_allowed_module($course, $module->id)) {
38             print_error('moduledisable');
39         }
41         $cm = null;
43         $form = new object();
44         $form->section          = $section;  // The section number itself - relative!!! (section column in course_sections)
45         $form->visible          = $cw->visible;
46         $form->course           = $course->id;
47         $form->module           = $module->id;
48         $form->modulename       = $module->name;
49         $form->groupmode        = $course->groupmode;
50         $form->groupingid       = $course->defaultgroupingid;
51         $form->groupmembersonly = 0;
52         $form->instance         = '';
53         $form->coursemodule     = '';
54         $form->add              = $add;
55         $form->return           = 0; //must be false if this is an add, go back to course view on cancel
57         if (plugin_supports('mod', $form->modulename, FEATURE_MOD_INTRO, true)) {
58             $draftid_editor = file_get_submitted_draft_itemid('introeditor');
59             file_prepare_draft_area($draftid_editor, null, null, null);
60             $form->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
61         }
63         // Turn off default grouping for modules that don't provide group mode
64         if ($add=='resource' || $add=='glossary' || $add=='label') {
65             $form->groupingid = 0;
66         }
67         
68         if (!empty($type)) {
69             $form->type = $type;
70         }
72         $sectionname = get_section_name($course->format);
73         $fullmodulename = get_string('modulename', $module->name);
75         if ($form->section && $course->format != 'site') {
76             $heading->what = $fullmodulename;
77             $heading->to   = "$sectionname $form->section";
78             $pageheading = get_string('addinganewto', 'moodle', $heading);
79         } else {
80             $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
81         }
83         $pagepath = 'mod-' . $module->name . '-';
84         if (!empty($type)) {
85             $pagepath .= $type;
86         } else {
87             $pagepath .= 'mod';
88         }
89         $PAGE->set_pagetype($pagepath);
91         $navlinksinstancename = '';
93     } else if (!empty($update)) {
94         if (!$cm = get_coursemodule_from_id('', $update, 0)) {
95             print_error('invalidcoursemodule');
96         }
98         if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
99             print_error('invalidcourseid');
100         }
102         require_login($course); // needed to setup proper $COURSE
103         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
104         require_capability('moodle/course:manageactivities', $context);
106         if (!$module = $DB->get_record('modules', array('id'=>$cm->module))) {
107             print_error('moduledoesnotexist');
108         }
110         if (!$form = $DB->get_record($module->name, array('id'=>$cm->instance))) {
111             print_error('moduleinstancedoesnotexist');
112         }
114         if (!$cw = $DB->get_record('course_sections', array('id'=>$cm->section))) {
115             print_error('sectionnotexist');
116         }
118         $form->coursemodule       = $cm->id;
119         $form->section            = $cw->section;  // The section number itself - relative!!! (section column in course_sections)
120         $form->visible            = $cm->visible; //??  $cw->visible ? $cm->visible : 0; // section hiding overrides
121         $form->cmidnumber         = $cm->idnumber;          // The cm IDnumber
122         $form->groupmode          = groups_get_activity_groupmode($cm); // locked later if forced
123         $form->groupingid         = $cm->groupingid;
124         $form->groupmembersonly   = $cm->groupmembersonly;
125         $form->course             = $course->id;
126         $form->module             = $module->id;
127         $form->modulename         = $module->name;
128         $form->instance           = $cm->instance;
129         $form->return             = $return;
130         $form->update             = $update;
131         $form->completion         = $cm->completion;
132         $form->completionview     = $cm->completionview;
133         $form->completionexpected = $cm->completionexpected;
134         $form->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
135         if(!empty($CFG->enableavailability)) {
136             $form->availablefrom      = $cm->availablefrom;
137             $form->availableuntil     = $cm->availableuntil;
138             $form->showavailability   = $cm->showavailability;
139         }
141         if (plugin_supports('mod', $form->modulename, FEATURE_MOD_INTRO, true)) {
142             $draftid_editor = file_get_submitted_draft_itemid('introeditor');
143             $currentintro = file_prepare_draft_area($draftid_editor, $context->id, $form->modulename.'_intro', 0, array('subdirs'=>true), $form->intro);
144             $form->introeditor = array('text'=>$currentintro, 'format'=>$form->introformat, 'itemid'=>$draftid_editor);
145         }
147         if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename,
148                                                  'iteminstance'=>$form->instance, 'courseid'=>$course->id))) {
149             // add existing outcomes
150             foreach ($items as $item) {
151                 if (!empty($item->outcomeid)) {
152                     $form->{'outcome_'.$item->outcomeid} = 1;
153                 }
154             }
156             // set category if present
157             $gradecat = false;
158             foreach ($items as $item) {
159                 if ($gradecat === false) {
160                     $gradecat = $item->categoryid;
161                     continue;
162                 }
163                 if ($gradecat != $item->categoryid) {
164                     //mixed categories
165                     $gradecat = false;
166                     break;
167                 }
168             }
169             if ($gradecat !== false) {
170                 // do not set if mixed categories present
171                 $form->gradecat = $gradecat;
172             }
173         }
175         $sectionname = get_section_name($course->format);
176         $fullmodulename = get_string('modulename', $module->name);
178         if ($form->section && $course->format != 'site') {
179             $heading->what = $fullmodulename;
180             $heading->in   = "$sectionname $cw->section";
181             $pageheading = get_string('updatingain', 'moodle', $heading);
182         } else {
183             $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
184         }
186         $navlinksinstancename = array('name' => format_string($form->name, true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
188         $pagetype = 'mod-' . $module->name . '-';
189         if (!empty($type)) {
190             $pagetype .= $type;
191         } else {
192             $pagetype .= 'mod';
193         }
194         $PAGE->set_pagetype($pagetype);
195     } else {
196         print_error('invalidaction');
197     }
199     $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
200     if (file_exists($modmoodleform)) {
201         require_once($modmoodleform);
203     } else {
204         print_error('noformdesc');
205     }
207     $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
208     if (file_exists($modlib)) {
209         include_once($modlib);
210     } else {
211         print_error('modulemissingcode', '', '', $modlib);
212     }
214     $mformclassname = 'mod_'.$module->name.'_mod_form';
215     $mform = new $mformclassname($form->instance, $cw->section, $cm);
216     $mform->set_data($form);
218     if ($mform->is_cancelled()) {
219         if ($return && !empty($cm->id)) {
220             redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
221         } else {
222             redirect("$CFG->wwwroot/course/view.php?id=$course->id#section-".$cw->section);
223         }
225     } else if ($fromform = $mform->get_data()) {
226         if (empty($fromform->coursemodule)) { //add
227             $cm = null;
228             if (!$course = $DB->get_record('course', array('id'=>$fromform->course))) {
229                 print_error('invalidcourseid');
230             }
231             $fromform->instance     = '';
232             $fromform->coursemodule = '';
233         } else { //update
234             if (!$cm = get_coursemodule_from_id('', $fromform->coursemodule, 0)) {
235                 print_error('invalidcoursemodule');
236             }
238             if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
239                 print_error('invalidcourseid');
240             }
241             $fromform->instance     = $cm->instance;
242             $fromform->coursemodule = $cm->id;
243         }
245         require_login($course); // needed to setup proper $COURSE
247         if (!empty($fromform->coursemodule)) {
248             $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
249         } else {
250             $context = get_context_instance(CONTEXT_COURSE, $course->id);
251         }
252         require_capability('moodle/course:manageactivities', $context);
254         $fromform->course = $course->id;
255         $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR);  // For safety
257         $addinstancefunction    = $fromform->modulename."_add_instance";
258         $updateinstancefunction = $fromform->modulename."_update_instance";
260         if (!isset($fromform->groupingid)) {
261             $fromform->groupingid = 0;
262         }
264         if (!isset($fromform->groupmembersonly)) {
265             $fromform->groupmembersonly = 0;
266         }
268         if (!isset($fromform->name)) { //label
269             $fromform->name = $fromform->modulename;
270         }
271         
272         if (!isset($fromform->completion)) {
273             $fromform->completion = COMPLETION_DISABLED;
274         }
275         if (!isset($fromform->completionview)) {
276             $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
277         }
279         // Convert the 'use grade' checkbox into a grade-item number: 0 if
280         // checked, null if not
281         $fromform->completiongradeitemnumber =
282             isset($fromform->completionusegrade) && $fromform->completionusegrade
283             ? 0 : null;
285         if (!empty($fromform->update)) {
287             if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
288                 $fromform->groupmode = $cm->groupmode; // keep original
289             }
291             // update course module first
292             $cm->groupmode        = $fromform->groupmode;
293             $cm->groupingid       = $fromform->groupingid;
294             $cm->groupmembersonly = $fromform->groupmembersonly;
295             $completion = new completion_info($course);
296             if ($completion->is_enabled()) {
297                 // Handle completion settings. If necessary, wipe existing completion
298                 // data first.
299                 if (!empty($fromform->completionunlocked)) {
300                     $completion = new completion_info($course);
301                     $completion->reset_all_state($cm);
302                 }
304                 $cm->completion                = $fromform->completion;
305                 $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
306                 $cm->completionview            = $fromform->completionview;
307                 $cm->completionexpected        = $fromform->completionexpected;
308             }
309             if(!empty($CFG->enableavailability)) {
310                 $cm->availablefrom             = $fromform->availablefrom;
311                 $cm->availableuntil            = $fromform->availableuntil;
312                 // The form time is midnight, but because we want it to be 
313                 // inclusive, set it to 23:59:59 on that day.
314                 if ($cm->availableuntil) {
315                     $cm->availableuntil = strtotime('23:59:59', 
316                         $cm->availableuntil);
317                 }
318                 $cm->showavailability          = $fromform->showavailability;
319                 condition_info::update_cm_from_form($cm,$fromform,true);
320             }
322             $DB->update_record('course_modules', $cm);
324             $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
325     
326             // update embedded links and save files
327             if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
328                 $fromform->intro = file_save_draft_area_files($fromform->introeditor['itemid'], $modcontext->id,
329                                                               $fromform->modulename.'_intro', 0,
330                                                               array('subdirs'=>true), $fromform->introeditor['text']);
331                 $fromform->introformat = $fromform->introeditor['format'];
332                 unset($fromform->introeditor);
333             }
334             
335             if (!$updateinstancefunction($fromform, $mform)) {
336                 print_error('cannotupdatemod', '', 'view.php?id=$course->id', $fromform->modulename);
337             }
339             // make sure visibility is set correctly (in particular in calendar)
340             set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
342             if (isset($fromform->cmidnumber)) { //label
343                 // set cm idnumber - uniqueness is already verified by form validation
344                 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
345             }
347             add_to_log($course->id, "course", "update mod",
348                        "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
349                        "$fromform->modulename $fromform->instance");
350             add_to_log($course->id, $fromform->modulename, "update",
351                        "view.php?id=$fromform->coursemodule",
352                        "$fromform->instance", $fromform->coursemodule);
354         } else if (!empty($fromform->add)) {
356             if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
357                 $fromform->groupmode = 0; // do not set groupmode
358             }
360             if (!course_allowed_module($course, $fromform->modulename)) {
361                 print_error('moduledisable', '', '', $fromform->modulename);
362             }
364             // first add course_module record because we need the context
365             $newcm = new object();
366             $newcm->course           = $course->id;
367             $newcm->module           = $fromform->module;
368             $newcm->instance         = 0; // not known yet, will be updated later (this is similar to restore code)
369             $newcm->visible          = $fromform->visible;
370             $newcm->groupmode        = $fromform->groupmode;
371             $newcm->groupingid       = $fromform->groupingid;
372             $newcm->groupmembersonly = $fromform->groupmembersonly;
373             $completion = new completion_info($course);
374             if ($completion->is_enabled()) {
375                 $newcm->completion                = $fromform->completion;
376                 $newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
377                 $newcm->completionview            = $fromform->completionview;
378                 $newcm->completionexpected        = $fromform->completionexpected;
379             }
380             if(!empty($CFG->enableavailability)) {
381                 $newcm->availablefrom             = $fromform->availablefrom;
382                 $newcm->availableuntil            = $fromform->availableuntil;
383                 // The form time is midnight, but because we want it to be 
384                 // inclusive, set it to 23:59:59 on that day.
385                 if ($newcm->availableuntil) {
386                     $newcm->availableuntil = strtotime('23:59:59', 
387                         $newcm->availableuntil);
388                 }
389                 $newcm->showavailability          = $fromform->showavailability;
390             }
392             if (!$fromform->coursemodule = add_course_module($newcm)) {
393                 print_error('cannotaddcoursemodule');
394             }
396             $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
397     
398             // update embedded links and save files
399             if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
400                 $fromform->intro = file_save_draft_area_files($fromform->introeditor['itemid'], $modcontext->id,
401                                                               $fromform->modulename.'_intro', 0,
402                                                               array('subdirs'=>true), $fromform->introeditor['text']);
403                 $fromform->introformat = $fromform->introeditor['format'];
404                 unset($fromform->introeditor);
405             }
407             $returnfromfunc = $addinstancefunction($fromform, $mform);
409             if (!$returnfromfunc or !is_number($returnfromfunc)) {
410                 // undo everything we can
411                 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
412                 $fs = get_file_storage();
413                 $fs->delete_area_files($modcontext->id);
414                 delete_context(CONTEXT_MODULE, $fromform->coursemodule);
415                 $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule));
417                 if (!is_number($returnfromfunc)) {
418                     print_error('invalidfunction', '', 'view.php?id=$course->id');
419                 } else {
420                     print_error('cannotaddnewmodule', '', "view.php?id=$course->id", $fromform->modulename);
421                 }
422             }
424             $fromform->instance = $returnfromfunc;
426             if (!$DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule))) {
427                 print_error('cannotaddcoursemodule');
428             }
430             // course_modules and course_sections each contain a reference
431             // to each other, so we have to update one of them twice.
432             if (!$sectionid = add_mod_to_section($fromform)) {
433                 print_error('cannotaddcmtosection');
434             }
436             if (!$DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule))) {
437                 print_error('cannotupdatecm');
438             }
440             // make sure visibility is set correctly (in particular in calendar)
441             set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
443             if (isset($fromform->cmidnumber)) { //label
444                 // set cm idnumber - uniqueness is already verified by form validation
445                 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
446             }
448             // Set up conditions
449             if ($CFG->enableavailability) {
450                 condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
451             }
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);
459         } else {
460             print_error('invaliddata');
461         }
463         // sync idnumber with grade_item
464         if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
465                      'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
466             if ($grade_item->idnumber != $fromform->cmidnumber) {
467                 $grade_item->idnumber = $fromform->cmidnumber;
468                 $grade_item->update();
469             }
470         }
472         $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
473                                              'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
475         // create parent category if requested and move to correct parent category
476         if ($items and isset($fromform->gradecat)) {
477             if ($fromform->gradecat == -1) {
478                 $grade_category = new grade_category();
479                 $grade_category->courseid = $course->id;
480                 $grade_category->fullname = $fromform->name;
481                 $grade_category->insert();
482                 if ($grade_item) {
483                     $parent = $grade_item->get_parent_category();
484                     $grade_category->set_parent($parent->id);
485                 }
486                 $fromform->gradecat = $grade_category->id;
487             }
488             foreach ($items as $itemid=>$unused) {
489                 $items[$itemid]->set_parent($fromform->gradecat);
490                 if ($itemid == $grade_item->id) {
491                     // use updated grade_item
492                     $grade_item = $items[$itemid];
493                 }
494             }
495         }
497         // add outcomes if requested
498         if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
499             $grade_items = array();
501             // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
502             $max_itemnumber = 999;
503             if ($items) {
504                 foreach($items as $item) {
505                     if ($item->itemnumber > $max_itemnumber) {
506                         $max_itemnumber = $item->itemnumber;
507                     }
508                 }
509             }
511             foreach($outcomes as $outcome) {
512                 $elname = 'outcome_'.$outcome->id;
514                 if (array_key_exists($elname, $fromform) and $fromform->$elname) {
515                     // so we have a request for new outcome grade item?
516                     if ($items) {
517                         foreach($items as $item) {
518                             if ($item->outcomeid == $outcome->id) {
519                                 //outcome aready exists
520                                 continue 2;
521                             }
522                         }
523                     }
525                     $max_itemnumber++;
527                     $outcome_item = new grade_item();
528                     $outcome_item->courseid     = $course->id;
529                     $outcome_item->itemtype     = 'mod';
530                     $outcome_item->itemmodule   = $fromform->modulename;
531                     $outcome_item->iteminstance = $fromform->instance;
532                     $outcome_item->itemnumber   = $max_itemnumber;
533                     $outcome_item->itemname     = $outcome->fullname;
534                     $outcome_item->outcomeid    = $outcome->id;
535                     $outcome_item->gradetype    = GRADE_TYPE_SCALE;
536                     $outcome_item->scaleid      = $outcome->scaleid;
537                     $outcome_item->insert();
539                     // move the new outcome into correct category and fix sortorder if needed
540                     if ($grade_item) {
541                         $outcome_item->set_parent($grade_item->categoryid);
542                         $outcome_item->move_after_sortorder($grade_item->sortorder);
544                     } else if (isset($fromform->gradecat)) {
545                         $outcome_item->set_parent($fromform->gradecat);
546                     }
547                 }
548             }
549         }
551         rebuild_course_cache($course->id);
552         grade_regrade_final_grades($course->id);
554         if (isset($fromform->submitbutton)) { 
555             redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
556         } else {
557             redirect("$CFG->wwwroot/course/view.php?id=$course->id");
558         }
559         exit;
561     } else {
562         if (!empty($cm->id)) {
563             $context = get_context_instance(CONTEXT_MODULE, $cm->id);
564         } else {
565             $context = get_context_instance(CONTEXT_COURSE, $course->id);
566         }
567         require_capability('moodle/course:manageactivities', $context);
569         $streditinga = get_string('editinga', 'moodle', $fullmodulename);
570         $strmodulenameplural = get_string('modulenameplural', $module->name);
572         $navlinks = array();
573         $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
574         if ($navlinksinstancename) {
575             $navlinks[] = $navlinksinstancename;
576         }
577         $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title');
579         $navigation = build_navigation($navlinks);
581         print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false);
583         if (!empty($cm->id)) {
584             $context = get_context_instance(CONTEXT_MODULE, $cm->id);
585             $overridableroles = get_overridable_roles($context);
586             $assignableroles  = get_assignable_roles($context);
587             $currenttab = 'update';
588             require($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
589         }
590         $icon = '<img src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt=""/>';
592         print_heading_with_help($pageheading, 'mods', $module->name, $icon);
593         $mform->display();
594         print_footer($course);
595     }