MDL-14589 file api improvements - converting more params to general $options array
[moodle.git] / course / modedit.php
CommitLineData
264867fd 1<?php // $Id$
2
3// adds or updates modules in a course using new formslib
4
c327e150 5 require_once("../config.php");
6 require_once("lib.php");
43b44d5e 7 require_once($CFG->libdir.'/filelib.php');
71ee4471 8 require_once($CFG->libdir.'/gradelib.php');
d857e8b6 9 require_once($CFG->libdir.'/completionlib.php');
82bd6a5e 10 require_once($CFG->libdir.'/conditionlib.php');
264867fd 11
d857e8b6 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);
264867fd 16
d857e8b6 17 require_login();
145dbd7c 18
52273766 19 if (!empty($add)) {
264867fd 20 $section = required_param('section', PARAM_INT);
d857e8b6 21 $course = required_param('course', PARAM_INT);
264867fd 22
d857e8b6 23 if (!$course = $DB->get_record('course', array('id'=>$course))) {
24 print_error('invalidcourseid');
264867fd 25 }
26
24f41672 27 require_login($course);
28 $context = get_context_instance(CONTEXT_COURSE, $course->id);
29 require_capability('moodle/course:manageactivities', $context);
30
d857e8b6 31 if (!$module = $DB->get_record('modules', array('name'=>$add))) {
32 print_error('moduledoesnotexist');
264867fd 33 }
34
97928ddf 35 $cw = get_course_section($section, $course->id);
264867fd 36
37 if (!course_allowed_module($course, $module->id)) {
d857e8b6 38 print_error('moduledisable');
264867fd 39 }
40
24f41672 41 $cm = null;
42
c18269c7 43 $form = new object();
24f41672 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
264867fd 56
dc5c2bd9 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 }
62
9824e567 63 // Turn off default grouping for modules that don't provide group mode
d857e8b6 64 if ($add=='resource' || $add=='glossary' || $add=='label') {
65 $form->groupingid = 0;
9824e567 66 }
67
264867fd 68 if (!empty($type)) {
69 $form->type = $type;
70 }
71
ae628043 72 $sectionname = get_section_name($course->format);
d857e8b6 73 $fullmodulename = get_string('modulename', $module->name);
264867fd 74
75 if ($form->section && $course->format != 'site') {
76 $heading->what = $fullmodulename;
77 $heading->to = "$sectionname $form->section";
d857e8b6 78 $pageheading = get_string('addinganewto', 'moodle', $heading);
264867fd 79 } else {
d857e8b6 80 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
264867fd 81 }
264867fd 82
d529807a 83 $pagepath = 'mod-' . $module->name . '-';
52273766 84 if (!empty($type)) {
d529807a 85 $pagepath .= $type;
52273766 86 } else {
d529807a 87 $pagepath .= 'mod';
52273766 88 }
d529807a 89 $PAGE->set_pagetype($pagepath);
52273766 90
24f41672 91 $navlinksinstancename = '';
c18269c7 92
52273766 93 } else if (!empty($update)) {
d857e8b6 94 if (!$cm = get_coursemodule_from_id('', $update, 0)) {
95 print_error('invalidcoursemodule');
264867fd 96 }
97
d857e8b6 98 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
99 print_error('invalidcourseid');
264867fd 100 }
101
24f41672 102 require_login($course); // needed to setup proper $COURSE
217a8ee9 103 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
264867fd 104 require_capability('moodle/course:manageactivities', $context);
105
d857e8b6 106 if (!$module = $DB->get_record('modules', array('id'=>$cm->module))) {
107 print_error('moduledoesnotexist');
264867fd 108 }
109
d857e8b6 110 if (!$form = $DB->get_record($module->name, array('id'=>$cm->instance))) {
111 print_error('moduleinstancedoesnotexist');
cb141e5a 112 }
113
d857e8b6 114 if (!$cw = $DB->get_record('course_sections', array('id'=>$cm->section))) {
115 print_error('sectionnotexist');
264867fd 116 }
117
d857e8b6 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;
4e781c7b 133 $form->completionexpected = $cm->completionexpected;
134 $form->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
82bd6a5e 135 if(!empty($CFG->enableavailability)) {
136 $form->availablefrom = $cm->availablefrom;
137 $form->availableuntil = $cm->availableuntil;
138 $form->showavailability = $cm->showavailability;
139 }
264867fd 140
dc5c2bd9 141 if (plugin_supports('mod', $form->modulename, FEATURE_MOD_INTRO, true)) {
142 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
a19a06d0 143 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, $form->modulename.'_intro', 0, array('subdirs'=>true), $form->intro);
dc5c2bd9 144 $form->introeditor = array('text'=>$currentintro, 'format'=>$form->introformat, 'itemid'=>$draftid_editor);
145 }
146
71ee4471 147 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename,
d857e8b6 148 'iteminstance'=>$form->instance, 'courseid'=>$course->id))) {
4b86bb08 149 // add existing outcomes
71ee4471 150 foreach ($items as $item) {
151 if (!empty($item->outcomeid)) {
152 $form->{'outcome_'.$item->outcomeid} = 1;
153 }
154 }
4b86bb08 155
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 }
71ee4471 173 }
174
ae628043 175 $sectionname = get_section_name($course->format);
d857e8b6 176 $fullmodulename = get_string('modulename', $module->name);
264867fd 177
178 if ($form->section && $course->format != 'site') {
179 $heading->what = $fullmodulename;
180 $heading->in = "$sectionname $cw->section";
d857e8b6 181 $pageheading = get_string('updatingain', 'moodle', $heading);
264867fd 182 } else {
d857e8b6 183 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
264867fd 184 }
61defed9 185
d857e8b6 186 $navlinksinstancename = array('name' => format_string($form->name, true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
61defed9 187
ad5d5997 188 $pagetype = 'mod-' . $module->name . '-';
52273766 189 if (!empty($type)) {
ad5d5997 190 $pagetype .= $type;
52273766 191 } else {
ad5d5997 192 $pagetype .= 'mod';
52273766 193 }
ad5d5997 194 $PAGE->set_pagetype($pagetype);
52273766 195 } else {
ba6018a9 196 print_error('invalidaction');
264867fd 197 }
198
3cd7940b 199 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
264867fd 200 if (file_exists($modmoodleform)) {
201 require_once($modmoodleform);
264867fd 202
52273766 203 } else {
ba6018a9 204 print_error('noformdesc');
264867fd 205 }
3cd7940b 206
207 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
208 if (file_exists($modlib)) {
209 include_once($modlib);
210 } else {
d857e8b6 211 print_error('modulemissingcode', '', '', $modlib);
3cd7940b 212 }
213
1d284fbd 214 $mformclassname = 'mod_'.$module->name.'_mod_form';
d857e8b6 215 $mform = new $mformclassname($form->instance, $cw->section, $cm);
beac4717 216 $mform->set_data($form);
3cd7940b 217
19110c57 218 if ($mform->is_cancelled()) {
d857e8b6 219 if ($return && !empty($cm->id)) {
19110c57 220 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
221 } else {
681cf965 222 redirect("$CFG->wwwroot/course/view.php?id=$course->id#section-".$cw->section);
19110c57 223 }
d857e8b6 224
294ce987 225 } else if ($fromform = $mform->get_data()) {
264867fd 226 if (empty($fromform->coursemodule)) { //add
21090e44 227 $cm = null;
d857e8b6 228 if (!$course = $DB->get_record('course', array('id'=>$fromform->course))) {
229 print_error('invalidcourseid');
264867fd 230 }
d857e8b6 231 $fromform->instance = '';
264867fd 232 $fromform->coursemodule = '';
233 } else { //update
d857e8b6 234 if (!$cm = get_coursemodule_from_id('', $fromform->coursemodule, 0)) {
235 print_error('invalidcoursemodule');
264867fd 236 }
237
d857e8b6 238 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
239 print_error('invalidcourseid');
264867fd 240 }
d857e8b6 241 $fromform->instance = $cm->instance;
264867fd 242 $fromform->coursemodule = $cm->id;
243 }
244
d857e8b6 245 require_login($course); // needed to setup proper $COURSE
61defed9 246
217a8ee9 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 }
264867fd 252 require_capability('moodle/course:manageactivities', $context);
253
254 $fromform->course = $course->id;
255 $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety
264867fd 256
264867fd 257 $addinstancefunction = $fromform->modulename."_add_instance";
258 $updateinstancefunction = $fromform->modulename."_update_instance";
259
273eb2f5 260 if (!isset($fromform->groupingid)) {
261 $fromform->groupingid = 0;
262 }
264867fd 263
273eb2f5 264 if (!isset($fromform->groupmembersonly)) {
265 $fromform->groupmembersonly = 0;
266 }
e04ff2d5 267
273eb2f5 268 if (!isset($fromform->name)) { //label
269 $fromform->name = $fromform->modulename;
270 }
4e781c7b 271
272 if (!isset($fromform->completion)) {
d857e8b6 273 $fromform->completion = COMPLETION_DISABLED;
4e781c7b 274 }
275 if (!isset($fromform->completionview)) {
d857e8b6 276 $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
4e781c7b 277 }
278
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;
273eb2f5 284
285 if (!empty($fromform->update)) {
264867fd 286
21090e44 287 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
288 $fromform->groupmode = $cm->groupmode; // keep original
289 }
290
d857e8b6 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()) {
9ddbd445 297 // Handle completion settings. If necessary, wipe existing completion
298 // data first.
d857e8b6 299 if (!empty($fromform->completionunlocked)) {
300 $completion = new completion_info($course);
9ddbd445 301 $completion->reset_all_state($cm);
302 }
d857e8b6 303
304 $cm->completion = $fromform->completion;
305 $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
306 $cm->completionview = $fromform->completionview;
307 $cm->completionexpected = $fromform->completionexpected;
308 }
82bd6a5e 309 if(!empty($CFG->enableavailability)) {
310 $cm->availablefrom = $fromform->availablefrom;
311 $cm->availableuntil = $fromform->availableuntil;
e7c6bf0e 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 }
82bd6a5e 318 $cm->showavailability = $fromform->showavailability;
319 condition_info::update_cm_from_form($cm,$fromform,true);
320 }
d857e8b6 321
90ee78e2 322 $DB->update_record('course_modules', $cm);
4e781c7b 323
dc5c2bd9 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,
a08171c5 330 array('subdirs'=>true), $fromform->introeditor['text']);
dc5c2bd9 331 $fromform->introformat = $fromform->introeditor['format'];
332 unset($fromform->introeditor);
333 }
334
d857e8b6 335 if (!$updateinstancefunction($fromform, $mform)) {
336 print_error('cannotupdatemod', '', 'view.php?id=$course->id', $fromform->modulename);
337 }
338
339 // make sure visibility is set correctly (in particular in calendar)
340 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
341
273eb2f5 342 if (isset($fromform->cmidnumber)) { //label
d857e8b6 343 // set cm idnumber - uniqueness is already verified by form validation
273eb2f5 344 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
345 }
24f41672 346
264867fd 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);
52273766 353
d857e8b6 354 } else if (!empty($fromform->add)) {
273eb2f5 355
21090e44 356 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
357 $fromform->groupmode = 0; // do not set groupmode
358 }
359
d857e8b6 360 if (!course_allowed_module($course, $fromform->modulename)) {
ba6018a9 361 print_error('moduledisable', '', '', $fromform->modulename);
264867fd 362 }
363
d857e8b6 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 }
82bd6a5e 380 if(!empty($CFG->enableavailability)) {
381 $newcm->availablefrom = $fromform->availablefrom;
382 $newcm->availableuntil = $fromform->availableuntil;
e7c6bf0e 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 }
82bd6a5e 389 $newcm->showavailability = $fromform->showavailability;
390 }
d857e8b6 391
392 if (!$fromform->coursemodule = add_course_module($newcm)) {
393 print_error('cannotaddcoursemodule');
264867fd 394 }
d857e8b6 395
dc5c2bd9 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,
a08171c5 402 array('subdirs'=>true), $fromform->introeditor['text']);
dc5c2bd9 403 $fromform->introformat = $fromform->introeditor['format'];
404 unset($fromform->introeditor);
405 }
406
d857e8b6 407 $returnfromfunc = $addinstancefunction($fromform, $mform);
408
409 if (!$returnfromfunc or !is_number($returnfromfunc)) {
410 // undo everything we can
c765b6b7 411 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
d857e8b6 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));
416
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 }
264867fd 422 }
423
19110c57 424 $fromform->instance = $returnfromfunc;
264867fd 425
d857e8b6 426 if (!$DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule))) {
427 print_error('cannotaddcoursemodule');
428 }
429
264867fd 430 // course_modules and course_sections each contain a reference
431 // to each other, so we have to update one of them twice.
d857e8b6 432 if (!$sectionid = add_mod_to_section($fromform)) {
433 print_error('cannotaddcmtosection');
264867fd 434 }
435
d857e8b6 436 if (!$DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule))) {
437 print_error('cannotupdatecm');
264867fd 438 }
439
264867fd 440 // make sure visibility is set correctly (in particular in calendar)
441 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
61defed9 442
273eb2f5 443 if (isset($fromform->cmidnumber)) { //label
d857e8b6 444 // set cm idnumber - uniqueness is already verified by form validation
273eb2f5 445 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
446 }
61defed9 447
82bd6a5e 448 // Set up conditions
dc5c2bd9 449 if ($CFG->enableavailability) {
450 condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
82bd6a5e 451 }
452
264867fd 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);
52273766 459 } else {
d857e8b6 460 print_error('invaliddata');
264867fd 461 }
462
4b86bb08 463 // sync idnumber with grade_item
60243313 464 if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
d857e8b6 465 'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
e79541a3 466 if ($grade_item->idnumber != $fromform->cmidnumber) {
467 $grade_item->idnumber = $fromform->cmidnumber;
60243313 468 $grade_item->update();
469 }
470 }
471
4b86bb08 472 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
d857e8b6 473 'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
4b86bb08 474
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();
d857e8b6 479 $grade_category->courseid = $course->id;
294ce987 480 $grade_category->fullname = $fromform->name;
4b86bb08 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);
8c1c197e 490 if ($itemid == $grade_item->id) {
4b86bb08 491 // use updated grade_item
8c1c197e 492 $grade_item = $items[$itemid];
4b86bb08 493 }
494 }
495 }
496
71ee4471 497 // add outcomes if requested
d857e8b6 498 if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
61defed9 499 $grade_items = array();
500
d19f4770 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 }
510
71ee4471 511 foreach($outcomes as $outcome) {
512 $elname = 'outcome_'.$outcome->id;
61defed9 513
71ee4471 514 if (array_key_exists($elname, $fromform) and $fromform->$elname) {
d19f4770 515 // so we have a request for new outcome grade item?
4b86bb08 516 if ($items) {
71ee4471 517 foreach($items as $item) {
518 if ($item->outcomeid == $outcome->id) {
d19f4770 519 //outcome aready exists
520 continue 2;
71ee4471 521 }
522 }
523 }
d19f4770 524
525 $max_itemnumber++;
2d6df44f 526
4b86bb08 527 $outcome_item = new grade_item();
d857e8b6 528 $outcome_item->courseid = $course->id;
4b86bb08 529 $outcome_item->itemtype = 'mod';
530 $outcome_item->itemmodule = $fromform->modulename;
531 $outcome_item->iteminstance = $fromform->instance;
d19f4770 532 $outcome_item->itemnumber = $max_itemnumber;
4b86bb08 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();
538
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);
543
544 } else if (isset($fromform->gradecat)) {
545 $outcome_item->set_parent($fromform->gradecat);
2d6df44f 546 }
71ee4471 547 }
548 }
549 }
550
264867fd 551 rebuild_course_cache($course->id);
b45d8391 552 grade_regrade_final_grades($course->id);
553
de6f158f 554 if (isset($fromform->submitbutton)) {
0b19ec1e 555 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
0b19ec1e 556 } else {
55da5e81 557 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
6b467109 558 }
264867fd 559 exit;
560
52273766 561 } else {
217a8ee9 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 }
264867fd 567 require_capability('moodle/course:manageactivities', $context);
61defed9 568
d857e8b6 569 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
570 $strmodulenameplural = get_string('modulenameplural', $module->name);
61defed9 571
01e38388 572 $navlinks = array();
3b27b0fe 573 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
24f41672 574 if ($navlinksinstancename) {
3b27b0fe 575 $navlinks[] = $navlinksinstancename;
e1d2eb00 576 }
3b27b0fe 577 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title');
61defed9 578
3b27b0fe 579 $navigation = build_navigation($navlinks);
61defed9 580
e1d2eb00 581 print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false);
264867fd 582
583 if (!empty($cm->id)) {
584 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
21090e44 585 $overridableroles = get_overridable_roles($context);
586 $assignableroles = get_assignable_roles($context);
264867fd 587 $currenttab = 'update';
d857e8b6 588 require($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
264867fd 589 }
0d905d9f 590 $icon = '<img src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt=""/>';
264867fd 591
d857e8b6 592 print_heading_with_help($pageheading, 'mods', $module->name, $icon);
264867fd 593 $mform->display();
594 print_footer($course);
595 }