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');
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
);
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");
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;
49 $form->coursemodule
= '';
51 $form->return = 0; //must be false if this is an add, go back to course view on cancel
57 $sectionname = get_section_name($course->format
);
58 $fullmodulename = get_string("modulename", $module->name
);
60 if ($form->section
&& $course->format
!= 'site') {
61 $heading->what
= $fullmodulename;
62 $heading->to
= "$sectionname $form->section";
63 $pageheading = get_string("addinganewto", "moodle", $heading);
65 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
68 $CFG->pagepath
= 'mod/'.$module->name
;
70 $CFG->pagepath
.= '/'.$type;
72 $CFG->pagepath
.= '/mod';
75 $navlinksinstancename = '';
76 } else if (!empty($update)) {
77 if (! $cm = get_record("course_modules", "id", $update)) {
78 error("This course module doesn't exist");
81 if (! $course = get_record("course", "id", $cm->course
)) {
82 error("This course doesn't exist");
85 require_login($course); // needed to setup proper $COURSE
86 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
87 require_capability('moodle/course:manageactivities', $context);
89 if (! $module = get_record("modules", "id", $cm->module
)) {
90 error("This module doesn't exist");
93 if (! $form = get_record($module->name
, "id", $cm->instance
)) {
94 error("The required instance of this module doesn't exist");
97 if (! $cw = get_record("course_sections", "id", $cm->section
)) {
98 error("This course section doesn't exist");
101 $form->coursemodule
= $cm->id
;
102 $form->section
= $cw->section
; // The section number itself - relative!!! (section column in course_sections)
103 $form->visible
= $cm->visible
; //?? $cw->visible ? $cm->visible : 0; // section hiding overrides
104 $form->cmidnumber
= $cm->idnumber
; // The cm IDnumber
105 $form->groupmode
= groups_get_activity_groupmode($cm); // locked later if forced
106 $form->groupingid
= $cm->groupingid
;
107 $form->groupmembersonly
= $cm->groupmembersonly
;
108 $form->course
= $course->id
;
109 $form->module
= $module->id
;
110 $form->modulename
= $module->name
;
111 $form->instance
= $cm->instance
;
112 $form->return = $return;
113 $form->update
= $update;
115 if ($items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename
,
116 'iteminstance'=>$form->instance
, 'courseid'=>$COURSE->id
))) {
117 // add existing outcomes
118 foreach ($items as $item) {
119 if (!empty($item->outcomeid
)) {
120 $form->{'outcome_'.$item->outcomeid
} = 1;
124 // set category if present
126 foreach ($items as $item) {
127 if ($gradecat === false) {
128 $gradecat = $item->categoryid
;
131 if ($gradecat != $item->categoryid
) {
137 if ($gradecat !== false) {
138 // do not set if mixed categories present
139 $form->gradecat
= $gradecat;
143 $sectionname = get_section_name($course->format
);
144 $fullmodulename = get_string("modulename", $module->name
);
146 if ($form->section
&& $course->format
!= 'site') {
147 $heading->what
= $fullmodulename;
148 $heading->in
= "$sectionname $cw->section";
149 $pageheading = get_string("updatingain", "moodle", $heading);
151 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
154 $navlinksinstancename = array('name' => format_string($form->name
,true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
156 $CFG->pagepath
= 'mod/'.$module->name
;
158 $CFG->pagepath
.= '/'.$type;
160 $CFG->pagepath
.= '/mod';
163 error('Invalid operation.');
166 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
167 if (file_exists($modmoodleform)) {
168 require_once($modmoodleform);
171 error('No formslib form description file found for this activity.');
174 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
175 if (file_exists($modlib)) {
176 include_once($modlib);
178 error("This module is missing important code! ($modlib)");
181 $mformclassname = 'mod_'.$module->name
.'_mod_form';
182 $mform =& new $mformclassname($form->instance
, $cw->section
, $cm);
183 $mform->set_data($form);
185 if ($mform->is_cancelled()) {
186 if ($return && !empty($cm->id
)){
187 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
189 redirect("view.php?id=$course->id#section-".$cw->section
);
191 } else if ($fromform = $mform->get_data()) {
192 if (empty($fromform->coursemodule
)) { //add
194 if (! $course = get_record("course", "id", $fromform->course
)) {
195 error("This course doesn't exist");
197 $fromform->instance
= '';
198 $fromform->coursemodule
= '';
200 if (! $cm = get_record("course_modules", "id", $fromform->coursemodule
)) {
201 error("This course module doesn't exist");
204 if (! $course = get_record("course", "id", $cm->course
)) {
205 error("This course doesn't exist");
207 $fromform->instance
= $cm->instance
;
208 $fromform->coursemodule
= $cm->id
;
211 require_login($course->id
); // needed to setup proper $COURSE
213 if (!empty($fromform->coursemodule
)) {
214 $context = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
216 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
218 require_capability('moodle/course:manageactivities', $context);
220 $fromform->course
= $course->id
;
221 $fromform->modulename
= clean_param($fromform->modulename
, PARAM_SAFEDIR
); // For safety
223 $addinstancefunction = $fromform->modulename
."_add_instance";
224 $updateinstancefunction = $fromform->modulename
."_update_instance";
226 if (!isset($fromform->groupingid
)) {
227 $fromform->groupingid
= 0;
230 if (!isset($fromform->groupmembersonly
)) {
231 $fromform->groupmembersonly
= 0;
234 if (!isset($fromform->name
)) { //label
235 $fromform->name
= $fromform->modulename
;
238 if (!empty($fromform->update
)) {
240 if (!empty($course->groupmodeforce
) or !isset($fromform->groupmode
)) {
241 $fromform->groupmode
= $cm->groupmode
; // keep original
244 $returnfromfunc = $updateinstancefunction($fromform);
245 if (!$returnfromfunc) {
246 error("Could not update the $fromform->modulename", "view.php?id=$course->id");
248 if (is_string($returnfromfunc)) {
249 error($returnfromfunc, "view.php?id=$course->id");
252 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
253 set_coursemodule_groupmode($fromform->coursemodule
, $fromform->groupmode
);
254 set_coursemodule_groupingid($fromform->coursemodule
, $fromform->groupingid
);
255 set_coursemodule_groupmembersonly($fromform->coursemodule
, $fromform->groupmembersonly
);
257 if (isset($fromform->cmidnumber
)) { //label
259 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
262 add_to_log($course->id
, "course", "update mod",
263 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
264 "$fromform->modulename $fromform->instance");
265 add_to_log($course->id
, $fromform->modulename
, "update",
266 "view.php?id=$fromform->coursemodule",
267 "$fromform->instance", $fromform->coursemodule
);
269 } else if (!empty($fromform->add
)){
271 if (!empty($course->groupmodeforce
) or !isset($fromform->groupmode
)) {
272 $fromform->groupmode
= 0; // do not set groupmode
275 if (!course_allowed_module($course,$fromform->modulename
)) {
276 error("This module ($fromform->modulename) has been disabled for this particular course");
279 $returnfromfunc = $addinstancefunction($fromform);
280 if (!$returnfromfunc) {
281 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
283 if (is_string($returnfromfunc)) {
284 error($returnfromfunc, "view.php?id=$course->id");
287 $fromform->instance
= $returnfromfunc;
289 // course_modules and course_sections each contain a reference
290 // to each other, so we have to update one of them twice.
292 if (! $fromform->coursemodule
= add_course_module($fromform) ) {
293 error("Could not add a new course module");
295 if (! $sectionid = add_mod_to_section($fromform) ) {
296 error("Could not add the new course module to that section");
299 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule
)) {
300 error("Could not update the course module with the correct section");
303 // make sure visibility is set correctly (in particular in calendar)
304 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
306 if (isset($fromform->cmidnumber
)) { //label
308 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
311 add_to_log($course->id
, "course", "add mod",
312 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
313 "$fromform->modulename $fromform->instance");
314 add_to_log($course->id
, $fromform->modulename
, "add",
315 "view.php?id=$fromform->coursemodule",
316 "$fromform->instance", $fromform->coursemodule
);
318 error("Data submitted is invalid.");
321 // sync idnumber with grade_item
322 if ($grade_item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
323 'iteminstance'=>$fromform->instance
, 'itemnumber'=>0, 'courseid'=>$COURSE->id
))) {
324 if ($grade_item->idnumber
!= $fromform->cmidnumber
) {
325 $grade_item->idnumber
= $fromform->cmidnumber
;
326 $grade_item->update();
330 $items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
331 'iteminstance'=>$fromform->instance
, 'courseid'=>$COURSE->id
));
333 // create parent category if requested and move to correct parent category
334 if ($items and isset($fromform->gradecat
)) {
335 if ($fromform->gradecat
== -1) {
336 $grade_category = new grade_category();
337 $grade_category->courseid
= $COURSE->id
;
338 $grade_category->fullname
= stripslashes($fromform->name
);
339 $grade_category->insert();
341 $parent = $grade_item->get_parent_category();
342 $grade_category->set_parent($parent->id
);
344 $fromform->gradecat
= $grade_category->id
;
346 foreach ($items as $itemid=>$unused) {
347 $items[$itemid]->set_parent($fromform->gradecat
);
348 if ($itemid == $grade_item->id
) {
349 // use updated grade_item
350 $grade_item = $items[$itemid];
355 // add outcomes if requested
356 if ($outcomes = grade_outcome
::fetch_all_available($COURSE->id
)) {
357 $grade_items = array();
359 // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
360 $max_itemnumber = 999;
362 foreach($items as $item) {
363 if ($item->itemnumber
> $max_itemnumber) {
364 $max_itemnumber = $item->itemnumber
;
369 foreach($outcomes as $outcome) {
370 $elname = 'outcome_'.$outcome->id
;
372 if (array_key_exists($elname, $fromform) and $fromform->$elname) {
373 // so we have a request for new outcome grade item?
375 foreach($items as $item) {
376 if ($item->outcomeid
== $outcome->id
) {
377 //outcome aready exists
385 $outcome_item = new grade_item();
386 $outcome_item->courseid
= $COURSE->id
;
387 $outcome_item->itemtype
= 'mod';
388 $outcome_item->itemmodule
= $fromform->modulename
;
389 $outcome_item->iteminstance
= $fromform->instance
;
390 $outcome_item->itemnumber
= $max_itemnumber;
391 $outcome_item->itemname
= $outcome->fullname
;
392 $outcome_item->outcomeid
= $outcome->id
;
393 $outcome_item->gradetype
= GRADE_TYPE_SCALE
;
394 $outcome_item->scaleid
= $outcome->scaleid
;
395 $outcome_item->insert();
397 // move the new outcome into correct category and fix sortorder if needed
399 $outcome_item->set_parent($grade_item->categoryid
);
400 $outcome_item->move_after_sortorder($grade_item->sortorder
);
402 } else if (isset($fromform->gradecat
)) {
403 $outcome_item->set_parent($fromform->gradecat
);
409 rebuild_course_cache($course->id
);
410 grade_regrade_final_grades($course->id
);
412 if (isset($fromform->submitbutton
)) {
413 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
415 redirect("$CFG->wwwroot/course/view.php?id=$course->id");
420 if (!empty($cm->id
)) {
421 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
423 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
425 require_capability('moodle/course:manageactivities', $context);
427 $streditinga = get_string("editinga", "moodle", $fullmodulename);
428 $strmodulenameplural = get_string("modulenameplural", $module->name
);
431 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
432 if ($navlinksinstancename) {
433 $navlinks[] = $navlinksinstancename;
435 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title');
437 $navigation = build_navigation($navlinks);
439 print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false);
441 if (!empty($cm->id
)) {
442 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
443 $overridableroles = get_overridable_roles($context);
444 $assignableroles = get_assignable_roles($context);
445 $currenttab = 'update';
446 include_once($CFG->dirroot
.'/'.$CFG->admin
.'/roles/tabs.php');
448 $icon = '<img src="'.$CFG->modpixpath
.'/'.$module->name
.'/icon.gif" alt=""/>';
450 print_heading_with_help($pageheading, "mods", $module->name
, $icon);
452 print_footer($course);