3 // adds or updates modules in a course using new formslib
5 require("../config.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
);
16 $section = required_param('section', PARAM_INT
);
17 $course = required_param('course', PARAM_INT
);
19 if (! $course = get_record("course", "id", $course)) {
20 error("This course doesn't exist");
23 if (! $module = get_record("modules", "name", $add)) {
24 error("This module type doesn't exist");
27 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
28 require_capability('moodle/course:manageactivities', $context);
30 if (!course_allowed_module($course, $module->id
)) {
31 error("This module has been disabled for this particular course");
34 require_login($course->id
); // needed to setup proper $COURSE
36 $form->section
= $section; // The section number itself
37 $form->course
= $course->id
;
38 $form->module
= $module->id
;
39 $form->modulename
= $module->name
;
41 $form->coursemodule
= "";
43 $form->return=0;//must be false if this is an add, go back to course view on cancel
48 $sectionname = get_section_name($course->format
);
49 $fullmodulename = get_string("modulename", $module->name
);
51 if ($form->section
&& $course->format
!= 'site') {
52 $heading->what
= $fullmodulename;
53 $heading->to
= "$sectionname $form->section";
54 $pageheading = get_string("addinganewto", "moodle", $heading);
56 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
60 }elseif (!empty($update)){
61 if (! $cm = get_record("course_modules", "id", $update)) {
62 error("This course module doesn't exist");
65 if (! $course = get_record("course", "id", $cm->course
)) {
66 error("This course doesn't exist");
69 require_login($course->id
); // needed to setup proper $COURSE
70 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
71 require_capability('moodle/course:manageactivities', $context);
73 if (! $module = get_record("modules", "id", $cm->module
)) {
74 error("This module doesn't exist");
77 if (! $form = get_record($module->name
, "id", $cm->instance
)) {
78 error("The required instance of this module doesn't exist");
81 if (! $cw = get_record("course_sections", "id", $cm->section
)) {
82 error("This course section doesn't exist");
86 $form->coursemodule
= $cm->id
;
87 $form->section
= $cm->section
; // The section ID
88 $form->course
= $course->id
;
89 $form->module
= $module->id
;
90 $form->modulename
= $module->name
;
91 $form->instance
= $cm->instance
;
92 $form->return = $return;
93 $form->update
= $update;
95 $sectionname = get_section_name($course->format
);
96 $fullmodulename = get_string("modulename", $module->name
);
98 if ($form->section
&& $course->format
!= 'site') {
99 $heading->what
= $fullmodulename;
100 $heading->in
= "$sectionname $cw->section";
101 $pageheading = get_string("updatingain", "moodle", $heading);
103 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
105 $strnav = "<a href=\"$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id\">".format_string($form->name
,true)."</a> ->";
107 error('Invalid operation.');
110 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
111 if (file_exists($modmoodleform)) {
112 require_once($modmoodleform);
115 error('No formslib form description file found for this activity.');
118 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
119 if (file_exists($modlib)) {
120 include_once($modlib);
122 error("This module is missing important code! ($modlib)");
125 $mformclassname = 'mod_'.$module->name
.'_mod_form';
126 $cousesection=isset($cw->section
)?
$cw->section
:$section;
127 $mform=& new $mformclassname($form->instance
, $cousesection, ((isset($cm))?
$cm:null));
128 $mform->set_data($form);
130 if ($mform->is_cancelled()) {
131 if ($return && isset($cm)){
132 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
134 redirect("view.php?id=$course->id#section-".$cousesection);
136 } elseif ($fromform=$mform->get_data()){
137 if (empty($fromform->coursemodule
)) { //add
138 if (! $course = get_record("course", "id", $fromform->course
)) {
139 error("This course doesn't exist");
141 $fromform->instance
= '';
142 $fromform->coursemodule
= '';
144 if (! $cm = get_record("course_modules", "id", $fromform->coursemodule
)) {
145 error("This course module doesn't exist");
148 if (! $course = get_record("course", "id", $cm->course
)) {
149 error("This course doesn't exist");
151 $fromform->instance
= $cm->instance
;
152 $fromform->coursemodule
= $cm->id
;
155 require_login($course->id
); // needed to setup proper $COURSE
156 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
157 require_capability('moodle/course:manageactivities', $context);
159 $fromform->course
= $course->id
;
160 $fromform->modulename
= clean_param($fromform->modulename
, PARAM_SAFEDIR
); // For safety
162 $addinstancefunction = $fromform->modulename
."_add_instance";
163 $updateinstancefunction = $fromform->modulename
."_update_instance";
165 if (!empty($fromform->update
)) {
168 if (isset($fromform->name
)) {
169 if (trim($fromform->name
) == '') {
170 unset($fromform->name
);
174 $returnfromfunc = $updateinstancefunction($fromform);
175 if (!$returnfromfunc) {
176 error("Could not update the $fromform->modulename", "view.php?id=$course->id");
178 if (is_string($returnfromfunc)) {
179 error($returnfromfunc, "view.php?id=$course->id");
182 if (isset($fromform->visible
)) {
183 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
186 if (isset($fromform->groupmode
)) {
187 set_coursemodule_groupmode($fromform->coursemodule
, $fromform->groupmode
);
191 add_to_log($course->id
, "course", "update mod",
192 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
193 "$fromform->modulename $fromform->instance");
194 add_to_log($course->id
, $fromform->modulename
, "update",
195 "view.php?id=$fromform->coursemodule",
196 "$fromform->instance", $fromform->coursemodule
);
197 }elseif (!empty($fromform->add
)){
199 if (!course_allowed_module($course,$fromform->modulename
)) {
200 error("This module ($fromform->modulename) has been disabled for this particular course");
203 if (!isset($fromform->name
) ||
trim($fromform->name
) == '') {
204 $fromform->name
= get_string("modulename", $fromform->modulename
);
207 $returnfromfunc = $addinstancefunction($fromform);
208 if (!$returnfromfunc) {
209 /*if (file_exists($moderr)) {
211 include_once($moderr);
214 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
216 if (is_string($returnfromfunc)) {
217 error($returnfromfunc, "view.php?id=$course->id");
220 if (!isset($fromform->groupmode
)) { // to deal with pre-1.5 modules
221 $fromform->groupmode
= $course->groupmode
; /// Default groupmode the same as course
224 $fromform->instance
= $returnfromfunc;
226 // course_modules and course_sections each contain a reference
227 // to each other, so we have to update one of them twice.
229 if (! $fromform->coursemodule
= add_course_module($fromform) ) {
230 error("Could not add a new course module");
232 if (! $sectionid = add_mod_to_section($fromform) ) {
233 error("Could not add the new course module to that section");
236 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule
)) {
237 error("Could not update the course module with the correct section");
240 if (!isset($fromform->visible
)) { // We get the section's visible field status
241 $fromform->visible
= get_field("course_sections","visible","id",$sectionid);
243 // make sure visibility is set correctly (in particular in calendar)
244 set_coursemodule_visible($fromform->coursemodule
, $fromform->visible
);
246 add_to_log($course->id
, "course", "add mod",
247 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
248 "$fromform->modulename $fromform->instance");
249 add_to_log($course->id
, $fromform->modulename
, "add",
250 "view.php?id=$fromform->coursemodule",
251 "$fromform->instance", $fromform->coursemodule
);
253 error("Data submitted is invalid.");
256 rebuild_course_cache($course->id
);
258 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
262 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
263 require_capability('moodle/course:manageactivities', $context);
265 $streditinga = get_string("editinga", "moodle", $fullmodulename);
266 $strmodulenameplural = get_string("modulenameplural", $module->name
);
268 print_header_simple($streditinga, '',
269 "<a href=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</a> ->
270 $strnav $streditinga", $mform->focus(), "", false);
272 if (!empty($cm->id
)) {
273 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
274 $currenttab = 'update';
275 include_once($CFG->dirroot
.'/'.$CFG->admin
.'/roles/tabs.php');
277 $icon = '<img src="'.$CFG->modpixpath
.'/'.$module->name
.'/icon.gif" alt=""/>';
279 print_heading_with_help($pageheading, "mods", $module->name
, $icon);
281 print_footer($course);