no message
[moodle.git] / mod / assignment / lib.php
CommitLineData
04eba58f 1<?PHP // $Id$
2
d699cd1e 3include_once("$CFG->dirroot/files/mimetypes.php");
4
b7b42874 5define("OFFLINE", "0");
6define("UPLOADSINGLE", "1");
7
8$ASSIGNMENT_TYPE = array (OFFLINE => get_string("typeoffline", "assignment"),
9 UPLOADSINGLE => get_string("typeuploadsingle", "assignment") );
d699cd1e 10
11
04eba58f 12function assignment_add_instance($assignment) {
13// Given an object containing all the necessary data,
14// (defined by the form in mod.html) this function
15// will create a new instance and return the id number
16// of the new instance.
17
18 $assignment->timemodified = time();
d699cd1e 19
20 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
21 $assignment->duehour, $assignment->dueminute);
04eba58f 22
23 return insert_record("assignment", $assignment);
24}
25
26
27function assignment_update_instance($assignment) {
28// Given an object containing all the necessary data,
29// (defined by the form in mod.html) this function
30// will update an existing instance with new data.
31
32 $assignment->timemodified = time();
d699cd1e 33 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
34 $assignment->duehour, $assignment->dueminute);
04eba58f 35 $assignment->id = $assignment->instance;
36
37 return update_record("assignment", $assignment);
38}
39
40
41function assignment_delete_instance($id) {
42// Given an ID of an instance of this module,
43// this function will permanently delete the instance
44// and any data that depends on it.
45
46 if (! $assignment = get_record("assignment", "id", "$id")) {
47 return false;
48 }
49
50 $result = true;
51
52 if (! delete_records("assignment_submissions", "assignment", "$assignment->id")) {
53 $result = false;
54 }
55
56 if (! delete_records("assignment", "id", "$assignment->id")) {
57 $result = false;
58 }
59
60 return $result;
61}
62
77db7e4c 63function assignment_user_outline($course, $user, $mod, $assignment) {
64 if ($submission = assignment_get_submission($assignment, $user)) {
98092498 65
66 if ($submission->grade) {
67 $result->info = get_string("grade").": $submission->grade";
77db7e4c 68 }
77db7e4c 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
a16c2180 111 global $CFG, $USER;
d699cd1e 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
a5a4cd60 131 $USER->lang = $user->lang;
132
d699cd1e 133 if (! $course = get_record("course", "id", "$submission->course")) {
134 echo "Could not find course $submission->course\n";
135 continue;
136 }
137
138 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
139 continue; // Not an active participant
140 }
141
142 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
143 echo "Could not find teacher $submission->teacher\n";
144 continue;
145 }
146
147 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
148 echo "Could not find course module for assignment id $submission->assignment\n";
149 continue;
150 }
151
152 $strassignments = get_string("modulenameplural", "assignment");
153 $strassignment = get_string("modulename", "assignment");
154
155 $postsubject = "$course->shortname: $strassignments: $submission->name";
156 $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
157 $posttext .= "---------------------------------------------------------------------\n";
158 $posttext .= "$teacher->firstname $teacher->lastname has posted some feedback on your\n";
159 $posttext .= "assignment submission for '$submission->name'\n\n";
160 $posttext .= "You can see it appended to your assignment submission:\n";
161 $posttext .= " $CFG->wwwroot/mod/assignment/view.php?id=$mod->id\n";
162 $posttext .= "---------------------------------------------------------------------\n";
163 if ($user->mailformat == 1) { // HTML
164 $posthtml = "<P><FONT FACE=sans-serif>".
165 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->".
166 "<A HREF=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</A> ->".
167 "<A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">$submission->name</A></FONT></P>";
168 $posthtml .= "<HR><FONT FACE=sans-serif>";
169 $posthtml .= "<P>$teacher->firstname $teacher->lastname has posted some feedback on your";
170 $posthtml .= " assignment submission for '<B>$submission->name</B>'</P>";
171 $posthtml .= "<P>You can see it <A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">";
172 $posthtml .= "appended to your assignment submission</A>.</P></FONT><HR>";
173 } else {
174 $posthtml = "";
175 }
176
177 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
178 echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
179 }
180 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
181 echo "Could not update the mailed field for id $submission->id\n";
182 }
183 }
184 }
185
186 return true;
187}
188
3446205d 189function assignment_print_recent_activity(&$logs, $isteacher=false) {
190 global $CFG, $COURSE_TEACHER_COLOR;
191
192 $content = false;
193 $assignments = NULL;
194
195 foreach ($logs as $log) {
196 if ($log->module == "assignment" and $log->action == "upload") {
197 $assignments[$log->info] = get_record_sql("SELECT a.name, u.firstname, u.lastname
198 FROM assignment a, user u
199 WHERE a.id = '$log->info' AND u.id = '$log->user'");
200 $assignments[$log->info]->time = $log->time;
201 $assignments[$log->info]->url = $log->url;
202 }
203 }
204
205 if ($assignments) {
206 $content = true;
207 print_headline(get_string("newsubmissions", "assignment").":");
208 foreach ($assignments as $assignment) {
62c13a2f 209 $date = userdate($assignment->time, "%d %b, %H:%M");
3446205d 210 echo "<P><FONT SIZE=1>$date - $assignment->firstname $assignment->lastname<BR>";
211 echo "\"<A HREF=\"$CFG->wwwroot/mod/assignment/$assignment->url\">";
212 echo "$assignment->name";
213 echo "</A>\"</FONT></P>";
214 }
215 }
216
217 return $content;
218}
d699cd1e 219
d0ac6bc2 220function assignment_grades($assignmentid) {
858deff0 221/// Must return an array of grades, indexed by user, and a max grade.
d0ac6bc2 222
858deff0 223 $return->grades = get_records_sql_menu("SELECT user,grade FROM assignment_submissions WHERE assignment = '$assignmentid'");
224 $return->maxgrade = get_field("assignment", "grade", "id", "$assignmentid");
225 return $return;
d0ac6bc2 226}
227
d699cd1e 228//////////////////////////////////////////////////////////////////////////////////////
229
230function assignment_file_area_name($assignment, $user) {
231// Creates a directory file name, suitable for make_upload_directory()
ca4f8eb8 232 global $CFG;
233
234 return "$assignment->course/$CFG->moddata/assignment/$assignment->id/$user->id";
d699cd1e 235}
236
237function assignment_file_area($assignment, $user) {
238 return make_upload_directory( assignment_file_area_name($assignment, $user) );
239}
240
241function assignment_get_submission($assignment, $user) {
242 return get_record_sql("SELECT * from assignment_submissions
243 WHERE assignment = '$assignment->id' AND user = '$user->id'");
244}
245
246function assignment_get_all_submissions($assignment) {
7482d8b9 247// Return all assignment submissions by ENROLLED students
248 return get_records_sql("SELECT a.* FROM assignment_submissions a, user_students s
249 WHERE a.user = s.user
250 AND s.course = '$assignment->course'
251 AND a.assignment = '$assignment->id'
252 ORDER BY a.timemodified DESC");
d699cd1e 253}
254
255function assignment_get_users_done($assignment) {
256 return get_records_sql("SELECT u.* FROM user u, user_students s, assignment_submissions a
257 WHERE s.course = '$assignment->course' AND s.user = u.id
258 AND u.id = a.user AND a.assignment = '$assignment->id'
259 ORDER BY a.timemodified DESC");
260}
261
262function assignment_print_difference($time) {
263 if ($time < 0) {
264 $timetext = get_string("late", "assignment", format_time($time));
265 return " (<FONT COLOR=RED>$timetext</FONT>)";
266 } else {
267 $timetext = get_string("early", "assignment", format_time($time));
268 return " ($timetext)";
269 }
270}
271
272function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
273 global $THEME;
274
b7b42874 275 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10 align=center>";
d699cd1e 276
277 echo "\n<TR>";
b7b42874 278 if ($assignment->type == OFFLINE) {
279 echo "\n<TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
280 } else {
281 echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
282 }
d699cd1e 283 print_user_picture($user->id, $assignment->course, $user->picture);
284 echo "</TD>";
b7b42874 285 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">$user->firstname $user->lastname";
286 if ($submission->timemodified) {
d699cd1e 287 echo "&nbsp;&nbsp;<FONT SIZE=1>".get_string("lastmodified").": ";
288 echo userdate($submission->timemodified);
289 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
290 echo "</FONT>";
291 }
292 echo "</TR>";
293
b7b42874 294 if ($assignment->type != OFFLINE) {
295 echo "\n<TR><TD BGCOLOR=\"$THEME->cellcontent\">";
ff446576 296 if ($submission->timemodified) {
b7b42874 297 assignment_print_user_files($assignment, $user);
298 } else {
299 print_string("notsubmittedyet", "assignment");
d699cd1e 300 }
d699cd1e 301 echo "</TD></TR>";
302 }
b7b42874 303
304 echo "\n<TR>";
305 echo "<TD WIDTH=35 VALIGN=TOP>";
306 if (!$submission->teacher) {
307 $submission->teacher = $USER->id;
308 }
309 print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
310 if ($submission->timemodified > $submission->timemarked) {
311 echo "<TD BGCOLOR=\"$THEME->cellheading2\">";
312 } else {
313 echo "<TD BGCOLOR=\"$THEME->cellheading\">";
314 }
f830b324 315 echo get_string("feedback", "assignment").":";
b7b42874 316 choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("grade")."...");
317 if ($submission->timemarked) {
318 echo "&nbsp;&nbsp;<FONT SIZE=1>".userdate($submission->timemarked)."</FONT>";
319 }
320 echo "<BR><TEXTAREA NAME=\"c$submission->id\" ROWS=6 COLS=60 WRAP=virtual>";
321 p($submission->comment);
322 echo "</TEXTAREA><BR>";
323 echo "</TD></TR>";
324
d699cd1e 325 echo "</TABLE><BR CLEAR=ALL>\n";
326}
327
328function assignment_print_feedback($course, $submission) {
329 global $CFG, $THEME, $RATING;
330
331 if (! $teacher = get_record("user", "id", $submission->teacher)) {
332 error("Weird assignment error");
333 }
334
b0f01dff 335 echo "\n<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 ALIGN=CENTER><TR><TD BGCOLOR=#888888>";
336 echo "\n<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 VALIGN=TOP>";
d699cd1e 337
338 echo "\n<TR>";
339 echo "\n<TD ROWSPAN=3 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
340 print_user_picture($teacher->id, $course->id, $teacher->picture);
341 echo "</TD>";
342 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$teacher->firstname $teacher->lastname";
343 echo "&nbsp;&nbsp;<FONT SIZE=2><I>".userdate($submission->timemarked)."</I>";
344 echo "</TR>";
345
346 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
347
348 echo "<P ALIGN=RIGHT><FONT SIZE=-1><I>";
349 if ($submission->grade) {
350 echo get_string("grade").": $submission->grade";
351 } else {
352 echo get_string("nograde");
353 }
354 echo "</I></FONT></P>";
355
356 echo text_to_html($submission->comment);
357 echo "</TD></TR></TABLE>";
b0f01dff 358 echo "</TD></TR></TABLE>";
d699cd1e 359}
360
361
362function assignment_print_user_files($assignment, $user) {
363// Arguments are objects
364
365 global $CFG;
366
367 $filearea = assignment_file_area_name($assignment, $user);
368
369 if ($basedir = assignment_file_area($assignment, $user)) {
370 if ($files = get_directory_list($basedir)) {
371 foreach ($files as $file) {
372 $icon = mimeinfo("icon", $file);
3f8247c2 373 if ($CFG->slasharguments) {
374 $ffurl = "file.php/$filearea/$file";
375 } else {
376 $ffurl = "file.php?file=/$filearea/$file";
377 }
378
d699cd1e 379 echo "<IMG SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"File\">";
3f8247c2 380 echo "&nbsp;<A TARGET=\"uploadedfile\" HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
d699cd1e 381 echo "<BR>";
382 }
383 }
384 }
385}
386
387function assignment_delete_user_files($assignment, $user, $exception) {
388// Deletes all the user files in the assignment area for a user
389// EXCEPT for any file named $exception
390
391 if ($basedir = assignment_file_area($assignment, $user)) {
392 if ($files = get_directory_list($basedir)) {
393 foreach ($files as $file) {
394 if ($file != $exception) {
395 unlink("$basedir/$file");
396 notify("Existing file '$file' has been deleted!");
397 }
398 }
399 }
400 }
401}
402
403function assignment_print_upload_form($assignment) {
404// Arguments are objects
405
406 echo "<DIV ALIGN=CENTER>";
407 echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"POST\" ACTION=upload.php>";
408 echo " <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=\"$assignment->maxfilesize\">";
409 echo " <INPUT TYPE=hidden NAME=id VALUE=\"$assignment->id\">";
410 echo " <INPUT NAME=\"newfile\" TYPE=\"file\" size=\"50\">";
411 echo " <INPUT TYPE=submit NAME=save VALUE=\"".get_string("uploadthisfile")."\">";
412 echo "</FORM>";
413 echo "</DIV>";
414}
04eba58f 415
416?>