MDL-29877 core_grade:add some upgrade code to fix broken grade items introduced by...
[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
38 $sectionreturn = optional_param('sr', null, PARAM_INT);
40 $url = new moodle_url('/course/modedit.php');
41 $url->param('sr', $sectionreturn);
42 if (!empty($return)) {
43     $url->param('return', $return);
44 }
46 if (!empty($add)) {
47     $section = required_param('section', PARAM_INT);
48     $course  = required_param('course', PARAM_INT);
50     $url->param('add', $add);
51     $url->param('section', $section);
52     $url->param('course', $course);
53     $PAGE->set_url($url);
55     $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
56     $module = $DB->get_record('modules', array('name'=>$add), '*', MUST_EXIST);
58     require_login($course);
59     $context = get_context_instance(CONTEXT_COURSE, $course->id);
60     require_capability('moodle/course:manageactivities', $context);
62     $cw = get_course_section($section, $course->id);
64     if (!course_allowed_module($course, $module->name)) {
65         print_error('moduledisable');
66     }
68     $cm = null;
70     $data = new stdClass();
71     $data->section          = $section;  // The section number itself - relative!!! (section column in course_sections)
72     $data->visible          = $cw->visible;
73     $data->course           = $course->id;
74     $data->module           = $module->id;
75     $data->modulename       = $module->name;
76     $data->groupmode        = $course->groupmode;
77     $data->groupingid       = $course->defaultgroupingid;
78     $data->groupmembersonly = 0;
79     $data->id               = '';
80     $data->instance         = '';
81     $data->coursemodule     = '';
82     $data->add              = $add;
83     $data->return           = 0; //must be false if this is an add, go back to course view on cancel
84     $data->sr               = $sectionreturn;
86     if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
87         $draftid_editor = file_get_submitted_draft_itemid('introeditor');
88         file_prepare_draft_area($draftid_editor, null, null, null, null);
89         $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
90     }
92     if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
93             and has_capability('moodle/grade:managegradingforms', $context)) {
94         require_once($CFG->dirroot.'/grade/grading/lib.php');
96         $data->_advancedgradingdata['methods'] = grading_manager::available_methods();
97         $areas = grading_manager::available_areas('mod_'.$module->name);
99         foreach ($areas as $areaname => $areatitle) {
100             $data->_advancedgradingdata['areas'][$areaname] = array(
101                 'title'  => $areatitle,
102                 'method' => '',
103             );
104             $formfield = 'advancedgradingmethod_'.$areaname;
105             $data->{$formfield} = '';
106         }
107     }
109     if (!empty($type)) { //TODO: hopefully will be removed in 2.0
110         $data->type = $type;
111     }
113     $sectionname = get_section_name($course, $cw);
114     $fullmodulename = get_string('modulename', $module->name);
116     if ($data->section && $course->format != 'site') {
117         $heading = new stdClass();
118         $heading->what = $fullmodulename;
119         $heading->to   = $sectionname;
120         $pageheading = get_string('addinganewto', 'moodle', $heading);
121     } else {
122         $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
123     }
125 } else if (!empty($update)) {
127     $url->param('update', $update);
128     $PAGE->set_url($url);
130     $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
131     $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
133     require_login($course, false, $cm); // needed to setup proper $COURSE
134     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
135     require_capability('moodle/course:manageactivities', $context);
137     $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
138     $data = $data = $DB->get_record($module->name, array('id'=>$cm->instance), '*', MUST_EXIST);
139     $cw = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
141     $data->coursemodule       = $cm->id;
142     $data->section            = $cw->section;  // The section number itself - relative!!! (section column in course_sections)
143     $data->visible            = $cm->visible; //??  $cw->visible ? $cm->visible : 0; // section hiding overrides
144     $data->cmidnumber         = $cm->idnumber;          // The cm IDnumber
145     $data->groupmode          = groups_get_activity_groupmode($cm); // locked later if forced
146     $data->groupingid         = $cm->groupingid;
147     $data->groupmembersonly   = $cm->groupmembersonly;
148     $data->course             = $course->id;
149     $data->module             = $module->id;
150     $data->modulename         = $module->name;
151     $data->instance           = $cm->instance;
152     $data->return             = $return;
153     $data->sr                 = $sectionreturn;
154     $data->update             = $update;
155     $data->completion         = $cm->completion;
156     $data->completionview     = $cm->completionview;
157     $data->completionexpected = $cm->completionexpected;
158     $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
159     $data->showdescription    = $cm->showdescription;
160     if (!empty($CFG->enableavailability)) {
161         $data->availablefrom      = $cm->availablefrom;
162         $data->availableuntil     = $cm->availableuntil;
163         $data->showavailability   = $cm->showavailability;
164     }
166     if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
167         $draftid_editor = file_get_submitted_draft_itemid('introeditor');
168         $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
169         $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
170     }
172     if (plugin_supports('mod', $data->modulename, FEATURE_ADVANCED_GRADING, false)
173             and has_capability('moodle/grade:managegradingforms', $context)) {
174         require_once($CFG->dirroot.'/grade/grading/lib.php');
175         $gradingman = get_grading_manager($context, 'mod_'.$data->modulename);
176         $data->_advancedgradingdata['methods'] = $gradingman->get_available_methods();
177         $areas = $gradingman->get_available_areas();
179         foreach ($areas as $areaname => $areatitle) {
180             $gradingman->set_area($areaname);
181             $method = $gradingman->get_active_method();
182             $data->_advancedgradingdata['areas'][$areaname] = array(
183                 'title'  => $areatitle,
184                 'method' => $method,
185             );
186             $formfield = 'advancedgradingmethod_'.$areaname;
187             $data->{$formfield} = $method;
188         }
189     }
191     if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
192                                              'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
193         // add existing outcomes
194         foreach ($items as $item) {
195             if (!empty($item->outcomeid)) {
196                 $data->{'outcome_'.$item->outcomeid} = 1;
197             }
198         }
200         // set category if present
201         $gradecat = false;
202         foreach ($items as $item) {
203             if ($gradecat === false) {
204                 $gradecat = $item->categoryid;
205                 continue;
206             }
207             if ($gradecat != $item->categoryid) {
208                 //mixed categories
209                 $gradecat = false;
210                 break;
211             }
212         }
213         if ($gradecat !== false) {
214             // do not set if mixed categories present
215             $data->gradecat = $gradecat;
216         }
217     }
219     $sectionname = get_section_name($course, $cw);
220     $fullmodulename = get_string('modulename', $module->name);
222     if ($data->section && $course->format != 'site') {
223         $heading = new stdClass();
224         $heading->what = $fullmodulename;
225         $heading->in   = $sectionname;
226         $pageheading = get_string('updatingain', 'moodle', $heading);
227     } else {
228         $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
229     }
231 } else {
232     require_login();
233     print_error('invalidaction');
236 $pagepath = 'mod-' . $module->name . '-';
237 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
238     $pagepath .= $type;
239 } else {
240     $pagepath .= 'mod';
242 $PAGE->set_pagetype($pagepath);
243 $PAGE->set_pagelayout('admin');
245 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
246 if (file_exists($modmoodleform)) {
247     require_once($modmoodleform);
248 } else {
249     print_error('noformdesc');
252 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
253 if (file_exists($modlib)) {
254     include_once($modlib);
255 } else {
256     print_error('modulemissingcode', '', '', $modlib);
259 $mformclassname = 'mod_'.$module->name.'_mod_form';
260 $mform = new $mformclassname($data, $cw->section, $cm, $course);
261 $mform->set_data($data);
263 if ($mform->is_cancelled()) {
264     if ($return && !empty($cm->id)) {
265         redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
266     } else {
267         redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
268     }
269 } else if ($fromform = $mform->get_data()) {
270     if (empty($fromform->coursemodule)) {
271         // Add
272         $cm = null;
273         $course = $DB->get_record('course', array('id'=>$fromform->course), '*', MUST_EXIST);
274         $fromform->instance     = '';
275         $fromform->coursemodule = '';
276     } else {
277         // Update
278         $cm = get_coursemodule_from_id('', $fromform->coursemodule, 0, false, MUST_EXIST);
279         $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
280         $fromform->instance     = $cm->instance;
281         $fromform->coursemodule = $cm->id;
282     }
284     if (!empty($fromform->coursemodule)) {
285         $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
286     } else {
287         $context = get_context_instance(CONTEXT_COURSE, $course->id);
288     }
290     $fromform->course = $course->id;
291     $fromform->modulename = clean_param($fromform->modulename, PARAM_PLUGIN);  // For safety
293     $addinstancefunction    = $fromform->modulename."_add_instance";
294     $updateinstancefunction = $fromform->modulename."_update_instance";
296     if (!isset($fromform->groupingid)) {
297         $fromform->groupingid = 0;
298     }
300     if (!isset($fromform->groupmembersonly)) {
301         $fromform->groupmembersonly = 0;
302     }
304     if (!isset($fromform->name)) { //label
305         $fromform->name = $fromform->modulename;
306     }
308     if (!isset($fromform->completion)) {
309         $fromform->completion = COMPLETION_DISABLED;
310     }
311     if (!isset($fromform->completionview)) {
312         $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
313     }
315     // Convert the 'use grade' checkbox into a grade-item number: 0 if
316     // checked, null if not
317     if (isset($fromform->completionusegrade) && $fromform->completionusegrade) {
318         $fromform->completiongradeitemnumber = 0;
319     } else {
320         $fromform->completiongradeitemnumber = null;
321     }
323     // the type of event to trigger (mod_created/mod_updated)
324     $eventname = '';
326     if (!empty($fromform->update)) {
328         if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
329             $fromform->groupmode = $cm->groupmode; // keep original
330         }
332         // update course module first
333         $cm->groupmode        = $fromform->groupmode;
334         $cm->groupingid       = $fromform->groupingid;
335         $cm->groupmembersonly = $fromform->groupmembersonly;
337         $completion = new completion_info($course);
338         if ($completion->is_enabled()) {
339             // Update completion settings
340             $cm->completion                = $fromform->completion;
341             $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
342             $cm->completionview            = $fromform->completionview;
343             $cm->completionexpected        = $fromform->completionexpected;
344         }
345         if (!empty($CFG->enableavailability)) {
346             $cm->availablefrom             = $fromform->availablefrom;
347             $cm->availableuntil            = $fromform->availableuntil;
348             $cm->showavailability          = $fromform->showavailability;
349             condition_info::update_cm_from_form($cm,$fromform,true);
350         }
351         if (isset($fromform->showdescription)) {
352             $cm->showdescription = $fromform->showdescription;
353         } else {
354             $cm->showdescription = 0;
355         }
357         $DB->update_record('course_modules', $cm);
359         $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
361         // update embedded links and save files
362         if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
363             $fromform->intro = file_save_draft_area_files($fromform->introeditor['itemid'], $modcontext->id,
364                                                           'mod_'.$fromform->modulename, 'intro', 0,
365                                                           array('subdirs'=>true), $fromform->introeditor['text']);
366             $fromform->introformat = $fromform->introeditor['format'];
367             unset($fromform->introeditor);
368         }
370         if (!$updateinstancefunction($fromform, $mform)) {
371             print_error('cannotupdatemod', '', course_get_url($course, $cw->section), $fromform->modulename);
372         }
374         // make sure visibility is set correctly (in particular in calendar)
375         if (has_capability('moodle/course:activityvisibility', $modcontext)) {
376             set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
377         }
379         if (isset($fromform->cmidnumber)) { //label
380             // set cm idnumber - uniqueness is already verified by form validation
381             set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
382         }
384         // Now that module is fully updated, also update completion data if
385         // required (this will wipe all user completion data and recalculate it)
386         if ($completion->is_enabled() && !empty($fromform->completionunlocked)) {
387             $completion->reset_all_state($cm);
388         }
390         $eventname = 'mod_updated';
392         add_to_log($course->id, "course", "update mod",
393                    "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
394                    "$fromform->modulename $fromform->instance");
395         add_to_log($course->id, $fromform->modulename, "update",
396                    "view.php?id=$fromform->coursemodule",
397                    "$fromform->instance", $fromform->coursemodule);
399     } else if (!empty($fromform->add)) {
401         if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
402             $fromform->groupmode = 0; // do not set groupmode
403         }
405         if (!course_allowed_module($course, $fromform->modulename)) {
406             print_error('moduledisable', '', '', $fromform->modulename);
407         }
409         // first add course_module record because we need the context
410         $newcm = new stdClass();
411         $newcm->course           = $course->id;
412         $newcm->module           = $fromform->module;
413         $newcm->instance         = 0; // not known yet, will be updated later (this is similar to restore code)
414         $newcm->visible          = $fromform->visible;
415         $newcm->groupmode        = $fromform->groupmode;
416         $newcm->groupingid       = $fromform->groupingid;
417         $newcm->groupmembersonly = $fromform->groupmembersonly;
418         $completion = new completion_info($course);
419         if ($completion->is_enabled()) {
420             $newcm->completion                = $fromform->completion;
421             $newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
422             $newcm->completionview            = $fromform->completionview;
423             $newcm->completionexpected        = $fromform->completionexpected;
424         }
425         if(!empty($CFG->enableavailability)) {
426             $newcm->availablefrom             = $fromform->availablefrom;
427             $newcm->availableuntil            = $fromform->availableuntil;
428             $newcm->showavailability          = $fromform->showavailability;
429         }
430         if (isset($fromform->showdescription)) {
431             $newcm->showdescription = $fromform->showdescription;
432         } else {
433             $newcm->showdescription = 0;
434         }
436         if (!$fromform->coursemodule = add_course_module($newcm)) {
437             print_error('cannotaddcoursemodule');
438         }
440         if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
441             $introeditor = $fromform->introeditor;
442             unset($fromform->introeditor);
443             $fromform->intro       = $introeditor['text'];
444             $fromform->introformat = $introeditor['format'];
445         }
447         $returnfromfunc = $addinstancefunction($fromform, $mform);
449         if (!$returnfromfunc or !is_number($returnfromfunc)) {
450             // undo everything we can
451             $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
452             delete_context(CONTEXT_MODULE, $fromform->coursemodule);
453             $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule));
455             if (!is_number($returnfromfunc)) {
456                 print_error('invalidfunction', '', course_get_url($course, $cw->section));
457             } else {
458                 print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $fromform->modulename);
459             }
460         }
462         $fromform->instance = $returnfromfunc;
464         $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule));
466         // update embedded links and save files
467         $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
468         if (!empty($introeditor)) {
469             $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
470                                                           'mod_'.$fromform->modulename, 'intro', 0,
471                                                           array('subdirs'=>true), $introeditor['text']);
472             $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id'=>$fromform->instance));
473         }
475         // course_modules and course_sections each contain a reference
476         // to each other, so we have to update one of them twice.
477         $sectionid = add_mod_to_section($fromform);
479         $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule));
481         // make sure visibility is set correctly (in particular in calendar)
482         // note: allow them to set it even without moodle/course:activityvisibility
483         set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
484         $DB->set_field('course_modules', 'visibleold', 1, array('id' => $fromform->coursemodule));
486         if (isset($fromform->cmidnumber)) { //label
487             // set cm idnumber - uniqueness is already verified by form validation
488             set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
489         }
491         // Set up conditions
492         if ($CFG->enableavailability) {
493             condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
494         }
496         $eventname = 'mod_created';
498         add_to_log($course->id, "course", "add mod",
499                    "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
500                    "$fromform->modulename $fromform->instance");
501         add_to_log($course->id, $fromform->modulename, "add",
502                    "view.php?id=$fromform->coursemodule",
503                    "$fromform->instance", $fromform->coursemodule);
504     } else {
505         print_error('invaliddata');
506     }
508     // Trigger mod_created/mod_updated event with information about this module.
509     $eventdata = new stdClass();
510     $eventdata->modulename = $fromform->modulename;
511     $eventdata->name       = $fromform->name;
512     $eventdata->cmid       = $fromform->coursemodule;
513     $eventdata->courseid   = $course->id;
514     $eventdata->userid     = $USER->id;
515     events_trigger($eventname, $eventdata);
517     // sync idnumber with grade_item
518     if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
519                  'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
520         if ($grade_item->idnumber != $fromform->cmidnumber) {
521             $grade_item->idnumber = $fromform->cmidnumber;
522             $grade_item->update();
523         }
524     }
526     $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
527                                          'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
529     // create parent category if requested and move to correct parent category
530     if ($items and isset($fromform->gradecat)) {
531         if ($fromform->gradecat == -1) {
532             $grade_category = new grade_category();
533             $grade_category->courseid = $course->id;
534             $grade_category->fullname = $fromform->name;
535             $grade_category->insert();
536             if ($grade_item) {
537                 $parent = $grade_item->get_parent_category();
538                 $grade_category->set_parent($parent->id);
539             }
540             $fromform->gradecat = $grade_category->id;
541         }
542         foreach ($items as $itemid=>$unused) {
543             $items[$itemid]->set_parent($fromform->gradecat);
544             if ($itemid == $grade_item->id) {
545                 // use updated grade_item
546                 $grade_item = $items[$itemid];
547             }
548         }
549     }
551     // add outcomes if requested
552     if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
553         $grade_items = array();
555         // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
556         $max_itemnumber = 999;
557         if ($items) {
558             foreach($items as $item) {
559                 if ($item->itemnumber > $max_itemnumber) {
560                     $max_itemnumber = $item->itemnumber;
561                 }
562             }
563         }
565         foreach($outcomes as $outcome) {
566             $elname = 'outcome_'.$outcome->id;
568             if (property_exists($fromform, $elname) and $fromform->$elname) {
569                 // so we have a request for new outcome grade item?
570                 if ($items) {
571                     foreach($items as $item) {
572                         if ($item->outcomeid == $outcome->id) {
573                             //outcome aready exists
574                             continue 2;
575                         }
576                     }
577                 }
579                 $max_itemnumber++;
581                 $outcome_item = new grade_item();
582                 $outcome_item->courseid     = $course->id;
583                 $outcome_item->itemtype     = 'mod';
584                 $outcome_item->itemmodule   = $fromform->modulename;
585                 $outcome_item->iteminstance = $fromform->instance;
586                 $outcome_item->itemnumber   = $max_itemnumber;
587                 $outcome_item->itemname     = $outcome->fullname;
588                 $outcome_item->outcomeid    = $outcome->id;
589                 $outcome_item->gradetype    = GRADE_TYPE_SCALE;
590                 $outcome_item->scaleid      = $outcome->scaleid;
591                 $outcome_item->insert();
593                 // move the new outcome into correct category and fix sortorder if needed
594                 if ($grade_item) {
595                     $outcome_item->set_parent($grade_item->categoryid);
596                     $outcome_item->move_after_sortorder($grade_item->sortorder);
598                 } else if (isset($fromform->gradecat)) {
599                     $outcome_item->set_parent($fromform->gradecat);
600                 }
601             }
602         }
603     }
605     if (plugin_supports('mod', $fromform->modulename, FEATURE_ADVANCED_GRADING, false)
606             and has_capability('moodle/grade:managegradingforms', $modcontext)) {
607         require_once($CFG->dirroot.'/grade/grading/lib.php');
608         $gradingman = get_grading_manager($modcontext, 'mod_'.$fromform->modulename);
609         $showgradingmanagement = false;
610         foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
611             $formfield = 'advancedgradingmethod_'.$areaname;
612             if (isset($fromform->{$formfield})) {
613                 $gradingman->set_area($areaname);
614                 $methodchanged = $gradingman->set_active_method($fromform->{$formfield});
615                 if (empty($fromform->{$formfield})) {
616                     // going back to the simple direct grading is not a reason
617                     // to open the management screen
618                     $methodchanged = false;
619                 }
620                 $showgradingmanagement = $showgradingmanagement || $methodchanged;
621             }
622         }
623     }
625     rebuild_course_cache($course->id);
626     grade_regrade_final_grades($course->id);
627     plagiarism_save_form_elements($fromform); //save plagiarism settings
629     if (isset($fromform->submitbutton)) {
630         if (empty($showgradingmanagement)) {
631             redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
632         } else {
633             $returnurl = new moodle_url("/mod/$module->name/view.php", array('id' => $fromform->coursemodule));
634             redirect($gradingman->get_management_url($returnurl));
635         }
636     } else {
637         redirect(course_get_url($course, $cw->section, array('sr' => $sectionreturn)));
638     }
639     exit;
641 } else {
643     $streditinga = get_string('editinga', 'moodle', $fullmodulename);
644     $strmodulenameplural = get_string('modulenameplural', $module->name);
646     if (!empty($cm->id)) {
647         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
648     } else {
649         $context = get_context_instance(CONTEXT_COURSE, $course->id);
650     }
652     $PAGE->set_heading($course->fullname);
653     $PAGE->set_title($streditinga);
654     $PAGE->set_cacheable(false);
655     echo $OUTPUT->header();
657     if (get_string_manager()->string_exists('modulename_help', $module->name)) {
658         echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
659     } else {
660         echo $OUTPUT->heading_with_help($pageheading, '', $module->name, 'icon');
661     }
663     $mform->display();
665     echo $OUTPUT->footer();