moodle/course/edit.php

215 lines
7.1 KiB
PHP
Raw Normal View History

2001-11-22 06:23:56 +00:00
<?PHP // $Id$
// Edit course settings
2001-11-22 06:23:56 +00:00
require_once("../config.php");
require_once("lib.php");
2001-11-22 06:23:56 +00:00
optional_variable($id, 0); // course id
optional_variable($category, 0); // category id
2001-11-22 06:23:56 +00:00
require_login();
2001-11-22 06:23:56 +00:00
if ($id) {
if (! $course = get_record("course", "id", $id)) {
error("Course ID was incorrect");
}
if (!isteacheredit($course->id)) {
error("You do not currently have editing privileges!");
2001-11-22 06:23:56 +00:00
}
} else { // Admin is creating a new course
if (!iscreator()) {
error("You do not currently have course creation privileges!");
2001-11-22 06:23:56 +00:00
}
2003-04-17 13:20:26 +00:00
$course = NULL;
}
if (! $site = get_site()) {
redirect("$CFG->wwwroot/$CFG->admin/index.php");
2001-11-22 06:23:56 +00:00
}
2003-04-17 13:20:26 +00:00
2001-11-22 06:23:56 +00:00
/// If data submitted, then process and store.
if ($form = data_submitted()) {
2001-11-22 06:23:56 +00:00
$form->startdate = make_timestamp($form->startyear, $form->startmonth, $form->startday);
2001-11-22 06:23:56 +00:00
validate_form($course, $form, $err);
if (count($err) == 0) {
$form->timemodified = time();
if (!empty($course)) {
2001-11-22 06:23:56 +00:00
if (update_record("course", $form)) {
2002-05-31 09:27:30 +00:00
add_to_log($course->id, "course", "update", "edit.php?id=$id", "");
fix_course_sortorder($form->category);
2002-08-04 02:10:00 +00:00
redirect("view.php?id=$course->id", get_string("changessaved"));
2001-11-22 06:23:56 +00:00
} else {
error("Serious Error! Could not update the course record! (id = $form->id)");
}
} else {
$form->timecreated = time();
2003-02-26 04:18:46 +00:00
if ($newcourseid = insert_record("course", $form)) { // Set up new course
$section = NULL;
$section->course = $newcourseid; // Create a default section.
$section->section = 0;
$section->id = insert_record("course_sections", $section);
2001-11-22 06:23:56 +00:00
fix_course_sortorder($form->category);
2003-02-26 04:18:46 +00:00
add_to_log($newcourseid, "course", "new", "view.php?id=$newcourseid", "");
if (isadmin()) { // Redirect admin to add teachers
redirect("teacher.php?id=$newcourseid", get_string("changessaved"));
2003-02-26 04:18:46 +00:00
} else { // Add current teacher and send to course
$newteacher = NULL;
$newteacher->userid = $USER->id;
$newteacher->course = $newcourseid;
$newteacher->authority = 1; // First teacher is the main teacher
$newteacher->editall = 1; // Course creator can edit their own course
2003-02-26 04:18:46 +00:00
if (!$newteacher->id = insert_record("user_teachers", $newteacher)) {
error("Could not add you to this new course!");
}
$USER->teacher[$newcourseid] = true;
$USER->teacheredit[$newcourseid] = true;
2003-02-26 04:18:46 +00:00
redirect("view.php?id=$newcourseid", get_string("changessaved"));
}
2001-11-22 06:23:56 +00:00
} else {
error("Serious Error! Could not create the new course!");
}
}
die;
} else {
foreach ($err as $key => $value) {
$focus = "form.$key";
}
}
}
/// Otherwise fill and print the form.
2002-12-30 06:07:03 +00:00
if (empty($form)) {
2003-01-13 12:16:46 +00:00
if (!empty($course)) {
2001-11-22 06:23:56 +00:00
$form = $course;
} else {
$form->startdate = time() + 3600 * 24;
2002-08-04 02:10:00 +00:00
$form->fullname = get_string("defaultcoursefullname");
$form->shortname = get_string("defaultcourseshortname");
$form->summary = get_string("defaultcoursesummary");
$form->format = "weeks";
2003-01-20 09:30:24 +00:00
$form->password = "";
2003-01-13 12:16:46 +00:00
$form->guest = 0;
$form->numsections = 10;
$form->newsitems = 5;
2002-12-09 06:35:14 +00:00
$form->showrecent = 1;
2003-09-08 13:01:47 +00:00
$form->showgrades = 1;
$form->category = $category;
2003-04-17 13:20:26 +00:00
$form->id = "";
$form->visible = 1;
if (current_language() == $CFG->lang) {
$form->teacher = $site->teacher;
$form->teachers = $site->teachers;
$form->student = $site->student;
$form->students = $site->students;
} else {
$form->teacher = get_string("defaultcourseteacher");
$form->teachers = get_string("defaultcourseteachers");
$form->student = get_string("defaultcoursestudent");
$form->students = get_string("defaultcoursestudents");
}
2001-11-22 06:23:56 +00:00
}
}
2002-12-30 06:07:03 +00:00
if (empty($focus)) {
$focus = "";
}
$form->categories = get_records_select_menu("course_categories", "", "name", "id,name");
$courseformats = get_list_of_plugins("course/format");
$form->courseformats = array();
foreach ($courseformats as $courseformat) {
$form->courseformats["$courseformat"] = get_string("format$courseformat");
}
2001-11-22 06:23:56 +00:00
2002-08-04 02:10:00 +00:00
$streditcoursesettings = get_string("editcoursesettings");
$straddnewcourse = get_string("addnewcourse");
$stradministration = get_string("administration");
$strcategories = get_string("categories");
2002-07-19 12:01:35 +00:00
2003-04-17 13:20:26 +00:00
if (!empty($course)) {
2002-08-04 02:10:00 +00:00
print_header($streditcoursesettings, "$course->fullname",
"<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
2002-08-04 02:10:00 +00:00
-> $streditcoursesettings", $focus);
} else {
print_header("$site->shortname: $straddnewcourse", "$site->fullname",
"<a href=\"../$CFG->admin/index.php\">$stradministration</a> -> ".
"<a href=\"index.php\">$strcategories</a> -> $straddnewcourse", $focus);
2001-11-22 06:23:56 +00:00
}
2002-08-04 02:10:00 +00:00
print_heading($streditcoursesettings);
print_simple_box_start("center", "", "$THEME->cellheading");
2001-11-22 06:23:56 +00:00
include("edit.html");
print_simple_box_end();
print_footer($course);
exit;
/// Functions /////////////////////////////////////////////////////////////////
function validate_form($course, &$form, &$err) {
if (empty($form->fullname))
2002-08-04 02:10:00 +00:00
$err["fullname"] = get_string("missingfullname");
2001-11-22 06:23:56 +00:00
if (empty($form->shortname))
2002-08-04 02:10:00 +00:00
$err["shortname"] = get_string("missingshortname");
2001-11-22 06:23:56 +00:00
if ($foundcourses = get_records("course", "shortname", $form->shortname)) {
if (!empty($course->id)) {
unset($foundcourses[$course->id]);
}
if (!empty($foundcourses)) {
foreach ($foundcourses as $foundcourse) {
$foundcoursenames[] = $foundcourse->fullname;
}
$foundcoursenamestring = addslashes(implode(',', $foundcoursenames));
$err["shortname"] = get_string("shortnametaken", "", $foundcoursenamestring);
}
}
2001-11-22 06:23:56 +00:00
if (empty($form->summary))
2002-08-04 02:10:00 +00:00
$err["summary"] = get_string("missingsummary");
2001-11-22 06:23:56 +00:00
if (empty($form->teacher))
2002-08-04 02:10:00 +00:00
$err["teacher"] = get_string("missingteacher");
2001-11-22 06:23:56 +00:00
if (empty($form->student))
2002-08-04 02:10:00 +00:00
$err["student"] = get_string("missingstudent");
2001-11-22 06:23:56 +00:00
if (! $form->category)
2002-08-04 02:10:00 +00:00
$err["category"] = get_string("missingcategory");
2001-11-22 06:23:56 +00:00
return;
}
?>