132bd36093f3f89048d7d6d11fbecf32d5638399
[moodle.git] / mod / assignment / submissions.php
1 <?PHP  // $Id$
3     require_once("../../config.php");
4     require_once("lib.php");
6     require_variable($id);    // Assignment
8     if (! $assignment = get_record("assignment", "id", $id)) {
9         error("Course module is incorrect");
10     }
11     if (! $course = get_record("course", "id", $assignment->course)) {
12         error("Course is misconfigured");
13     }
14     if (! $cm = get_coursemodule_from_instance("assignment", $assignment->id, $course->id)) {
15         error("Course Module ID was incorrect");
16     }
18     require_login($course->id);
20     if (!isteacher($course->id)) {
21         error("Only teachers can look at this page");
22     }
25     if ($course->category) {
26         $navigation = "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> ->";
27     }
29     $strassignments = get_string("modulenameplural", "assignment");
30     $strassignment  = get_string("modulename", "assignment");
31     $strsubmissions = get_string("submissions", "assignment");
32     $strsaveallfeedback = get_string("saveallfeedback", "assignment");
34     print_header("$course->shortname: $assignment->name", "$course->fullname",
35                  "$navigation <A HREF=index.php?id=$course->id>$strassignments</A> -> 
36                   <A HREF=\"view.php?a=$assignment->id\">$assignment->name</A> -> $strsubmissions", 
37                   "", "", true);
39 /// Get all teachers and students
40     $teachers = get_course_teachers($course->id);
42     if (!$users = get_course_students($course->id)) {
43         print_heading(get_string("nostudentsyet"));
44         print_footer($course);
45         exit;
46     }
48 /// Make some easy ways to reference submissions
49     if ($submissions = assignment_get_all_submissions($assignment, $sort)) {
50         foreach ($submissions as $submission) {
51             $submissionbyuser[$submission->userid] = $submission;
52         }
53     }
55 /// Get all existing submissions and check for missing ones
56     foreach($users as $user) {
57         if (!isset($submissionbyuser[$user->id])) {  // Need to create empty entry
58             $newsubmission->assignment = $assignment->id;
59             $newsubmission->userid = $user->id;
60             $newsubmission->timecreated = time();
61             if (!insert_record("assignment_submissions", $newsubmission)) {
62                 error("Could not insert a new empty submission");
63             }
64         }
65     }
67     if (isset($newsubmission)) {   // Get them all out again to be sure
68         $submissions = assignment_get_all_submissions($assignment, $sort);
69     }
72 /// If data is being submitted, then process it
73     if ($data = data_submitted()) {
74        
75         $feedback = array();
77         // Peel out all the data from variable names.
78         foreach ($data as $key => $val) {
79             if ($key <> "id") {
80                 $type = substr($key,0,1);
81                 $num  = substr($key,1); 
82                 $feedback[$num][$type] = $val;
83             }
84         }
86         $timenow = time();
87         $count = 0;
88         foreach ($feedback as $num => $vals) {
89             $submission = $submissions[$num];
90             // Only update entries where feedback has actually changed.
91             if (($vals['g'] <> $submission->grade) || ($vals['c'] <> addslashes($submission->comment))) {
92                 unset($newsubmission);
93                 $newsubmission->grade      = $vals['g'];
94                 $newsubmission->comment    = $vals['c'];
95                 $newsubmission->teacher    = $USER->id;
96                 $newsubmission->timemarked = $timenow;
97                 $newsubmission->mailed     = 0;           // Make sure mail goes out (again, even)
98                 $newsubmission->id         = $num;
99                 if (empty($submission->timemodified)) {   // eg for offline assignments
100                     $newsubmission->timemodified = $timenow;
101                 }
102                 if (! update_record("assignment_submissions", $newsubmission)) {
103                     notify(get_string("failedupdatefeedback", "assignment", $submission->userid));
104                 } else {
105                     $count++;
106                 }
107             }
108         }
109         $submissions = assignment_get_all_submissions($assignment,$sort);
110         add_to_log($course->id, "assignment", "update grades", "submissions.php?id=$assignment->id", "$count users");
111         notify(get_string("feedbackupdated", "assignment", $count));
112     } else {
113         add_to_log($course->id, "assignment", "view submissions", "submissions.php?id=$assignment->id", "$assignment->id");
114     }
116     for ($i=$assignment->grade; $i>=0; $i--) {
117         $grades[$i] = $i;
118     }
120     // Submission sorting
121     print_simple_box_start("CENTER", "50%");
122     echo "<P align=\"CENTER\">";
123     print_string("order");
124     echo ": <A HREF=\"$ME?id=$assignment->id&sort=lastname&dir=ASC\">".get_string("name")."</a> - ";
125     echo "<A HREF=\"$ME?id=$assignment->id\">".get_string("lastmodified")."</a> - ";
126     echo "<A HREF=\"$ME?id=$assignment->id&sort=grade\">".get_string("feedback")."</a>";
127     echo "</P>";
128     print_simple_box_end();
129     print_spacer(8,1);
131     echo "<FORM ACTION=submissions.php METHOD=post>\n";
132     
133     foreach ($submissions as $submission) {
134         $user = $users[$submission->userid];
135         assignment_print_submission($assignment, $user, $submission, $teachers, $grades);
136     }
138     echo "<CENTER>";
139     echo "<INPUT TYPE=hidden NAME=id VALUE=\"$assignment->id\">";
140     echo "<INPUT TYPE=submit VALUE=\"$strsaveallfeedback\">";
141     echo "</CENTER>";
142     echo "</FORM>";
143     
144     print_footer($course);
145  
146 ?>