Polished.
[moodle.git] / course / edit.php
blob72f1c32ddf35fab992aef1f7ea0148e50ceb63a4
1 <?PHP // $Id$
2 // Edit course settings
4 require_once("../config.php");
5 require_once("lib.php");
7 optional_variable($id, 0); // course id
9 require_login();
11 if ($id) {
12 if (! $course = get_record("course", "id", $id)) {
13 error("Course ID was incorrect");
16 if (!isteacher($course->id)) {
17 error("Only teachers can edit the course!");
19 } else { // Admin is creating a new course
21 if (!iscreator()) {
22 error("Only administrators and teachers can use this page");
25 $course = NULL;
28 if (! $site = get_site()) {
29 redirect("$CFG->wwwroot/$CFG->admin/index.php");
35 /// If data submitted, then process and store.
37 if ($form = data_submitted()) {
39 $form->startdate = make_timestamp($form->startyear, $form->startmonth, $form->startday);
41 validate_form($course, $form, $err);
43 if (count($err) == 0) {
45 $form->timemodified = time();
47 if (!empty($course)) {
48 if (update_record("course", $form)) {
49 add_to_log($course->id, "course", "update", "edit.php?id=$id", "");
50 redirect("view.php?id=$course->id", get_string("changessaved"));
51 } else {
52 error("Serious Error! Could not update the course record! (id = $form->id)");
54 } else {
55 $form->timecreated = time();
57 if ($newcourseid = insert_record("course", $form)) { // Set up new course
58 $section = NULL;
59 $section->course = $newcourseid; // Create a default section.
60 $section->section = 0;
61 $section->id = insert_record("course_sections", $section);
63 add_to_log($newcourseid, "course", "new", "view.php?id=$newcourseid", "");
65 if (isadmin()) { // Redirect admin to add teachers
66 redirect("../$CFG->admin/teacher.php?id=$newcourseid", get_string("changessaved"));
68 } else { // Add current teacher and send to course
70 $newteacher = NULL;
71 $newteacher->userid = $USER->id;
72 $newteacher->course = $newcourseid;
73 $newteacher->authority = 1; // First teacher is the main teacher
75 if (!$newteacher->id = insert_record("user_teachers", $newteacher)) {
76 error("Could not add you to this new course!");
79 $USER->teacher[$newcourseid] = true;
81 redirect("view.php?id=$newcourseid", get_string("changessaved"));
84 } else {
85 error("Serious Error! Could not create the new course!");
88 die;
89 } else {
90 foreach ($err as $key => $value) {
91 $focus = "form.$key";
97 /// Otherwise fill and print the form.
99 if (empty($form)) {
100 if (!empty($course)) {
101 $form = $course;
102 } else {
103 $form->startdate = time() + 3600 * 24;
104 $form->fullname = get_string("defaultcoursefullname");
105 $form->shortname = get_string("defaultcourseshortname");
106 $form->teacher = get_string("defaultcourseteacher");
107 $form->teachers = get_string("defaultcourseteachers");
108 $form->student = get_string("defaultcoursestudent");
109 $form->students = get_string("defaultcoursestudents");
110 $form->summary = get_string("defaultcoursesummary");
111 $form->format = "weeks";
112 $form->password = "";
113 $form->guest = 0;
114 $form->numsections = 10;
115 $form->newsitems = 5;
116 $form->showrecent = 1;
117 $form->category = 1;
118 $form->id = "";
122 if (empty($focus)) {
123 $focus = "";
126 $form->categories = get_records_select_menu("course_categories", "", "name", "id,name");
128 $form->courseformats = array (
129 "weeks" => get_string("formatweeks"),
130 "social" => get_string("formatsocial"),
131 "topics" => get_string("formattopics")
134 $streditcoursesettings = get_string("editcoursesettings");
135 $straddnewcourse = get_string("addnewcourse");
136 $stradministration = get_string("administration");
138 if (!empty($course)) {
139 print_header($streditcoursesettings, "$course->fullname",
140 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
141 -> $streditcoursesettings", $focus);
142 } else {
143 print_header("$site->shortname: $straddnewcourse", "$site->fullname",
144 "<A HREF=\"../$CFG->admin/index.php\">$stradministration</A>
145 -> $straddnewcourse", $focus);
148 print_heading($streditcoursesettings);
149 print_simple_box_start("center", "", "$THEME->cellheading");
150 include("edit.html");
151 print_simple_box_end();
153 print_footer($course);
155 exit;
157 /// Functions /////////////////////////////////////////////////////////////////
159 function validate_form($course, &$form, &$err) {
161 if (empty($form->fullname))
162 $err["fullname"] = get_string("missingfullname");
164 if (empty($form->shortname))
165 $err["shortname"] = get_string("missingshortname");
167 if ($foundcourses = get_records("course", "shortname", $form->shortname)) {
168 if (!empty($course->id)) {
169 unset($foundcourses[$course->id]);
171 if (!empty($foundcourses)) {
172 foreach ($foundcourses as $foundcourse) {
173 $foundcoursenames[] = $foundcourse->fullname;
175 $foundcoursenamestring = addslashes(implode(',', $foundcoursenames));
177 $err["shortname"] = get_string("shortnametaken", "", $foundcoursenamestring);
181 if (empty($form->summary))
182 $err["summary"] = get_string("missingsummary");
184 if (empty($form->teacher))
185 $err["teacher"] = get_string("missingteacher");
187 if (empty($form->student))
188 $err["student"] = get_string("missingstudent");
190 if (! $form->category)
191 $err["category"] = get_string("missingcategory");
193 return;