MDL-25478 - stop fatal errors adding blocks on the module edit page
[moodle.git] / course / modedit.php
CommitLineData
aa54ed7b 1<?php
2
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/>.
17
18/**
ba9dc077
SH
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*/
25
26require_once("../config.php");
27require_once("lib.php");
28require_once($CFG->libdir.'/filelib.php');
29require_once($CFG->libdir.'/gradelib.php');
30require_once($CFG->libdir.'/completionlib.php');
31require_once($CFG->libdir.'/conditionlib.php');
bce59524 32require_once($CFG->libdir.'/plagiarismlib.php');
ba9dc077
SH
33
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
39$url = new moodle_url('/course/modedit.php');
40if (!empty($return)) {
41 $url->param('return', $return);
42}
43
44if (!empty($add)) {
ba9dc077
SH
45 $section = required_param('section', PARAM_INT);
46 $course = required_param('course', PARAM_INT);
2b9e3bac 47
ebb32067
SV
48 $url->param('add', $add);
49 $url->param('section', $section);
50 $url->param('course', $course);
51 $PAGE->set_url($url);
52
ba9dc077
SH
53 $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
54 $module = $DB->get_record('modules', array('name'=>$add), '*', MUST_EXIST);
264867fd 55
ba9dc077
SH
56 require_login($course);
57 $context = get_context_instance(CONTEXT_COURSE, $course->id);
58 require_capability('moodle/course:manageactivities', $context);
264867fd 59
ba9dc077 60 $cw = get_course_section($section, $course->id);
264867fd 61
ba9dc077
SH
62 if (!course_allowed_module($course, $module->id)) {
63 print_error('moduledisable');
64 }
52273766 65
ba9dc077
SH
66 $cm = null;
67
fbaea88f 68 $data = new stdClass();
ba9dc077
SH
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;
77 $data->id = '';
78 $data->instance = '';
79 $data->coursemodule = '';
80 $data->add = $add;
81 $data->return = 0; //must be false if this is an add, go back to course view on cancel
82
83 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
84 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
64f93798 85 file_prepare_draft_area($draftid_editor, null, null, null, null);
ba9dc077
SH
86 $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
87 }
c18269c7 88
ba9dc077
SH
89 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
90 $data->type = $type;
91 }
264867fd 92
7487c856 93 $sectionname = get_section_name($course, $cw);
ba9dc077 94 $fullmodulename = get_string('modulename', $module->name);
264867fd 95
ba9dc077 96 if ($data->section && $course->format != 'site') {
fbaea88f 97 $heading = new stdClass();
ba9dc077 98 $heading->what = $fullmodulename;
7487c856 99 $heading->to = $sectionname;
ba9dc077
SH
100 $pageheading = get_string('addinganewto', 'moodle', $heading);
101 } else {
102 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
103 }
264867fd 104
ba9dc077 105} else if (!empty($update)) {
cb141e5a 106
ba9dc077
SH
107 $url->param('update', $update);
108 $PAGE->set_url($url);
264867fd 109
ba9dc077
SH
110 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
111 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
112
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);
116
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);
120
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;
142 }
aa54ed7b 143
ba9dc077
SH
144 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
145 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
64f93798 146 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
ba9dc077
SH
147 $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
148 }
dc5c2bd9 149
ba9dc077
SH
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;
71ee4471 156 }
ba9dc077 157 }
4b86bb08 158
ba9dc077
SH
159 // set category if present
160 $gradecat = false;
161 foreach ($items as $item) {
162 if ($gradecat === false) {
163 $gradecat = $item->categoryid;
164 continue;
4b86bb08 165 }
ba9dc077
SH
166 if ($gradecat != $item->categoryid) {
167 //mixed categories
168 $gradecat = false;
169 break;
4b86bb08 170 }
71ee4471 171 }
ba9dc077
SH
172 if ($gradecat !== false) {
173 // do not set if mixed categories present
174 $data->gradecat = $gradecat;
264867fd 175 }
ba9dc077 176 }
61defed9 177
7487c856 178 $sectionname = get_section_name($course, $cw);
ba9dc077 179 $fullmodulename = get_string('modulename', $module->name);
61defed9 180
ba9dc077 181 if ($data->section && $course->format != 'site') {
fbaea88f 182 $heading = new stdClass();
ba9dc077 183 $heading->what = $fullmodulename;
7487c856 184 $heading->in = $sectionname;
ba9dc077 185 $pageheading = get_string('updatingain', 'moodle', $heading);
52273766 186 } else {
ba9dc077 187 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
264867fd 188 }
189
ba9dc077
SH
190} else {
191 require_login();
192 print_error('invalidaction');
193}
194
195$pagepath = 'mod-' . $module->name . '-';
196if (!empty($type)) { //TODO: hopefully will be removed in 2.0
197 $pagepath .= $type;
198} else {
199 $pagepath .= 'mod';
200}
201$PAGE->set_pagetype($pagepath);
ae2fa74f 202$PAGE->set_pagelayout('admin');
ba9dc077
SH
203
204$modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
205if (file_exists($modmoodleform)) {
206 require_once($modmoodleform);
207} else {
208 print_error('noformdesc');
209}
210
211$modlib = "$CFG->dirroot/mod/$module->name/lib.php";
212if (file_exists($modlib)) {
213 include_once($modlib);
214} else {
215 print_error('modulemissingcode', '', '', $modlib);
216}
217
218$mformclassname = 'mod_'.$module->name.'_mod_form';
219$mform = new $mformclassname($data, $cw->section, $cm, $course);
220$mform->set_data($data);
221
222if ($mform->is_cancelled()) {
223 if ($return && !empty($cm->id)) {
224 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
52273766 225 } else {
ba9dc077
SH
226 redirect("$CFG->wwwroot/course/view.php?id=$course->id#section-".$cw->section);
227 }
228} else if ($fromform = $mform->get_data()) {
229 if (empty($fromform->coursemodule)) {
230 // Add
231 $cm = null;
232 $course = $DB->get_record('course', array('id'=>$fromform->course), '*', MUST_EXIST);
233 $fromform->instance = '';
234 $fromform->coursemodule = '';
54632b02 235 } else {
ba9dc077
SH
236 // Update
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;
264867fd 241 }
3cd7940b 242
ba9dc077
SH
243 if (!empty($fromform->coursemodule)) {
244 $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
3cd7940b 245 } else {
ba9dc077 246 $context = get_context_instance(CONTEXT_COURSE, $course->id);
3cd7940b 247 }
248
ba9dc077
SH
249 $fromform->course = $course->id;
250 $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety
d857e8b6 251
ba9dc077
SH
252 $addinstancefunction = $fromform->modulename."_add_instance";
253 $updateinstancefunction = $fromform->modulename."_update_instance";
264867fd 254
ba9dc077
SH
255 if (!isset($fromform->groupingid)) {
256 $fromform->groupingid = 0;
257 }
264867fd 258
ba9dc077
SH
259 if (!isset($fromform->groupmembersonly)) {
260 $fromform->groupmembersonly = 0;
261 }
264867fd 262
ba9dc077
SH
263 if (!isset($fromform->name)) { //label
264 $fromform->name = $fromform->modulename;
265 }
264867fd 266
ba9dc077
SH
267 if (!isset($fromform->completion)) {
268 $fromform->completion = COMPLETION_DISABLED;
269 }
270 if (!isset($fromform->completionview)) {
271 $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
272 }
264867fd 273
ba9dc077
SH
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;
278 } else {
279 $fromform->completiongradeitemnumber = null;
280 }
e04ff2d5 281
ba9dc077 282 if (!empty($fromform->update)) {
aa54ed7b 283
ba9dc077
SH
284 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
285 $fromform->groupmode = $cm->groupmode; // keep original
4e781c7b 286 }
287
ba9dc077
SH
288 // update course module first
289 $cm->groupmode = $fromform->groupmode;
290 $cm->groupingid = $fromform->groupingid;
291 $cm->groupmembersonly = $fromform->groupmembersonly;
21090e44 292
ba9dc077
SH
293 $completion = new completion_info($course);
294 if ($completion->is_enabled()) {
91d0bb0a 295 // Update completion settings
ba9dc077
SH
296 $cm->completion = $fromform->completion;
297 $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
298 $cm->completionview = $fromform->completionview;
299 $cm->completionexpected = $fromform->completionexpected;
300 }
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);
dc5c2bd9 309 }
ba9dc077
SH
310 $cm->showavailability = $fromform->showavailability;
311 condition_info::update_cm_from_form($cm,$fromform,true);
312 }
313
314 $DB->update_record('course_modules', $cm);
315
316 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
317
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,
64f93798 321 'mod_'.$fromform->modulename, 'intro', 0,
ba9dc077
SH
322 array('subdirs'=>true), $fromform->introeditor['text']);
323 $fromform->introformat = $fromform->introeditor['format'];
324 unset($fromform->introeditor);
325 }
326
327 if (!$updateinstancefunction($fromform, $mform)) {
328 print_error('cannotupdatemod', '', 'view.php?id=$course->id', $fromform->modulename);
329 }
330
331 // make sure visibility is set correctly (in particular in calendar)
332 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
333
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);
337 }
9645a484 338
91d0bb0a
SM
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);
343 }
344
9645a484 345 // Trigger mod_updated event with information about this module.
fbaea88f 346 $eventdata = new stdClass();
9645a484
MD
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);
ba9dc077
SH
353
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);
360
361 } else if (!empty($fromform->add)) {
362
363 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
364 $fromform->groupmode = 0; // do not set groupmode
365 }
366
367 if (!course_allowed_module($course, $fromform->modulename)) {
368 print_error('moduledisable', '', '', $fromform->modulename);
369 }
370
371 // first add course_module record because we need the context
fbaea88f 372 $newcm = new stdClass();
ba9dc077
SH
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;
386 }
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);
d857e8b6 395 }
ba9dc077
SH
396 $newcm->showavailability = $fromform->showavailability;
397 }
d857e8b6 398
ba9dc077
SH
399 if (!$fromform->coursemodule = add_course_module($newcm)) {
400 print_error('cannotaddcoursemodule');
401 }
21090e44 402
ff7670e7 403 if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
ba9dc077
SH
404 $introeditor = $fromform->introeditor;
405 unset($fromform->introeditor);
406 $fromform->intro = $introeditor['text'];
407 $fromform->introformat = $introeditor['format'];
408 }
264867fd 409
ba9dc077 410 $returnfromfunc = $addinstancefunction($fromform, $mform);
d857e8b6 411
ba9dc077
SH
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));
d857e8b6 417
ba9dc077
SH
418 if (!is_number($returnfromfunc)) {
419 print_error('invalidfunction', '', 'view.php?id=$course->id');
420 } else {
421 print_error('cannotaddnewmodule', '', "view.php?id=$course->id", $fromform->modulename);
b1a2d9d2 422 }
ba9dc077 423 }
dc5c2bd9 424
ba9dc077 425 $fromform->instance = $returnfromfunc;
d857e8b6 426
ba9dc077 427 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule));
d857e8b6 428
ba9dc077
SH
429 // update embedded links and save files
430 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
ff7670e7 431 if (!empty($introeditor)) {
ba9dc077 432 $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
64f93798 433 'mod_'.$fromform->modulename, 'intro', 0,
ba9dc077
SH
434 array('subdirs'=>true), $introeditor['text']);
435 $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id'=>$fromform->instance));
436 }
264867fd 437
ba9dc077
SH
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);
264867fd 441
ba9dc077 442 $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule));
d857e8b6 443
ba9dc077
SH
444 // make sure visibility is set correctly (in particular in calendar)
445 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
aa54ed7b 446
ba9dc077
SH
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);
450 }
264867fd 451
ba9dc077
SH
452 // Set up conditions
453 if ($CFG->enableavailability) {
454 condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
455 }
9645a484
MD
456
457 // Trigger mod_created event with information about this module.
fbaea88f 458 $eventdata = new stdClass();
9645a484
MD
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);
264867fd 465
ba9dc077
SH
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);
472 } else {
473 print_error('invaliddata');
474 }
61defed9 475
ba9dc077
SH
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();
482 }
483 }
61defed9 484
ba9dc077
SH
485 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
486 'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
487
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();
495 if ($grade_item) {
496 $parent = $grade_item->get_parent_category();
497 $grade_category->set_parent($parent->id);
82bd6a5e 498 }
ba9dc077 499 $fromform->gradecat = $grade_category->id;
264867fd 500 }
ba9dc077
SH
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];
60243313 506 }
507 }
ba9dc077 508 }
60243313 509
ba9dc077
SH
510 // add outcomes if requested
511 if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
512 $grade_items = array();
4b86bb08 513
ba9dc077
SH
514 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
515 $max_itemnumber = 999;
516 if ($items) {
517 foreach($items as $item) {
518 if ($item->itemnumber > $max_itemnumber) {
519 $max_itemnumber = $item->itemnumber;
4b86bb08 520 }
521 }
522 }
523
ba9dc077
SH
524 foreach($outcomes as $outcome) {
525 $elname = 'outcome_'.$outcome->id;
61defed9 526
4cc977a6 527 if (property_exists($fromform, $elname) and $fromform->$elname) {
ba9dc077
SH
528 // so we have a request for new outcome grade item?
529 if ($items) {
530 foreach($items as $item) {
531 if ($item->outcomeid == $outcome->id) {
532 //outcome aready exists
533 continue 2;
534 }
d19f4770 535 }
536 }
d19f4770 537
ba9dc077
SH
538 $max_itemnumber++;
539
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();
551
552 // move the new outcome into correct category and fix sortorder if needed
553 if ($grade_item) {
554 $outcome_item->set_parent($grade_item->categoryid);
555 $outcome_item->move_after_sortorder($grade_item->sortorder);
d19f4770 556
ba9dc077
SH
557 } else if (isset($fromform->gradecat)) {
558 $outcome_item->set_parent($fromform->gradecat);
71ee4471 559 }
560 }
561 }
ba9dc077 562 }
71ee4471 563
ba9dc077
SH
564 rebuild_course_cache($course->id);
565 grade_regrade_final_grades($course->id);
bce59524 566 plagiarism_save_form_elements($fromform); //save plagiarism settings
264867fd 567
ba9dc077
SH
568 if (isset($fromform->submitbutton)) {
569 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
52273766 570 } else {
ba9dc077
SH
571 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
572 }
573 exit;
61defed9 574
ba9dc077 575} else {
aa6c1ced 576
ba9dc077
SH
577 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
578 $strmodulenameplural = get_string('modulenameplural', $module->name);
aa6c1ced 579
ba9dc077
SH
580 if (!empty($cm->id)) {
581 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
582 } else {
583 $context = get_context_instance(CONTEXT_COURSE, $course->id);
584 }
585
586 $PAGE->set_heading($course->fullname);
c20b001b 587 $PAGE->set_title($streditinga);
ba9dc077 588 $PAGE->set_cacheable(false);
ba9dc077
SH
589 echo $OUTPUT->header();
590
5435c9dc 591 if (get_string_manager()->string_exists('modulename_help', $module->name)) {
ba9dc077
SH
592 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
593 } else {
594 echo $OUTPUT->heading_with_help($pageheading, '', '', 'icon');
264867fd 595 }
ba9dc077
SH
596
597 $mform->display();
598
599 echo $OUTPUT->footer();
5435c9dc 600}