adding some strings
[moodle-linuxchix.git] / course / modedit.php
blobfa01f3c804022350df7e498ae4c751ef86efe725
1 <?php // $Id$
3 // adds or updates modules in a course using new formslib
5 require_once("../config.php");
6 require_once("lib.php");
8 require_login();
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);
16 if (!empty($add)) {
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;
41 $form->instance = "";
42 $form->coursemodule = "";
43 $form->add=$add;
44 $form->return=0;//must be false if this is an add, go back to course view on cancel
45 if (!empty($type)) {
46 $form->type = $type;
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);
56 } else {
57 $pageheading = get_string("addinganew", "moodle", $fullmodulename);
60 $CFG->pagepath = 'mod/'.$module->name;
61 if (!empty($type)) {
62 $CFG->pagepath .= '/'.$type;
63 } else {
64 $CFG->pagepath .= '/mod';
67 } else if (!empty($update)) {
68 if (! $cm = get_record("course_modules", "id", $update)) {
69 error("This course module doesn't exist");
72 if (! $course = get_record("course", "id", $cm->course)) {
73 error("This course doesn't exist");
76 require_login($course->id); // needed to setup proper $COURSE
77 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
78 require_capability('moodle/course:manageactivities', $context);
80 if (! $module = get_record("modules", "id", $cm->module)) {
81 error("This module doesn't exist");
84 if (! $form = get_record($module->name, "id", $cm->instance)) {
85 error("The required instance of this module doesn't exist");
88 if (! $cw = get_record("course_sections", "id", $cm->section)) {
89 error("This course section doesn't exist");
93 $form->coursemodule = $cm->id;
94 $form->section = $cm->section; // The section ID
95 $form->cmidnumber = $cm->idnumber; // The cm IDnumber
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);
110 } else {
111 $pageheading = get_string("updatinga", "moodle", $fullmodulename);
114 $navlinksinstancename = array('name' => format_string($form->name,true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
116 $CFG->pagepath = 'mod/'.$module->name;
117 if (!empty($type)) {
118 $CFG->pagepath .= '/'.$type;
119 } else {
120 $CFG->pagepath .= '/mod';
122 } else {
123 error('Invalid operation.');
126 $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
127 if (file_exists($modmoodleform)) {
128 require_once($modmoodleform);
130 } else {
131 error('No formslib form description file found for this activity.');
134 $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
135 if (file_exists($modlib)) {
136 include_once($modlib);
137 } else {
138 error("This module is missing important code! ($modlib)");
141 $mformclassname = 'mod_'.$module->name.'_mod_form';
142 $cousesection=isset($cw->section)?$cw->section:$section;
143 $mform=& new $mformclassname($form->instance, $cousesection, ((isset($cm))?$cm:null));
144 $mform->set_data($form);
146 if ($mform->is_cancelled()) {
147 if ($return && isset($cm)){
148 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
149 } else {
150 redirect("view.php?id=$course->id#section-".$cousesection);
152 } else if ($fromform=$mform->get_data()){
153 if (empty($fromform->coursemodule)) { //add
154 if (! $course = get_record("course", "id", $fromform->course)) {
155 error("This course doesn't exist");
157 $fromform->instance = '';
158 $fromform->coursemodule = '';
159 } else { //update
160 if (! $cm = get_record("course_modules", "id", $fromform->coursemodule)) {
161 error("This course module doesn't exist");
164 if (! $course = get_record("course", "id", $cm->course)) {
165 error("This course doesn't exist");
167 $fromform->instance = $cm->instance;
168 $fromform->coursemodule = $cm->id;
171 require_login($course->id); // needed to setup proper $COURSE
173 if (!empty($fromform->coursemodule)) {
174 $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
175 } else {
176 $context = get_context_instance(CONTEXT_COURSE, $course->id);
178 require_capability('moodle/course:manageactivities', $context);
180 $fromform->course = $course->id;
181 $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR); // For safety
183 $addinstancefunction = $fromform->modulename."_add_instance";
184 $updateinstancefunction = $fromform->modulename."_update_instance";
186 if (!empty($fromform->update)) {
188 if (isset($fromform->name)) {
189 if (trim($fromform->name) == '') {
190 unset($fromform->name);
194 $returnfromfunc = $updateinstancefunction($fromform);
195 if (!$returnfromfunc) {
196 error("Could not update the $fromform->modulename", "view.php?id=$course->id");
198 if (is_string($returnfromfunc)) {
199 error($returnfromfunc, "view.php?id=$course->id");
202 if (isset($fromform->visible)) {
203 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
206 if (isset($fromform->groupmode)) {
207 set_coursemodule_groupmode($fromform->coursemodule, $fromform->groupmode);
210 // set cm id number
211 if (isset($fromform->cmidnumber)) {
212 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
215 add_to_log($course->id, "course", "update mod",
216 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
217 "$fromform->modulename $fromform->instance");
218 add_to_log($course->id, $fromform->modulename, "update",
219 "view.php?id=$fromform->coursemodule",
220 "$fromform->instance", $fromform->coursemodule);
222 } else if (!empty($fromform->add)){
224 if (!course_allowed_module($course,$fromform->modulename)) {
225 error("This module ($fromform->modulename) has been disabled for this particular course");
228 if (!isset($fromform->name) || trim($fromform->name) == '') {
229 $fromform->name = get_string("modulename", $fromform->modulename);
232 $returnfromfunc = $addinstancefunction($fromform);
233 if (!$returnfromfunc) {
234 /*if (file_exists($moderr)) {
235 $form = $fromform;
236 include_once($moderr);
237 die;
239 error("Could not add a new instance of $fromform->modulename", "view.php?id=$course->id");
241 if (is_string($returnfromfunc)) {
242 error($returnfromfunc, "view.php?id=$course->id");
245 if (!isset($fromform->groupmode)) { // to deal with pre-1.5 modules
246 $fromform->groupmode = $course->groupmode; /// Default groupmode the same as course
249 $fromform->instance = $returnfromfunc;
251 // course_modules and course_sections each contain a reference
252 // to each other, so we have to update one of them twice.
254 if (! $fromform->coursemodule = add_course_module($fromform) ) {
255 error("Could not add a new course module");
257 if (! $sectionid = add_mod_to_section($fromform) ) {
258 error("Could not add the new course module to that section");
261 if (! set_field("course_modules", "section", $sectionid, "id", $fromform->coursemodule)) {
262 error("Could not update the course module with the correct section");
265 if (!isset($fromform->visible)) { // We get the section's visible field status
266 $fromform->visible = get_field("course_sections","visible","id",$sectionid);
268 // make sure visibility is set correctly (in particular in calendar)
269 set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
271 // set cm idnumber
272 if (isset($fromform->cmidnumber)) {
273 set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
276 add_to_log($course->id, "course", "add mod",
277 "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
278 "$fromform->modulename $fromform->instance");
279 add_to_log($course->id, $fromform->modulename, "add",
280 "view.php?id=$fromform->coursemodule",
281 "$fromform->instance", $fromform->coursemodule);
282 } else {
283 error("Data submitted is invalid.");
286 rebuild_course_cache($course->id);
288 redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
289 exit;
291 } else {
292 if (!empty($cm->id)) {
293 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
294 } else {
295 $context = get_context_instance(CONTEXT_COURSE, $course->id);
297 require_capability('moodle/course:manageactivities', $context);
299 $streditinga = get_string("editinga", "moodle", $fullmodulename);
300 $strmodulenameplural = get_string("modulenameplural", $module->name);
302 $navlinks = array();
303 $navlinks[] = array('name' => $strmodulenameplural, 'link' => "$CFG->wwwroot/mod/$module->name/index.php?id=$course->id", 'type' => 'activity');
304 if (isset($navlinksinstancename)) {
305 $navlinks[] = $navlinksinstancename;
307 $navlinks[] = array('name' => $streditinga, 'link' => '', 'type' => 'title');
309 $navigation = build_navigation($navlinks);
311 print_header_simple($streditinga, '', $navigation, $mform->focus(), "", false);
313 if (!empty($cm->id)) {
314 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
315 $currenttab = 'update';
316 include_once($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
318 $icon = '<img src="'.$CFG->modpixpath.'/'.$module->name.'/icon.gif" alt=""/>';
320 print_heading_with_help($pageheading, "mods", $module->name, $icon);
321 $mform->display();
322 print_footer($course);