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', '', PARAM_ALPHA
);
12 $update = optional_param('update', 0, PARAM_INT
);
13 //return to course/view.php if false or mod/modname/view.php if true
14 $return = optional_param('return', 0, PARAM_BOOL
);
15 $type = optional_param('type', '', PARAM_ALPHANUM
);
18 $section = required_param('section', PARAM_INT
);
19 $course = required_param('course', PARAM_INT
);
21 if (! $course = get_record("course", "id", $course)) {
22 error("This course doesn't exist");
25 if (! $module = get_record("modules", "name", $add)) {
26 error("This module type doesn't exist");
29 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
30 require_capability('moodle/course:manageactivities', $context);
32 if (!course_allowed_module($course, $module->id
)) {
33 error("This module has been disabled for this particular course");
36 require_login($course->id
); // needed to setup proper $COURSE
38 $form->section
= $section; // The section number itself
39 $form->course
= $course->id
;
40 $form->module
= $module->id
;
41 $form->modulename
= $module->name
;
43 $form->coursemodule
= "";
45 $form->return=0;//must be false if this is an add, go back to course view on cancel
50 $sectionname = get_section_name($course->format
);
51 $fullmodulename = get_string("modulename", $module->name
);
53 if ($form->section
&& $course->format
!= 'site') {
54 $heading->what
= $fullmodulename;
55 $heading->to
= "$sectionname $form->section";
56 $pageheading = get_string("addinganewto", "moodle", $heading);
58 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
61 $CFG->pagepath
= 'mod/'.$module->name
;
63 $CFG->pagepath
.= '/'.$type;
65 $CFG->pagepath
.= '/mod';
68 } else if (!empty($update)) {
69 if (! $cm = get_record("course_modules", "id", $update)) {
70 error("This course module doesn't exist");
73 if (! $course = get_record("course", "id", $cm->course
)) {
74 error("This course doesn't exist");
77 require_login($course->id
); // needed to setup proper $COURSE
78 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
79 require_capability('moodle/course:manageactivities', $context);
81 if (! $module = get_record("modules", "id", $cm->module
)) {
82 error("This module doesn't exist");
85 if (! $form = get_record($module->name
, "id", $cm->instance
)) {
86 error("The required instance of this module doesn't exist");
89 if (! $cw = get_record("course_sections", "id", $cm->section
)) {
90 error("This course section doesn't exist");
94 $form->coursemodule
= $cm->id
;
95 $form->section
= $cm->section
; // The section ID
96 $form->cmidnumber
= $cm->idnumber
; // The cm IDnumber
97 $form->course
= $course->id
;
98 $form->module
= $module->id
;
99 $form->modulename
= $module->name
;
100 $form->instance
= $cm->instance
;
101 $form->return = $return;
102 $form->update
= $update;
104 // add existing outcomes
105 if ($items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename
,
106 'iteminstance'=>$form->instance
, 'courseid'=>$COURSE->id
))) {
107 foreach ($items as $item) {
108 if (!empty($item->outcomeid
)) {
109 $form->{'outcome_'.$item->outcomeid
} = 1;
114 $sectionname = get_section_name($course->format
);
115 $fullmodulename = get_string("modulename", $module->name
);
117 if ($form->section
&& $course->format
!= 'site') {
118 $heading->what
= $fullmodulename;
119 $heading->in
= "$sectionname $cw->section";
120 $pageheading = get_string("updatingain", "moodle", $heading);
122 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
125 $navlinksinstancename = array('name' => format_string($form->name
,true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
127 $CFG->pagepath
= 'mod/'.$module->name
;
129 $CFG->pagepath
.= '/'.$type;
131 $CFG->pagepath
.= '/mod';
134 error('Invalid operation.');
137 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
138 if (file_exists($modmoodleform)) {
139 require_once($modmoodleform);
142 error('No formslib form description file found for this activity.');
145 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
146 if (file_exists($modlib)) {
147 include_once($modlib);
149 error("This module is missing important code! ($modlib)");
152 $mformclassname = 'mod_'.$module->name
.'_mod_form';
153 $cousesection=isset($cw->section
)?
$cw->section
:$section;
154 $mform=& new $mformclassname($form->instance
, $cousesection, ((isset($cm))?
$cm:null));
155 $mform->set_data($form);
157 if ($mform->is_cancelled()) {
158 if ($return && isset($cm)){
159 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
161 redirect("view.php?id=$course->id#section-".$cousesection);
163 } else if ($fromform = $mform->get_data()) {
164 if (empty($fromform->coursemodule
)) { //add
165 if (! $course = get_record("course", "id", $fromform->course
)) {
166 error("This course doesn't exist");
168 $fromform->instance
= '';
169 $fromform->coursemodule
= '';
171 if (! $cm = get_record("course_modules", "id", $fromform->coursemodule
)) {
172 error("This course module doesn't exist");
175 if (! $course = get_record("course", "id", $cm->course
)) {
176 error("This course doesn't exist");
178 $fromform->instance
= $cm->instance
;
179 $fromform->coursemodule
= $cm->id
;
182 require_login($course->id
); // needed to setup proper $COURSE
184 if (!empty($fromform->coursemodule
)) {
185 $context = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
187 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
189 require_capability('moodle/course:manageactivities', $context);
191 $fromform->course
= $course->id
;
192 $fromform->modulename
= clean_param($fromform->modulename
, PARAM_SAFEDIR
); // For safety
194 $addinstancefunction = $fromform->modulename
."_add_instance";
195 $updateinstancefunction = $fromform->modulename
."_update_instance";
197 if (!empty($fromform->update
)) {
199 if (isset($fromform->name
)) {
200 if (trim($fromform->name
) == '') {
201 unset($fromform->name
);
205 $returnfromfunc = $updateinstancefunction($fromform);
206 if (!$returnfromfunc) {
207 error("Could not update the $fromform->modulename", "view.php?id=$course->id");
209 if (is_string($returnfromfunc)) {
210 error($returnfromfunc, "view.php?id=$course->id");
213 if (isset($fromform->visible
)) {
214 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
217 if (isset($fromform->groupmode
)) {
218 set_coursemodule_groupmode($fromform->coursemodule
, $fromform->groupmode
);
222 if (isset($fromform->cmidnumber
)) {
223 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
226 add_to_log($course->id
, "course", "update mod",
227 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
228 "$fromform->modulename $fromform->instance");
229 add_to_log($course->id
, $fromform->modulename
, "update",
230 "view.php?id=$fromform->coursemodule",
231 "$fromform->instance", $fromform->coursemodule
);
233 } else if (!empty($fromform->add
)){
235 if (!course_allowed_module($course,$fromform->modulename
)) {
236 error("This module ($fromform->modulename) has been disabled for this particular course");
239 if (!isset($fromform->name
) ||
trim($fromform->name
) == '') {
240 $fromform->name
= get_string("modulename", $fromform->modulename
);
243 $returnfromfunc = $addinstancefunction($fromform);
244 if (!$returnfromfunc) {
245 /*if (file_exists($moderr)) {
247 include_once($moderr);
250 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
252 if (is_string($returnfromfunc)) {
253 error($returnfromfunc, "view.php?id=$course->id");
256 if (!isset($fromform->groupmode
)) { // to deal with pre-1.5 modules
257 $fromform->groupmode
= $course->groupmode
; /// Default groupmode the same as course
260 $fromform->instance
= $returnfromfunc;
262 // course_modules and course_sections each contain a reference
263 // to each other, so we have to update one of them twice.
265 if (! $fromform->coursemodule
= add_course_module($fromform) ) {
266 error("Could not add a new course module");
268 if (! $sectionid = add_mod_to_section($fromform) ) {
269 error("Could not add the new course module to that section");
272 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule
)) {
273 error("Could not update the course module with the correct section");
276 if (!isset($fromform->visible
)) { // We get the section's visible field status
277 $fromform->visible
= get_field("course_sections","visible","id",$sectionid);
279 // make sure visibility is set correctly (in particular in calendar)
280 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
283 if (isset($fromform->cmidnumber
)) {
284 set_coursemodule_idnumber($fromform->coursemodule
, $fromform->cmidnumber
);
287 add_to_log($course->id
, "course", "add mod",
288 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
289 "$fromform->modulename $fromform->instance");
290 add_to_log($course->id
, $fromform->modulename
, "add",
291 "view.php?id=$fromform->coursemodule",
292 "$fromform->instance", $fromform->coursemodule
);
294 error("Data submitted is invalid.");
297 //sync idnumber with grade_item
298 if ($grade_item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
299 'iteminstance'=>$fromform->instance
, 'itemnumber'=>0, 'courseid'=>$COURSE->id
))) {
300 if ($grade_item->idnumber
!= $fromform->cmidnumber
) {
301 $grade_item->idnumber
= $fromform->cmidnumber
;
302 $grade_item->update();
306 // add outcomes if requested
307 if ($outcomes = grade_outcome
::fetch_all_available($COURSE->id
)) {
308 $grade_items = array();
310 foreach($outcomes as $outcome) {
311 $elname = 'outcome_'.$outcome->id
;
313 if (array_key_exists($elname, $fromform) and $fromform->$elname) {
314 // we have a request for new outcome grade item
315 $grade_item = new grade_item();
317 // Outcome grade_item.itemnumber start at 1000
318 $max_itemnumber = 999;
319 if ($items = grade_item
::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename
,
320 'iteminstance'=>$fromform->instance
, 'courseid'=>$COURSE->id
))) {
322 foreach($items as $item) {
323 if ($item->outcomeid
== $outcome->id
) {
327 if (empty($item->outcomeid
)) {
330 if ($item->itemnumber
> $max_itemnumber) {
331 $max_itemnumber = $item->itemnumber
;
338 $grade_item->courseid
= $COURSE->id
;
339 $grade_item->itemtype
= 'mod';
340 $grade_item->itemmodule
= $fromform->modulename
;
341 $grade_item->iteminstance
= $fromform->instance
;
342 $grade_item->itemnumber
= $max_itemnumber +
1;
343 $grade_item->itemname
= $outcome->fullname
;
344 $grade_item->outcomeid
= $outcome->id
;
345 $grade_item->gradetype
= GRADE_TYPE_SCALE
;
346 $grade_item->scaleid
= $outcome->scaleid
;
348 $grade_item->insert();
350 // TODO comment on these next 4 lines
351 if ($item = grade_item
::fetch(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule
,
352 'iteminstance'=>$grade_item->iteminstance
, 'itemnumber'=>0, 'courseid'=>$COURSE->id
))) {
353 $grade_item->set_parent($item->categoryid
);
354 $grade_item->move_after_sortorder($item->sortorder
);
356 $grade_items[] = $grade_item;
360 // Create a grade_category to represent this module, if outcomes have been attached
361 if (!empty($grade_items)) {
362 // Add the module's normal grade_item as a child of this category
363 $item_params = array('itemtype'=>'mod',
364 'itemmodule'=>$fromform->modulename
,
365 'iteminstance'=>$fromform->instance
,
367 'courseid'=>$COURSE->id
);
368 $item = grade_item
::fetch($item_params);
370 // Only create the category if it will contain at least 2 items
371 if ($item OR count($grade_items) > 1) { // If we are here it means there is at least 1 outcome
372 $cat_params = array('courseid'=>$COURSE->id
, 'fullname'=>$fromform->name
);
373 $grade_category = grade_category
::fetch($cat_params);
375 if (!$grade_category) {
376 $grade_category = new grade_category($cat_params);
377 $grade_category->courseid
= $COURSE->id
;
378 $grade_category->fullname
= $fromform->name
;
379 $grade_category->insert();
382 $sortorder = $grade_category->sortorder
;
385 $item->set_parent($grade_category->id
);
386 $sortorder = $item->sortorder
;
389 // Add the outcomes as children of this category
390 foreach ($grade_items as $gi) {
391 $gi->set_parent($grade_category->id
);
392 $gi->move_after_sortorder($sortorder);
398 rebuild_course_cache($course->id
);
400 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
404 if (!empty($cm->id
)) {
405 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
407 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
409 require_capability('moodle/course:manageactivities', $context);
411 $streditinga = get_string("editinga", "moodle", $fullmodulename);
412 $strmodulenameplural = get_string("modulenameplural", $module->name
);
415 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
416 if (isset($navlinksinstancename)) {
417 $navlinks[] = $navlinksinstancename;
419 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title');
421 $navigation = build_navigation($navlinks);
423 print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false);
425 if (!empty($cm->id
)) {
426 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
427 $currenttab = 'update';
428 include_once($CFG->dirroot
.'/'.$CFG->admin
.'/roles/tabs.php');
430 $icon = '<img src="'.$CFG->modpixpath
.'/'.$module->name
.'/icon.gif" alt=""/>';
432 print_heading_with_help($pageheading, "mods", $module->name
, $icon);
434 print_footer($course);