2 // Edit course settings
4 require_once("../config.php");
5 require_once("lib.php");
6 require_once("$CFG->libdir/blocklib.php");
7 require_once("$CFG->dirroot/enrol/enrol.class.php");
9 $id = optional_param('id', 0, PARAM_INT
); // course id
10 $category = optional_param('category', 0, PARAM_INT
); // possible default category
14 $disable_meta = false;
18 if (! $course = get_record("course", "id", $id)) {
19 error("Course ID was incorrect");
22 if (!isteacheredit($course->id
)) {
23 error("You do not currently have editing privileges!");
26 if (course_in_meta($course)) {
27 $disable_meta = get_string('metaalreadyinmeta');
29 else if ($course->metacourse
) {
30 if (count_records("course_meta","parent_course",$course->id
) > 0) {
31 $disable_meta = get_string('metaalreadyhascourses');
35 if (count_records("user_students","course",$course->id
) > 0) {
36 $disable_meta = get_string('metaalreadyhasenrolments');
39 } else { // Admin is creating a new course
42 error("You do not currently have course creation privileges!");
48 if (! $site = get_site()) {
49 redirect("$CFG->wwwroot/$CFG->admin/index.php");
53 /// If data submitted, then process and store.
55 if ($form = data_submitted() and confirm_sesskey()) {
58 check_for_restricted_user($USER->username
, "$CFG->wwwroot");
60 check_for_restricted_user($USER->username
, "$CFG->wwwroot/course/view.php?id=$course->id");
63 $form->startdate
= make_timestamp($form->startyear
, $form->startmonth
, $form->startday
);
64 $form->category
= clean_param($form->category
, PARAM_INT
);
66 if (empty($form->enrolstartdisabled
)) {
67 $form->enrolstartdate
= make_timestamp($form->enrolstartyear
, $form->enrolstartmonth
, $form->enrolstartday
);
69 $form->enrolstartdate
= 0;
72 if (empty($form->enrolenddisabled
)) {
73 $form->enrolenddate
= make_timestamp($form->enrolendyear
, $form->enrolendmonth
, $form->enrolendday
);
75 $form->enrolenddate
= 0;
78 $form->format
= optional_param('format', 'social', PARAM_ALPHA
);
80 validate_form($course, $form, $err);
82 if (count($err) == 0) {
84 $allowedmods = array();
85 if (!empty($form->allowedmods
)) {
86 $allowedmods = $form->allowedmods
;
87 unset($form->allowedmods
);
90 $form->timemodified
= time();
92 if (!empty($course)) {
93 // Test for and remove blocks which aren't appropriate anymore
94 $page = page_create_object(PAGE_COURSE_VIEW
, $course->id
);
95 blocks_remove_inappropriate($page);
97 // Update with the new data
98 if (update_record('course', $form)) {
99 add_to_log($course->id
, "course", "update", "edit.php?id=$id", "");
101 $course->restrictmodules
= $form->restrictmodules
;
102 update_restricted_mods($course,$allowedmods);
104 fix_course_sortorder();
105 redirect($page->url_get_full(), get_string('changessaved'));
107 error("Serious Error! Could not update the course record! (id = $form->id)");
110 $form->timecreated
= time();
112 // place at beginning of category
113 fix_course_sortorder();
114 $form->sortorder
= get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$form->category");
115 if (empty($form->sortorder
)) {
116 $form->sortorder
= 100;
119 if ($newcourseid = insert_record('course', $form)) { // Set up new course
122 $page = page_create_object(PAGE_COURSE_VIEW
, $newcourseid);
123 blocks_repopulate_page($page); // Return value not checked because you can always edit later
126 $course = get_record("course","id",$newcourseid);
127 update_restricted_mods($course,$allowedmods);
131 $section->course
= $newcourseid; // Create a default section.
132 $section->section
= 0;
133 $section->id
= insert_record("course_sections", $section);
135 fix_course_sortorder();
136 add_to_log(SITEID
, "course", "new", "view.php?id=$newcourseid", "$form->fullname (ID $newcourseid)");
138 if (isadmin()) { // Redirect admin to add teachers
139 redirect("teacher.php?id=$newcourseid", get_string("changessaved"));
141 } else { // Add current teacher and send to course
144 $newteacher->userid
= $USER->id
;
145 $newteacher->course
= $newcourseid;
146 $newteacher->authority
= 1; // First teacher is the main teacher
147 $newteacher->editall
= 1; // Course creator can edit their own course
148 $newteacher->enrol
= 'manual';// enrol the teacher too - bug #6120
150 if (!$newteacher->id
= insert_record("user_teachers", $newteacher)) {
151 error("Could not add you to this new course!");
154 $USER->teacher
[$newcourseid] = true;
155 $USER->teacheredit
[$newcourseid] = true;
157 fix_course_sortorder();
159 redirect("view.php?id=$newcourseid", get_string("changessaved"));
163 error("Serious Error! Could not create the new course!");
168 foreach ($err as $key => $value) {
169 $focus = "form.$key";
175 /// Otherwise fill and print the form.
178 if (!empty($course)) {
181 $form->startdate
= time() +
3600 * 24;
182 $form->fullname
= get_string("defaultcoursefullname");
183 $form->shortname
= get_string("defaultcourseshortname");
184 $form->summary
= get_string("defaultcoursesummary");
185 $form->format
= "weeks";
186 $form->password
= "";
188 $form->numsections
= 10;
189 $form->idnumber
= '';
191 $form->currency
= empty($CFG->enrol_currency
) ?
'USD' : $CFG->enrol_currency
;
192 $form->newsitems
= 5;
193 $form->showgrades
= 1;
194 $form->groupmode
= 0;
195 $form->groupmodeforce
= 0;
196 $form->category
= $category;
200 if (current_language() == $CFG->lang
) {
201 $form->teacher
= $site->teacher
;
202 $form->teachers
= $site->teachers
;
203 $form->student
= $site->student
;
204 $form->students
= $site->students
;
206 $form->teacher
= get_string("defaultcourseteacher");
207 $form->teachers
= get_string("defaultcourseteachers");
208 $form->student
= get_string("defaultcoursestudent");
209 $form->students
= get_string("defaultcoursestudents");
213 $form = stripslashes_safe($form);
216 // !! no db access using data from $form beyond this point !!
218 $form->categories
= get_records_select_menu("course_categories", "", "name", "id,name");
220 $courseformats = get_list_of_plugins("course/format");
221 $form->courseformats
= array();
223 foreach ($courseformats as $courseformat) {
224 $form->courseformats
["$courseformat"] = get_string("format$courseformat");
227 if (empty($allowedmods)) {
228 $allowedmods = array();
229 if (!empty($course)) {
230 if ($am = get_records("course_allowed_modules","course",$course->id
)) {
231 foreach ($am as $m) {
232 $allowedmods[] = $m->module
;
235 if (empty($course->restrictmodules
)) {
236 $allowedmods = explode(',',$CFG->defaultallowedmodules
);
237 } // it'll be greyed out but we want these by default anyway.
240 if ($CFG->restrictmodulesfor
== 'all') {
241 $allowedmods = explode(',',$CFG->defaultallowedmodules
);
242 if (!empty($CFG->restrictbydefault
)) {
243 $form->restrictmodules
= 1;
250 $usehtmleditor = can_use_html_editor();
252 $streditcoursesettings = get_string("editcoursesettings");
253 $straddnewcourse = get_string("addnewcourse");
254 $stradministration = get_string("administration");
255 $strcategories = get_string("categories");
257 if (!empty($course)) {
258 print_header($streditcoursesettings, "$course->fullname",
259 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
260 -> $streditcoursesettings", $focus);
262 print_header("$site->shortname: $straddnewcourse", "$site->fullname",
263 "<a href=\"../$CFG->admin/index.php\">$stradministration</a> -> ".
264 "<a href=\"index.php\">$strcategories</a> -> $straddnewcourse", $focus);
267 $form->sesskey
= !empty($USER->id
) ?
$USER->sesskey
: '';
269 print_heading($streditcoursesettings);
270 print_simple_box_start("center");
271 include("edit.html");
272 print_simple_box_end();
274 if ($usehtmleditor) {
275 use_html_editor("summary");
278 print_footer($course);
282 /// Functions /////////////////////////////////////////////////////////////////
284 function validate_form($course, &$form, &$err) {
286 if (empty($form->enrolenddisabled
) && $form->enrolenddate
<= $form->enrolstartdate
) {
287 $err["enroldate"] = get_string("enrolenddaterror");
290 if (empty($form->fullname
))
291 $err["fullname"] = get_string("missingfullname");
293 if (empty($form->shortname
))
294 $err["shortname"] = get_string("missingshortname");
296 if ($foundcourses = get_records("course", "shortname", $form->shortname
)) {
297 if (!empty($course->id
)) {
298 unset($foundcourses[$course->id
]);
300 if (!empty($foundcourses)) {
301 foreach ($foundcourses as $foundcourse) {
302 $foundcoursenames[] = $foundcourse->fullname
;
304 $foundcoursenamestring = addslashes(implode(',', $foundcoursenames));
306 $err["shortname"] = get_string("shortnametaken", "", $foundcoursenamestring);
310 if (empty($form->summary
))
311 $err["summary"] = get_string("missingsummary");
313 if (empty($form->teacher
))
314 $err["teacher"] = get_string("missingteacher");
316 if (empty($form->student
))
317 $err["student"] = get_string("missingstudent");
319 if (! $form->category
)
320 $err["category"] = get_string("missingcategory");