MDL-15476
[moodle-linuxchix.git] / mod / exercise / upload.php
blob8a6796c0733ed35626aea17041db70daea5765eb
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("locallib.php");
7 $id = required_param('id', PARAM_INT); // course module ID
8 $title = optional_param('title', '', PARAM_CLEAN);
10 $timenow = time();
12 // get some esential stuff...
13 if (! $cm = get_coursemodule_from_id('exercise', $id)) {
14 error("Course Module ID was incorrect");
17 if (! $course = get_record("course", "id", $cm->course)) {
18 error("Course is misconfigured");
21 if (! $exercise = get_record("exercise", "id", $cm->instance)) {
22 error("Course module is incorrect");
25 require_login($course->id, false, $cm);
27 $strexercises = get_string("modulenameplural", "exercise");
28 $strexercise = get_string("modulename", "exercise");
29 $strupload = get_string("upload");
31 $navigation = build_navigation($strupload, $cm);
32 print_header_simple(format_string($exercise->name)." : $strupload", "", $navigation,
33 "", "", true);
35 // check that this is not a "rapid" second submission, caused by using the back button
36 // only check if a student, teachers may want to submit a set of exercise variants
37 if (isstudent($course->id)) {
38 if ($submissions = exercise_get_user_submissions($exercise, $USER)) {
39 // returns all submissions, newest on first
40 foreach ($submissions as $submission) {
41 if ($submission->timecreated > $timenow - $CFG->maxeditingtime) {
42 // ignore this submission
43 redirect("view.php?id=$cm->id");
44 print_footer($course);
45 exit();
51 // check existence of title
52 if ($title == '') {
53 notify(get_string("notitlegiven", "exercise") );
55 else {
56 require_once($CFG->dirroot.'/lib/uploadlib.php');
57 $um = new upload_manager('newfile',false,false,$course,false,$exercise->maxbytes);
58 if ($um->preprocess_files()) {
59 $newsubmission->exerciseid = $exercise->id;
60 if (isteacher($course->id)) {
61 // it's an exercise submission, flag it as such
62 $newsubmission->userid = 0;
63 $newsubmission->isexercise = 1; // it's a description of an exercise
65 else {
66 $newsubmission->userid = $USER->id;
68 $newsubmission->title = $title;
69 $newsubmission->timecreated = $timenow;
70 if ($timenow > $exercise->deadline) {
71 $newsubmission->late = 1;
73 if (!$newsubmission->id = insert_record("exercise_submissions", $newsubmission)) {
74 error("exercise upload: Failure to create new submission record!");
76 $dir = exercise_file_area_name($exercise, $newsubmission);
77 if ($um->save_files($dir)) {
78 add_to_log($course->id, "exercise", "submit", "view.php?id=$cm->id", "$exercise->id");
79 print_heading(get_string("uploadsuccess", "assignment", $um->get_new_filename()) );
81 // upload manager will print errors.
82 // clear resubmit flags
83 if (!set_field("exercise_submissions", "resubmit", 0, "exerciseid", $exercise->id, "userid", $USER->id)) {
84 error("Exercise Upload: unable to reset resubmit flag");
87 // upload manager will print errors.
89 print_continue("view.php?id=$cm->id");
91 print_footer($course);