Activity completion MDL-24746 If you turn on autocompletion for an activity after...
[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)) {
45 $url->param('add', $add);
2b9e3bac 46 $PAGE->set_url($url);
54632b02 47
ba9dc077
SH
48 $section = required_param('section', PARAM_INT);
49 $course = required_param('course', PARAM_INT);
2b9e3bac 50
ba9dc077
SH
51 $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
52 $module = $DB->get_record('modules', array('name'=>$add), '*', MUST_EXIST);
264867fd 53
ba9dc077
SH
54 require_login($course);
55 $context = get_context_instance(CONTEXT_COURSE, $course->id);
56 require_capability('moodle/course:manageactivities', $context);
264867fd 57
ba9dc077 58 $cw = get_course_section($section, $course->id);
264867fd 59
ba9dc077
SH
60 if (!course_allowed_module($course, $module->id)) {
61 print_error('moduledisable');
62 }
52273766 63
ba9dc077
SH
64 $cm = null;
65
fbaea88f 66 $data = new stdClass();
ba9dc077
SH
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
80
81 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
82 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
64f93798 83 file_prepare_draft_area($draftid_editor, null, null, null, null);
ba9dc077
SH
84 $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
85 }
c18269c7 86
ba9dc077
SH
87 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
88 $data->type = $type;
89 }
264867fd 90
7487c856 91 $sectionname = get_section_name($course, $cw);
ba9dc077 92 $fullmodulename = get_string('modulename', $module->name);
264867fd 93
ba9dc077 94 if ($data->section && $course->format != 'site') {
fbaea88f 95 $heading = new stdClass();
ba9dc077 96 $heading->what = $fullmodulename;
7487c856 97 $heading->to = $sectionname;
ba9dc077
SH
98 $pageheading = get_string('addinganewto', 'moodle', $heading);
99 } else {
100 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
101 }
264867fd 102
ba9dc077 103} else if (!empty($update)) {
cb141e5a 104
ba9dc077
SH
105 $url->param('update', $update);
106 $PAGE->set_url($url);
264867fd 107
ba9dc077
SH
108 $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
109 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
110
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);
114
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);
118
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 }
aa54ed7b 141
ba9dc077
SH
142 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
143 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
64f93798 144 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, 'mod_'.$data->modulename, 'intro', 0, array('subdirs'=>true), $data->intro);
ba9dc077
SH
145 $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
146 }
dc5c2bd9 147
ba9dc077
SH
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;
71ee4471 154 }
ba9dc077 155 }
4b86bb08 156
ba9dc077
SH
157 // set category if present
158 $gradecat = false;
159 foreach ($items as $item) {
160 if ($gradecat === false) {
161 $gradecat = $item->categoryid;
162 continue;
4b86bb08 163 }
ba9dc077
SH
164 if ($gradecat != $item->categoryid) {
165 //mixed categories
166 $gradecat = false;
167 break;
4b86bb08 168 }
71ee4471 169 }
ba9dc077
SH
170 if ($gradecat !== false) {
171 // do not set if mixed categories present
172 $data->gradecat = $gradecat;
264867fd 173 }
ba9dc077 174 }
61defed9 175
7487c856 176 $sectionname = get_section_name($course, $cw);
ba9dc077 177 $fullmodulename = get_string('modulename', $module->name);
61defed9 178
ba9dc077 179 if ($data->section && $course->format != 'site') {
fbaea88f 180 $heading = new stdClass();
ba9dc077 181 $heading->what = $fullmodulename;
7487c856 182 $heading->in = $sectionname;
ba9dc077 183 $pageheading = get_string('updatingain', 'moodle', $heading);
52273766 184 } else {
ba9dc077 185 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
264867fd 186 }
187
ba9dc077
SH
188} else {
189 require_login();
190 print_error('invalidaction');
191}
192
193$pagepath = 'mod-' . $module->name . '-';
194if (!empty($type)) { //TODO: hopefully will be removed in 2.0
195 $pagepath .= $type;
196} else {
197 $pagepath .= 'mod';
198}
199$PAGE->set_pagetype($pagepath);
ae2fa74f 200$PAGE->set_pagelayout('admin');
ba9dc077
SH
201
202$modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
203if (file_exists($modmoodleform)) {
204 require_once($modmoodleform);
205} else {
206 print_error('noformdesc');
207}
208
209$modlib = "$CFG->dirroot/mod/$module->name/lib.php";
210if (file_exists($modlib)) {
211 include_once($modlib);
212} else {
213 print_error('modulemissingcode', '', '', $modlib);
214}
215
216$mformclassname = 'mod_'.$module->name.'_mod_form';
217$mform = new $mformclassname($data, $cw->section, $cm, $course);
218$mform->set_data($data);
219
220if ($mform->is_cancelled()) {
221 if ($return && !empty($cm->id)) {
222 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
52273766 223 } else {
ba9dc077
SH
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 = '';
54632b02 233 } else {
ba9dc077
SH
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;
264867fd 239 }
3cd7940b 240
ba9dc077
SH
241 if (!empty($fromform->coursemodule)) {
242 $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
3cd7940b 243 } else {
ba9dc077 244 $context = get_context_instance(CONTEXT_COURSE, $course->id);
3cd7940b 245 }
246
ba9dc077
SH
247 $fromform->course = $course->id;
248 $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety
d857e8b6 249
ba9dc077
SH
250 $addinstancefunction = $fromform->modulename."_add_instance";
251 $updateinstancefunction = $fromform->modulename."_update_instance";
264867fd 252
ba9dc077
SH
253 if (!isset($fromform->groupingid)) {
254 $fromform->groupingid = 0;
255 }
264867fd 256
ba9dc077
SH
257 if (!isset($fromform->groupmembersonly)) {
258 $fromform->groupmembersonly = 0;
259 }
264867fd 260
ba9dc077
SH
261 if (!isset($fromform->name)) { //label
262 $fromform->name = $fromform->modulename;
263 }
264867fd 264
ba9dc077
SH
265 if (!isset($fromform->completion)) {
266 $fromform->completion = COMPLETION_DISABLED;
267 }
268 if (!isset($fromform->completionview)) {
269 $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
270 }
264867fd 271
ba9dc077
SH
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 }
e04ff2d5 279
ba9dc077 280 if (!empty($fromform->update)) {
aa54ed7b 281
ba9dc077
SH
282 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
283 $fromform->groupmode = $cm->groupmode; // keep original
4e781c7b 284 }
285
ba9dc077
SH
286 // update course module first
287 $cm->groupmode = $fromform->groupmode;
288 $cm->groupingid = $fromform->groupingid;
289 $cm->groupmembersonly = $fromform->groupmembersonly;
21090e44 290
ba9dc077
SH
291 $completion = new completion_info($course);
292 if ($completion->is_enabled()) {
91d0bb0a 293 // Update completion settings
ba9dc077
SH
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);
dc5c2bd9 307 }
ba9dc077
SH
308 $cm->showavailability = $fromform->showavailability;
309 condition_info::update_cm_from_form($cm,$fromform,true);
310 }
311
312 $DB->update_record('course_modules', $cm);
313
314 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
315
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,
64f93798 319 'mod_'.$fromform->modulename, 'intro', 0,
ba9dc077
SH
320 array('subdirs'=>true), $fromform->introeditor['text']);
321 $fromform->introformat = $fromform->introeditor['format'];
322 unset($fromform->introeditor);
323 }
324
325 if (!$updateinstancefunction($fromform, $mform)) {
326 print_error('cannotupdatemod', '', 'view.php?id=$course->id', $fromform->modulename);
327 }
328
329 // make sure visibility is set correctly (in particular in calendar)
330 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
331
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 }
9645a484 336
91d0bb0a
SM
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 }
342
9645a484 343 // Trigger mod_updated event with information about this module.
fbaea88f 344 $eventdata = new stdClass();
9645a484
MD
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);
ba9dc077
SH
351
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);
358
359 } else if (!empty($fromform->add)) {
360
361 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
362 $fromform->groupmode = 0; // do not set groupmode
363 }
364
365 if (!course_allowed_module($course, $fromform->modulename)) {
366 print_error('moduledisable', '', '', $fromform->modulename);
367 }
368
369 // first add course_module record because we need the context
fbaea88f 370 $newcm = new stdClass();
ba9dc077
SH
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);
d857e8b6 393 }
ba9dc077
SH
394 $newcm->showavailability = $fromform->showavailability;
395 }
d857e8b6 396
ba9dc077
SH
397 if (!$fromform->coursemodule = add_course_module($newcm)) {
398 print_error('cannotaddcoursemodule');
399 }
21090e44 400
ba9dc077
SH
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 }
264867fd 407
ba9dc077 408 $returnfromfunc = $addinstancefunction($fromform, $mform);
d857e8b6 409
ba9dc077
SH
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));
d857e8b6 415
ba9dc077
SH
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);
b1a2d9d2 420 }
ba9dc077 421 }
dc5c2bd9 422
ba9dc077 423 $fromform->instance = $returnfromfunc;
d857e8b6 424
ba9dc077 425 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule));
d857e8b6 426
ba9dc077
SH
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,
64f93798 431 'mod_'.$fromform->modulename, 'intro', 0,
ba9dc077
SH
432 array('subdirs'=>true), $introeditor['text']);
433 $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id'=>$fromform->instance));
434 }
264867fd 435
ba9dc077
SH
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);
264867fd 439
ba9dc077 440 $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule));
d857e8b6 441
ba9dc077
SH
442 // make sure visibility is set correctly (in particular in calendar)
443 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
aa54ed7b 444
ba9dc077
SH
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 }
264867fd 449
ba9dc077
SH
450 // Set up conditions
451 if ($CFG->enableavailability) {
452 condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
453 }
9645a484
MD
454
455 // Trigger mod_created event with information about this module.
fbaea88f 456 $eventdata = new stdClass();
9645a484
MD
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);
264867fd 463
ba9dc077
SH
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 }
61defed9 473
ba9dc077
SH
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 }
61defed9 482
ba9dc077
SH
483 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
484 'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
485
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);
82bd6a5e 496 }
ba9dc077 497 $fromform->gradecat = $grade_category->id;
264867fd 498 }
ba9dc077
SH
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];
60243313 504 }
505 }
ba9dc077 506 }
60243313 507
ba9dc077
SH
508 // add outcomes if requested
509 if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
510 $grade_items = array();
4b86bb08 511
ba9dc077
SH
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;
4b86bb08 518 }
519 }
520 }
521
ba9dc077
SH
522 foreach($outcomes as $outcome) {
523 $elname = 'outcome_'.$outcome->id;
61defed9 524
4cc977a6 525 if (property_exists($fromform, $elname) and $fromform->$elname) {
ba9dc077
SH
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 }
d19f4770 533 }
534 }
d19f4770 535
ba9dc077
SH
536 $max_itemnumber++;
537
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();
549
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);
d19f4770 554
ba9dc077
SH
555 } else if (isset($fromform->gradecat)) {
556 $outcome_item->set_parent($fromform->gradecat);
71ee4471 557 }
558 }
559 }
ba9dc077 560 }
71ee4471 561
ba9dc077
SH
562 rebuild_course_cache($course->id);
563 grade_regrade_final_grades($course->id);
bce59524 564 plagiarism_save_form_elements($fromform); //save plagiarism settings
264867fd 565
ba9dc077
SH
566 if (isset($fromform->submitbutton)) {
567 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
52273766 568 } else {
ba9dc077
SH
569 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
570 }
571 exit;
61defed9 572
ba9dc077 573} else {
aa6c1ced 574
ba9dc077
SH
575 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
576 $strmodulenameplural = get_string('modulenameplural', $module->name);
aa6c1ced 577
ba9dc077
SH
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 }
583
584 $PAGE->set_heading($course->fullname);
c20b001b 585 $PAGE->set_title($streditinga);
ba9dc077 586 $PAGE->set_cacheable(false);
ba9dc077
SH
587 echo $OUTPUT->header();
588
5435c9dc 589 if (get_string_manager()->string_exists('modulename_help', $module->name)) {
ba9dc077
SH
590 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
591 } else {
592 echo $OUTPUT->heading_with_help($pageheading, '', '', 'icon');
264867fd 593 }
ba9dc077
SH
594
595 $mform->display();
596
597 echo $OUTPUT->footer();
5435c9dc 598}