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 // add existing outcomes
116 if ($items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename
,
117 'iteminstance'=>$form->instance
, 'courseid'=>$COURSE->id
))) {
118 foreach ($items as $item) {
119 if (!empty($item->outcomeid
)) {
120 $form->{'outcome_'.$item->outcomeid
} = 1;
125 $sectionname = get_section_name($course->format
);
126 $fullmodulename = get_string("modulename", $module->name
);
128 if ($form->section
&& $course->format
!= 'site') {
129 $heading->what
= $fullmodulename;
130 $heading->in
= "$sectionname $cw->section";
131 $pageheading = get_string("updatingain", "moodle", $heading);
133 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
136 $navlinksinstancename = array('name' => format_string($form->name
,true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
138 $CFG->pagepath
= 'mod/'.$module->name
;
140 $CFG->pagepath
.= '/'.$type;
142 $CFG->pagepath
.= '/mod';
145 error('Invalid operation.');
148 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
149 if (file_exists($modmoodleform)) {
150 require_once($modmoodleform);
153 error('No formslib form description file found for this activity.');
156 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
157 if (file_exists($modlib)) {
158 include_once($modlib);
160 error("This module is missing important code! ($modlib)");
163 $mformclassname = 'mod_'.$module->name
.'_mod_form';
164 $mform =& new $mformclassname($form->instance
, $cw->section
, $cm);
165 $mform->set_data($form);
167 if ($mform->is_cancelled()) {
168 if ($return && !empty($cm->id
)){
169 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
171 redirect("view.php?id=$course->id#section-".$cw->section
);
173 } else if ($fromform = $mform->get_data()) {
174 if (empty($fromform->coursemodule
)) { //add
175 if (! $course = get_record("course", "id", $fromform->course
)) {
176 error("This course doesn't exist");
178 $fromform->instance
= '';
179 $fromform->coursemodule
= '';
181 if (! $cm = get_record("course_modules", "id", $fromform->coursemodule
)) {
182 error("This course module doesn't exist");
185 if (! $course = get_record("course", "id", $cm->course
)) {
186 error("This course doesn't exist");
188 $fromform->instance
= $cm->instance
;
189 $fromform->coursemodule
= $cm->id
;
192 require_login($course->id
); // needed to setup proper $COURSE
194 if (!empty($fromform->coursemodule
)) {
195 $context = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
197 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
199 require_capability('moodle/course:manageactivities', $context);
201 $fromform->course
= $course->id
;
202 $fromform->modulename
= clean_param($fromform->modulename
, PARAM_SAFEDIR
); // For safety
204 $addinstancefunction = $fromform->modulename
."_add_instance";
205 $updateinstancefunction = $fromform->modulename
."_update_instance";
207 if (!isset($fromform->groupingid
)) {
208 $fromform->groupingid
= 0;
211 if (!isset($fromform->groupmembersonly
)) {
212 $fromform->groupmembersonly
= 0;
215 if (!isset($fromform->groupmode
)) {
216 $fromform->groupmode
= 0;
219 if (!isset($fromform->name
)) { //label
220 $fromform->name
= $fromform->modulename
;
223 if (!empty($fromform->update
)) {
225 $returnfromfunc = $updateinstancefunction($fromform);
226 if (!$returnfromfunc) {
227 error("Could not update the $fromform->modulename", "view.php?id=$course->id");
229 if (is_string($returnfromfunc)) {
230 error($returnfromfunc, "view.php?id=$course->id");
233 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
234 set_coursemodule_groupmode($fromform->coursemodule
, $fromform->groupmode
);
235 set_coursemodule_groupingid($fromform->coursemodule
, $fromform->groupingid
);
236 set_coursemodule_groupmembersonly($fromform->coursemodule
, $fromform->groupmembersonly
);
238 if (isset($fromform->cmidnumber
)) { //label
240 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
243 add_to_log($course->id
, "course", "update mod",
244 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
245 "$fromform->modulename $fromform->instance");
246 add_to_log($course->id
, $fromform->modulename
, "update",
247 "view.php?id=$fromform->coursemodule",
248 "$fromform->instance", $fromform->coursemodule
);
250 } else if (!empty($fromform->add
)){
252 if (!course_allowed_module($course,$fromform->modulename
)) {
253 error("This module ($fromform->modulename) has been disabled for this particular course");
256 $returnfromfunc = $addinstancefunction($fromform);
257 if (!$returnfromfunc) {
258 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
260 if (is_string($returnfromfunc)) {
261 error($returnfromfunc, "view.php?id=$course->id");
264 $fromform->instance
= $returnfromfunc;
266 // course_modules and course_sections each contain a reference
267 // to each other, so we have to update one of them twice.
269 if (! $fromform->coursemodule
= add_course_module($fromform) ) {
270 error("Could not add a new course module");
272 if (! $sectionid = add_mod_to_section($fromform) ) {
273 error("Could not add the new course module to that section");
276 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule
)) {
277 error("Could not update the course module with the correct section");
280 // make sure visibility is set correctly (in particular in calendar)
281 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
283 if (isset($fromform->cmidnumber
)) { //label
285 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
288 add_to_log($course->id
, "course", "add mod",
289 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
290 "$fromform->modulename $fromform->instance");
291 add_to_log($course->id
, $fromform->modulename
, "add",
292 "view.php?id=$fromform->coursemodule",
293 "$fromform->instance", $fromform->coursemodule
);
295 error("Data submitted is invalid.");
298 //sync idnumber with grade_item
299 if ($grade_item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
300 'iteminstance'=>$fromform->instance
, 'itemnumber'=>0, 'courseid'=>$COURSE->id
))) {
301 if ($grade_item->idnumber
!= $fromform->cmidnumber
) {
302 $grade_item->idnumber
= $fromform->cmidnumber
;
303 $grade_item->update();
307 // add outcomes if requested
308 if ($outcomes = grade_outcome
::fetch_all_available($COURSE->id
)) {
309 $grade_items = array();
311 foreach($outcomes as $outcome) {
312 $elname = 'outcome_'.$outcome->id
;
314 if (array_key_exists($elname, $fromform) and $fromform->$elname) {
315 // we have a request for new outcome grade item
316 $grade_item = new grade_item();
318 // Outcome grade_item.itemnumber start at 1000
319 $max_itemnumber = 999;
320 if ($items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
321 'iteminstance'=>$fromform->instance
, 'courseid'=>$COURSE->id
))) {
323 foreach($items as $item) {
324 if ($item->outcomeid
== $outcome->id
) {
328 if (empty($item->outcomeid
)) {
331 if ($item->itemnumber
> $max_itemnumber) {
332 $max_itemnumber = $item->itemnumber
;
339 $grade_item->courseid
= $COURSE->id
;
340 $grade_item->itemtype
= 'mod';
341 $grade_item->itemmodule
= $fromform->modulename
;
342 $grade_item->iteminstance
= $fromform->instance
;
343 $grade_item->itemnumber
= $max_itemnumber +
1;
344 $grade_item->itemname
= $outcome->fullname
;
345 $grade_item->outcomeid
= $outcome->id
;
346 $grade_item->gradetype
= GRADE_TYPE_SCALE
;
347 $grade_item->scaleid
= $outcome->scaleid
;
349 $grade_item->insert();
351 // TODO comment on these next 4 lines
352 if ($item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule
,
353 'iteminstance'=>$grade_item->iteminstance
, 'itemnumber'=>0, 'courseid'=>$COURSE->id
))) {
354 $grade_item->set_parent($item->categoryid
);
355 $grade_item->move_after_sortorder($item->sortorder
);
357 $grade_items[] = $grade_item;
361 // Create a grade_category to represent this module, if outcomes have been attached
362 if (!empty($grade_items)) {
363 // Add the module's normal grade_item as a child of this category
364 $item_params = array('itemtype'=>'mod',
365 'itemmodule'=>$fromform->modulename
,
366 'iteminstance'=>$fromform->instance
,
368 'courseid'=>$COURSE->id
);
369 $item = grade_item
::fetch($item_params);
371 // Only create the category if it will contain at least 2 items
372 if ($item OR count($grade_items) > 1) { // If we are here it means there is at least 1 outcome
373 $cat_params = array('courseid'=>$COURSE->id
, 'fullname'=>$fromform->name
);
374 $grade_category = grade_category
::fetch($cat_params);
376 if (!$grade_category) {
377 $grade_category = new grade_category($cat_params);
378 $grade_category->courseid
= $COURSE->id
;
379 $grade_category->fullname
= $fromform->name
;
380 $grade_category->insert();
383 $sortorder = $grade_category->sortorder
;
386 $item->set_parent($grade_category->id
);
387 $sortorder = $item->sortorder
;
390 // Add the outcomes as children of this category
391 foreach ($grade_items as $gi) {
392 $gi->set_parent($grade_category->id
);
393 $gi->move_after_sortorder($sortorder);
399 rebuild_course_cache($course->id
);
401 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
405 if (!empty($cm->id
)) {
406 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
408 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
410 require_capability('moodle/course:manageactivities', $context);
412 $streditinga = get_string("editinga", "moodle", $fullmodulename);
413 $strmodulenameplural = get_string("modulenameplural", $module->name
);
416 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
417 if ($navlinksinstancename) {
418 $navlinks[] = $navlinksinstancename;
420 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title');
422 $navigation = build_navigation($navlinks);
424 print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false);
426 if (!empty($cm->id
)) {
427 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
428 $currenttab = 'update';
429 include_once($CFG->dirroot
.'/'.$CFG->admin
.'/roles/tabs.php');
431 $icon = '<img src="'.$CFG->modpixpath
.'/'.$module->name
.'/icon.gif" alt=""/>';
433 print_heading_with_help($pageheading, "mods", $module->name
, $icon);
435 print_footer($course);