Slightly bigger window
[moodle.git] / mod / assignment / lib.php
CommitLineData
04eba58f 1<?PHP // $Id$
2
d699cd1e 3include_once("$CFG->dirroot/files/mimetypes.php");
4
5
6
04eba58f 7function assignment_add_instance($assignment) {
8// Given an object containing all the necessary data,
9// (defined by the form in mod.html) this function
10// will create a new instance and return the id number
11// of the new instance.
12
13 $assignment->timemodified = time();
d699cd1e 14
15 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
16 $assignment->duehour, $assignment->dueminute);
04eba58f 17
18 return insert_record("assignment", $assignment);
19}
20
21
22function assignment_update_instance($assignment) {
23// Given an object containing all the necessary data,
24// (defined by the form in mod.html) this function
25// will update an existing instance with new data.
26
27 $assignment->timemodified = time();
d699cd1e 28 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
29 $assignment->duehour, $assignment->dueminute);
04eba58f 30 $assignment->id = $assignment->instance;
31
32 return update_record("assignment", $assignment);
33}
34
35
36function assignment_delete_instance($id) {
37// Given an ID of an instance of this module,
38// this function will permanently delete the instance
39// and any data that depends on it.
40
41 if (! $assignment = get_record("assignment", "id", "$id")) {
42 return false;
43 }
44
45 $result = true;
46
47 if (! delete_records("assignment_submissions", "assignment", "$assignment->id")) {
48 $result = false;
49 }
50
51 if (! delete_records("assignment", "id", "$assignment->id")) {
52 $result = false;
53 }
54
55 return $result;
56}
57
77db7e4c 58function assignment_user_outline($course, $user, $mod, $assignment) {
59 if ($submission = assignment_get_submission($assignment, $user)) {
60 if ($basedir = assignment_file_area($assignment, $user)) {
61 if ($files = get_directory_list($basedir)) {
62 $countfiles = count($files)." ".get_string("uploadedfiles", "assignment");
63 foreach ($files as $file) {
64 $countfiles .= "; $file";
65 }
66 }
67 }
68 $result->info = $countfiles;
69 $result->time = $submission->timemodified;
70 return $result;
71 }
72 return NULL;
73}
74
75function assignment_user_complete($course, $user, $mod, $assignment) {
76 if ($submission = assignment_get_submission($assignment, $user)) {
77 if ($basedir = assignment_file_area($assignment, $user)) {
78 if ($files = get_directory_list($basedir)) {
79 $countfiles = count($files)." ".get_string("uploadedfiles", "assignment");
80 foreach ($files as $file) {
81 $countfiles .= "; $file";
82 }
83 }
84 }
85
86 print_simple_box_start();
87 echo "<P><FONT SIZE=1>";
88 echo get_string("lastmodified").": ";
89 echo userdate($submission->timemodified);
90 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
91 echo "</FONT></P>";
92
93 assignment_print_user_files($assignment, $user);
94
95 echo "<BR>";
96
97 assignment_print_feedback($course, $submission);
98
99 print_simple_box_end();
100
101 } else {
102 print_string("notsubmittedyet", "assignment");
103 }
104}
105
106
d699cd1e 107function assignment_cron () {
108// Function to be run periodically according to the moodle cron
109// Finds all assignment notifications that have yet to be mailed out, and mails them
110
111 global $CFG;
112
113 $cutofftime = time() - $CFG->maxeditingtime;
114
115 if ($submissions = get_records_sql("SELECT s.*, a.course, a.name
116 FROM assignment_submissions s, assignment a
117 WHERE s.mailed = '0'
118 AND s.timemarked < '$cutofftime' AND s.timemarked > 0
119 AND s.assignment = a.id")) {
120 $timenow = time();
121
122 foreach ($submissions as $submission) {
123
124 echo "Processing assignment submission $submission->id\n";
125
126 if (! $user = get_record("user", "id", "$submission->user")) {
127 echo "Could not find user $post->user\n";
128 continue;
129 }
130
131 if (! $course = get_record("course", "id", "$submission->course")) {
132 echo "Could not find course $submission->course\n";
133 continue;
134 }
135
136 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
137 continue; // Not an active participant
138 }
139
140 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
141 echo "Could not find teacher $submission->teacher\n";
142 continue;
143 }
144
145 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
146 echo "Could not find course module for assignment id $submission->assignment\n";
147 continue;
148 }
149
150 $strassignments = get_string("modulenameplural", "assignment");
151 $strassignment = get_string("modulename", "assignment");
152
153 $postsubject = "$course->shortname: $strassignments: $submission->name";
154 $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
155 $posttext .= "---------------------------------------------------------------------\n";
156 $posttext .= "$teacher->firstname $teacher->lastname has posted some feedback on your\n";
157 $posttext .= "assignment submission for '$submission->name'\n\n";
158 $posttext .= "You can see it appended to your assignment submission:\n";
159 $posttext .= " $CFG->wwwroot/mod/assignment/view.php?id=$mod->id\n";
160 $posttext .= "---------------------------------------------------------------------\n";
161 if ($user->mailformat == 1) { // HTML
162 $posthtml = "<P><FONT FACE=sans-serif>".
163 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->".
164 "<A HREF=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</A> ->".
165 "<A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">$submission->name</A></FONT></P>";
166 $posthtml .= "<HR><FONT FACE=sans-serif>";
167 $posthtml .= "<P>$teacher->firstname $teacher->lastname has posted some feedback on your";
168 $posthtml .= " assignment submission for '<B>$submission->name</B>'</P>";
169 $posthtml .= "<P>You can see it <A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">";
170 $posthtml .= "appended to your assignment submission</A>.</P></FONT><HR>";
171 } else {
172 $posthtml = "";
173 }
174
175 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
176 echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
177 }
178 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
179 echo "Could not update the mailed field for id $submission->id\n";
180 }
181 }
182 }
183
184 return true;
185}
186
187
188//////////////////////////////////////////////////////////////////////////////////////
189
190function assignment_file_area_name($assignment, $user) {
191// Creates a directory file name, suitable for make_upload_directory()
192 return "$assignment->course/assignment/$assignment->id/$user->id";
193}
194
195function assignment_file_area($assignment, $user) {
196 return make_upload_directory( assignment_file_area_name($assignment, $user) );
197}
198
199function assignment_get_submission($assignment, $user) {
200 return get_record_sql("SELECT * from assignment_submissions
201 WHERE assignment = '$assignment->id' AND user = '$user->id'");
202}
203
204function assignment_get_all_submissions($assignment) {
7482d8b9 205// Return all assignment submissions by ENROLLED students
206 return get_records_sql("SELECT a.* FROM assignment_submissions a, user_students s
207 WHERE a.user = s.user
208 AND s.course = '$assignment->course'
209 AND a.assignment = '$assignment->id'
210 ORDER BY a.timemodified DESC");
d699cd1e 211}
212
213function assignment_get_users_done($assignment) {
214 return get_records_sql("SELECT u.* FROM user u, user_students s, assignment_submissions a
215 WHERE s.course = '$assignment->course' AND s.user = u.id
216 AND u.id = a.user AND a.assignment = '$assignment->id'
217 ORDER BY a.timemodified DESC");
218}
219
220function assignment_print_difference($time) {
221 if ($time < 0) {
222 $timetext = get_string("late", "assignment", format_time($time));
223 return " (<FONT COLOR=RED>$timetext</FONT>)";
224 } else {
225 $timetext = get_string("early", "assignment", format_time($time));
226 return " ($timetext)";
227 }
228}
229
230function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
231 global $THEME;
232
233 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10>";
234
235 echo "\n<TR>";
236 echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
237 print_user_picture($user->id, $assignment->course, $user->picture);
238 echo "</TD>";
239 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$user->firstname $user->lastname";
240 if ($submission) {
241 echo "&nbsp;&nbsp;<FONT SIZE=1>".get_string("lastmodified").": ";
242 echo userdate($submission->timemodified);
243 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
244 echo "</FONT>";
245 }
246 echo "</TR>";
247
248 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
249 if ($submission) {
250 assignment_print_user_files($assignment, $user);
251 } else {
252 print_string("notsubmittedyet", "assignment");
253 }
254 echo "</TD></TR>";
255
256 if ($submission) {
257 echo "\n<TR>";
258 echo "<TD WIDTH=35 VALIGN=TOP>";
259 if (!$submission->teacher) {
260 $submission->teacher = $USER->id;
261 }
262 print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
263 echo "<TD BGCOLOR=\"$THEME->cellheading\">Teacher Feedback:";
264 choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("grade")."...");
265 if ($submission->timemarked) {
266 echo "&nbsp;&nbsp;<FONT SIZE=1>".userdate($submission->timemarked)."</FONT>";
267 }
268 echo "<BR><TEXTAREA NAME=\"c$submission->id\" ROWS=6 COLS=60 WRAP=virtual>";
269 p($submission->comment);
270 echo "</TEXTAREA><BR>";
271 echo "</TD></TR>";
272 }
273 echo "</TABLE><BR CLEAR=ALL>\n";
274}
275
276function assignment_print_feedback($course, $submission) {
277 global $CFG, $THEME, $RATING;
278
279 if (! $teacher = get_record("user", "id", $submission->teacher)) {
280 error("Weird assignment error");
281 }
282
b0f01dff 283 echo "\n<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 ALIGN=CENTER><TR><TD BGCOLOR=#888888>";
284 echo "\n<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 VALIGN=TOP>";
d699cd1e 285
286 echo "\n<TR>";
287 echo "\n<TD ROWSPAN=3 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
288 print_user_picture($teacher->id, $course->id, $teacher->picture);
289 echo "</TD>";
290 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$teacher->firstname $teacher->lastname";
291 echo "&nbsp;&nbsp;<FONT SIZE=2><I>".userdate($submission->timemarked)."</I>";
292 echo "</TR>";
293
294 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
295
296 echo "<P ALIGN=RIGHT><FONT SIZE=-1><I>";
297 if ($submission->grade) {
298 echo get_string("grade").": $submission->grade";
299 } else {
300 echo get_string("nograde");
301 }
302 echo "</I></FONT></P>";
303
304 echo text_to_html($submission->comment);
305 echo "</TD></TR></TABLE>";
b0f01dff 306 echo "</TD></TR></TABLE>";
d699cd1e 307}
308
309
310function assignment_print_user_files($assignment, $user) {
311// Arguments are objects
312
313 global $CFG;
314
315 $filearea = assignment_file_area_name($assignment, $user);
316
317 if ($basedir = assignment_file_area($assignment, $user)) {
318 if ($files = get_directory_list($basedir)) {
319 foreach ($files as $file) {
320 $icon = mimeinfo("icon", $file);
321 echo "<IMG SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"File\">";
322 echo "&nbsp;<A TARGET=\"uploadedfile\" HREF=\"$CFG->wwwroot/file.php/$filearea/$file\">$file</A>";
323 echo "<BR>";
324 }
325 }
326 }
327}
328
329function assignment_delete_user_files($assignment, $user, $exception) {
330// Deletes all the user files in the assignment area for a user
331// EXCEPT for any file named $exception
332
333 if ($basedir = assignment_file_area($assignment, $user)) {
334 if ($files = get_directory_list($basedir)) {
335 foreach ($files as $file) {
336 if ($file != $exception) {
337 unlink("$basedir/$file");
338 notify("Existing file '$file' has been deleted!");
339 }
340 }
341 }
342 }
343}
344
345function assignment_print_upload_form($assignment) {
346// Arguments are objects
347
348 echo "<DIV ALIGN=CENTER>";
349 echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"POST\" ACTION=upload.php>";
350 echo " <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=\"$assignment->maxfilesize\">";
351 echo " <INPUT TYPE=hidden NAME=id VALUE=\"$assignment->id\">";
352 echo " <INPUT NAME=\"newfile\" TYPE=\"file\" size=\"50\">";
353 echo " <INPUT TYPE=submit NAME=save VALUE=\"".get_string("uploadthisfile")."\">";
354 echo "</FORM>";
355 echo "</DIV>";
356}
04eba58f 357
358?>