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 if ($data->metacourse
and has_capability('moodle/course:managemetacourse', $context)) {
104 // Redirect users with metacourse capability to student import
105 redirect($CFG->wwwroot
."/course/importstudents.php?id=$course->id");
107 // Redirect to roles assignment
108 redirect($CFG->wwwroot
."/$CFG->admin/roles/assign.php?contextid=$context->id");
112 if (!update_course($data)) {
113 print_error('coursenotupdated');
116 events_trigger('course_updated', $data);
117 redirect($CFG->wwwroot
."/course/view.php?id=$course->id");
126 $streditcoursesettings = get_string("editcoursesettings");
127 $straddnewcourse = get_string("addnewcourse");
128 $stradministration = get_string("administration");
129 $strcategories = get_string("categories");
132 if (!empty($course)) {
133 $navlinks[] = array('name' => $streditcoursesettings,
136 $title = $streditcoursesettings;
137 $fullname = $course->fullname
;
139 $navlinks[] = array('name' => $stradministration,
140 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
142 $navlinks[] = array('name' => $strcategories,
143 'link' => 'index.php',
145 $navlinks[] = array('name' => $straddnewcourse,
148 $title = "$site->shortname: $straddnewcourse";
149 $fullname = $site->fullname
;
152 $navigation = build_navigation($navlinks);
153 print_header($title, $fullname, $navigation, $editform->focus());
154 print_heading($streditcoursesettings);
156 $editform->display();
158 print_footer($course);