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
16 if (!$course = get_record('course', 'id', $id)) {
17 error('Course ID was incorrect');
19 require_login($course->id
);
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
26 if (!$category = get_record('course_categories', 'id', $categoryid)) {
27 error('Category ID was incorrect');
29 require_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT
, $category->id
));
32 error('Either course id or category must be specified');
36 if (!empty($course)) {
37 $allowedmods = array();
38 if (!empty($course)) {
39 if ($am = get_records('course_allowed_modules','course',$course->id
)) {
41 $allowedmods[] = $m->module
;
44 if (empty($course->restrictmodules
)) {
45 $allowedmods = explode(',',$CFG->defaultallowedmodules
);
46 } // it'll be greyed out but we want these by default anyway.
48 $course->allowedmods
= $allowedmods;
50 if ($course->enrolstartdate
){
51 $course->enrolstartdisabled
= 0;
54 if ($course->enrolenddate
) {
55 $course->enrolenddisabled
= 0;
61 /// first create the form
62 $editform = new course_edit_form('edit.php', compact('course', 'category'));
63 // now override defaults if course already exists
64 if (!empty($course)) {
65 $course->enrolpassword
= $course->password
; // we need some other name for password field MDL-9929
66 $editform->set_data($course);
68 if ($editform->is_cancelled()){
70 redirect($CFG->wwwroot
);
72 redirect($CFG->wwwroot
.'/course/view.php?id='.$course->id
);
75 } else if ($data = $editform->get_data()) {
77 $data->password
= $data->enrolpassword
; // we need some other name for password field MDL-9929
78 /// process data if submitted
81 if ($data->enrolstartdisabled
){
82 $data->enrolstartdate
= 0;
85 if ($data->enrolenddisabled
) {
86 $data->enrolenddate
= 0;
89 $data->timemodified
= time();
92 if (!$course = create_course($data)) {
93 print_error('coursenotcreated');
96 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
98 // assign default role to creator if not already having permission to manage course assignments
99 if (!has_capability('moodle/course:view', $context) or !has_capability('moodle/role:assign', $context)) {
100 role_assign($CFG->creatornewroleid
, $USER->id
, 0, $context->id
);
103 // ensure we can use the course right after creating it
104 // this means trigger a reload of accessinfo...
105 mark_context_dirty($context->path
);
107 if ($data->metacourse
and has_capability('moodle/course:managemetacourse', $context)) {
108 // Redirect users with metacourse capability to student import
109 redirect($CFG->wwwroot
."/course/importstudents.php?id=$course->id");
111 // Redirect to roles assignment
112 redirect($CFG->wwwroot
."/$CFG->admin/roles/assign.php?contextid=$context->id");
116 if (!update_course($data)) {
117 print_error('coursenotupdated');
120 events_trigger('course_updated', $data);
121 redirect($CFG->wwwroot
."/course/view.php?id=$course->id");
130 $streditcoursesettings = get_string("editcoursesettings");
131 $straddnewcourse = get_string("addnewcourse");
132 $stradministration = get_string("administration");
133 $strcategories = get_string("categories");
136 if (!empty($course)) {
137 $navlinks[] = array('name' => $streditcoursesettings,
140 $title = $streditcoursesettings;
141 $fullname = $course->fullname
;
143 $navlinks[] = array('name' => $stradministration,
144 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
146 $navlinks[] = array('name' => $strcategories,
147 'link' => 'index.php',
149 $navlinks[] = array('name' => $straddnewcourse,
152 $title = "$site->shortname: $straddnewcourse";
153 $fullname = $site->fullname
;
156 $navigation = build_navigation($navlinks);
157 print_header($title, $fullname, $navigation, $editform->focus());
158 print_heading($streditcoursesettings);
160 $editform->display();
162 print_footer($course);