MDL-21695 adding support for module help
[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/**
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 */
264867fd 25
c327e150 26 require_once("../config.php");
27 require_once("lib.php");
43b44d5e 28 require_once($CFG->libdir.'/filelib.php');
71ee4471 29 require_once($CFG->libdir.'/gradelib.php');
d857e8b6 30 require_once($CFG->libdir.'/completionlib.php');
82bd6a5e 31 require_once($CFG->libdir.'/conditionlib.php');
264867fd 32
aa54ed7b 33 $add = optional_param('add', '', PARAM_ALPHA); // module name
d857e8b6 34 $update = optional_param('update', 0, PARAM_INT);
aa54ed7b 35 $return = optional_param('return', 0, PARAM_BOOL); //return to course/view.php if false or mod/modname/view.php if true
36 $type = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0
264867fd 37
a6855934 38 $url = new moodle_url('/course/modedit.php');
2b9e3bac 39 if (!empty($add)) {
40 $url->param('add', $add);
41 } else if (!empty($update)) {
42 $url->param('update', $update);
43 } else {
44 debugging('Neither add nor update arguments were provided, PAGE->set_url needs to be checked', DEBUG_DEVELOPER);
45 }
46 if (!empty($return)) {
47 $url->param('return', $return);
48 }
49 if (!empty($type)) {
50 $url->param('type', $type);
51 }
52 $PAGE->set_url($url);
53
52273766 54 if (!empty($add)) {
264867fd 55 $section = required_param('section', PARAM_INT);
d857e8b6 56 $course = required_param('course', PARAM_INT);
264867fd 57
d857e8b6 58 if (!$course = $DB->get_record('course', array('id'=>$course))) {
59 print_error('invalidcourseid');
264867fd 60 }
61
24f41672 62 require_login($course);
63 $context = get_context_instance(CONTEXT_COURSE, $course->id);
64 require_capability('moodle/course:manageactivities', $context);
65
d857e8b6 66 if (!$module = $DB->get_record('modules', array('name'=>$add))) {
67 print_error('moduledoesnotexist');
264867fd 68 }
69
97928ddf 70 $cw = get_course_section($section, $course->id);
264867fd 71
72 if (!course_allowed_module($course, $module->id)) {
d857e8b6 73 print_error('moduledisable');
264867fd 74 }
75
24f41672 76 $cm = null;
77
aa54ed7b 78 $data = new object();
79 $data->section = $section; // The section number itself - relative!!! (section column in course_sections)
80 $data->visible = $cw->visible;
81 $data->course = $course->id;
82 $data->module = $module->id;
83 $data->modulename = $module->name;
84 $data->groupmode = $course->groupmode;
85 $data->groupingid = $course->defaultgroupingid;
86 $data->groupmembersonly = 0;
87 $data->id = '';
88 $data->instance = '';
89 $data->coursemodule = '';
90 $data->add = $add;
91 $data->return = 0; //must be false if this is an add, go back to course view on cancel
92
93 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
dc5c2bd9 94 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
95 file_prepare_draft_area($draftid_editor, null, null, null);
aa54ed7b 96 $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
dc5c2bd9 97 }
98
aa54ed7b 99 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
100 $data->type = $type;
264867fd 101 }
102
ae628043 103 $sectionname = get_section_name($course->format);
d857e8b6 104 $fullmodulename = get_string('modulename', $module->name);
264867fd 105
aa54ed7b 106 if ($data->section && $course->format != 'site') {
107 $heading = new object();
264867fd 108 $heading->what = $fullmodulename;
aa54ed7b 109 $heading->to = "$sectionname $data->section";
d857e8b6 110 $pageheading = get_string('addinganewto', 'moodle', $heading);
264867fd 111 } else {
d857e8b6 112 $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
264867fd 113 }
264867fd 114
d529807a 115 $pagepath = 'mod-' . $module->name . '-';
aa54ed7b 116 if (!empty($type)) { //TODO: hopefully will be removed in 2.0
d529807a 117 $pagepath .= $type;
52273766 118 } else {
d529807a 119 $pagepath .= 'mod';
52273766 120 }
d529807a 121 $PAGE->set_pagetype($pagepath);
52273766 122
24f41672 123 $navlinksinstancename = '';
c18269c7 124
52273766 125 } else if (!empty($update)) {
d857e8b6 126 if (!$cm = get_coursemodule_from_id('', $update, 0)) {
127 print_error('invalidcoursemodule');
264867fd 128 }
129
d857e8b6 130 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
131 print_error('invalidcourseid');
264867fd 132 }
133
aa54ed7b 134 require_login($course, false, $cm); // needed to setup proper $COURSE
217a8ee9 135 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
264867fd 136 require_capability('moodle/course:manageactivities', $context);
137
d857e8b6 138 if (!$module = $DB->get_record('modules', array('id'=>$cm->module))) {
139 print_error('moduledoesnotexist');
264867fd 140 }
141
aa54ed7b 142 if (!$data = $DB->get_record($module->name, array('id'=>$cm->instance))) {
d857e8b6 143 print_error('moduleinstancedoesnotexist');
cb141e5a 144 }
145
d857e8b6 146 if (!$cw = $DB->get_record('course_sections', array('id'=>$cm->section))) {
147 print_error('sectionnotexist');
264867fd 148 }
149
aa54ed7b 150 $data->coursemodule = $cm->id;
151 $data->section = $cw->section; // The section number itself - relative!!! (section column in course_sections)
152 $data->visible = $cm->visible; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
153 $data->cmidnumber = $cm->idnumber; // The cm IDnumber
154 $data->groupmode = groups_get_activity_groupmode($cm); // locked later if forced
155 $data->groupingid = $cm->groupingid;
156 $data->groupmembersonly = $cm->groupmembersonly;
157 $data->course = $course->id;
158 $data->module = $module->id;
159 $data->modulename = $module->name;
160 $data->instance = $cm->instance;
161 $data->return = $return;
162 $data->update = $update;
163 $data->completion = $cm->completion;
164 $data->completionview = $cm->completionview;
165 $data->completionexpected = $cm->completionexpected;
166 $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
167 if (!empty($CFG->enableavailability)) {
168 $data->availablefrom = $cm->availablefrom;
169 $data->availableuntil = $cm->availableuntil;
170 $data->showavailability = $cm->showavailability;
171 }
172
173 if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
dc5c2bd9 174 $draftid_editor = file_get_submitted_draft_itemid('introeditor');
aa54ed7b 175 $currentintro = file_prepare_draft_area($draftid_editor, $context->id, $data->modulename.'_intro', 0, array('subdirs'=>true), $data->intro);
176 $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
dc5c2bd9 177 }
178
aa54ed7b 179 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
180 'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
4b86bb08 181 // add existing outcomes
71ee4471 182 foreach ($items as $item) {
183 if (!empty($item->outcomeid)) {
aa54ed7b 184 $data->{'outcome_'.$item->outcomeid} = 1;
71ee4471 185 }
186 }
4b86bb08 187
188 // set category if present
189 $gradecat = false;
190 foreach ($items as $item) {
191 if ($gradecat === false) {
192 $gradecat = $item->categoryid;
193 continue;
194 }
195 if ($gradecat != $item->categoryid) {
196 //mixed categories
197 $gradecat = false;
198 break;
199 }
200 }
201 if ($gradecat !== false) {
202 // do not set if mixed categories present
aa54ed7b 203 $data->gradecat = $gradecat;
4b86bb08 204 }
71ee4471 205 }
206
ae628043 207 $sectionname = get_section_name($course->format);
d857e8b6 208 $fullmodulename = get_string('modulename', $module->name);
264867fd 209
aa54ed7b 210 if ($data->section && $course->format != 'site') {
211 $heading = new object();
264867fd 212 $heading->what = $fullmodulename;
213 $heading->in = "$sectionname $cw->section";
d857e8b6 214 $pageheading = get_string('updatingain', 'moodle', $heading);
264867fd 215 } else {
d857e8b6 216 $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
264867fd 217 }
61defed9 218
aa54ed7b 219 $navlinksinstancename = array('name' => format_string($data->name, true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
61defed9 220
ad5d5997 221 $pagetype = 'mod-' . $module->name . '-';
52273766 222 if (!empty($type)) {
ad5d5997 223 $pagetype .= $type;
52273766 224 } else {
ad5d5997 225 $pagetype .= 'mod';
52273766 226 }
ad5d5997 227 $PAGE->set_pagetype($pagetype);
52273766 228 } else {
aa54ed7b 229 require_login();
ba6018a9 230 print_error('invalidaction');
264867fd 231 }
232
3cd7940b 233 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
264867fd 234 if (file_exists($modmoodleform)) {
235 require_once($modmoodleform);
264867fd 236
52273766 237 } else {
ba6018a9 238 print_error('noformdesc');
264867fd 239 }
3cd7940b 240
241 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
242 if (file_exists($modlib)) {
243 include_once($modlib);
244 } else {
d857e8b6 245 print_error('modulemissingcode', '', '', $modlib);
3cd7940b 246 }
247
1d284fbd 248 $mformclassname = 'mod_'.$module->name.'_mod_form';
aa54ed7b 249 $mform = new $mformclassname($data, $cw->section, $cm, $course);
250 $mform->set_data($data);
3cd7940b 251
19110c57 252 if ($mform->is_cancelled()) {
d857e8b6 253 if ($return && !empty($cm->id)) {
19110c57 254 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
255 } else {
681cf965 256 redirect("$CFG->wwwroot/course/view.php?id=$course->id#section-".$cw->section);
19110c57 257 }
d857e8b6 258
294ce987 259 } else if ($fromform = $mform->get_data()) {
264867fd 260 if (empty($fromform->coursemodule)) { //add
21090e44 261 $cm = null;
d857e8b6 262 if (!$course = $DB->get_record('course', array('id'=>$fromform->course))) {
263 print_error('invalidcourseid');
264867fd 264 }
d857e8b6 265 $fromform->instance = '';
264867fd 266 $fromform->coursemodule = '';
267 } else { //update
d857e8b6 268 if (!$cm = get_coursemodule_from_id('', $fromform->coursemodule, 0)) {
269 print_error('invalidcoursemodule');
264867fd 270 }
271
d857e8b6 272 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
273 print_error('invalidcourseid');
264867fd 274 }
d857e8b6 275 $fromform->instance = $cm->instance;
264867fd 276 $fromform->coursemodule = $cm->id;
277 }
278
61defed9 279
217a8ee9 280 if (!empty($fromform->coursemodule)) {
281 $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
282 } else {
283 $context = get_context_instance(CONTEXT_COURSE, $course->id);
284 }
264867fd 285
286 $fromform->course = $course->id;
287 $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety
264867fd 288
264867fd 289 $addinstancefunction = $fromform->modulename."_add_instance";
290 $updateinstancefunction = $fromform->modulename."_update_instance";
291
273eb2f5 292 if (!isset($fromform->groupingid)) {
293 $fromform->groupingid = 0;
294 }
264867fd 295
273eb2f5 296 if (!isset($fromform->groupmembersonly)) {
297 $fromform->groupmembersonly = 0;
298 }
e04ff2d5 299
273eb2f5 300 if (!isset($fromform->name)) { //label
301 $fromform->name = $fromform->modulename;
302 }
aa54ed7b 303
4e781c7b 304 if (!isset($fromform->completion)) {
d857e8b6 305 $fromform->completion = COMPLETION_DISABLED;
4e781c7b 306 }
307 if (!isset($fromform->completionview)) {
d857e8b6 308 $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
4e781c7b 309 }
310
311 // Convert the 'use grade' checkbox into a grade-item number: 0 if
312 // checked, null if not
313 $fromform->completiongradeitemnumber =
314 isset($fromform->completionusegrade) && $fromform->completionusegrade
315 ? 0 : null;
273eb2f5 316
317 if (!empty($fromform->update)) {
264867fd 318
21090e44 319 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
320 $fromform->groupmode = $cm->groupmode; // keep original
321 }
322
d857e8b6 323 // update course module first
324 $cm->groupmode = $fromform->groupmode;
325 $cm->groupingid = $fromform->groupingid;
326 $cm->groupmembersonly = $fromform->groupmembersonly;
aa54ed7b 327
d857e8b6 328 $completion = new completion_info($course);
329 if ($completion->is_enabled()) {
9ddbd445 330 // Handle completion settings. If necessary, wipe existing completion
331 // data first.
d857e8b6 332 if (!empty($fromform->completionunlocked)) {
333 $completion = new completion_info($course);
9ddbd445 334 $completion->reset_all_state($cm);
335 }
d857e8b6 336
337 $cm->completion = $fromform->completion;
338 $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
339 $cm->completionview = $fromform->completionview;
340 $cm->completionexpected = $fromform->completionexpected;
341 }
aa54ed7b 342 if (!empty($CFG->enableavailability)) {
82bd6a5e 343 $cm->availablefrom = $fromform->availablefrom;
344 $cm->availableuntil = $fromform->availableuntil;
aa54ed7b 345 // The form time is midnight, but because we want it to be
e7c6bf0e 346 // inclusive, set it to 23:59:59 on that day.
347 if ($cm->availableuntil) {
aa54ed7b 348 $cm->availableuntil = strtotime('23:59:59',
e7c6bf0e 349 $cm->availableuntil);
350 }
82bd6a5e 351 $cm->showavailability = $fromform->showavailability;
352 condition_info::update_cm_from_form($cm,$fromform,true);
353 }
d857e8b6 354
90ee78e2 355 $DB->update_record('course_modules', $cm);
4e781c7b 356
dc5c2bd9 357 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
aa54ed7b 358
dc5c2bd9 359 // update embedded links and save files
360 if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
361 $fromform->intro = file_save_draft_area_files($fromform->introeditor['itemid'], $modcontext->id,
362 $fromform->modulename.'_intro', 0,
a08171c5 363 array('subdirs'=>true), $fromform->introeditor['text']);
dc5c2bd9 364 $fromform->introformat = $fromform->introeditor['format'];
365 unset($fromform->introeditor);
366 }
aa54ed7b 367
d857e8b6 368 if (!$updateinstancefunction($fromform, $mform)) {
369 print_error('cannotupdatemod', '', 'view.php?id=$course->id', $fromform->modulename);
370 }
371
372 // make sure visibility is set correctly (in particular in calendar)
373 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
374
273eb2f5 375 if (isset($fromform->cmidnumber)) { //label
d857e8b6 376 // set cm idnumber - uniqueness is already verified by form validation
273eb2f5 377 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
378 }
75ba5fe4
DM
379 //trigger event with information about this module.
380 $eventdata = new object();
381 $eventdata->component = 'mod/'.$fromform->modulename;
382 $eventdata->course = $course->id;
383 $eventdata->cm = $fromform->coursemodule;
384 events_trigger($fromform->modulename.'_mod_updated', $eventdata);
24f41672 385
264867fd 386 add_to_log($course->id, "course", "update mod",
387 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
388 "$fromform->modulename $fromform->instance");
389 add_to_log($course->id, $fromform->modulename, "update",
390 "view.php?id=$fromform->coursemodule",
391 "$fromform->instance", $fromform->coursemodule);
52273766 392
d857e8b6 393 } else if (!empty($fromform->add)) {
273eb2f5 394
21090e44 395 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
396 $fromform->groupmode = 0; // do not set groupmode
397 }
398
d857e8b6 399 if (!course_allowed_module($course, $fromform->modulename)) {
ba6018a9 400 print_error('moduledisable', '', '', $fromform->modulename);
264867fd 401 }
402
d857e8b6 403 // first add course_module record because we need the context
404 $newcm = new object();
405 $newcm->course = $course->id;
406 $newcm->module = $fromform->module;
407 $newcm->instance = 0; // not known yet, will be updated later (this is similar to restore code)
408 $newcm->visible = $fromform->visible;
409 $newcm->groupmode = $fromform->groupmode;
410 $newcm->groupingid = $fromform->groupingid;
411 $newcm->groupmembersonly = $fromform->groupmembersonly;
412 $completion = new completion_info($course);
413 if ($completion->is_enabled()) {
414 $newcm->completion = $fromform->completion;
415 $newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
416 $newcm->completionview = $fromform->completionview;
417 $newcm->completionexpected = $fromform->completionexpected;
418 }
82bd6a5e 419 if(!empty($CFG->enableavailability)) {
420 $newcm->availablefrom = $fromform->availablefrom;
421 $newcm->availableuntil = $fromform->availableuntil;
aa54ed7b 422 // The form time is midnight, but because we want it to be
e7c6bf0e 423 // inclusive, set it to 23:59:59 on that day.
424 if ($newcm->availableuntil) {
aa54ed7b 425 $newcm->availableuntil = strtotime('23:59:59',
e7c6bf0e 426 $newcm->availableuntil);
427 }
82bd6a5e 428 $newcm->showavailability = $fromform->showavailability;
429 }
d857e8b6 430
431 if (!$fromform->coursemodule = add_course_module($newcm)) {
432 print_error('cannotaddcoursemodule');
264867fd 433 }
d857e8b6 434
b1a2d9d2
SH
435 if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, false)) {
436 $introeditor = $fromform->introeditor;
437 unset($fromform->introeditor);
438 $fromform->intro = $introeditor['text'];
439 $fromform->introformat = $introeditor['format'];
440 }
dc5c2bd9 441
d857e8b6 442 $returnfromfunc = $addinstancefunction($fromform, $mform);
443
444 if (!$returnfromfunc or !is_number($returnfromfunc)) {
445 // undo everything we can
c765b6b7 446 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
d857e8b6 447 delete_context(CONTEXT_MODULE, $fromform->coursemodule);
448 $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule));
449
450 if (!is_number($returnfromfunc)) {
451 print_error('invalidfunction', '', 'view.php?id=$course->id');
452 } else {
453 print_error('cannotaddnewmodule', '', "view.php?id=$course->id", $fromform->modulename);
454 }
264867fd 455 }
456
19110c57 457 $fromform->instance = $returnfromfunc;
264867fd 458
bf8e93d7 459 $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule));
d857e8b6 460
aa54ed7b 461 // update embedded links and save files
462 $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
463 if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
464 $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
465 $fromform->modulename.'_intro', 0,
466 array('subdirs'=>true), $introeditor['text']);
467 $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id'=>$fromform->instance));
468 }
469
264867fd 470 // course_modules and course_sections each contain a reference
471 // to each other, so we have to update one of them twice.
bf8e93d7 472 $sectionid = add_mod_to_section($fromform);
264867fd 473
bf8e93d7 474 $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule));
264867fd 475
264867fd 476 // make sure visibility is set correctly (in particular in calendar)
477 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
61defed9 478
273eb2f5 479 if (isset($fromform->cmidnumber)) { //label
d857e8b6 480 // set cm idnumber - uniqueness is already verified by form validation
273eb2f5 481 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
482 }
61defed9 483
82bd6a5e 484 // Set up conditions
dc5c2bd9 485 if ($CFG->enableavailability) {
486 condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
82bd6a5e 487 }
75ba5fe4
DM
488 //trigger event with information about this module.
489 $eventdata = new object();
490 $eventdata->component = 'mod/'.$fromform->modulename;
491 $eventdata->course = $course->id;
492 $eventdata->cm = $fromform->coursemodule;
493 events_trigger($fromform->modulename.'_mod_created', $eventdata);
82bd6a5e 494
264867fd 495 add_to_log($course->id, "course", "add mod",
496 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
497 "$fromform->modulename $fromform->instance");
498 add_to_log($course->id, $fromform->modulename, "add",
499 "view.php?id=$fromform->coursemodule",
500 "$fromform->instance", $fromform->coursemodule);
52273766 501 } else {
d857e8b6 502 print_error('invaliddata');
264867fd 503 }
504
4b86bb08 505 // sync idnumber with grade_item
60243313 506 if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
d857e8b6 507 'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
e79541a3 508 if ($grade_item->idnumber != $fromform->cmidnumber) {
509 $grade_item->idnumber = $fromform->cmidnumber;
60243313 510 $grade_item->update();
511 }
512 }
513
4b86bb08 514 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
d857e8b6 515 'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
4b86bb08 516
517 // create parent category if requested and move to correct parent category
518 if ($items and isset($fromform->gradecat)) {
519 if ($fromform->gradecat == -1) {
520 $grade_category = new grade_category();
d857e8b6 521 $grade_category->courseid = $course->id;
294ce987 522 $grade_category->fullname = $fromform->name;
4b86bb08 523 $grade_category->insert();
524 if ($grade_item) {
525 $parent = $grade_item->get_parent_category();
526 $grade_category->set_parent($parent->id);
527 }
528 $fromform->gradecat = $grade_category->id;
529 }
530 foreach ($items as $itemid=>$unused) {
531 $items[$itemid]->set_parent($fromform->gradecat);
8c1c197e 532 if ($itemid == $grade_item->id) {
4b86bb08 533 // use updated grade_item
8c1c197e 534 $grade_item = $items[$itemid];
4b86bb08 535 }
536 }
537 }
538
71ee4471 539 // add outcomes if requested
d857e8b6 540 if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
61defed9 541 $grade_items = array();
542
d19f4770 543 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
544 $max_itemnumber = 999;
545 if ($items) {
546 foreach($items as $item) {
547 if ($item->itemnumber > $max_itemnumber) {
548 $max_itemnumber = $item->itemnumber;
549 }
550 }
551 }
552
71ee4471 553 foreach($outcomes as $outcome) {
554 $elname = 'outcome_'.$outcome->id;
61defed9 555
71ee4471 556 if (array_key_exists($elname, $fromform) and $fromform->$elname) {
d19f4770 557 // so we have a request for new outcome grade item?
4b86bb08 558 if ($items) {
71ee4471 559 foreach($items as $item) {
560 if ($item->outcomeid == $outcome->id) {
d19f4770 561 //outcome aready exists
562 continue 2;
71ee4471 563 }
564 }
565 }
d19f4770 566
567 $max_itemnumber++;
2d6df44f 568
4b86bb08 569 $outcome_item = new grade_item();
d857e8b6 570 $outcome_item->courseid = $course->id;
4b86bb08 571 $outcome_item->itemtype = 'mod';
572 $outcome_item->itemmodule = $fromform->modulename;
573 $outcome_item->iteminstance = $fromform->instance;
d19f4770 574 $outcome_item->itemnumber = $max_itemnumber;
4b86bb08 575 $outcome_item->itemname = $outcome->fullname;
576 $outcome_item->outcomeid = $outcome->id;
577 $outcome_item->gradetype = GRADE_TYPE_SCALE;
578 $outcome_item->scaleid = $outcome->scaleid;
579 $outcome_item->insert();
580
581 // move the new outcome into correct category and fix sortorder if needed
582 if ($grade_item) {
583 $outcome_item->set_parent($grade_item->categoryid);
584 $outcome_item->move_after_sortorder($grade_item->sortorder);
585
586 } else if (isset($fromform->gradecat)) {
587 $outcome_item->set_parent($fromform->gradecat);
2d6df44f 588 }
71ee4471 589 }
590 }
591 }
592
264867fd 593 rebuild_course_cache($course->id);
b45d8391 594 grade_regrade_final_grades($course->id);
595
aa54ed7b 596 if (isset($fromform->submitbutton)) {
0b19ec1e 597 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
0b19ec1e 598 } else {
55da5e81 599 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
6b467109 600 }
264867fd 601 exit;
602
52273766 603 } else {
217a8ee9 604 if (!empty($cm->id)) {
605 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
606 } else {
607 $context = get_context_instance(CONTEXT_COURSE, $course->id);
608 }
61defed9 609
d857e8b6 610 $streditinga = get_string('editinga', 'moodle', $fullmodulename);
611 $strmodulenameplural = get_string('modulenameplural', $module->name);
61defed9 612
3856ca56 613 $PAGE->navbar->add($streditinga);
480b5db6 614 $PAGE->set_heading($course->fullname);
3856ca56 615 $PAGE->set_title($streditinga);
616 $PAGE->set_focuscontrol($mform->focus());
617 $PAGE->set_cacheable(false);
d0ccc7e9 618 $PAGE->set_pagelayout('admin');
3856ca56 619 echo $OUTPUT->header();
264867fd 620
621 if (!empty($cm->id)) {
622 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
21090e44 623 $overridableroles = get_overridable_roles($context);
624 $assignableroles = get_assignable_roles($context);
264867fd 625 $currenttab = 'update';
d857e8b6 626 require($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
264867fd 627 }
aa6c1ced 628
3fc06691
PS
629 if (get_string_manager()->string_exists('modulename_hlp', $module->name)) {
630 echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
631 } else {
632 echo $OUTPUT->heading_with_help($pageheading, '', '', 'icon');
633 }
aa6c1ced 634
264867fd 635 $mform->display();
aa6c1ced 636
d60c1124 637 echo $OUTPUT->footer();
264867fd 638 }