Formatting tweak
[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
d699cd1e 58function assignment_cron () {
59// Function to be run periodically according to the moodle cron
60// Finds all assignment notifications that have yet to be mailed out, and mails them
61
62 global $CFG;
63
64 $cutofftime = time() - $CFG->maxeditingtime;
65
66 if ($submissions = get_records_sql("SELECT s.*, a.course, a.name
67 FROM assignment_submissions s, assignment a
68 WHERE s.mailed = '0'
69 AND s.timemarked < '$cutofftime' AND s.timemarked > 0
70 AND s.assignment = a.id")) {
71 $timenow = time();
72
73 foreach ($submissions as $submission) {
74
75 echo "Processing assignment submission $submission->id\n";
76
77 if (! $user = get_record("user", "id", "$submission->user")) {
78 echo "Could not find user $post->user\n";
79 continue;
80 }
81
82 if (! $course = get_record("course", "id", "$submission->course")) {
83 echo "Could not find course $submission->course\n";
84 continue;
85 }
86
87 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
88 continue; // Not an active participant
89 }
90
91 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
92 echo "Could not find teacher $submission->teacher\n";
93 continue;
94 }
95
96 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
97 echo "Could not find course module for assignment id $submission->assignment\n";
98 continue;
99 }
100
101 $strassignments = get_string("modulenameplural", "assignment");
102 $strassignment = get_string("modulename", "assignment");
103
104 $postsubject = "$course->shortname: $strassignments: $submission->name";
105 $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
106 $posttext .= "---------------------------------------------------------------------\n";
107 $posttext .= "$teacher->firstname $teacher->lastname has posted some feedback on your\n";
108 $posttext .= "assignment submission for '$submission->name'\n\n";
109 $posttext .= "You can see it appended to your assignment submission:\n";
110 $posttext .= " $CFG->wwwroot/mod/assignment/view.php?id=$mod->id\n";
111 $posttext .= "---------------------------------------------------------------------\n";
112 if ($user->mailformat == 1) { // HTML
113 $posthtml = "<P><FONT FACE=sans-serif>".
114 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->".
115 "<A HREF=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</A> ->".
116 "<A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">$submission->name</A></FONT></P>";
117 $posthtml .= "<HR><FONT FACE=sans-serif>";
118 $posthtml .= "<P>$teacher->firstname $teacher->lastname has posted some feedback on your";
119 $posthtml .= " assignment submission for '<B>$submission->name</B>'</P>";
120 $posthtml .= "<P>You can see it <A HREF=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">";
121 $posthtml .= "appended to your assignment submission</A>.</P></FONT><HR>";
122 } else {
123 $posthtml = "";
124 }
125
126 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
127 echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
128 }
129 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
130 echo "Could not update the mailed field for id $submission->id\n";
131 }
132 }
133 }
134
135 return true;
136}
137
138
139//////////////////////////////////////////////////////////////////////////////////////
140
141function assignment_file_area_name($assignment, $user) {
142// Creates a directory file name, suitable for make_upload_directory()
143 return "$assignment->course/assignment/$assignment->id/$user->id";
144}
145
146function assignment_file_area($assignment, $user) {
147 return make_upload_directory( assignment_file_area_name($assignment, $user) );
148}
149
150function assignment_get_submission($assignment, $user) {
151 return get_record_sql("SELECT * from assignment_submissions
152 WHERE assignment = '$assignment->id' AND user = '$user->id'");
153}
154
155function assignment_get_all_submissions($assignment) {
156 return get_records("assignment_submissions", "assignment", $assignment->id, "timemodified DESC");
157}
158
159function assignment_get_users_done($assignment) {
160 return get_records_sql("SELECT u.* FROM user u, user_students s, assignment_submissions a
161 WHERE s.course = '$assignment->course' AND s.user = u.id
162 AND u.id = a.user AND a.assignment = '$assignment->id'
163 ORDER BY a.timemodified DESC");
164}
165
166function assignment_print_difference($time) {
167 if ($time < 0) {
168 $timetext = get_string("late", "assignment", format_time($time));
169 return " (<FONT COLOR=RED>$timetext</FONT>)";
170 } else {
171 $timetext = get_string("early", "assignment", format_time($time));
172 return " ($timetext)";
173 }
174}
175
176function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
177 global $THEME;
178
179 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10>";
180
181 echo "\n<TR>";
182 echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
183 print_user_picture($user->id, $assignment->course, $user->picture);
184 echo "</TD>";
185 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$user->firstname $user->lastname";
186 if ($submission) {
187 echo "&nbsp;&nbsp;<FONT SIZE=1>".get_string("lastmodified").": ";
188 echo userdate($submission->timemodified);
189 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
190 echo "</FONT>";
191 }
192 echo "</TR>";
193
194 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
195 if ($submission) {
196 assignment_print_user_files($assignment, $user);
197 } else {
198 print_string("notsubmittedyet", "assignment");
199 }
200 echo "</TD></TR>";
201
202 if ($submission) {
203 echo "\n<TR>";
204 echo "<TD WIDTH=35 VALIGN=TOP>";
205 if (!$submission->teacher) {
206 $submission->teacher = $USER->id;
207 }
208 print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
209 echo "<TD BGCOLOR=\"$THEME->cellheading\">Teacher Feedback:";
210 choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("grade")."...");
211 if ($submission->timemarked) {
212 echo "&nbsp;&nbsp;<FONT SIZE=1>".userdate($submission->timemarked)."</FONT>";
213 }
214 echo "<BR><TEXTAREA NAME=\"c$submission->id\" ROWS=6 COLS=60 WRAP=virtual>";
215 p($submission->comment);
216 echo "</TEXTAREA><BR>";
217 echo "</TD></TR>";
218 }
219 echo "</TABLE><BR CLEAR=ALL>\n";
220}
221
222function assignment_print_feedback($course, $submission) {
223 global $CFG, $THEME, $RATING;
224
225 if (! $teacher = get_record("user", "id", $submission->teacher)) {
226 error("Weird assignment error");
227 }
228
229 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10>";
230
231 echo "\n<TR>";
232 echo "\n<TD ROWSPAN=3 BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
233 print_user_picture($teacher->id, $course->id, $teacher->picture);
234 echo "</TD>";
235 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$THEME->cellheading\">$teacher->firstname $teacher->lastname";
236 echo "&nbsp;&nbsp;<FONT SIZE=2><I>".userdate($submission->timemarked)."</I>";
237 echo "</TR>";
238
239 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
240
241 echo "<P ALIGN=RIGHT><FONT SIZE=-1><I>";
242 if ($submission->grade) {
243 echo get_string("grade").": $submission->grade";
244 } else {
245 echo get_string("nograde");
246 }
247 echo "</I></FONT></P>";
248
249 echo text_to_html($submission->comment);
250 echo "</TD></TR></TABLE>";
251}
252
253
254function assignment_print_user_files($assignment, $user) {
255// Arguments are objects
256
257 global $CFG;
258
259 $filearea = assignment_file_area_name($assignment, $user);
260
261 if ($basedir = assignment_file_area($assignment, $user)) {
262 if ($files = get_directory_list($basedir)) {
263 foreach ($files as $file) {
264 $icon = mimeinfo("icon", $file);
265 echo "<IMG SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"File\">";
266 echo "&nbsp;<A TARGET=\"uploadedfile\" HREF=\"$CFG->wwwroot/file.php/$filearea/$file\">$file</A>";
267 echo "<BR>";
268 }
269 }
270 }
271}
272
273function assignment_delete_user_files($assignment, $user, $exception) {
274// Deletes all the user files in the assignment area for a user
275// EXCEPT for any file named $exception
276
277 if ($basedir = assignment_file_area($assignment, $user)) {
278 if ($files = get_directory_list($basedir)) {
279 foreach ($files as $file) {
280 if ($file != $exception) {
281 unlink("$basedir/$file");
282 notify("Existing file '$file' has been deleted!");
283 }
284 }
285 }
286 }
287}
288
289function assignment_print_upload_form($assignment) {
290// Arguments are objects
291
292 echo "<DIV ALIGN=CENTER>";
293 echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"POST\" ACTION=upload.php>";
294 echo " <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=\"$assignment->maxfilesize\">";
295 echo " <INPUT TYPE=hidden NAME=id VALUE=\"$assignment->id\">";
296 echo " <INPUT NAME=\"newfile\" TYPE=\"file\" size=\"50\">";
297 echo " <INPUT TYPE=submit NAME=save VALUE=\"".get_string("uploadthisfile")."\">";
298 echo "</FORM>";
299 echo "</DIV>";
300}
04eba58f 301
302?>