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
14 /// basic access control checks
15 if ($id) { // editing course
18 // don't allow editing of 'site course' using this from
19 error('You cannot edit the site course using this form');
22 if (!$course = get_record('course', 'id', $id)) {
23 error('Course ID was incorrect');
25 require_login($course->id
);
26 $category = get_record('course_categories', 'id', $course->category
);
27 require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE
, $course->id
));
29 } else if ($categoryid) { // creating new course in this category
32 if (!$category = get_record('course_categories', 'id', $categoryid)) {
33 error('Category ID was incorrect');
35 require_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT
, $category->id
));
38 error('Either course id or category must be specified');
42 if (!empty($course)) {
43 $allowedmods = array();
44 if (!empty($course)) {
45 if ($am = get_records('course_allowed_modules','course',$course->id
)) {
47 $allowedmods[] = $m->module
;
50 if (empty($course->restrictmodules
)) {
51 $allowedmods = explode(',',$CFG->defaultallowedmodules
);
52 } // it'll be greyed out but we want these by default anyway.
54 $course->allowedmods
= $allowedmods;
56 if ($course->enrolstartdate
){
57 $course->enrolstartdisabled
= 0;
60 if ($course->enrolenddate
) {
61 $course->enrolenddisabled
= 0;
67 /// first create the form
68 $editform = new course_edit_form('edit.php', compact('course', 'category'));
69 // now override defaults if course already exists
70 if (!empty($course)) {
71 $course->enrolpassword
= $course->password
; // we need some other name for password field MDL-9929
72 $editform->set_data($course);
74 if ($editform->is_cancelled()){
76 redirect($CFG->wwwroot
);
78 redirect($CFG->wwwroot
.'/course/view.php?id='.$course->id
);
81 } else if ($data = $editform->get_data()) {
83 $data->password
= $data->enrolpassword
; // we need some other name for password field MDL-9929
84 /// process data if submitted
87 if ($data->enrolstartdisabled
){
88 $data->enrolstartdate
= 0;
91 if ($data->enrolenddisabled
) {
92 $data->enrolenddate
= 0;
95 $data->timemodified
= time();
98 if (!$course = create_course($data)) {
99 print_error('coursenotcreated');
102 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
104 // assign default role to creator if not already having permission to manage course assignments
105 if (!has_capability('moodle/course:view', $context) or !has_capability('moodle/role:assign', $context)) {
106 role_assign($CFG->creatornewroleid
, $USER->id
, 0, $context->id
);
109 // ensure we can use the course right after creating it
110 // this means trigger a reload of accessinfo...
111 mark_context_dirty($context->path
);
113 if ($data->metacourse
and has_capability('moodle/course:managemetacourse', $context)) {
114 // Redirect users with metacourse capability to student import
115 redirect($CFG->wwwroot
."/course/importstudents.php?id=$course->id");
117 // Redirect to roles assignment
118 redirect($CFG->wwwroot
."/$CFG->admin/roles/assign.php?contextid=$context->id");
122 if (!update_course($data)) {
123 print_error('coursenotupdated');
125 redirect($CFG->wwwroot
."/course/view.php?id=$course->id");
134 $streditcoursesettings = get_string("editcoursesettings");
135 $straddnewcourse = get_string("addnewcourse");
136 $stradministration = get_string("administration");
137 $strcategories = get_string("categories");
140 if (!empty($course)) {
141 $navlinks[] = array('name' => $streditcoursesettings,
144 $title = $streditcoursesettings;
145 $fullname = $course->fullname
;
147 $navlinks[] = array('name' => $stradministration,
148 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
150 $navlinks[] = array('name' => $strcategories,
151 'link' => 'index.php',
153 $navlinks[] = array('name' => $straddnewcourse,
156 $title = "$site->shortname: $straddnewcourse";
157 $fullname = $site->fullname
;
160 $navigation = build_navigation($navlinks);
161 print_header($title, $fullname, $navigation, $editform->focus());
162 print_heading($streditcoursesettings);
164 $editform->display();
166 print_footer($course);