Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b50b67e50b
Fetching contributors…

Cannot retrieve contributors at this time

file 168 lines (124 sloc) 5.137 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
<?PHP // $Id$

require("../config.php");
require("lib.php");

    optional_variable($id, 0); // course id

    if ($id) {
        if (! $course = get_record("course", "id", $id)) {
            error("Course ID was incorrect");
        }

require_login($course->id);

        if (!isteacher($course->id)) {
            error("Only teachers can edit the course!");
        }
    } else { // Admin is creating a new course
        require_login();

        if (!isadmin()) {
            error("Only administrators can use this page");
        }
    }


/// If data submitted, then process and store.

if (match_referer() && isset($HTTP_POST_VARS)) {

        $form = (object)$HTTP_POST_VARS;

        $form->startdate = mktime(0,0,0,(int)$form->startmonth,(int)$form->startday,(int)$form->startyear);
        $form->enddate = mktime(0,0,0,(int)$form->endmonth,(int)$form->endday,(int)$form->endyear);

        validate_form($course, $form, $err);


        if (count($err) == 0) {

            $form->timemodified = time();

            if ($course) {
                if (update_record("course", $form)) {
                    add_to_log($course->id, "course", "update", "edit.php?id=$id", "");
redirect("view.php?id=$course->id", "Changes saved");
                } else {
                    error("Serious Error! Could not update the course record! (id = $form->id)");
                }
            } else {
                if ($newid = insert_record("course", $form)) { // Set up new course
                    $week->course = $newid; // Create a default week.
                    $week->week = 0;
                    $week->timemodified = time();
                    $week->id = insert_record("course_weeks", $week);

                    add_to_log($newid, "course", "new", "view.php?id=$newid", "");
redirect("$CFG->wwwroot/admin/teacher.php?id=$newid", "Changes saved");
                } 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.

    if (!$form) {
        if ($course) {
            $form = $course;
            $ts = getdate($course->startdate);
            $te = getdate($course->enddate);
        } else {
            $ts = getdate(time() + 3600 * 24);
            $te = getdate(time() + 3600 * 24 * 7 * 16);
        }

        $form->startday = $ts[mday];
        $form->startmonth = $ts[mon];
        $form->startyear = $ts[year];

        $form->endday = $te[mday];
        $form->endmonth = $te[mon];
        $form->endyear = $te[year];

        if (!$course) {
            $form->teacher = "Facilitator";
            $form->fullname = "Course Fullname 101";
            $form->shortname = "CF101";
            $form->summary = "Write a concise and interesting paragraph here that explains what this course is about.";
            $form->format = 0;
            $form->category = 1;
        }
    }

    for ($i=1;$i<=31;$i++) {
        $form->days[$i] = "$i";
    }
    for ($i=1;$i<=12;$i++) {
        $form->months[$i] = date("F", mktime(0,0,0,$i,1,2000));
    }
    for ($i=2000;$i<=2005;$i++) {
        $form->years[$i] = $i;
    }

    $form->categories = get_records_sql_menu("SELECT id,name FROM course_categories");

    //$form->owners = get_records_sql_menu("SELECT u.id, CONCAT(u.firstname, " ", u.lastname) FROM users u, teachers t WHERE t.user = u.id");

    if (isadmin()) {
        print_header("Admin: Creating a new course", "$CFG->sitename: Administration",
                     "<A HREF=\"$CFG->wwwroot/admin/\">Admin</A>
-> Create a new course", $focus);

    } else {
print_header("Edit course settings", "$course->fullname",
                     "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
                      -> Edit course settings", $focus);
}

print_simple_box_start("center", "", "$THEME->cellheading");
print_heading("Editing course settings");
include("edit.html");
print_simple_box_end();

print_footer($course);

exit;

/// Functions /////////////////////////////////////////////////////////////////

function validate_form($course, &$form, &$err) {

if (empty($form->fullname))
$err["fullname"] = "Missing full name";

if (empty($form->shortname))
$err["shortname"] = "Missing short name";

if (empty($form->summary))
$err["summary"] = "Missing summary";

if (empty($form->teacher))
$err["teacher"] = "Missing Teacher/Tutor/Instructor/Facilitator";


if ($form->startdate > $form->enddate)
$err["startdate"] = "Starts after it ends!";

if (($form->startdate < time()) && ($course->format <> $form->format)) {
$err["format"] = "Can't change the format now";
$form->format = $course->format;
}

if (! $form->category)
$err["category"] = "You need to choose a category";

return;
}


?>
Something went wrong with that request. Please try again.