3 // adds or updates modules in a course using new formslib
5 require_once("../config.php");
6 require_once("lib.php");
10 $add = optional_param('add', '', PARAM_ALPHA
);
11 $update = optional_param('update', 0, PARAM_INT
);
12 //return to course/view.php if false or mod/modname/view.php if true
13 $return = optional_param('return', 0, PARAM_BOOL
);
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 if (! $module = get_record("modules", "name", $add)) {
25 error("This module type doesn't exist");
28 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
29 require_capability('moodle/course:manageactivities', $context);
31 if (!course_allowed_module($course, $module->id
)) {
32 error("This module has been disabled for this particular course");
35 require_login($course->id
); // needed to setup proper $COURSE
37 $form->section
= $section; // The section number itself
38 $form->course
= $course->id
;
39 $form->module
= $module->id
;
40 $form->modulename
= $module->name
;
42 $form->coursemodule
= "";
44 $form->return=0;//must be false if this is an add, go back to course view on cancel
49 $sectionname = get_section_name($course->format
);
50 $fullmodulename = get_string("modulename", $module->name
);
52 if ($form->section
&& $course->format
!= 'site') {
53 $heading->what
= $fullmodulename;
54 $heading->to
= "$sectionname $form->section";
55 $pageheading = get_string("addinganewto", "moodle", $heading);
57 $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->course
= $course->id
;
97 $form->module
= $module->id
;
98 $form->modulename
= $module->name
;
99 $form->instance
= $cm->instance
;
100 $form->return = $return;
101 $form->update
= $update;
103 $sectionname = get_section_name($course->format
);
104 $fullmodulename = get_string("modulename", $module->name
);
106 if ($form->section
&& $course->format
!= 'site') {
107 $heading->what
= $fullmodulename;
108 $heading->in
= "$sectionname $cw->section";
109 $pageheading = get_string("updatingain", "moodle", $heading);
111 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
113 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">".format_string($form->name
,true)."</a> ->";
114 $CFG->pagepath
= 'mod/'.$module->name
;
116 $CFG->pagepath
.= '/'.$type;
118 $CFG->pagepath
.= '/mod';
121 error('Invalid operation.');
124 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
125 if (file_exists($modmoodleform)) {
126 require_once($modmoodleform);
129 error('No formslib form description file found for this activity.');
132 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
133 if (file_exists($modlib)) {
134 include_once($modlib);
136 error("This module is missing important code! ($modlib)");
139 $mformclassname = 'mod_'.$module->name
.'_mod_form';
140 $cousesection=isset($cw->section
)?
$cw->section
:$section;
141 $mform=& new $mformclassname($form->instance
, $cousesection, ((isset($cm))?
$cm:null));
142 $mform->set_data($form);
144 if ($mform->is_cancelled()) {
145 if ($return && isset($cm)){
146 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
148 redirect("view.php?id=$course->id#section-".$cousesection);
150 } else if ($fromform=$mform->get_data()){
151 if (empty($fromform->coursemodule
)) { //add
152 if (! $course = get_record("course", "id", $fromform->course
)) {
153 error("This course doesn't exist");
155 $fromform->instance
= '';
156 $fromform->coursemodule
= '';
158 if (! $cm = get_record("course_modules", "id", $fromform->coursemodule
)) {
159 error("This course module doesn't exist");
162 if (! $course = get_record("course", "id", $cm->course
)) {
163 error("This course doesn't exist");
165 $fromform->instance
= $cm->instance
;
166 $fromform->coursemodule
= $cm->id
;
169 require_login($course->id
); // needed to setup proper $COURSE
171 if (!empty($fromform->coursemodule
)) {
172 $context = get_context_instance(CONTEXT_MODULE
, $fromform->coursemodule
);
174 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
176 require_capability('moodle/course:manageactivities', $context);
178 $fromform->course
= $course->id
;
179 $fromform->modulename
= clean_param($fromform->modulename
, PARAM_SAFEDIR
); // For safety
181 $addinstancefunction = $fromform->modulename
."_add_instance";
182 $updateinstancefunction = $fromform->modulename
."_update_instance";
184 if (!empty($fromform->update
)) {
187 if (isset($fromform->name
)) {
188 if (trim($fromform->name
) == '') {
189 unset($fromform->name
);
193 $returnfromfunc = $updateinstancefunction($fromform);
194 if (!$returnfromfunc) {
195 error("Could not update the $fromform->modulename", "view.php?id=$course->id");
197 if (is_string($returnfromfunc)) {
198 error($returnfromfunc, "view.php?id=$course->id");
201 if (isset($fromform->visible
)) {
202 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
205 if (isset($fromform->groupmode
)) {
206 set_coursemodule_groupmode($fromform->coursemodule
, $fromform->groupmode
);
210 add_to_log($course->id
, "course", "update mod",
211 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
212 "$fromform->modulename $fromform->instance");
213 add_to_log($course->id
, $fromform->modulename
, "update",
214 "view.php?id=$fromform->coursemodule",
215 "$fromform->instance", $fromform->coursemodule
);
217 } else if (!empty($fromform->add
)){
219 if (!course_allowed_module($course,$fromform->modulename
)) {
220 error("This module ($fromform->modulename) has been disabled for this particular course");
223 if (!isset($fromform->name
) ||
trim($fromform->name
) == '') {
224 $fromform->name
= get_string("modulename", $fromform->modulename
);
227 $returnfromfunc = $addinstancefunction($fromform);
228 if (!$returnfromfunc) {
229 /*if (file_exists($moderr)) {
231 include_once($moderr);
234 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
236 if (is_string($returnfromfunc)) {
237 error($returnfromfunc, "view.php?id=$course->id");
240 if (!isset($fromform->groupmode
)) { // to deal with pre-1.5 modules
241 $fromform->groupmode
= $course->groupmode
; /// Default groupmode the same as course
244 $fromform->instance
= $returnfromfunc;
246 // course_modules and course_sections each contain a reference
247 // to each other, so we have to update one of them twice.
249 if (! $fromform->coursemodule
= add_course_module($fromform) ) {
250 error("Could not add a new course module");
252 if (! $sectionid = add_mod_to_section($fromform) ) {
253 error("Could not add the new course module to that section");
256 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule
)) {
257 error("Could not update the course module with the correct section");
260 if (!isset($fromform->visible
)) { // We get the section's visible field status
261 $fromform->visible
= get_field("course_sections","visible","id",$sectionid);
263 // make sure visibility is set correctly (in particular in calendar)
264 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
266 add_to_log($course->id
, "course", "add mod",
267 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
268 "$fromform->modulename $fromform->instance");
269 add_to_log($course->id
, $fromform->modulename
, "add",
270 "view.php?id=$fromform->coursemodule",
271 "$fromform->instance", $fromform->coursemodule
);
273 error("Data submitted is invalid.");
276 rebuild_course_cache($course->id
);
278 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
282 if (!empty($cm->id
)) {
283 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
285 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
287 require_capability('moodle/course:manageactivities', $context);
289 $streditinga = get_string("editinga", "moodle", $fullmodulename);
290 $strmodulenameplural = get_string("modulenameplural", $module->name
);
292 print_header_simple($streditinga, '',
293 "<a href=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</a> ->
294 $strnav $streditinga", $mform->focus(), "", false);
296 if (!empty($cm->id
)) {
297 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
298 $currenttab = 'update';
299 include_once($CFG->dirroot
.'/'.$CFG->admin
.'/roles/tabs.php');
301 $icon = '<img src="'.$CFG->modpixpath
.'/'.$module->name
.'/icon.gif" alt=""/>';
303 print_heading_with_help($pageheading, "mods", $module->name
, $icon);
305 print_footer($course);