When using loginas to be a student, you can now return to being
[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)) {
98092498 60
61 if ($submission->grade) {
62 $result->info = get_string("grade").": $submission->grade";
77db7e4c 63 }
77db7e4c 64 $result->time = $submission->timemodified;
65 return $result;
66 }
67 return NULL;
68}
69
70function assignment_user_complete($course, $user, $mod, $assignment) {
71 if ($submission = assignment_get_submission($assignment, $user)) {
72 if ($basedir = assignment_file_area($assignment, $user)) {
73 if ($files = get_directory_list($basedir)) {
74 $countfiles = count($files)." ".get_string("uploadedfiles", "assignment");
75 foreach ($files as $file) {
76 $countfiles .= "; $file";
77 }
78 }
79 }
80
81 print_simple_box_start();
82 echo "<P><FONT SIZE=1>";
83 echo get_string("lastmodified").": ";
84 echo userdate($submission->timemodified);
85 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
86 echo "</FONT></P>";
87
88 assignment_print_user_files($assignment, $user);
89
90 echo "<BR>";
91
92 assignment_print_feedback($course, $submission);
93
94 print_simple_box_end();
95
96 } else {
97 print_string("notsubmittedyet", "assignment");
98 }
99}
100
101
d699cd1e 102function assignment_cron () {
103// Function to be run periodically according to the moodle cron
104// Finds all assignment notifications that have yet to be mailed out, and mails them
105
a16c2180 106 global $CFG, $USER;
d699cd1e 107
108 $cutofftime = time() - $CFG->maxeditingtime;
109
110 if ($submissions = get_records_sql("SELECT s.*, a.course, a.name
111 FROM assignment_submissions s, assignment a
112 WHERE s.mailed = '0'
113 AND s.timemarked < '$cutofftime' AND s.timemarked > 0
114 AND s.assignment = a.id")) {
115 $timenow = time();
116
117 foreach ($submissions as $submission) {
118
119 echo "Processing assignment submission $submission->id\n";
120
121 if (! $user = get_record("user", "id", "$submission->user")) {
122 echo "Could not find user $post->user\n";
123 continue;
124 }
125
a5a4cd60 126 $USER->lang = $user->lang;
127
d699cd1e 128 if (! $course = get_record("course", "id", "$submission->course")) {
129 echo "Could not find course $submission->course\n";
130 continue;
131 }
132
133 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
134 continue; // Not an active participant
135 }
136
137 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
138 echo "Could not find teacher $submission->teacher\n";
139 continue;
140 }
141
142 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
143 echo "Could not find course module for assignment id $submission->assignment\n";
144 continue;
145 }
146
147 $strassignments = get_string("modulenameplural", "assignment");
148 $strassignment = get_string("modulename", "assignment");
149
150 $postsubject = "$course->shortname: $strassignments: $submission->name";
151 $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
152 $posttext .= "---------------------------------------------------------------------\n";
153 $posttext .= "$teacher->firstname $teacher->lastname has posted some feedback on your\n";
154 $posttext .= "assignment submission for '$submission->name'\n\n";
155 $posttext .= "You can see it appended to your assignment submission:\n";
156 $posttext .= " $CFG->wwwroot/mod/assignment/view.php?id=$mod->id\n";
157 $posttext .= "---------------------------------------------------------------------\n";
158 if ($user->mailformat == 1) { // HTML
159 $posthtml = "<P><FONT FACE=sans-serif>".
160 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->".
161 "<A HREF=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</A> ->".
162 "<A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">$submission->name</A></FONT></P>";
163 $posthtml .= "<HR><FONT FACE=sans-serif>";
164 $posthtml .= "<P>$teacher->firstname $teacher->lastname has posted some feedback on your";
165 $posthtml .= " assignment submission for '<B>$submission->name</B>'</P>";
166 $posthtml .= "<P>You can see it <A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">";
167 $posthtml .= "appended to your assignment submission</A>.</P></FONT><HR>";
168 } else {
169 $posthtml = "";
170 }
171
172 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
173 echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
174 }
175 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
176 echo "Could not update the mailed field for id $submission->id\n";
177 }
178 }
179 }
180
181 return true;
182}
183
3446205d 184function assignment_print_recent_activity(&$logs, $isteacher=false) {
185 global $CFG, $COURSE_TEACHER_COLOR;
186
187 $content = false;
188 $assignments = NULL;
189
190 foreach ($logs as $log) {
191 if ($log->module == "assignment" and $log->action == "upload") {
192 $assignments[$log->info] = get_record_sql("SELECT a.name, u.firstname, u.lastname
193 FROM assignment a, user u
194 WHERE a.id = '$log->info' AND u.id = '$log->user'");
195 $assignments[$log->info]->time = $log->time;
196 $assignments[$log->info]->url = $log->url;
197 }
198 }
199
200 if ($assignments) {
201 $content = true;
202 print_headline(get_string("newsubmissions", "assignment").":");
203 foreach ($assignments as $assignment) {
204 $date = userdate($assignment->time, "%e %b, %H:%M");
205 echo "<P><FONT SIZE=1>$date - $assignment->firstname $assignment->lastname<BR>";
206 echo "\"<A HREF=\"$CFG->wwwroot/mod/assignment/$assignment->url\">";
207 echo "$assignment->name";
208 echo "</A>\"</FONT></P>";
209 }
210 }
211
212 return $content;
213}
d699cd1e 214
d0ac6bc2 215function assignment_grades($assignmentid) {
858deff0 216/// Must return an array of grades, indexed by user, and a max grade.
d0ac6bc2 217
858deff0 218 $return->grades = get_records_sql_menu("SELECT user,grade FROM assignment_submissions WHERE assignment = '$assignmentid'");
219 $return->maxgrade = get_field("assignment", "grade", "id", "$assignmentid");
220 return $return;
d0ac6bc2 221}
222
d699cd1e 223//////////////////////////////////////////////////////////////////////////////////////
224
225function assignment_file_area_name($assignment, $user) {
226// Creates a directory file name, suitable for make_upload_directory()
ca4f8eb8 227 global $CFG;
228
229 return "$assignment->course/$CFG->moddata/assignment/$assignment->id/$user->id";
d699cd1e 230}
231
232function assignment_file_area($assignment, $user) {
233 return make_upload_directory( assignment_file_area_name($assignment, $user) );
234}
235
236function assignment_get_submission($assignment, $user) {
237 return get_record_sql("SELECT * from assignment_submissions
238 WHERE assignment = '$assignment->id' AND user = '$user->id'");
239}
240
241function assignment_get_all_submissions($assignment) {
7482d8b9 242// Return all assignment submissions by ENROLLED students
243 return get_records_sql("SELECT a.* FROM assignment_submissions a, user_students s
244 WHERE a.user = s.user
245 AND s.course = '$assignment->course'
246 AND a.assignment = '$assignment->id'
247 ORDER BY a.timemodified DESC");
d699cd1e 248}
249
250function assignment_get_users_done($assignment) {
251 return get_records_sql("SELECT u.* FROM user u, user_students s, assignment_submissions a
252 WHERE s.course = '$assignment->course' AND s.user = u.id
253 AND u.id = a.user AND a.assignment = '$assignment->id'
254 ORDER BY a.timemodified DESC");
255}
256
257function assignment_print_difference($time) {
258 if ($time < 0) {
259 $timetext = get_string("late", "assignment", format_time($time));
260 return " (<FONT COLOR=RED>$timetext</FONT>)";
261 } else {
262 $timetext = get_string("early", "assignment", format_time($time));
263 return " ($timetext)";
264 }
265}
266
267function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
268 global $THEME;
269
270 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10>";
271
272 echo "\n<TR>";
273 echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
274 print_user_picture($user->id, $assignment->course, $user->picture);
275 echo "</TD>";
276 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$user->firstname $user->lastname";
277 if ($submission) {
278 echo "&nbsp;&nbsp;<FONT SIZE=1>".get_string("lastmodified").": ";
279 echo userdate($submission->timemodified);
280 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
281 echo "</FONT>";
282 }
283 echo "</TR>";
284
285 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
286 if ($submission) {
287 assignment_print_user_files($assignment, $user);
288 } else {
289 print_string("notsubmittedyet", "assignment");
290 }
291 echo "</TD></TR>";
292
293 if ($submission) {
294 echo "\n<TR>";
295 echo "<TD WIDTH=35 VALIGN=TOP>";
296 if (!$submission->teacher) {
297 $submission->teacher = $USER->id;
298 }
299 print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
300 echo "<TD BGCOLOR=\"$THEME->cellheading\">Teacher Feedback:";
301 choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("grade")."...");
302 if ($submission->timemarked) {
303 echo "&nbsp;&nbsp;<FONT SIZE=1>".userdate($submission->timemarked)."</FONT>";
304 }
305 echo "<BR><TEXTAREA NAME=\"c$submission->id\" ROWS=6 COLS=60 WRAP=virtual>";
306 p($submission->comment);
307 echo "</TEXTAREA><BR>";
308 echo "</TD></TR>";
309 }
310 echo "</TABLE><BR CLEAR=ALL>\n";
311}
312
313function assignment_print_feedback($course, $submission) {
314 global $CFG, $THEME, $RATING;
315
316 if (! $teacher = get_record("user", "id", $submission->teacher)) {
317 error("Weird assignment error");
318 }
319
b0f01dff 320 echo "\n<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 ALIGN=CENTER><TR><TD BGCOLOR=#888888>";
321 echo "\n<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 VALIGN=TOP>";
d699cd1e 322
323 echo "\n<TR>";
324 echo "\n<TD ROWSPAN=3 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
325 print_user_picture($teacher->id, $course->id, $teacher->picture);
326 echo "</TD>";
327 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$teacher->firstname $teacher->lastname";
328 echo "&nbsp;&nbsp;<FONT SIZE=2><I>".userdate($submission->timemarked)."</I>";
329 echo "</TR>";
330
331 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
332
333 echo "<P ALIGN=RIGHT><FONT SIZE=-1><I>";
334 if ($submission->grade) {
335 echo get_string("grade").": $submission->grade";
336 } else {
337 echo get_string("nograde");
338 }
339 echo "</I></FONT></P>";
340
341 echo text_to_html($submission->comment);
342 echo "</TD></TR></TABLE>";
b0f01dff 343 echo "</TD></TR></TABLE>";
d699cd1e 344}
345
346
347function assignment_print_user_files($assignment, $user) {
348// Arguments are objects
349
350 global $CFG;
351
352 $filearea = assignment_file_area_name($assignment, $user);
353
354 if ($basedir = assignment_file_area($assignment, $user)) {
355 if ($files = get_directory_list($basedir)) {
356 foreach ($files as $file) {
357 $icon = mimeinfo("icon", $file);
3f8247c2 358 if ($CFG->slasharguments) {
359 $ffurl = "file.php/$filearea/$file";
360 } else {
361 $ffurl = "file.php?file=/$filearea/$file";
362 }
363
d699cd1e 364 echo "<IMG SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"File\">";
3f8247c2 365 echo "&nbsp;<A TARGET=\"uploadedfile\" HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
d699cd1e 366 echo "<BR>";
367 }
368 }
369 }
370}
371
372function assignment_delete_user_files($assignment, $user, $exception) {
373// Deletes all the user files in the assignment area for a user
374// EXCEPT for any file named $exception
375
376 if ($basedir = assignment_file_area($assignment, $user)) {
377 if ($files = get_directory_list($basedir)) {
378 foreach ($files as $file) {
379 if ($file != $exception) {
380 unlink("$basedir/$file");
381 notify("Existing file '$file' has been deleted!");
382 }
383 }
384 }
385 }
386}
387
388function assignment_print_upload_form($assignment) {
389// Arguments are objects
390
391 echo "<DIV ALIGN=CENTER>";
392 echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"POST\" ACTION=upload.php>";
393 echo " <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=\"$assignment->maxfilesize\">";
394 echo " <INPUT TYPE=hidden NAME=id VALUE=\"$assignment->id\">";
395 echo " <INPUT NAME=\"newfile\" TYPE=\"file\" size=\"50\">";
396 echo " <INPUT TYPE=submit NAME=save VALUE=\"".get_string("uploadthisfile")."\">";
397 echo "</FORM>";
398 echo "</DIV>";
399}
04eba58f 400
401?>