Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / course / edit.php
blob1c916acc8a38d4f5e3808c6621d7fd89110a5e5d
1 <?php // $Id$
2 // Edit course settings
4 require_once('../config.php');
5 require_once($CFG->dirroot.'/enrol/enrol.class.php');
6 require_once($CFG->libdir.'/blocklib.php');
7 require_once('lib.php');
8 require_once('edit_form.php');
10 $id = optional_param('id', 0, PARAM_INT); // course id
11 $categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form
13 require_login();
15 /// basic access control checks
16 if ($id) { // editing course
17 if (!$course = get_record('course', 'id', $id)) {
18 error('Course ID was incorrect');
20 $category = get_record('course_categories', 'id', $course->category);
21 require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
23 } else if ($categoryid) { // creating new course in this category
24 $course = null;
25 if (!$category = get_record('course_categories', 'id', $categoryid)) {
26 error('Category ID was incorrect');
28 require_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $category->id));
29 } else {
30 error('Either course id or category must be specified');
33 /// prepare course
34 if (!empty($course)) {
35 $allowedmods = array();
36 if (!empty($course)) {
37 if ($am = get_records('course_allowed_modules','course',$course->id)) {
38 foreach ($am as $m) {
39 $allowedmods[] = $m->module;
41 } else {
42 if (empty($course->restrictmodules)) {
43 $allowedmods = explode(',',$CFG->defaultallowedmodules);
44 } // it'll be greyed out but we want these by default anyway.
46 $course->allowedmods = $allowedmods;
48 if ($course->enrolstartdate){
49 $course->enrolstartdisabled = 0;
52 if ($course->enrolenddate) {
53 $course->enrolenddisabled = 0;
59 /// first create the form
60 $editform = new course_edit_form('edit.php', compact('course', 'category'));
61 // now override defaults if course already exists
62 if (!empty($course)) {
63 $course->enrolpassword = $course->password; // we need some other name for password field MDL-9929
64 $editform->set_data($course);
66 if ($editform->is_cancelled()){
67 if (empty($course)) {
68 redirect($CFG->wwwroot);
69 } else {
70 redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
73 } else if ($data = $editform->get_data()) {
75 $data->password = $data->enrolpassword; // we need some other name for password field MDL-9929
76 /// process data if submitted
78 //preprocess data
79 if ($data->enrolstartdisabled){
80 $data->enrolstartdate = 0;
83 if ($data->enrolenddisabled) {
84 $data->enrolenddate = 0;
87 $data->timemodified = time();
89 if (empty($course)) {
90 if (!$course = create_course($data)) {
91 print_error('coursenotcreated');
94 $context = get_context_instance(CONTEXT_COURSE, $course->id);
96 // assign default role to creator if not already having permission to manage course assignments
97 if (!has_capability('moodle/course:view', $context) or !has_capability('moodle/role:assign', $context)) {
98 role_assign($CFG->creatornewroleid, $USER->id, 0, $context->id);
101 if ($data->metacourse and has_capability('moodle/course:managemetacourse', $context)) {
102 // Redirect users with metacourse capability to student import
103 redirect($CFG->wwwroot."/course/importstudents.php?id=$course->id");
104 } else {
105 // Redirect to roles assignment
106 redirect($CFG->wwwroot."/$CFG->admin/roles/assign.php?contextid=$context->id");
109 } else {
110 if (!update_course($data)) {
111 print_error('coursenotupdated');
113 // MDL-9983
114 events_trigger('course_updated', $data);
115 redirect($CFG->wwwroot."/course/view.php?id=$course->id");
120 /// Print the form
122 $site = get_site();
124 $streditcoursesettings = get_string("editcoursesettings");
125 $straddnewcourse = get_string("addnewcourse");
126 $stradministration = get_string("administration");
127 $strcategories = get_string("categories");
129 if (!empty($course)) {
130 print_header($streditcoursesettings, $course->fullname,
131 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
132 -> $streditcoursesettings", $editform->focus());
133 } else {
134 print_header("$site->shortname: $straddnewcourse", $site->fullname,
135 "<a href=\"$CFG->wwwroot/$CFG->admin/index.php\">$stradministration</a> -> ".
136 "<a href=\"index.php\">$strcategories</a> -> $straddnewcourse", $editform->focus());
139 print_heading($streditcoursesettings);
141 $editform->display();
143 print_footer($course);