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
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
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
));
31 error('Either course id or category must be specified');
35 if (!empty($course)) {
36 $allowedmods = array();
37 if (!empty($course)) {
38 if ($am = get_records('course_allowed_modules','course',$course->id
)) {
40 $allowedmods[] = $m->module
;
43 if (empty($course->restrictmodules
)) {
44 $allowedmods = explode(',',$CFG->defaultallowedmodules
);
45 } // it'll be greyed out but we want these by default anyway.
47 $course->allowedmods
= $allowedmods;
49 if ($course->enrolstartdate
){
50 $course->enrolstartdisabled
= 0;
53 if ($course->enrolenddate
) {
54 $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 $editform->set_data($course);
67 if ($editform->is_cancelled()){
69 redirect($CFG->wwwroot
);
71 redirect($CFG->wwwroot
.'/course/view.php?id='.$course->id
);
74 } elseif ($data = $editform->get_data()) {
75 /// process data if submitted
78 if ($data->enrolstartdisabled
){
79 $data->enrolstartdate
= 0;
82 if ($data->enrolenddisabled
) {
83 $data->enrolenddate
= 0;
86 $data->timemodified
= time();
100 $streditcoursesettings = get_string("editcoursesettings");
101 $straddnewcourse = get_string("addnewcourse");
102 $stradministration = get_string("administration");
103 $strcategories = get_string("categories");
105 if (!empty($course)) {
106 print_header($streditcoursesettings, "$course->fullname",
107 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
108 -> $streditcoursesettings", $editform->focus());
110 print_header("$site->shortname: $straddnewcourse", "$site->fullname",
111 "<a href=\"$CFG->wwwroot/$CFG->admin/index.php\">$stradministration</a> -> ".
112 "<a href=\"index.php\">$strcategories</a> -> $straddnewcourse", $editform->focus());
115 print_heading($streditcoursesettings);
116 $editform->display();
117 print_footer($course);
122 /// internal functions
124 function create_course($data) {
127 // preprocess allowed mods
128 $allowedmods = empty($data->allowedmods
) ?
array() : $data->allowedmods
;
129 unset($data->allowedmods
);
130 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
131 if ($CFG->restrictmodulesfor
== 'all') {
132 $data->restrictmodules
= 1;
134 $data->restrictmodules
= 0;
139 $data->timecreated
= time();
141 // place at beginning of category
142 fix_course_sortorder();
143 $data->sortorder
= get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$data->category");
144 if (empty($data->sortorder
)) {
145 $data->sortorder
= 100;
148 if ($newcourseid = insert_record('course', $data)) { // Set up new course
150 $course = get_record('course', 'id', $newcourseid);
153 $page = page_create_object(PAGE_COURSE_VIEW
, $course->id
);
154 blocks_repopulate_page($page); // Return value not checked because you can always edit later
156 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
157 update_restricted_mods($course, $allowedmods);
160 $section = new object();
161 $section->course
= $course->id
; // Create a default section.
162 $section->section
= 0;
163 $section->id
= insert_record('course_sections', $section);
165 fix_course_sortorder();
166 add_to_log(SITEID
, "course", "new", "view.php?id=$course->id", "$data->fullname (ID $course->id)") ;
167 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
169 if ($data->metacourse
and has_capability('moodle/course:managemetacourse', $context)) { // Redirect users with metacourse capability to student import
170 redirect($CFG->wwwroot
."/course/importstudents.php?id=$course->id");
172 } else if (has_capability('moodle/role:assign', $context)) { // Redirect users with assign capability to assign users to different roles
173 redirect($CFG->wwwroot
."/$CFG->admin/roles/assign.php?contextid=$context->id");
175 } else { // Add current teacher and send to course
176 // find a role with legacy:edittingteacher
177 if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW
, $context)) {
178 // assign the role to this user
179 $teachereditrole = array_shift($teacherroles);
180 role_assign($teachereditrole->id
, $USER->id
, 0, $context->id
);
182 redirect($CFG->wwwroot
."/course/view.php?id=$course->id");
186 error("Serious Error! Could not create the new course!");
191 function update_course($data) {
194 // preprocess allowed mods
195 $allowedmods = empty($data->allowedmods
) ?
array() : $data->allowedmods
;
196 unset($data->allowedmods
);
197 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
198 unset($data->restrictmodules
);
201 $oldcourse = get_record('course', 'id', $data->id
); // should not fail, already tested above
202 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT
, $oldcourse->category
))
203 or !has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT
, $data->category
))) {
204 // can not move to new category, keep the old one
205 unset($data->category
);
208 // Update with the new data
209 if (update_record('course', $data)) {
211 $course = get_record('course', 'id', $data->id
);
213 add_to_log($course->id
, "course", "update", "edit.php?id=$course->id", "");
214 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
215 update_restricted_mods($course, $allowedmods);
217 fix_course_sortorder();
219 // Test for and remove blocks which aren't appropriate anymore
220 $page = page_create_object(PAGE_COURSE_VIEW
, $course->id
);
221 blocks_remove_inappropriate($page);
223 redirect($CFG->wwwroot
."/course/view.php?id=$course->id");
226 error("Serious Error! Could not update the course record! (id = $form->id)");