MDL-10723 Removed all uses of print_navigation() throughout moodle, the function...
[moodle-pu.git] / course / modedit.php
blob7f575ec0908ce5b603eaa71bbac629e3a07b05d0
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 if (! $cw = get_record("course_sections", "section", $section, "course", $course->id)) {
33 error("This course section doesn't exist");
36 if (!course_allowed_module($course, $module->id)) {
37 error("This module has been disabled for this particular course");
40 $cm = null;
42 $form->section = $section; // The section number itself - relative!!! (section column in course_sections)
43 $form->visible = $cw->visible;
44 $form->course = $course->id;
45 $form->module = $module->id;
46 $form->modulename = $module->name;
47 $form->groupmode = $course->groupmode;
48 $form->groupingid = $course->defaultgroupingid;
49 $form->groupmembersonly = 0;
50 $form->instance = '';
51 $form->coursemodule = '';
52 $form->add = $add;
53 $form->return = 0; //must be false if this is an add, go back to course view on cancel
55 if (!empty($type)) {
56 $form->type = $type;
59 $sectionname = get_section_name($course->format);
60 $fullmodulename = get_string("modulename", $module->name);
62 if ($form->section && $course->format != 'site') {
63 $heading->what = $fullmodulename;
64 $heading->to = "$sectionname $form->section";
65 $pageheading = get_string("addinganewto", "moodle", $heading);
66 } else {
67 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
70 $CFG->pagepath = 'mod/'.$module->name;
71 if (!empty($type)) {
72 $CFG->pagepath .= '/'.$type;
73 } else {
74 $CFG->pagepath .= '/mod';
77 $navlinksinstancename = '';
79 } else if (!empty($update)) {
80 if (! $cm = get_record("course_modules", "id", $update)) {
81 error("This course module doesn't exist");
84 if (! $course = get_record("course", "id", $cm->course)) {
85 error("This course doesn't exist");
88 require_login($course); // needed to setup proper $COURSE
89 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
90 require_capability('moodle/course:manageactivities', $context);
92 if (! $module = get_record("modules", "id", $cm->module)) {
93 error("This module doesn't exist");
96 if (! $form = get_record($module->name, "id", $cm->instance)) {
97 error("The required instance of this module doesn't exist");
100 if (! $cw = get_record("course_sections", "id", $cm->section)) {
101 error("This course section doesn't exist");
104 $form->coursemodule = $cm->id;
105 $form->section = $cw->section; // The section number itself - relative!!! (section column in course_sections)
106 $form->visible = $cm->visible; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
107 $form->cmidnumber = $cm->idnumber; // The cm IDnumber
108 $form->groupmode = groupmode($COURSE,$cm); // locked later if forced
109 $form->groupingid = $cm->groupingid;
110 $form->groupmembersonly = $cm->groupmembersonly;
111 $form->course = $course->id;
112 $form->module = $module->id;
113 $form->modulename = $module->name;
114 $form->instance = $cm->instance;
115 $form->return = $return;
116 $form->update = $update;
118 // add existing outcomes
119 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename,
120 'iteminstance'=>$form->instance, 'courseid'=>$COURSE->id))) {
121 foreach ($items as $item) {
122 if (!empty($item->outcomeid)) {
123 $form->{'outcome_'.$item->outcomeid} = 1;
128 $sectionname = get_section_name($course->format);
129 $fullmodulename = get_string("modulename", $module->name);
131 if ($form->section && $course->format != 'site') {
132 $heading->what = $fullmodulename;
133 $heading->in = "$sectionname $cw->section";
134 $pageheading = get_string("updatingain", "moodle", $heading);
135 } else {
136 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
139 $navlinksinstancename = array('name' => format_string($form->name,true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
141 $CFG->pagepath = 'mod/'.$module->name;
142 if (!empty($type)) {
143 $CFG->pagepath .= '/'.$type;
144 } else {
145 $CFG->pagepath .= '/mod';
147 } else {
148 error('Invalid operation.');
151 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
152 if (file_exists($modmoodleform)) {
153 require_once($modmoodleform);
155 } else {
156 error('No formslib form description file found for this activity.');
159 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
160 if (file_exists($modlib)) {
161 include_once($modlib);
162 } else {
163 error("This module is missing important code! ($modlib)");
166 $mformclassname = 'mod_'.$module->name.'_mod_form';
167 $mform =& new $mformclassname($form->instance, $cw->section, $cm);
168 $mform->set_data($form);
170 if ($mform->is_cancelled()) {
171 if ($return && !empty($cm->id)){
172 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
173 } else {
174 redirect("view.php?id=$course->id#section-".$cw->section);
176 } else if ($fromform = $mform->get_data()) {
177 if (empty($fromform->coursemodule)) { //add
178 if (! $course = get_record("course", "id", $fromform->course)) {
179 error("This course doesn't exist");
181 $fromform->instance = '';
182 $fromform->coursemodule = '';
183 } else { //update
184 if (! $cm = get_record("course_modules", "id", $fromform->coursemodule)) {
185 error("This course module doesn't exist");
188 if (! $course = get_record("course", "id", $cm->course)) {
189 error("This course doesn't exist");
191 $fromform->instance = $cm->instance;
192 $fromform->coursemodule = $cm->id;
195 require_login($course->id); // needed to setup proper $COURSE
197 if (!empty($fromform->coursemodule)) {
198 $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
199 } else {
200 $context = get_context_instance(CONTEXT_COURSE, $course->id);
202 require_capability('moodle/course:manageactivities', $context);
204 $fromform->course = $course->id;
205 $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety
207 $addinstancefunction = $fromform->modulename."_add_instance";
208 $updateinstancefunction = $fromform->modulename."_update_instance";
210 if (!empty($fromform->update)) {
211 if (!isset($fromform->groupingid)) {
212 $fromform->groupingid = 0;
215 if (!isset($fromform->groupmembersonly)) {
216 $fromform->groupmembersonly = 0;
219 if (!isset($fromform->groupmode)) {
220 $fromform->groupmode = 0;
223 $returnfromfunc = $updateinstancefunction($fromform);
224 if (!$returnfromfunc) {
225 error("Could not update the $fromform->modulename", "view.php?id=$course->id");
227 if (is_string($returnfromfunc)) {
228 error($returnfromfunc, "view.php?id=$course->id");
231 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
232 set_coursemodule_groupmode($fromform->coursemodule, $fromform->groupmode);
233 set_coursemodule_groupingid($fromform->coursemodule, $fromform->groupingid);
234 set_coursemodule_groupmembersonly($fromform->coursemodule, $fromform->groupmembersonly);
236 // set cm id number
237 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
239 add_to_log($course->id, "course", "update mod",
240 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
241 "$fromform->modulename $fromform->instance");
242 add_to_log($course->id, $fromform->modulename, "update",
243 "view.php?id=$fromform->coursemodule",
244 "$fromform->instance", $fromform->coursemodule);
246 } else if (!empty($fromform->add)){
247 if (!course_allowed_module($course,$fromform->modulename)) {
248 error("This module ($fromform->modulename) has been disabled for this particular course");
251 if (!isset($fromform->groupingid)) {
252 $fromform->groupingid = 0;
255 if (!isset($fromform->groupmembersonly)) {
256 $fromform->groupmembersonly = 0;
259 if (!isset($fromform->groupmode)) {
260 $fromform->groupmode = 0;
263 $returnfromfunc = $addinstancefunction($fromform);
264 if (!$returnfromfunc) {
265 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
267 if (is_string($returnfromfunc)) {
268 error($returnfromfunc, "view.php?id=$course->id");
271 $fromform->instance = $returnfromfunc;
273 // course_modules and course_sections each contain a reference
274 // to each other, so we have to update one of them twice.
276 if (! $fromform->coursemodule = add_course_module($fromform) ) {
277 error("Could not add a new course module");
279 if (! $sectionid = add_mod_to_section($fromform) ) {
280 error("Could not add the new course module to that section");
283 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule)) {
284 error("Could not update the course module with the correct section");
287 // make sure visibility is set correctly (in particular in calendar)
288 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
290 // set cm idnumber
291 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
293 add_to_log($course->id, "course", "add mod",
294 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
295 "$fromform->modulename $fromform->instance");
296 add_to_log($course->id, $fromform->modulename, "add",
297 "view.php?id=$fromform->coursemodule",
298 "$fromform->instance", $fromform->coursemodule);
299 } else {
300 error("Data submitted is invalid.");
303 //sync idnumber with grade_item
304 if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
305 'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) {
306 if ($grade_item->idnumber != $fromform->cmidnumber) {
307 $grade_item->idnumber = $fromform->cmidnumber;
308 $grade_item->update();
312 // add outcomes if requested
313 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
314 $grade_items = array();
316 foreach($outcomes as $outcome) {
317 $elname = 'outcome_'.$outcome->id;
319 if (array_key_exists($elname, $fromform) and $fromform->$elname) {
320 // we have a request for new outcome grade item
321 $grade_item = new grade_item();
323 // Outcome grade_item.itemnumber start at 1000
324 $max_itemnumber = 999;
325 if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
326 'iteminstance'=>$fromform->instance, 'courseid'=>$COURSE->id))) {
327 $exists = false;
328 foreach($items as $item) {
329 if ($item->outcomeid == $outcome->id) {
330 $exists = true;
331 break;
333 if (empty($item->outcomeid)) {
334 continue;
336 if ($item->itemnumber > $max_itemnumber) {
337 $max_itemnumber = $item->itemnumber;
341 if ($exists) {
342 continue;
344 $grade_item->courseid = $COURSE->id;
345 $grade_item->itemtype = 'mod';
346 $grade_item->itemmodule = $fromform->modulename;
347 $grade_item->iteminstance = $fromform->instance;
348 $grade_item->itemnumber = $max_itemnumber + 1;
349 $grade_item->itemname = $outcome->fullname;
350 $grade_item->outcomeid = $outcome->id;
351 $grade_item->gradetype = GRADE_TYPE_SCALE;
352 $grade_item->scaleid = $outcome->scaleid;
354 $grade_item->insert();
356 // TODO comment on these next 4 lines
357 if ($item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule,
358 'iteminstance'=>$grade_item->iteminstance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) {
359 $grade_item->set_parent($item->categoryid);
360 $grade_item->move_after_sortorder($item->sortorder);
362 $grade_items[] = $grade_item;
366 // Create a grade_category to represent this module, if outcomes have been attached
367 if (!empty($grade_items)) {
368 // Add the module's normal grade_item as a child of this category
369 $item_params = array('itemtype'=>'mod',
370 'itemmodule'=>$fromform->modulename,
371 'iteminstance'=>$fromform->instance,
372 'itemnumber'=>0,
373 'courseid'=>$COURSE->id);
374 $item = grade_item::fetch($item_params);
376 // Only create the category if it will contain at least 2 items
377 if ($item OR count($grade_items) > 1) { // If we are here it means there is at least 1 outcome
378 $cat_params = array('courseid'=>$COURSE->id, 'fullname'=>$fromform->name);
379 $grade_category = grade_category::fetch($cat_params);
381 if (!$grade_category) {
382 $grade_category = new grade_category($cat_params);
383 $grade_category->courseid = $COURSE->id;
384 $grade_category->fullname = $fromform->name;
385 $grade_category->insert();
388 $sortorder = $grade_category->sortorder;
390 if ($item) {
391 $item->set_parent($grade_category->id);
392 $sortorder = $item->sortorder;
395 // Add the outcomes as children of this category
396 foreach ($grade_items as $gi) {
397 $gi->set_parent($grade_category->id);
398 $gi->move_after_sortorder($sortorder);
404 rebuild_course_cache($course->id);
406 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
407 exit;
409 } else {
410 if (!empty($cm->id)) {
411 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
412 } else {
413 $context = get_context_instance(CONTEXT_COURSE, $course->id);
415 require_capability('moodle/course:manageactivities', $context);
417 $streditinga = get_string("editinga", "moodle", $fullmodulename);
418 $strmodulenameplural = get_string("modulenameplural", $module->name);
420 $navlinks = array();
421 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
422 if ($navlinksinstancename) {
423 $navlinks[] = $navlinksinstancename;
425 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title');
427 $navigation = build_navigation($navlinks);
429 print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false);
431 if (!empty($cm->id)) {
432 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
433 $currenttab = 'update';
434 include_once($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
436 $icon = '<img src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt=""/>';
438 print_heading_with_help($pageheading, "mods", $module->name, $icon);
439 $mform->display();
440 print_footer($course);