Added string for choosereportfilter (for course/recent.php)
[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
4909e176 11if (!isset($CFG->assignment_maxbytes)) {
12 set_config("assignment_maxbytes", 1024000); // Default maximum size for all assignments
13}
14
d699cd1e 15
04eba58f 16function assignment_add_instance($assignment) {
17// Given an object containing all the necessary data,
18// (defined by the form in mod.html) this function
19// will create a new instance and return the id number
20// of the new instance.
21
22 $assignment->timemodified = time();
d699cd1e 23
24 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
25 $assignment->duehour, $assignment->dueminute);
04eba58f 26
27 return insert_record("assignment", $assignment);
28}
29
30
31function assignment_update_instance($assignment) {
32// Given an object containing all the necessary data,
33// (defined by the form in mod.html) this function
34// will update an existing instance with new data.
35
36 $assignment->timemodified = time();
d699cd1e 37 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
38 $assignment->duehour, $assignment->dueminute);
04eba58f 39 $assignment->id = $assignment->instance;
40
41 return update_record("assignment", $assignment);
42}
43
44
45function assignment_delete_instance($id) {
46// Given an ID of an instance of this module,
47// this function will permanently delete the instance
48// and any data that depends on it.
49
50 if (! $assignment = get_record("assignment", "id", "$id")) {
51 return false;
52 }
53
54 $result = true;
55
56 if (! delete_records("assignment_submissions", "assignment", "$assignment->id")) {
57 $result = false;
58 }
59
60 if (! delete_records("assignment", "id", "$assignment->id")) {
61 $result = false;
62 }
63
64 return $result;
65}
66
77db7e4c 67function assignment_user_outline($course, $user, $mod, $assignment) {
68 if ($submission = assignment_get_submission($assignment, $user)) {
98092498 69
70 if ($submission->grade) {
71 $result->info = get_string("grade").": $submission->grade";
77db7e4c 72 }
77db7e4c 73 $result->time = $submission->timemodified;
74 return $result;
75 }
76 return NULL;
77}
78
79function assignment_user_complete($course, $user, $mod, $assignment) {
80 if ($submission = assignment_get_submission($assignment, $user)) {
81 if ($basedir = assignment_file_area($assignment, $user)) {
82 if ($files = get_directory_list($basedir)) {
83 $countfiles = count($files)." ".get_string("uploadedfiles", "assignment");
84 foreach ($files as $file) {
85 $countfiles .= "; $file";
86 }
87 }
88 }
89
90 print_simple_box_start();
0927b54b 91 echo "<p><font size=1>";
77db7e4c 92 echo get_string("lastmodified").": ";
93 echo userdate($submission->timemodified);
94 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
0927b54b 95 echo "</font></p>";
77db7e4c 96
97 assignment_print_user_files($assignment, $user);
98
0927b54b 99 echo "<br />";
77db7e4c 100
0927b54b 101 if (empty($submission->timemarked)) {
102 print_string("notgradedyet", "assignment");
103 } else {
104 assignment_print_feedback($course, $submission);
105 }
77db7e4c 106
107 print_simple_box_end();
108
109 } else {
110 print_string("notsubmittedyet", "assignment");
111 }
112}
113
114
d699cd1e 115function assignment_cron () {
116// Function to be run periodically according to the moodle cron
117// Finds all assignment notifications that have yet to be mailed out, and mails them
118
a16c2180 119 global $CFG, $USER;
d699cd1e 120
121 $cutofftime = time() - $CFG->maxeditingtime;
122
9fa49e22 123 if ($submissions = assignment_get_unmailed_submissions($cutofftime)) {
d699cd1e 124 $timenow = time();
125
126 foreach ($submissions as $submission) {
127
128 echo "Processing assignment submission $submission->id\n";
129
ebc3bd2b 130 if (! $user = get_record("user", "id", "$submission->userid")) {
131 echo "Could not find user $post->userid\n";
d699cd1e 132 continue;
133 }
134
a5a4cd60 135 $USER->lang = $user->lang;
136
d699cd1e 137 if (! $course = get_record("course", "id", "$submission->course")) {
138 echo "Could not find course $submission->course\n";
139 continue;
140 }
141
142 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
6f2e07e8 143 echo fullname($user)." not an active participant in $course->shortname\n";
b9287b2f 144 continue;
d699cd1e 145 }
146
147 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
148 echo "Could not find teacher $submission->teacher\n";
149 continue;
150 }
151
152 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
153 echo "Could not find course module for assignment id $submission->assignment\n";
154 continue;
155 }
156
157 $strassignments = get_string("modulenameplural", "assignment");
158 $strassignment = get_string("modulename", "assignment");
159
ae078a98 160 unset($assignmentinfo);
6f2e07e8 161 $assignmentinfo->teacher = fullname($teacher);
ae078a98 162 $assignmentinfo->assignment = "$submission->name";
163 $assignmentinfo->url = "$CFG->wwwroot/mod/assignment/view.php?id=$mod->id";
164
d699cd1e 165 $postsubject = "$course->shortname: $strassignments: $submission->name";
166 $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
167 $posttext .= "---------------------------------------------------------------------\n";
ae078a98 168 $posttext .= get_string("assignmentmail", "assignment", $assignmentinfo);
d699cd1e 169 $posttext .= "---------------------------------------------------------------------\n";
ae078a98 170
d699cd1e 171 if ($user->mailformat == 1) { // HTML
ae078a98 172 $posthtml = "<p><font face=\"sans-serif\">".
173 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
174 "<a href=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</a> ->".
175 "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">$submission->name</a></font></p>";
176 $posthtml .= "<hr><font face=\"sans-serif\">";
177 $posthtml .= "<p>".get_string("assignmentmailhtml", "assignment", $assignmentinfo)."</p>";
178 $posthtml .= "</font><hr>";
d699cd1e 179 } else {
ae078a98 180 $posthtml = "";
d699cd1e 181 }
182
183 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
184 echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
185 }
186 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
187 echo "Could not update the mailed field for id $submission->id\n";
188 }
189 }
190 }
191
192 return true;
193}
194
1b5910c4 195function assignment_print_recent_activity($course, $isteacher, $timestart) {
3d891989 196 global $CFG;
3446205d 197
198 $content = false;
199 $assignments = NULL;
200
1b5910c4 201 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
202 "course = '$course->id' AND ".
203 "module = 'assignment' AND ".
204 "action = 'upload' ", "time ASC")) {
205 return false;
206 }
207
208 foreach ($logs as $log) {
209 //Create a temp valid module structure (course,id)
210 $tempmod->course = $log->course;
211 $tempmod->id = $log->info;
212 //Obtain the visible property from the instance
213 $modvisible = instance_is_visible($log->module,$tempmod);
214
215 //Only if the mod is visible
216 if ($modvisible) {
217 $assignments[$log->info] = assignment_log_info($log);
218 $assignments[$log->info]->time = $log->time;
219 $assignments[$log->info]->url = $log->url;
3446205d 220 }
221 }
222
223 if ($assignments) {
dcde9f02 224 $strftimerecent = get_string("strftimerecent");
3446205d 225 $content = true;
226 print_headline(get_string("newsubmissions", "assignment").":");
227 foreach ($assignments as $assignment) {
dcde9f02 228 $date = userdate($assignment->time, $strftimerecent);
6f2e07e8 229 echo "<p><font size=1>$date - ".fullname($assignment)."<br />";
1b5910c4 230 echo "\"<a href=\"$CFG->wwwroot/mod/assignment/$assignment->url\">";
3446205d 231 echo "$assignment->name";
1b5910c4 232 echo "</a>\"</font></p>";
3446205d 233 }
234 }
235
236 return $content;
237}
d699cd1e 238
d0ac6bc2 239function assignment_grades($assignmentid) {
858deff0 240/// Must return an array of grades, indexed by user, and a max grade.
d0ac6bc2 241
91719320 242
243 if (!$assignment = get_record("assignment", "id", $assignmentid)) {
244 return NULL;
245 }
246
247 $grades = get_records_menu("assignment_submissions", "assignment",
248 $assignment->id, "", "userid,grade");
249
250 if ($assignment->grade >= 0) {
251 $return->grades = $grades;
734bec5d 252 $return->maxgrade = $assignment->grade;
91719320 253
254 } else {
255 $scaleid = - ($assignment->grade);
256 if ($scale = get_record("scale", "id", $scaleid)) {
257 $scalegrades = make_menu_from_list($scale->scale);
f7c225fb 258 if ($grades) {
259 foreach ($grades as $key => $grade) {
260 $grades[$key] = $scalegrades[$grade];
261 }
91719320 262 }
263 }
264 $return->grades = $grades;
265 $return->maxgrade = "";
266 }
267
858deff0 268 return $return;
d0ac6bc2 269}
270
05855091 271function assignment_get_participants($assignmentid) {
272//Returns the users with data in one assignment
273//(users with records in assignment_submissions, students and teachers)
274
275 global $CFG;
276
277 //Get students
278 $students = get_records_sql("SELECT DISTINCT u.*
279 FROM {$CFG->prefix}user u,
280 {$CFG->prefix}assignment_submissions a
281 WHERE a.assignment = '$assignmentid' and
282 u.id = a.userid");
283 //Get teachers
284 $teachers = get_records_sql("SELECT DISTINCT u.*
285 FROM {$CFG->prefix}user u,
286 {$CFG->prefix}assignment_submissions a
287 WHERE a.assignment = '$assignmentid' and
288 u.id = a.teacher");
289
290 //Add teachers to students
291 if ($teachers) {
292 foreach ($teachers as $teacher) {
293 $students[$teacher->id] = $teacher;
294 }
295 }
296 //Return students array (it contains an array of unique users)
297 return ($students);
298}
299
9fa49e22 300/// SQL STATEMENTS //////////////////////////////////////////////////////////////////
301
302function assignment_log_info($log) {
303 global $CFG;
304 return get_record_sql("SELECT a.name, u.firstname, u.lastname
305 FROM {$CFG->prefix}assignment a,
306 {$CFG->prefix}user u
307 WHERE a.id = '$log->info'
ebc3bd2b 308 AND u.id = '$log->userid'");
9fa49e22 309}
310
8ff79e8c 311function assignment_count_real_submissions($assignment) {
312/// Return all real assignment submissions by ENROLLED students (not empty ones)
313 global $CFG;
314
315 return count_records_sql("SELECT count(a.id)
316 FROM {$CFG->prefix}assignment_submissions a,
317 {$CFG->prefix}user_students s,
318 {$CFG->prefix}user u
319 WHERE a.userid = s.userid
320 AND u.id = a.userid
321 AND s.course = '$assignment->course'
322 AND a.assignment = '$assignment->id'
323 AND a.timemodified > 0");
324}
325
df48be74 326function assignment_get_all_submissions($assignment, $sort="", $dir="DESC") {
8ff79e8c 327/// Return all assignment submissions by ENROLLED students (even empty)
9fa49e22 328 global $CFG;
000cc405 329
330 if ($sort == "lastname" or $sort == "firstname") {
331 $sort = "u.$sort $dir";
332 } else if (empty($sort)) {
333 $sort = "a.timemodified DESC";
334 } else {
335 $sort = "a.$sort $dir";
336 }
9fa49e22 337 return get_records_sql("SELECT a.*
338 FROM {$CFG->prefix}assignment_submissions a,
000cc405 339 {$CFG->prefix}user_students s,
340 {$CFG->prefix}user u
ebc3bd2b 341 WHERE a.userid = s.userid
b2d0ca3b 342 AND u.id = a.userid
9fa49e22 343 AND s.course = '$assignment->course'
344 AND a.assignment = '$assignment->id'
000cc405 345 ORDER BY $sort");
9fa49e22 346}
347
348function assignment_get_users_done($assignment) {
349/// Return list of users who have done an assignment
350 global $CFG;
351 return get_records_sql("SELECT u.*
352 FROM {$CFG->prefix}user u,
353 {$CFG->prefix}user_students s,
354 {$CFG->prefix}assignment_submissions a
355 WHERE s.course = '$assignment->course'
ebc3bd2b 356 AND s.userid = u.id
357 AND u.id = a.userid
9fa49e22 358 AND a.assignment = '$assignment->id'
359 ORDER BY a.timemodified DESC");
360}
361
362function assignment_get_unmailed_submissions($cutofftime) {
a2631c99 363/// Return list of marked submissions that have not been mailed out for currently enrolled students
9fa49e22 364 global $CFG;
365 return get_records_sql("SELECT s.*, a.course, a.name
366 FROM {$CFG->prefix}assignment_submissions s,
a2631c99 367 {$CFG->prefix}assignment a,
368 {$CFG->prefix}user_students us
9fa49e22 369 WHERE s.mailed = 0
370 AND s.timemarked < $cutofftime
371 AND s.timemarked > 0
a2631c99 372 AND s.assignment = a.id
373 AND s.userid = us.userid
374 AND a.course = us.course");
9fa49e22 375}
376
377
d699cd1e 378//////////////////////////////////////////////////////////////////////////////////////
379
380function assignment_file_area_name($assignment, $user) {
381// Creates a directory file name, suitable for make_upload_directory()
ca4f8eb8 382 global $CFG;
383
384 return "$assignment->course/$CFG->moddata/assignment/$assignment->id/$user->id";
d699cd1e 385}
386
387function assignment_file_area($assignment, $user) {
388 return make_upload_directory( assignment_file_area_name($assignment, $user) );
389}
390
391function assignment_get_submission($assignment, $user) {
21a15d9f 392 $submission = get_record("assignment_submissions", "assignment", $assignment->id, "userid", $user->id);
393 if (!empty($submission->timemodified)) {
394 return $submission;
8e340cb0 395 }
396 return NULL;
d699cd1e 397}
398
399function assignment_print_difference($time) {
400 if ($time < 0) {
401 $timetext = get_string("late", "assignment", format_time($time));
402 return " (<FONT COLOR=RED>$timetext</FONT>)";
403 } else {
404 $timetext = get_string("early", "assignment", format_time($time));
405 return " ($timetext)";
406 }
407}
408
409function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
ce78926d 410 global $THEME, $USER;
d699cd1e 411
b7b42874 412 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10 align=center>";
d699cd1e 413
414 echo "\n<TR>";
b7b42874 415 if ($assignment->type == OFFLINE) {
416 echo "\n<TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
417 } else {
418 echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
419 }
d699cd1e 420 print_user_picture($user->id, $assignment->course, $user->picture);
421 echo "</TD>";
6f2e07e8 422 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">".fullname($user, true);
c4df6e17 423 if ($assignment->type != OFFLINE and $submission->timemodified) {
d699cd1e 424 echo "&nbsp;&nbsp;<FONT SIZE=1>".get_string("lastmodified").": ";
425 echo userdate($submission->timemodified);
426 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
427 echo "</FONT>";
428 }
429 echo "</TR>";
430
b7b42874 431 if ($assignment->type != OFFLINE) {
432 echo "\n<TR><TD BGCOLOR=\"$THEME->cellcontent\">";
ff446576 433 if ($submission->timemodified) {
b7b42874 434 assignment_print_user_files($assignment, $user);
435 } else {
436 print_string("notsubmittedyet", "assignment");
d699cd1e 437 }
d699cd1e 438 echo "</TD></TR>";
439 }
b7b42874 440
441 echo "\n<TR>";
442 echo "<TD WIDTH=35 VALIGN=TOP>";
443 if (!$submission->teacher) {
444 $submission->teacher = $USER->id;
445 }
446 print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
447 if ($submission->timemodified > $submission->timemarked) {
448 echo "<TD BGCOLOR=\"$THEME->cellheading2\">";
449 } else {
450 echo "<TD BGCOLOR=\"$THEME->cellheading\">";
451 }
f830b324 452 echo get_string("feedback", "assignment").":";
94d0cea5 453 choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("nograde"));
b7b42874 454 if ($submission->timemarked) {
455 echo "&nbsp;&nbsp;<FONT SIZE=1>".userdate($submission->timemarked)."</FONT>";
456 }
457 echo "<BR><TEXTAREA NAME=\"c$submission->id\" ROWS=6 COLS=60 WRAP=virtual>";
458 p($submission->comment);
459 echo "</TEXTAREA><BR>";
460 echo "</TD></TR>";
461
d699cd1e 462 echo "</TABLE><BR CLEAR=ALL>\n";
463}
464
465function assignment_print_feedback($course, $submission) {
466 global $CFG, $THEME, $RATING;
467
468 if (! $teacher = get_record("user", "id", $submission->teacher)) {
469 error("Weird assignment error");
470 }
471
b0f01dff 472 echo "\n<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 ALIGN=CENTER><TR><TD BGCOLOR=#888888>";
473 echo "\n<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 VALIGN=TOP>";
d699cd1e 474
475 echo "\n<TR>";
476 echo "\n<TD ROWSPAN=3 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
477 print_user_picture($teacher->id, $course->id, $teacher->picture);
478 echo "</TD>";
6f2e07e8 479 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">".fullname($teacher);
d699cd1e 480 echo "&nbsp;&nbsp;<FONT SIZE=2><I>".userdate($submission->timemarked)."</I>";
481 echo "</TR>";
482
483 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
484
485 echo "<P ALIGN=RIGHT><FONT SIZE=-1><I>";
486 if ($submission->grade) {
487 echo get_string("grade").": $submission->grade";
488 } else {
489 echo get_string("nograde");
490 }
491 echo "</I></FONT></P>";
492
493 echo text_to_html($submission->comment);
494 echo "</TD></TR></TABLE>";
b0f01dff 495 echo "</TD></TR></TABLE>";
d699cd1e 496}
497
498
499function assignment_print_user_files($assignment, $user) {
500// Arguments are objects
501
502 global $CFG;
503
504 $filearea = assignment_file_area_name($assignment, $user);
505
506 if ($basedir = assignment_file_area($assignment, $user)) {
507 if ($files = get_directory_list($basedir)) {
508 foreach ($files as $file) {
509 $icon = mimeinfo("icon", $file);
3f8247c2 510 if ($CFG->slasharguments) {
511 $ffurl = "file.php/$filearea/$file";
512 } else {
513 $ffurl = "file.php?file=/$filearea/$file";
514 }
515
d699cd1e 516 echo "<IMG SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"File\">";
3f8247c2 517 echo "&nbsp;<A TARGET=\"uploadedfile\" HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
d699cd1e 518 echo "<BR>";
519 }
520 }
521 }
522}
523
524function assignment_delete_user_files($assignment, $user, $exception) {
525// Deletes all the user files in the assignment area for a user
526// EXCEPT for any file named $exception
527
528 if ($basedir = assignment_file_area($assignment, $user)) {
529 if ($files = get_directory_list($basedir)) {
530 foreach ($files as $file) {
531 if ($file != $exception) {
532 unlink("$basedir/$file");
fed7c5ba 533 notify(get_string("existingfiledeleted", "assignment", $file));
d699cd1e 534 }
535 }
536 }
537 }
538}
539
540function assignment_print_upload_form($assignment) {
541// Arguments are objects
542
543 echo "<DIV ALIGN=CENTER>";
544 echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"POST\" ACTION=upload.php>";
ce78926d 545 echo " <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=\"$assignment->maxbytes\">";
d699cd1e 546 echo " <INPUT TYPE=hidden NAME=id VALUE=\"$assignment->id\">";
547 echo " <INPUT NAME=\"newfile\" TYPE=\"file\" size=\"50\">";
548 echo " <INPUT TYPE=submit NAME=save VALUE=\"".get_string("uploadthisfile")."\">";
549 echo "</FORM>";
550 echo "</DIV>";
551}
04eba58f 552
553?>