MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / exercise / upload.php
blob375f125be880405c7b825a294ca55ff0d153fc61
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 $navlinks = array();
32 $navlinks[] = array('name' => $strexercises, 'link' => "index.php?id=$course->id", 'type' => 'activity');
33 $navlinks[] = array('name' => format_string($exercise->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
34 $navlinks[] = array('name' => $strupload, 'link' => '', 'type' => 'title');
35 $navigation = build_navigation($navlinks);
37 print_header_simple(format_string($exercise->name)." : $strupload", "", $navigation,
38 "", "", true);
40 // check that this is not a "rapid" second submission, caused by using the back button
41 // only check if a student, teachers may want to submit a set of exercise variants
42 if (isstudent($course->id)) {
43 if ($submissions = exercise_get_user_submissions($exercise, $USER)) {
44 // returns all submissions, newest on first
45 foreach ($submissions as $submission) {
46 if ($submission->timecreated > $timenow - $CFG->maxeditingtime) {
47 // ignore this submission
48 redirect("view.php?id=$cm->id");
49 print_footer($course);
50 exit();
56 // check existence of title
57 if ($title == '') {
58 notify(get_string("notitlegiven", "exercise") );
60 else {
61 require_once($CFG->dirroot.'/lib/uploadlib.php');
62 $um = new upload_manager('newfile',false,false,$course,false,$exercise->maxbytes);
63 if ($um->preprocess_files()) {
64 $newsubmission->exerciseid = $exercise->id;
65 if (isteacher($course->id)) {
66 // it's an exercise submission, flag it as such
67 $newsubmission->userid = 0;
68 $newsubmission->isexercise = 1; // it's a description of an exercise
70 else {
71 $newsubmission->userid = $USER->id;
73 $newsubmission->title = $title;
74 $newsubmission->timecreated = $timenow;
75 if ($timenow > $exercise->deadline) {
76 $newsubmission->late = 1;
78 if (!$newsubmission->id = insert_record("exercise_submissions", $newsubmission)) {
79 error("exercise upload: Failure to create new submission record!");
81 $dir = exercise_file_area_name($exercise, $newsubmission);
82 if ($um->save_files($dir)) {
83 add_to_log($course->id, "exercise", "submit", "view.php?id=$cm->id", "$exercise->id");
84 print_heading(get_string("uploadsuccess", "assignment", $um->get_new_filename()) );
86 // upload manager will print errors.
87 // clear resubmit flags
88 if (!set_field("exercise_submissions", "resubmit", 0, "exerciseid", $exercise->id, "userid", $USER->id)) {
89 error("Exercise Upload: unable to reset resubmit flag");
92 // upload manager will print errors.
94 print_continue("view.php?id=$cm->id");
96 print_footer($course);