Stuff
[moodle.git] / mod / assignment / lib.php
CommitLineData
04eba58f 1<?PHP // $Id$
2
b0e3a925 3require_once("$CFG->dirroot/files/mimetypes.php");
d699cd1e 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
9fa49e22 115 if ($submissions = assignment_get_unmailed_submissions($cutofftime)) {
d699cd1e 116 $timenow = time();
117
118 foreach ($submissions as $submission) {
119
120 echo "Processing assignment submission $submission->id\n";
121
ebc3bd2b 122 if (! $user = get_record("user", "id", "$submission->userid")) {
123 echo "Could not find user $post->userid\n";
d699cd1e 124 continue;
125 }
126
a5a4cd60 127 $USER->lang = $user->lang;
128
d699cd1e 129 if (! $course = get_record("course", "id", "$submission->course")) {
130 echo "Could not find course $submission->course\n";
131 continue;
132 }
133
134 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
b9287b2f 135 echo "$user->firstname $user->lastname not an active participant in $course->shortname\n";
136 continue;
d699cd1e 137 }
138
139 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
140 echo "Could not find teacher $submission->teacher\n";
141 continue;
142 }
143
144 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
145 echo "Could not find course module for assignment id $submission->assignment\n";
146 continue;
147 }
148
149 $strassignments = get_string("modulenameplural", "assignment");
150 $strassignment = get_string("modulename", "assignment");
151
ae078a98 152 unset($assignmentinfo);
153 $assignmentinfo->teacher = "$teacher->firstname $teacher->lastname";
154 $assignmentinfo->assignment = "$submission->name";
155 $assignmentinfo->url = "$CFG->wwwroot/mod/assignment/view.php?id=$mod->id";
156
d699cd1e 157 $postsubject = "$course->shortname: $strassignments: $submission->name";
158 $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
159 $posttext .= "---------------------------------------------------------------------\n";
ae078a98 160 $posttext .= get_string("assignmentmail", "assignment", $assignmentinfo);
d699cd1e 161 $posttext .= "---------------------------------------------------------------------\n";
ae078a98 162
d699cd1e 163 if ($user->mailformat == 1) { // HTML
ae078a98 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>".get_string("assignmentmailhtml", "assignment", $assignmentinfo)."</p>";
170 $posthtml .= "</font><hr>";
d699cd1e 171 } else {
ae078a98 172 $posthtml = "";
d699cd1e 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
1b5910c4 187function assignment_print_recent_activity($course, $isteacher, $timestart) {
3d891989 188 global $CFG;
3446205d 189
190 $content = false;
191 $assignments = NULL;
192
1b5910c4 193 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
194 "course = '$course->id' AND ".
195 "module = 'assignment' AND ".
196 "action = 'upload' ", "time ASC")) {
197 return false;
198 }
199
200 foreach ($logs as $log) {
201 //Create a temp valid module structure (course,id)
202 $tempmod->course = $log->course;
203 $tempmod->id = $log->info;
204 //Obtain the visible property from the instance
205 $modvisible = instance_is_visible($log->module,$tempmod);
206
207 //Only if the mod is visible
208 if ($modvisible) {
209 $assignments[$log->info] = assignment_log_info($log);
210 $assignments[$log->info]->time = $log->time;
211 $assignments[$log->info]->url = $log->url;
3446205d 212 }
213 }
214
215 if ($assignments) {
dcde9f02 216 $strftimerecent = get_string("strftimerecent");
3446205d 217 $content = true;
218 print_headline(get_string("newsubmissions", "assignment").":");
219 foreach ($assignments as $assignment) {
dcde9f02 220 $date = userdate($assignment->time, $strftimerecent);
1b5910c4 221 echo "<p><font size=1>$date - $assignment->firstname $assignment->lastname<br>";
222 echo "\"<a href=\"$CFG->wwwroot/mod/assignment/$assignment->url\">";
3446205d 223 echo "$assignment->name";
1b5910c4 224 echo "</a>\"</font></p>";
3446205d 225 }
226 }
227
228 return $content;
229}
d699cd1e 230
d0ac6bc2 231function assignment_grades($assignmentid) {
858deff0 232/// Must return an array of grades, indexed by user, and a max grade.
d0ac6bc2 233
9fa49e22 234 $return->grades = get_records_menu("assignment_submissions", "assignment",
ebc3bd2b 235 $assignmentid, "", "userid,grade");
858deff0 236 $return->maxgrade = get_field("assignment", "grade", "id", "$assignmentid");
237 return $return;
d0ac6bc2 238}
239
9fa49e22 240/// SQL STATEMENTS //////////////////////////////////////////////////////////////////
241
242function assignment_log_info($log) {
243 global $CFG;
244 return get_record_sql("SELECT a.name, u.firstname, u.lastname
245 FROM {$CFG->prefix}assignment a,
246 {$CFG->prefix}user u
247 WHERE a.id = '$log->info'
ebc3bd2b 248 AND u.id = '$log->userid'");
9fa49e22 249}
250
df48be74 251function assignment_get_all_submissions($assignment, $sort="", $dir="DESC") {
9fa49e22 252/// Return all assignment submissions by ENROLLED students
253 global $CFG;
000cc405 254
255 if ($sort == "lastname" or $sort == "firstname") {
256 $sort = "u.$sort $dir";
257 } else if (empty($sort)) {
258 $sort = "a.timemodified DESC";
259 } else {
260 $sort = "a.$sort $dir";
261 }
9fa49e22 262 return get_records_sql("SELECT a.*
263 FROM {$CFG->prefix}assignment_submissions a,
000cc405 264 {$CFG->prefix}user_students s,
265 {$CFG->prefix}user u
ebc3bd2b 266 WHERE a.userid = s.userid
b2d0ca3b 267 AND u.id = a.userid
9fa49e22 268 AND s.course = '$assignment->course'
269 AND a.assignment = '$assignment->id'
000cc405 270 ORDER BY $sort");
9fa49e22 271}
272
273function assignment_get_users_done($assignment) {
274/// Return list of users who have done an assignment
275 global $CFG;
276 return get_records_sql("SELECT u.*
277 FROM {$CFG->prefix}user u,
278 {$CFG->prefix}user_students s,
279 {$CFG->prefix}assignment_submissions a
280 WHERE s.course = '$assignment->course'
ebc3bd2b 281 AND s.userid = u.id
282 AND u.id = a.userid
9fa49e22 283 AND a.assignment = '$assignment->id'
284 ORDER BY a.timemodified DESC");
285}
286
287function assignment_get_unmailed_submissions($cutofftime) {
a2631c99 288/// Return list of marked submissions that have not been mailed out for currently enrolled students
9fa49e22 289 global $CFG;
290 return get_records_sql("SELECT s.*, a.course, a.name
291 FROM {$CFG->prefix}assignment_submissions s,
a2631c99 292 {$CFG->prefix}assignment a,
293 {$CFG->prefix}user_students us
9fa49e22 294 WHERE s.mailed = 0
295 AND s.timemarked < $cutofftime
296 AND s.timemarked > 0
a2631c99 297 AND s.assignment = a.id
298 AND s.userid = us.userid
299 AND a.course = us.course");
9fa49e22 300}
301
302
d699cd1e 303//////////////////////////////////////////////////////////////////////////////////////
304
305function assignment_file_area_name($assignment, $user) {
306// Creates a directory file name, suitable for make_upload_directory()
ca4f8eb8 307 global $CFG;
308
309 return "$assignment->course/$CFG->moddata/assignment/$assignment->id/$user->id";
d699cd1e 310}
311
312function assignment_file_area($assignment, $user) {
313 return make_upload_directory( assignment_file_area_name($assignment, $user) );
314}
315
316function assignment_get_submission($assignment, $user) {
21a15d9f 317 $submission = get_record("assignment_submissions", "assignment", $assignment->id, "userid", $user->id);
318 if (!empty($submission->timemodified)) {
319 return $submission;
8e340cb0 320 }
321 return NULL;
d699cd1e 322}
323
324function assignment_print_difference($time) {
325 if ($time < 0) {
326 $timetext = get_string("late", "assignment", format_time($time));
327 return " (<FONT COLOR=RED>$timetext</FONT>)";
328 } else {
329 $timetext = get_string("early", "assignment", format_time($time));
330 return " ($timetext)";
331 }
332}
333
334function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
ce78926d 335 global $THEME, $USER;
d699cd1e 336
b7b42874 337 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10 align=center>";
d699cd1e 338
339 echo "\n<TR>";
b7b42874 340 if ($assignment->type == OFFLINE) {
341 echo "\n<TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
342 } else {
343 echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
344 }
d699cd1e 345 print_user_picture($user->id, $assignment->course, $user->picture);
346 echo "</TD>";
b7b42874 347 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">$user->firstname $user->lastname";
348 if ($submission->timemodified) {
d699cd1e 349 echo "&nbsp;&nbsp;<FONT SIZE=1>".get_string("lastmodified").": ";
350 echo userdate($submission->timemodified);
351 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
352 echo "</FONT>";
353 }
354 echo "</TR>";
355
b7b42874 356 if ($assignment->type != OFFLINE) {
357 echo "\n<TR><TD BGCOLOR=\"$THEME->cellcontent\">";
ff446576 358 if ($submission->timemodified) {
b7b42874 359 assignment_print_user_files($assignment, $user);
360 } else {
361 print_string("notsubmittedyet", "assignment");
d699cd1e 362 }
d699cd1e 363 echo "</TD></TR>";
364 }
b7b42874 365
366 echo "\n<TR>";
367 echo "<TD WIDTH=35 VALIGN=TOP>";
368 if (!$submission->teacher) {
369 $submission->teacher = $USER->id;
370 }
371 print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
372 if ($submission->timemodified > $submission->timemarked) {
373 echo "<TD BGCOLOR=\"$THEME->cellheading2\">";
374 } else {
375 echo "<TD BGCOLOR=\"$THEME->cellheading\">";
376 }
f830b324 377 echo get_string("feedback", "assignment").":";
b7b42874 378 choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("grade")."...");
379 if ($submission->timemarked) {
380 echo "&nbsp;&nbsp;<FONT SIZE=1>".userdate($submission->timemarked)."</FONT>";
381 }
382 echo "<BR><TEXTAREA NAME=\"c$submission->id\" ROWS=6 COLS=60 WRAP=virtual>";
383 p($submission->comment);
384 echo "</TEXTAREA><BR>";
385 echo "</TD></TR>";
386
d699cd1e 387 echo "</TABLE><BR CLEAR=ALL>\n";
388}
389
390function assignment_print_feedback($course, $submission) {
391 global $CFG, $THEME, $RATING;
392
393 if (! $teacher = get_record("user", "id", $submission->teacher)) {
394 error("Weird assignment error");
395 }
396
b0f01dff 397 echo "\n<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 ALIGN=CENTER><TR><TD BGCOLOR=#888888>";
398 echo "\n<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 VALIGN=TOP>";
d699cd1e 399
400 echo "\n<TR>";
401 echo "\n<TD ROWSPAN=3 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
402 print_user_picture($teacher->id, $course->id, $teacher->picture);
403 echo "</TD>";
404 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$teacher->firstname $teacher->lastname";
405 echo "&nbsp;&nbsp;<FONT SIZE=2><I>".userdate($submission->timemarked)."</I>";
406 echo "</TR>";
407
408 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
409
410 echo "<P ALIGN=RIGHT><FONT SIZE=-1><I>";
411 if ($submission->grade) {
412 echo get_string("grade").": $submission->grade";
413 } else {
414 echo get_string("nograde");
415 }
416 echo "</I></FONT></P>";
417
418 echo text_to_html($submission->comment);
419 echo "</TD></TR></TABLE>";
b0f01dff 420 echo "</TD></TR></TABLE>";
d699cd1e 421}
422
423
424function assignment_print_user_files($assignment, $user) {
425// Arguments are objects
426
427 global $CFG;
428
429 $filearea = assignment_file_area_name($assignment, $user);
430
431 if ($basedir = assignment_file_area($assignment, $user)) {
432 if ($files = get_directory_list($basedir)) {
433 foreach ($files as $file) {
434 $icon = mimeinfo("icon", $file);
3f8247c2 435 if ($CFG->slasharguments) {
436 $ffurl = "file.php/$filearea/$file";
437 } else {
438 $ffurl = "file.php?file=/$filearea/$file";
439 }
440
d699cd1e 441 echo "<IMG SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"File\">";
3f8247c2 442 echo "&nbsp;<A TARGET=\"uploadedfile\" HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
d699cd1e 443 echo "<BR>";
444 }
445 }
446 }
447}
448
449function assignment_delete_user_files($assignment, $user, $exception) {
450// Deletes all the user files in the assignment area for a user
451// EXCEPT for any file named $exception
452
453 if ($basedir = assignment_file_area($assignment, $user)) {
454 if ($files = get_directory_list($basedir)) {
455 foreach ($files as $file) {
456 if ($file != $exception) {
457 unlink("$basedir/$file");
458 notify("Existing file '$file' has been deleted!");
459 }
460 }
461 }
462 }
463}
464
465function assignment_print_upload_form($assignment) {
466// Arguments are objects
467
468 echo "<DIV ALIGN=CENTER>";
469 echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"POST\" ACTION=upload.php>";
ce78926d 470 echo " <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=\"$assignment->maxbytes\">";
d699cd1e 471 echo " <INPUT TYPE=hidden NAME=id VALUE=\"$assignment->id\">";
472 echo " <INPUT NAME=\"newfile\" TYPE=\"file\" size=\"50\">";
473 echo " <INPUT TYPE=submit NAME=save VALUE=\"".get_string("uploadthisfile")."\">";
474 echo "</FORM>";
475 echo "</DIV>";
476}
04eba58f 477
478?>