3 include_once("$CFG->dirroot/files/mimetypes.php");
5 define("OFFLINE", "0");
6 define("UPLOADSINGLE", "1");
8 $ASSIGNMENT_TYPE = array (OFFLINE
=> get_string("typeoffline", "assignment"),
9 UPLOADSINGLE
=> get_string("typeuploadsingle", "assignment") );
12 function 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.
18 $assignment->timemodified
= time();
20 $assignment->timedue
= make_timestamp($assignment->dueyear
, $assignment->duemonth
, $assignment->dueday
,
21 $assignment->duehour
, $assignment->dueminute
);
23 return insert_record("assignment", $assignment);
27 function 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.
32 $assignment->timemodified
= time();
33 $assignment->timedue
= make_timestamp($assignment->dueyear
, $assignment->duemonth
, $assignment->dueday
,
34 $assignment->duehour
, $assignment->dueminute
);
35 $assignment->id
= $assignment->instance
;
37 return update_record("assignment", $assignment);
41 function 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.
46 if (! $assignment = get_record("assignment", "id", "$id")) {
52 if (! delete_records("assignment_submissions", "assignment", "$assignment->id")) {
56 if (! delete_records("assignment", "id", "$assignment->id")) {
63 function assignment_user_outline($course, $user, $mod, $assignment) {
64 if ($submission = assignment_get_submission($assignment, $user)) {
66 if ($submission->grade
) {
67 $result->info
= get_string("grade").": $submission->grade";
69 $result->time
= $submission->timemodified
;
75 function 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";
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
);
93 assignment_print_user_files($assignment, $user);
97 assignment_print_feedback($course, $submission);
99 print_simple_box_end();
102 print_string("notsubmittedyet", "assignment");
107 function 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
113 $cutofftime = time() - $CFG->maxeditingtime
;
115 if ($submissions = get_records_sql("SELECT s.*, a.course, a.name
116 FROM assignment_submissions s, assignment a
118 AND s.timemarked < '$cutofftime' AND s.timemarked > 0
119 AND s.assignment = a.id")) {
122 foreach ($submissions as $submission) {
124 echo "Processing assignment submission $submission->id\n";
126 if (! $user = get_record("user", "id", "$submission->user")) {
127 echo "Could not find user $post->user\n";
131 $USER->lang
= $user->lang
;
133 if (! $course = get_record("course", "id", "$submission->course")) {
134 echo "Could not find course $submission->course\n";
138 if (! isstudent($course->id
, $user->id
) and !isteacher($course->id
, $user->id
)) {
139 continue; // Not an active participant
142 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
143 echo "Could not find teacher $submission->teacher\n";
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";
152 $strassignments = get_string("modulenameplural", "assignment");
153 $strassignment = get_string("modulename", "assignment");
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>";
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";
180 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
181 echo "Could not update the mailed field for id $submission->id\n";
189 function assignment_print_recent_activity(&$logs, $isteacher=false) {
190 global $CFG, $COURSE_TEACHER_COLOR;
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
;
207 print_headline(get_string("newsubmissions", "assignment").":");
208 foreach ($assignments as $assignment) {
209 $date = userdate($assignment->time
, "%d %b, %H:%M");
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>";
220 function assignment_grades($assignmentid) {
221 /// Must return an array of grades, indexed by user, and a max grade.
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");
228 //////////////////////////////////////////////////////////////////////////////////////
230 function assignment_file_area_name($assignment, $user) {
231 // Creates a directory file name, suitable for make_upload_directory()
234 return "$assignment->course/$CFG->moddata/assignment/$assignment->id/$user->id";
237 function assignment_file_area($assignment, $user) {
238 return make_upload_directory( assignment_file_area_name($assignment, $user) );
241 function assignment_get_submission($assignment, $user) {
242 return get_record_sql("SELECT * from assignment_submissions
243 WHERE assignment = '$assignment->id' AND user = '$user->id'");
246 function assignment_get_all_submissions($assignment) {
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");
255 function 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");
262 function assignment_print_difference($time) {
264 $timetext = get_string("late", "assignment", format_time($time));
265 return " (<FONT COLOR=RED>$timetext</FONT>)";
267 $timetext = get_string("early", "assignment", format_time($time));
268 return " ($timetext)";
272 function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
275 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10 align=center>";
278 if ($assignment->type
== OFFLINE
) {
279 echo "\n<TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
281 echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
283 print_user_picture($user->id
, $assignment->course
, $user->picture
);
285 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">$user->firstname $user->lastname";
286 if ($submission->timemodified
) {
287 echo " <FONT SIZE=1>".get_string("lastmodified").": ";
288 echo userdate($submission->timemodified
);
289 echo assignment_print_difference($assignment->timedue
- $submission->timemodified
);
294 if ($assignment->type
!= OFFLINE
) {
295 echo "\n<TR><TD BGCOLOR=\"$THEME->cellcontent\">";
296 if ($submission->timemodified
) {
297 assignment_print_user_files($assignment, $user);
299 print_string("notsubmittedyet", "assignment");
305 echo "<TD WIDTH=35 VALIGN=TOP>";
306 if (!$submission->teacher
) {
307 $submission->teacher
= $USER->id
;
309 print_user_picture($submission->teacher
, $assignment->course
, $teachers[$submission->teacher
]->picture
);
310 if ($submission->timemodified
> $submission->timemarked
) {
311 echo "<TD BGCOLOR=\"$THEME->cellheading2\">";
313 echo "<TD BGCOLOR=\"$THEME->cellheading\">";
315 echo "Teacher Feedback:";
316 choose_from_menu($grades, "g$submission->id", $submission->grade
, get_string("grade")."...");
317 if ($submission->timemarked
) {
318 echo " <FONT SIZE=1>".userdate($submission->timemarked
)."</FONT>";
320 echo "<BR><TEXTAREA NAME=\"c$submission->id\" ROWS=6 COLS=60 WRAP=virtual>";
321 p($submission->comment
);
322 echo "</TEXTAREA><BR>";
325 echo "</TABLE><BR CLEAR=ALL>\n";
328 function assignment_print_feedback($course, $submission) {
329 global $CFG, $THEME, $RATING;
331 if (! $teacher = get_record("user", "id", $submission->teacher
)) {
332 error("Weird assignment error");
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>";
339 echo "\n<TD ROWSPAN=3 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
340 print_user_picture($teacher->id
, $course->id
, $teacher->picture
);
342 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$teacher->firstname $teacher->lastname";
343 echo " <FONT SIZE=2><I>".userdate($submission->timemarked
)."</I>";
346 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
348 echo "<P ALIGN=RIGHT><FONT SIZE=-1><I>";
349 if ($submission->grade
) {
350 echo get_string("grade").": $submission->grade";
352 echo get_string("nograde");
354 echo "</I></FONT></P>";
356 echo text_to_html($submission->comment
);
357 echo "</TD></TR></TABLE>";
358 echo "</TD></TR></TABLE>";
362 function assignment_print_user_files($assignment, $user) {
363 // Arguments are objects
367 $filearea = assignment_file_area_name($assignment, $user);
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);
373 if ($CFG->slasharguments
) {
374 $ffurl = "file.php/$filearea/$file";
376 $ffurl = "file.php?file=/$filearea/$file";
379 echo "<IMG SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"File\">";
380 echo " <A TARGET=\"uploadedfile\" HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
387 function 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
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!");
403 function assignment_print_upload_form($assignment) {
404 // Arguments are objects
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")."\">";