| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php // $Id: modedit.php,v 1.34.2.12 2008/07/21 11:46:00 sam_marshall Exp $ 2 3 // adds or updates modules in a course using new formslib 4 5 require_once("../config.php"); 6 require_once ("lib.php"); 7 require_once($CFG->libdir.'/gradelib.php'); 8 9 require_login(); 10 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); 15 16 if (!empty($add)) { 17 $section = required_param('section', PARAM_INT); 18 $course = required_param('course', PARAM_INT); 19 20 if (! $course = get_record("course", "id", $course)) { 21 error("This course doesn't exist"); 22 } 23 24 require_login($course); 25 $context = get_context_instance(CONTEXT_COURSE, $course->id); 26 require_capability('moodle/course:manageactivities', $context); 27 28 if (! $module = get_record("modules", "name", $add)) { 29 error("This module type doesn't exist"); 30 } 31 32 $cw = get_course_section($section, $course->id); 33 34 if (!course_allowed_module($course, $module->id)) { 35 error("This module has been disabled for this particular course"); 36 } 37 38 $cm = null; 39 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 52 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; 56 } 57 58 if (!empty($type)) { 59 $form->type = $type; 60 } 61 62 $sectionname = get_section_name($course->format); 63 $fullmodulename = get_string("modulename", $module->name); 64 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); 71 } 72 73 $CFG->pagepath = 'mod/'.$module->name; 74 if (!empty($type)) { 75 $CFG->pagepath .= '/'.$type; 76 } else { 77 $CFG->pagepath .= '/mod'; 78 } 79 80 $navlinksinstancename = ''; 81 } else if (!empty($update)) { 82 if (! $cm = get_record("course_modules", "id", $update)) { 83 error("This course module doesn't exist"); 84 } 85 86 if (! $course = get_record("course", "id", $cm->course)) { 87 error("This course doesn't exist"); 88 } 89 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); 93 94 if (! $module = get_record("modules", "id", $cm->module)) { 95 error("This module doesn't exist"); 96 } 97 98 if (! $form = get_record($module->name, "id", $cm->instance)) { 99 error("The required instance of this module doesn't exist"); 100 } 101 102 if (! $cw = get_record("course_sections", "id", $cm->section)) { 103 error("This course section doesn't exist"); 104 } 105 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; 119 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; 126 } 127 } 128 129 // set category if present 130 $gradecat = false; 131 foreach ($items as $item) { 132 if ($gradecat === false) { 133 $gradecat = $item->categoryid; 134 continue; 135 } 136 if ($gradecat != $item->categoryid) { 137 //mixed categories 138 $gradecat = false; 139 break; 140 } 141 } 142 if ($gradecat !== false) { 143 // do not set if mixed categories present 144 $form->gradecat = $gradecat; 145 } 146 } 147 148 $sectionname = get_section_name($course->format); 149 $fullmodulename = get_string("modulename", $module->name); 150 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); 157 } 158 159 $navlinksinstancename = array('name' => format_string($form->name,true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance'); 160 161 $CFG->pagepath = 'mod/'.$module->name; 162 if (!empty($type)) { 163 $CFG->pagepath .= '/'.$type; 164 } else { 165 $CFG->pagepath .= '/mod'; 166 } 167 } else { 168 error('Invalid operation.'); 169 } 170 171 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php"; 172 if (file_exists($modmoodleform)) { 173 require_once($modmoodleform); 174 175 } else { 176 error('No formslib form description file found for this activity.'); 177 } 178 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)"); 184 } 185 186 $mformclassname = 'mod_'.$module->name.'_mod_form'; 187 $mform =& new $mformclassname($form->instance, $cw->section, $cm); 188 $mform->set_data($form); 189 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); 195 } 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"); 201 } 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"); 207 } 208 209 if (! $course = get_record("course", "id", $cm->course)) { 210 error("This course doesn't exist"); 211 } 212 $fromform->instance = $cm->instance; 213 $fromform->coursemodule = $cm->id; 214 } 215 216 require_login($course->id); // needed to setup proper $COURSE 217 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); 222 } 223 require_capability('moodle/course:manageactivities', $context); 224 225 $fromform->course = $course->id; 226 $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety 227 228 $addinstancefunction = $fromform->modulename."_add_instance"; 229 $updateinstancefunction = $fromform->modulename."_update_instance"; 230 231 if (!isset($fromform->groupingid)) { 232 $fromform->groupingid = 0; 233 } 234 235 if (!isset($fromform->groupmembersonly)) { 236 $fromform->groupmembersonly = 0; 237 } 238 239 if (!isset($fromform->name)) { //label 240 $fromform->name = $fromform->modulename; 241 } 242 243 if (!empty($fromform->update)) { 244 245 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) { 246 $fromform->groupmode = $cm->groupmode; // keep original 247 } 248 249 $returnfromfunc = $updateinstancefunction($fromform); 250 if (!$returnfromfunc) { 251 error("Could not update the $fromform->modulename", "view.php?id=$course->id"); 252 } 253 if (is_string($returnfromfunc)) { 254 error($returnfromfunc, "view.php?id=$course->id"); 255 } 256 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); 261 262 if (isset($fromform->cmidnumber)) { //label 263 // set cm idnumber 264 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber); 265 } 266 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); 273 274 } else if (!empty($fromform->add)){ 275 276 if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) { 277 $fromform->groupmode = 0; // do not set groupmode 278 } 279 280 if (!course_allowed_module($course,$fromform->modulename)) { 281 error("This module ($fromform->modulename) has been disabled for this particular course"); 282 } 283 284 $returnfromfunc = $addinstancefunction($fromform); 285 if (!$returnfromfunc) { 286 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id"); 287 } 288 if (is_string($returnfromfunc)) { 289 error($returnfromfunc, "view.php?id=$course->id"); 290 } 291 292 $fromform->instance = $returnfromfunc; 293 294 // course_modules and course_sections each contain a reference 295 // to each other, so we have to update one of them twice. 296 297 if (! $fromform->coursemodule = add_course_module($fromform) ) { 298 error("Could not add a new course module"); 299 } 300 if (! $sectionid = add_mod_to_section($fromform) ) { 301 error("Could not add the new course module to that section"); 302 } 303 304 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule)) { 305 error("Could not update the course module with the correct section"); 306 } 307 308 // make sure visibility is set correctly (in particular in calendar) 309 set_coursemodule_visible($fromform->coursemodule, $fromform->visible); 310 311 if (isset($fromform->cmidnumber)) { //label 312 // set cm idnumber 313 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber); 314 } 315 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."); 324 } 325 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(); 332 } 333 } 334 335 $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename, 336 'iteminstance'=>$fromform->instance, 'courseid'=>$COURSE->id)); 337 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); 348 } 349 $fromform->gradecat = $grade_category->id; 350 } 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]; 356 } 357 } 358 } 359 360 // add outcomes if requested 361 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) { 362 $grade_items = array(); 363 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; 370 } 371 } 372 } 373 374 foreach($outcomes as $outcome) { 375 $elname = 'outcome_'.$outcome->id; 376 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; 384 } 385 } 386 } 387 388 $max_itemnumber++; 389 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(); 401 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); 406 407 } else if (isset($fromform->gradecat)) { 408 $outcome_item->set_parent($fromform->gradecat); 409 } 410 } 411 } 412 } 413 414 rebuild_course_cache($course->id); 415 grade_regrade_final_grades($course->id); 416 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"); 421 } 422 exit; 423 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); 429 } 430 require_capability('moodle/course:manageactivities', $context); 431 432 $streditinga = get_string("editinga", "moodle", $fullmodulename); 433 $strmodulenameplural = get_string("modulenameplural", $module->name); 434 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; 439 } 440 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title'); 441 442 $navigation = build_navigation($navlinks); 443 444 print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false); 445 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'); 452 } 453 $icon = '<img src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt=""/>'; 454 455 print_heading_with_help($pageheading, "mods", $module->name, $icon); 456 $mform->display(); 457 print_footer($course); 458 } 459 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |