MDL-29148 MNet - improved handling of a verification function return code
[moodle.git] / course / modedit.php
blob2170034bf06aa85d884bf3cddccdd6bac603e639
1 <?php // $Id$
3 // adds or updates modules in a course using new formslib
5 require_once("../config.php");
6 require_once("lib.php");
7 require_once($CFG->libdir.'/gradelib.php');
9 require_login();
11 $add = optional_param('add', 0, PARAM_ALPHA);
12 $update = optional_param('update', 0, PARAM_INT);
13 $return = optional_param('return', 0, PARAM_BOOL); //return to course/view.php if false or mod/modname/view.php if true
14 $type = optional_param('type', '', PARAM_ALPHANUM);
16 if (!empty($add)) {
17 $section = required_param('section', PARAM_INT);
18 $course = required_param('course', PARAM_INT);
20 if (! $course = get_record("course", "id", $course)) {
21 error("This course doesn't exist");
24 require_login($course);
25 $context = get_context_instance(CONTEXT_COURSE, $course->id);
26 require_capability('moodle/course:manageactivities', $context);
28 if (! $module = get_record("modules", "name", $add)) {
29 error("This module type doesn't exist");
32 $cw = get_course_section($section, $course->id);
34 if (!course_allowed_module($course, $module->id)) {
35 error("This module has been disabled for this particular course");
38 $cm = null;
40 $form->section = $section; // The section number itself - relative!!! (section column in course_sections)
41 $form->visible = $cw->visible;
42 $form->course = $course->id;
43 $form->module = $module->id;
44 $form->modulename = $module->name;
45 $form->groupmode = $course->groupmode;
46 $form->groupingid = $course->defaultgroupingid;
47 $form->groupmembersonly = 0;
48 $form->instance = '';
49 $form->coursemodule = '';
50 $form->add = $add;
51 $form->return = 0; //must be false if this is an add, go back to course view on cancel
53 // Turn off default grouping for modules that don't provide group mode
54 if($add=='resource' || $add=='glossary' || $add=='label') {
55 $form->groupingid=0;
58 if (!empty($type)) {
59 $form->type = $type;
62 $sectionname = get_section_name($course->format);
63 $fullmodulename = get_string("modulename", $module->name);
65 if ($form->section && $course->format != 'site') {
66 $heading->what = $fullmodulename;
67 $heading->to = "$sectionname $form->section";
68 $pageheading = get_string("addinganewto", "moodle", $heading);
69 } else {
70 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
73 $CFG->pagepath = 'mod/'.$module->name;
74 if (!empty($type)) {
75 $CFG->pagepath .= '/'.$type;
76 } else {
77 $CFG->pagepath .= '/mod';
80 $navlinksinstancename = '';
81 } else if (!empty($update)) {
82 if (! $cm = get_record("course_modules", "id", $update)) {
83 error("This course module doesn't exist");
86 if (! $course = get_record("course", "id", $cm->course)) {
87 error("This course doesn't exist");
90 require_login($course); // needed to setup proper $COURSE
91 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
92 require_capability('moodle/course:manageactivities', $context);
94 if (! $module = get_record("modules", "id", $cm->module)) {
95 error("This module doesn't exist");
98 if (! $form = get_record($module->name, "id", $cm->instance)) {
99 error("The required instance of this module doesn't exist");
102 if (! $cw = get_record("course_sections", "id", $cm->section)) {
103 error("This course section doesn't exist");
106 $form->coursemodule = $cm->id;
107 $form->section = $cw->section; // The section number itself - relative!!! (section column in course_sections)
108 $form->visible = $cm->visible; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
109 $form->cmidnumber = $cm->idnumber; // The cm IDnumber
110 $form->groupmode = groups_get_activity_groupmode($cm); // locked later if forced
111 $form->groupingid = $cm->groupingid;
112 $form->groupmembersonly = $cm->groupmembersonly;
113 $form->course = $course->id;
114 $form->module = $module->id;
115 $form->modulename = $module->name;
116 $form->instance = $cm->instance;
117 $form->return = $return;
118 $form->update = $update;
120 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename,
121 'iteminstance'=>$form->instance, 'courseid'=>$COURSE->id))) {
122 // add existing outcomes
123 foreach ($items as $item) {
124 if (!empty($item->outcomeid)) {
125 $form->{'outcome_'.$item->outcomeid} = 1;
129 // set category if present
130 $gradecat = false;
131 foreach ($items as $item) {
132 if ($gradecat === false) {
133 $gradecat = $item->categoryid;
134 continue;
136 if ($gradecat != $item->categoryid) {
137 //mixed categories
138 $gradecat = false;
139 break;
142 if ($gradecat !== false) {
143 // do not set if mixed categories present
144 $form->gradecat = $gradecat;
148 $sectionname = get_section_name($course->format);
149 $fullmodulename = get_string("modulename", $module->name);
151 if ($form->section && $course->format != 'site') {
152 $heading->what = $fullmodulename;
153 $heading->in = "$sectionname $cw->section";
154 $pageheading = get_string("updatingain", "moodle", $heading);
155 } else {
156 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
159 $navlinksinstancename = array('name' => format_string($form->name,true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
161 $CFG->pagepath = 'mod/'.$module->name;
162 if (!empty($type)) {
163 $CFG->pagepath .= '/'.$type;
164 } else {
165 $CFG->pagepath .= '/mod';
167 } else {
168 error('Invalid operation.');
171 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
172 if (file_exists($modmoodleform)) {
173 require_once($modmoodleform);
175 } else {
176 error('No formslib form description file found for this activity.');
179 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
180 if (file_exists($modlib)) {
181 include_once($modlib);
182 } else {
183 error("This module is missing important code! ($modlib)");
186 $mformclassname = 'mod_'.$module->name.'_mod_form';
187 $mform =& new $mformclassname($form->instance, $cw->section, $cm);
188 $mform->set_data($form);
190 if ($mform->is_cancelled()) {
191 if ($return && !empty($cm->id)){
192 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
193 } else {
194 redirect("view.php?id=$course->id#section-".$cw->section);
196 } else if ($fromform = $mform->get_data()) {
197 if (empty($fromform->coursemodule)) { //add
198 $cm = null;
199 if (! $course = get_record("course", "id", $fromform->course)) {
200 error("This course doesn't exist");
202 $fromform->instance = '';
203 $fromform->coursemodule = '';
204 } else { //update
205 if (! $cm = get_record("course_modules", "id", $fromform->coursemodule)) {
206 error("This course module doesn't exist");
209 if (! $course = get_record("course", "id", $cm->course)) {
210 error("This course doesn't exist");
212 $fromform->instance = $cm->instance;
213 $fromform->coursemodule = $cm->id;
216 require_login($course->id); // needed to setup proper $COURSE
218 if (!empty($fromform->coursemodule)) {
219 $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
220 } else {
221 $context = get_context_instance(CONTEXT_COURSE, $course->id);
223 require_capability('moodle/course:manageactivities', $context);
225 $fromform->course = $course->id;
226 $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety
228 $addinstancefunction = $fromform->modulename."_add_instance";
229 $updateinstancefunction = $fromform->modulename."_update_instance";
231 if (!isset($fromform->groupingid)) {
232 $fromform->groupingid = 0;
235 if (!isset($fromform->groupmembersonly)) {
236 $fromform->groupmembersonly = 0;
239 if (!isset($fromform->name)) { //label
240 $fromform->name = $fromform->modulename;
243 if (!empty($fromform->update)) {
245 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
246 $fromform->groupmode = $cm->groupmode; // keep original
249 $returnfromfunc = $updateinstancefunction($fromform);
250 if (!$returnfromfunc) {
251 error("Could not update the $fromform->modulename", "view.php?id=$course->id");
253 if (is_string($returnfromfunc)) {
254 error($returnfromfunc, "view.php?id=$course->id");
257 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
258 set_coursemodule_groupmode($fromform->coursemodule, $fromform->groupmode);
259 set_coursemodule_groupingid($fromform->coursemodule, $fromform->groupingid);
260 set_coursemodule_groupmembersonly($fromform->coursemodule, $fromform->groupmembersonly);
262 if (isset($fromform->cmidnumber)) { //label
263 // set cm idnumber
264 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
267 add_to_log($course->id, "course", "update mod",
268 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
269 "$fromform->modulename $fromform->instance");
270 add_to_log($course->id, $fromform->modulename, "update",
271 "view.php?id=$fromform->coursemodule",
272 "$fromform->instance", $fromform->coursemodule);
274 } else if (!empty($fromform->add)){
276 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
277 $fromform->groupmode = 0; // do not set groupmode
280 if (!course_allowed_module($course,$fromform->modulename)) {
281 error("This module ($fromform->modulename) has been disabled for this particular course");
284 $returnfromfunc = $addinstancefunction($fromform);
285 if (!$returnfromfunc) {
286 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
288 if (is_string($returnfromfunc)) {
289 error($returnfromfunc, "view.php?id=$course->id");
292 $fromform->instance = $returnfromfunc;
294 // course_modules and course_sections each contain a reference
295 // to each other, so we have to update one of them twice.
297 if (! $fromform->coursemodule = add_course_module($fromform) ) {
298 error("Could not add a new course module");
300 if (! $sectionid = add_mod_to_section($fromform) ) {
301 error("Could not add the new course module to that section");
304 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule)) {
305 error("Could not update the course module with the correct section");
308 // make sure visibility is set correctly (in particular in calendar)
309 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
311 if (isset($fromform->cmidnumber)) { //label
312 // set cm idnumber
313 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
316 add_to_log($course->id, "course", "add mod",
317 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
318 "$fromform->modulename $fromform->instance");
319 add_to_log($course->id, $fromform->modulename, "add",
320 "view.php?id=$fromform->coursemodule",
321 "$fromform->instance", $fromform->coursemodule);
322 } else {
323 error("Data submitted is invalid.");
326 // sync idnumber with grade_item
327 if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
328 'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) {
329 if ($grade_item->idnumber != $fromform->cmidnumber) {
330 $grade_item->idnumber = $fromform->cmidnumber;
331 $grade_item->update();
335 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
336 'iteminstance'=>$fromform->instance, 'courseid'=>$COURSE->id));
338 // create parent category if requested and move to correct parent category
339 if ($items and isset($fromform->gradecat)) {
340 if ($fromform->gradecat == -1) {
341 $grade_category = new grade_category();
342 $grade_category->courseid = $COURSE->id;
343 $grade_category->fullname = stripslashes($fromform->name);
344 $grade_category->insert();
345 if ($grade_item) {
346 $parent = $grade_item->get_parent_category();
347 $grade_category->set_parent($parent->id);
349 $fromform->gradecat = $grade_category->id;
351 foreach ($items as $itemid=>$unused) {
352 $items[$itemid]->set_parent($fromform->gradecat);
353 if ($itemid == $grade_item->id) {
354 // use updated grade_item
355 $grade_item = $items[$itemid];
360 // add outcomes if requested
361 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
362 $grade_items = array();
364 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
365 $max_itemnumber = 999;
366 if ($items) {
367 foreach($items as $item) {
368 if ($item->itemnumber > $max_itemnumber) {
369 $max_itemnumber = $item->itemnumber;
374 foreach($outcomes as $outcome) {
375 $elname = 'outcome_'.$outcome->id;
377 if (array_key_exists($elname, $fromform) and $fromform->$elname) {
378 // so we have a request for new outcome grade item?
379 if ($items) {
380 foreach($items as $item) {
381 if ($item->outcomeid == $outcome->id) {
382 //outcome aready exists
383 continue 2;
388 $max_itemnumber++;
390 $outcome_item = new grade_item();
391 $outcome_item->courseid = $COURSE->id;
392 $outcome_item->itemtype = 'mod';
393 $outcome_item->itemmodule = $fromform->modulename;
394 $outcome_item->iteminstance = $fromform->instance;
395 $outcome_item->itemnumber = $max_itemnumber;
396 $outcome_item->itemname = $outcome->fullname;
397 $outcome_item->outcomeid = $outcome->id;
398 $outcome_item->gradetype = GRADE_TYPE_SCALE;
399 $outcome_item->scaleid = $outcome->scaleid;
400 $outcome_item->insert();
402 // move the new outcome into correct category and fix sortorder if needed
403 if ($grade_item) {
404 $outcome_item->set_parent($grade_item->categoryid);
405 $outcome_item->move_after_sortorder($grade_item->sortorder);
407 } else if (isset($fromform->gradecat)) {
408 $outcome_item->set_parent($fromform->gradecat);
414 rebuild_course_cache($course->id);
415 grade_regrade_final_grades($course->id);
417 if (isset($fromform->submitbutton)) {
418 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
419 } else {
420 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
422 exit;
424 } else {
425 if (!empty($cm->id)) {
426 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
427 } else {
428 $context = get_context_instance(CONTEXT_COURSE, $course->id);
430 require_capability('moodle/course:manageactivities', $context);
432 $streditinga = get_string("editinga", "moodle", $fullmodulename);
433 $strmodulenameplural = get_string("modulenameplural", $module->name);
435 $navlinks = array();
436 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
437 if ($navlinksinstancename) {
438 $navlinks[] = $navlinksinstancename;
440 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title');
442 $navigation = build_navigation($navlinks);
444 print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false);
446 if (!empty($cm->id)) {
447 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
448 $overridableroles = get_overridable_roles($context);
449 $assignableroles = get_assignable_roles($context);
450 $currenttab = 'update';
451 include_once($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
453 $icon = '<img src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt=""/>';
455 print_heading_with_help($pageheading, "mods", $module->name, $icon);
456 $mform->display();
457 print_footer($course);