SOme code tidyups, and fixes for missing langauge strings as per bug 2340
[moodle.git] / mod / assignment / lib.php
CommitLineData
9a58f7cb 1<?php // $Id$
04eba58f 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
e6a4906b 27 if ($returnid = insert_record("assignment", $assignment)) {
28
29 $event = NULL;
30 $event->name = $assignment->name;
31 $event->description = $assignment->description;
32 $event->courseid = $assignment->course;
33 $event->groupid = 0;
34 $event->userid = 0;
35 $event->modulename = 'assignment';
36 $event->instance = $returnid;
37 $event->eventtype = 'due';
38 $event->timestart = $assignment->timedue;
39 $event->timeduration = 0;
40
41 add_event($event);
42 }
43
44 return $returnid;
04eba58f 45}
46
47
48function assignment_update_instance($assignment) {
49// Given an object containing all the necessary data,
50// (defined by the form in mod.html) this function
51// will update an existing instance with new data.
52
53 $assignment->timemodified = time();
d699cd1e 54 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth, $assignment->dueday,
55 $assignment->duehour, $assignment->dueminute);
04eba58f 56 $assignment->id = $assignment->instance;
57
e6a4906b 58
59 if ($returnid = update_record("assignment", $assignment)) {
60
61 $event = NULL;
62
63 if ($event->id = get_field('event', 'id', 'modulename', 'assignment', 'instance', $assignment->id)) {
64
65 $event->name = $assignment->name;
66 $event->description = $assignment->description;
67 $event->timestart = $assignment->timedue;
68
69 update_event($event);
70 }
71 }
72
73 return $returnid;
04eba58f 74}
75
76
77function assignment_delete_instance($id) {
78// Given an ID of an instance of this module,
79// this function will permanently delete the instance
80// and any data that depends on it.
81
82 if (! $assignment = get_record("assignment", "id", "$id")) {
83 return false;
84 }
85
86 $result = true;
87
88 if (! delete_records("assignment_submissions", "assignment", "$assignment->id")) {
89 $result = false;
90 }
91
92 if (! delete_records("assignment", "id", "$assignment->id")) {
93 $result = false;
94 }
95
36eb856f 96 if (! delete_records('event', 'modulename', 'assignment', 'instance', $assignment->id)) {
97 $result = false;
98 }
99
04eba58f 100 return $result;
101}
102
9c48354d 103function assignment_refresh_events($courseid = 0) {
e6a4906b 104// This standard function will check all instances of this module
105// and make sure there are up-to-date events created for each of them.
9c48354d 106// If courseid = 0, then every assignment event in the site is checked, else
107// only assignment events belonging to the course specified are checked.
108// This function is used, in its new format, by restore_refresh_events()
e6a4906b 109
9c48354d 110 if ($courseid == 0) {
111 if (! $assignments = get_records("assignment")) {
112 return true;
113 }
114 } else {
115 if (! $assignments = get_records("assignment", "course", $courseid)) {
116 return true;
117 }
e6a4906b 118 }
dcd338ff 119 $moduleid = get_field('modules', 'id', 'name', 'assignment');
e6a4906b 120
121 foreach ($assignments as $assignment) {
122 $event = NULL;
cf55faf8 123 $event->name = addslashes($assignment->name);
124 $event->description = addslashes($assignment->description);
e6a4906b 125 $event->timestart = $assignment->timedue;
126
127 if ($event->id = get_field('event', 'id', 'modulename', 'assignment', 'instance', $assignment->id)) {
128 update_event($event);
129
130 } else {
131 $event->courseid = $assignment->course;
132 $event->groupid = 0;
133 $event->userid = 0;
134 $event->modulename = 'assignment';
135 $event->instance = $assignment->id;
136 $event->eventtype = 'due';
137 $event->timeduration = 0;
dcd338ff 138 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $assignment->id);
e6a4906b 139 add_event($event);
140 }
dcd338ff 141
e6a4906b 142 }
143 return true;
144}
145
146
77db7e4c 147function assignment_user_outline($course, $user, $mod, $assignment) {
148 if ($submission = assignment_get_submission($assignment, $user)) {
98092498 149
150 if ($submission->grade) {
151 $result->info = get_string("grade").": $submission->grade";
77db7e4c 152 }
77db7e4c 153 $result->time = $submission->timemodified;
154 return $result;
155 }
156 return NULL;
157}
158
159function assignment_user_complete($course, $user, $mod, $assignment) {
160 if ($submission = assignment_get_submission($assignment, $user)) {
161 if ($basedir = assignment_file_area($assignment, $user)) {
162 if ($files = get_directory_list($basedir)) {
163 $countfiles = count($files)." ".get_string("uploadedfiles", "assignment");
164 foreach ($files as $file) {
165 $countfiles .= "; $file";
166 }
167 }
168 }
169
170 print_simple_box_start();
0927b54b 171 echo "<p><font size=1>";
77db7e4c 172 echo get_string("lastmodified").": ";
173 echo userdate($submission->timemodified);
174 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
0927b54b 175 echo "</font></p>";
77db7e4c 176
177 assignment_print_user_files($assignment, $user);
178
0927b54b 179 echo "<br />";
77db7e4c 180
0927b54b 181 if (empty($submission->timemarked)) {
182 print_string("notgradedyet", "assignment");
183 } else {
31a5ee99 184 assignment_print_feedback($course, $submission, $assignment);
0927b54b 185 }
77db7e4c 186
187 print_simple_box_end();
188
189 } else {
190 print_string("notsubmittedyet", "assignment");
191 }
192}
193
194
d699cd1e 195function assignment_cron () {
196// Function to be run periodically according to the moodle cron
197// Finds all assignment notifications that have yet to be mailed out, and mails them
198
a16c2180 199 global $CFG, $USER;
d699cd1e 200
9cc9b7c1 201 /// Notices older than 1 day will not be mailed. This is to avoid the problem where
202 /// cron has not been running for a long time, and then suddenly people are flooded
203 /// with mail from the past few weeks or months
d699cd1e 204
9cc9b7c1 205 $timenow = time();
206 $endtime = $timenow - $CFG->maxeditingtime;
207 $starttime = $endtime - 24 * 3600; /// One day earlier
208
209 if ($submissions = assignment_get_unmailed_submissions($starttime, $endtime)) {
a56d79cd 210
211 foreach ($submissions as $key => $submission) {
212 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
213 echo "Could not update the mailed field for id $submission->id. Not mailed.\n";
214 unset($submissions[$key]);
215 }
216 }
217
d699cd1e 218 $timenow = time();
219
220 foreach ($submissions as $submission) {
221
222 echo "Processing assignment submission $submission->id\n";
223
ebc3bd2b 224 if (! $user = get_record("user", "id", "$submission->userid")) {
225 echo "Could not find user $post->userid\n";
d699cd1e 226 continue;
227 }
228
a5a4cd60 229 $USER->lang = $user->lang;
230
d699cd1e 231 if (! $course = get_record("course", "id", "$submission->course")) {
232 echo "Could not find course $submission->course\n";
233 continue;
234 }
235
236 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
6f2e07e8 237 echo fullname($user)." not an active participant in $course->shortname\n";
b9287b2f 238 continue;
d699cd1e 239 }
240
241 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
242 echo "Could not find teacher $submission->teacher\n";
243 continue;
244 }
245
246 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
247 echo "Could not find course module for assignment id $submission->assignment\n";
248 continue;
249 }
250
c69cb506 251 if (! $mod->visible) { /// Hold mail notification for hidden assignments until later
252 continue;
253 }
254
d699cd1e 255 $strassignments = get_string("modulenameplural", "assignment");
256 $strassignment = get_string("modulename", "assignment");
257
ae078a98 258 unset($assignmentinfo);
6f2e07e8 259 $assignmentinfo->teacher = fullname($teacher);
ae078a98 260 $assignmentinfo->assignment = "$submission->name";
261 $assignmentinfo->url = "$CFG->wwwroot/mod/assignment/view.php?id=$mod->id";
262
d699cd1e 263 $postsubject = "$course->shortname: $strassignments: $submission->name";
264 $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
265 $posttext .= "---------------------------------------------------------------------\n";
ae078a98 266 $posttext .= get_string("assignmentmail", "assignment", $assignmentinfo);
d699cd1e 267 $posttext .= "---------------------------------------------------------------------\n";
ae078a98 268
d699cd1e 269 if ($user->mailformat == 1) { // HTML
ae078a98 270 $posthtml = "<p><font face=\"sans-serif\">".
271 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
272 "<a href=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</a> ->".
273 "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">$submission->name</a></font></p>";
a9ef4a63 274 $posthtml .= "<hr /><font face=\"sans-serif\">";
ae078a98 275 $posthtml .= "<p>".get_string("assignmentmailhtml", "assignment", $assignmentinfo)."</p>";
a9ef4a63 276 $posthtml .= "</font><hr />";
d699cd1e 277 } else {
ae078a98 278 $posthtml = "";
d699cd1e 279 }
280
281 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
282 echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
283 }
d699cd1e 284 }
285 }
286
287 return true;
288}
289
1b5910c4 290function assignment_print_recent_activity($course, $isteacher, $timestart) {
3d891989 291 global $CFG;
3446205d 292
293 $content = false;
294 $assignments = NULL;
295
1b5910c4 296 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
297 "course = '$course->id' AND ".
298 "module = 'assignment' AND ".
299 "action = 'upload' ", "time ASC")) {
300 return false;
301 }
302
303 foreach ($logs as $log) {
304 //Create a temp valid module structure (course,id)
305 $tempmod->course = $log->course;
306 $tempmod->id = $log->info;
307 //Obtain the visible property from the instance
308 $modvisible = instance_is_visible($log->module,$tempmod);
309
310 //Only if the mod is visible
311 if ($modvisible) {
312 $assignments[$log->info] = assignment_log_info($log);
313 $assignments[$log->info]->time = $log->time;
de785682 314 $assignments[$log->info]->url = str_replace('&', '&amp;', $log->url);
3446205d 315 }
316 }
317
318 if ($assignments) {
dcde9f02 319 $strftimerecent = get_string("strftimerecent");
3446205d 320 $content = true;
321 print_headline(get_string("newsubmissions", "assignment").":");
322 foreach ($assignments as $assignment) {
dcde9f02 323 $date = userdate($assignment->time, $strftimerecent);
6f2e07e8 324 echo "<p><font size=1>$date - ".fullname($assignment)."<br />";
1b5910c4 325 echo "\"<a href=\"$CFG->wwwroot/mod/assignment/$assignment->url\">";
3446205d 326 echo "$assignment->name";
1b5910c4 327 echo "</a>\"</font></p>";
3446205d 328 }
329 }
330
331 return $content;
332}
d699cd1e 333
d0ac6bc2 334function assignment_grades($assignmentid) {
858deff0 335/// Must return an array of grades, indexed by user, and a max grade.
d0ac6bc2 336
91719320 337
338 if (!$assignment = get_record("assignment", "id", $assignmentid)) {
339 return NULL;
340 }
341
342 $grades = get_records_menu("assignment_submissions", "assignment",
343 $assignment->id, "", "userid,grade");
344
345 if ($assignment->grade >= 0) {
346 $return->grades = $grades;
734bec5d 347 $return->maxgrade = $assignment->grade;
91719320 348
349 } else {
350 $scaleid = - ($assignment->grade);
351 if ($scale = get_record("scale", "id", $scaleid)) {
352 $scalegrades = make_menu_from_list($scale->scale);
f7c225fb 353 if ($grades) {
354 foreach ($grades as $key => $grade) {
355 $grades[$key] = $scalegrades[$grade];
356 }
91719320 357 }
358 }
359 $return->grades = $grades;
360 $return->maxgrade = "";
361 }
362
858deff0 363 return $return;
d0ac6bc2 364}
365
05855091 366function assignment_get_participants($assignmentid) {
367//Returns the users with data in one assignment
368//(users with records in assignment_submissions, students and teachers)
369
370 global $CFG;
371
372 //Get students
373 $students = get_records_sql("SELECT DISTINCT u.*
374 FROM {$CFG->prefix}user u,
375 {$CFG->prefix}assignment_submissions a
376 WHERE a.assignment = '$assignmentid' and
377 u.id = a.userid");
378 //Get teachers
379 $teachers = get_records_sql("SELECT DISTINCT u.*
380 FROM {$CFG->prefix}user u,
381 {$CFG->prefix}assignment_submissions a
382 WHERE a.assignment = '$assignmentid' and
383 u.id = a.teacher");
384
385 //Add teachers to students
386 if ($teachers) {
387 foreach ($teachers as $teacher) {
388 $students[$teacher->id] = $teacher;
389 }
390 }
391 //Return students array (it contains an array of unique users)
392 return ($students);
393}
394
0f1a97c2 395function assignment_scale_used ($assignmentid,$scaleid) {
396//This function returns if a scale is being used by one assignment
397
398 $return = false;
399
400 $rec = get_record("assignment","id","$assignmentid","grade","-$scaleid");
401
2127fedd 402 if (!empty($rec) && !empty($scaleid)) {
0f1a97c2 403 $return = true;
404 }
405
406 return $return;
407}
408
9fa49e22 409/// SQL STATEMENTS //////////////////////////////////////////////////////////////////
410
411function assignment_log_info($log) {
412 global $CFG;
413 return get_record_sql("SELECT a.name, u.firstname, u.lastname
414 FROM {$CFG->prefix}assignment a,
415 {$CFG->prefix}user u
416 WHERE a.id = '$log->info'
ebc3bd2b 417 AND u.id = '$log->userid'");
9fa49e22 418}
419
9fa82712 420function assignment_count_real_submissions($assignment, $groupid=0) {
8ff79e8c 421/// Return all real assignment submissions by ENROLLED students (not empty ones)
422 global $CFG;
423
9fa82712 424 if ($groupid) { /// How many in a particular group?
08cfe980 425 return count_records_sql("SELECT COUNT(DISTINCT g.userid, g.groupid)
9fa82712 426 FROM {$CFG->prefix}assignment_submissions a,
427 {$CFG->prefix}groups_members g
428 WHERE a.assignment = $assignment->id
429 AND a.timemodified > 0
430 AND g.groupid = '$groupid'
431 AND a.userid = g.userid ");
f950af3c 432 } else {
433 $select = "s.course = '$assignment->course' AND";
222ac91b 434 if ($assignment->course == SITEID) {
f950af3c 435 $select = '';
436 }
9fa82712 437 return count_records_sql("SELECT COUNT(*)
438 FROM {$CFG->prefix}assignment_submissions a,
439 {$CFG->prefix}user_students s
440 WHERE a.assignment = '$assignment->id'
441 AND a.timemodified > 0
f950af3c 442 AND $select a.userid = s.userid ");
9fa82712 443 }
8ff79e8c 444}
445
df48be74 446function assignment_get_all_submissions($assignment, $sort="", $dir="DESC") {
8ff79e8c 447/// Return all assignment submissions by ENROLLED students (even empty)
9fa49e22 448 global $CFG;
000cc405 449
450 if ($sort == "lastname" or $sort == "firstname") {
451 $sort = "u.$sort $dir";
452 } else if (empty($sort)) {
453 $sort = "a.timemodified DESC";
454 } else {
455 $sort = "a.$sort $dir";
456 }
f950af3c 457
458 $select = "s.course = '$assignment->course' AND";
222ac91b 459 if ($assignment->course == SITEID) {
f950af3c 460 $select = '';
461 }
9fa49e22 462 return get_records_sql("SELECT a.*
463 FROM {$CFG->prefix}assignment_submissions a,
000cc405 464 {$CFG->prefix}user_students s,
465 {$CFG->prefix}user u
ebc3bd2b 466 WHERE a.userid = s.userid
b2d0ca3b 467 AND u.id = a.userid
f950af3c 468 AND $select a.assignment = '$assignment->id'
000cc405 469 ORDER BY $sort");
9fa49e22 470}
471
472function assignment_get_users_done($assignment) {
473/// Return list of users who have done an assignment
474 global $CFG;
f950af3c 475
476 $select = "s.course = '$assignment->course' AND";
222ac91b 477 if ($assignment->course == SITEID) {
f950af3c 478 $select = '';
479 }
9fa49e22 480 return get_records_sql("SELECT u.*
481 FROM {$CFG->prefix}user u,
482 {$CFG->prefix}user_students s,
483 {$CFG->prefix}assignment_submissions a
f950af3c 484 WHERE $select s.userid = u.id
ebc3bd2b 485 AND u.id = a.userid
9fa49e22 486 AND a.assignment = '$assignment->id'
487 ORDER BY a.timemodified DESC");
488}
489
9cc9b7c1 490function assignment_get_unmailed_submissions($starttime, $endtime) {
a2631c99 491/// Return list of marked submissions that have not been mailed out for currently enrolled students
9fa49e22 492 global $CFG;
d4eec243 493 return get_records_sql("SELECT s.*, a.course, a.name
9fa49e22 494 FROM {$CFG->prefix}assignment_submissions s,
a2631c99 495 {$CFG->prefix}assignment a,
496 {$CFG->prefix}user_students us
9fa49e22 497 WHERE s.mailed = 0
9cc9b7c1 498 AND s.timemarked <= $endtime
499 AND s.timemarked >= $starttime
a2631c99 500 AND s.assignment = a.id
501 AND s.userid = us.userid
502 AND a.course = us.course");
9fa49e22 503}
504
505
d699cd1e 506//////////////////////////////////////////////////////////////////////////////////////
507
508function assignment_file_area_name($assignment, $user) {
509// Creates a directory file name, suitable for make_upload_directory()
ca4f8eb8 510 global $CFG;
511
512 return "$assignment->course/$CFG->moddata/assignment/$assignment->id/$user->id";
d699cd1e 513}
514
515function assignment_file_area($assignment, $user) {
516 return make_upload_directory( assignment_file_area_name($assignment, $user) );
517}
518
519function assignment_get_submission($assignment, $user) {
21a15d9f 520 $submission = get_record("assignment_submissions", "assignment", $assignment->id, "userid", $user->id);
521 if (!empty($submission->timemodified)) {
522 return $submission;
8e340cb0 523 }
524 return NULL;
d699cd1e 525}
526
527function assignment_print_difference($time) {
528 if ($time < 0) {
529 $timetext = get_string("late", "assignment", format_time($time));
9a58f7cb 530 return " (<font COLOR=RED>$timetext</font>)";
d699cd1e 531 } else {
532 $timetext = get_string("early", "assignment", format_time($time));
533 return " ($timetext)";
534 }
535}
536
537function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
ce78926d 538 global $THEME, $USER;
d699cd1e 539
4ac21574 540 echo "\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"10\" align=\"center\">";
d699cd1e 541
9a58f7cb 542 echo "\n<tr>";
b7b42874 543 if ($assignment->type == OFFLINE) {
4ac21574 544 echo "\n<td bgcolor=\"$THEME->body\" width=\"35\" valign=\"top\">";
b7b42874 545 } else {
4ac21574 546 echo "\n<td rowspan=\"2\" bgcolor=\"$THEME->body\" width=\"35\" valign\"top\">";
b7b42874 547 }
d699cd1e 548 print_user_picture($user->id, $assignment->course, $user->picture);
9a58f7cb 549 echo "</td>";
4ac21574 550 echo "<td nowrap=\"nowrap\" bgcolor=\"$THEME->cellheading\">".fullname($user, true);
c4df6e17 551 if ($assignment->type != OFFLINE and $submission->timemodified) {
9a58f7cb 552 echo "&nbsp;&nbsp;<font SIZE=1>".get_string("lastmodified").": ";
d699cd1e 553 echo userdate($submission->timemodified);
554 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
9a58f7cb 555 echo "</font>";
d699cd1e 556 }
4ac21574 557 echo "</td>\n";
9a58f7cb 558 echo "</tr>";
d699cd1e 559
b7b42874 560 if ($assignment->type != OFFLINE) {
4ac21574 561 echo "\n<tr><td bgcolor=\"$THEME->cellcontent\">";
ff446576 562 if ($submission->timemodified) {
b7b42874 563 assignment_print_user_files($assignment, $user);
564 } else {
565 print_string("notsubmittedyet", "assignment");
d699cd1e 566 }
9a58f7cb 567 echo "</td></tr>";
d699cd1e 568 }
b7b42874 569
9a58f7cb 570 echo "\n<tr>";
4ac21574 571 echo "<td width=\"35\" valign=\"top\">";
b7b42874 572 if (!$submission->teacher) {
573 $submission->teacher = $USER->id;
574 }
575 print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
4ac21574 576 echo "</td>\n";
b7b42874 577 if ($submission->timemodified > $submission->timemarked) {
9a58f7cb 578 echo "<td bgcolor=\"$THEME->cellheading2\">";
b7b42874 579 } else {
9a58f7cb 580 echo "<td bgcolor=\"$THEME->cellheading\">";
b7b42874 581 }
0acd6863 582 if (!$submission->grade and !$submission->timemarked) {
583 $submission->grade = -1; /// Hack to stop zero being selected on the menu below (so it shows 'no grade')
584 }
f830b324 585 echo get_string("feedback", "assignment").":";
94d0cea5 586 choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("nograde"));
b7b42874 587 if ($submission->timemarked) {
9a58f7cb 588 echo "&nbsp;&nbsp;<font size=1>".userdate($submission->timemarked)."</font>";
b7b42874 589 }
4ac21574 590 echo "<br /><textarea name=\"c$submission->id\" rows=\"6\" cols=\"60\">";
b7b42874 591 p($submission->comment);
1c5d9bbb 592 echo "</textarea><br />";
9a58f7cb 593 echo "</td></tr>";
b7b42874 594
1c5d9bbb 595 echo "</table><br clear=\"all\" />\n";
d699cd1e 596}
597
31a5ee99 598function assignment_print_feedback($course, $submission, $assignment) {
d699cd1e 599 global $CFG, $THEME, $RATING;
600
601 if (! $teacher = get_record("user", "id", $submission->teacher)) {
602 error("Weird assignment error");
603 }
604
4ac21574 605 echo "\n<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" align=\"center\"><tr><td bgcolor=\"#888888\">";
606 echo "\n<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" valign=\"top\">";
d699cd1e 607
9a58f7cb 608 echo "\n<tr>";
4ac21574 609 echo "\n<td rowspan=\"3\" bgcolor=\"$THEME->body\" width=\"35\" valign=\"top\">";
d699cd1e 610 print_user_picture($teacher->id, $course->id, $teacher->picture);
9a58f7cb 611 echo "</td>";
4ac21574 612 echo "<td nowrap=\"nowrap\" width=\"100%\" bgcolor=\"$THEME->cellheading\">".fullname($teacher);
613 echo "&nbsp;&nbsp;<font size=\"2\"><i>".userdate($submission->timemarked)."</i>";
9a58f7cb 614 echo "</tr>";
d699cd1e 615
4ac21574 616 echo "\n<tr><td width=\"100%\" bgcolor=\"$THEME->cellcontent\">";
d699cd1e 617
4ac21574 618 echo "<p align=\"right\"><font size=\"-1\"><i>";
31a5ee99 619 if ($assignment->grade) {
620 if ($submission->grade or $submission->timemarked) {
621 echo get_string("grade").": $submission->grade";
622 } else {
623 echo get_string("nograde");
624 }
d699cd1e 625 }
9a58f7cb 626 echo "</i></font></p>";
d699cd1e 627
628 echo text_to_html($submission->comment);
9a58f7cb 629 echo "</td></tr></table>";
630 echo "</td></tr></table>";
d699cd1e 631}
632
633
634function assignment_print_user_files($assignment, $user) {
635// Arguments are objects
636
637 global $CFG;
638
639 $filearea = assignment_file_area_name($assignment, $user);
640
641 if ($basedir = assignment_file_area($assignment, $user)) {
642 if ($files = get_directory_list($basedir)) {
643 foreach ($files as $file) {
644 $icon = mimeinfo("icon", $file);
3f8247c2 645 if ($CFG->slasharguments) {
646 $ffurl = "file.php/$filearea/$file";
647 } else {
648 $ffurl = "file.php?file=/$filearea/$file";
649 }
650
096b5432 651 echo "<img src=\"$CFG->pixpath/f/$icon\" height=16 width=16 border=0 alt=\"file\">";
652 echo "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$ffurl\">$file</a>";
653 echo "<br />";
d699cd1e 654 }
655 }
656 }
657}
658
18b8fbfa 659// this function should be defunct now that we're using uploadlib.php
d699cd1e 660function assignment_delete_user_files($assignment, $user, $exception) {
661// Deletes all the user files in the assignment area for a user
662// EXCEPT for any file named $exception
663
664 if ($basedir = assignment_file_area($assignment, $user)) {
665 if ($files = get_directory_list($basedir)) {
666 foreach ($files as $file) {
667 if ($file != $exception) {
668 unlink("$basedir/$file");
fed7c5ba 669 notify(get_string("existingfiledeleted", "assignment", $file));
d699cd1e 670 }
671 }
672 }
673 }
674}
675
676function assignment_print_upload_form($assignment) {
677// Arguments are objects
678
18b8fbfa 679 global $CFG;
680
9a58f7cb 681 echo "<div align=CENTER>";
682 echo "<form enctype=\"multipart/form-data\" method=\"POST\" action=\"upload.php?id=$assignment->id\">";
9a58f7cb 683 echo " <input type=hidden name=id value=\"$assignment->id\" />";
18b8fbfa 684 require_once($CFG->dirroot.'/lib/uploadlib.php');
685 upload_print_form_fragment(1,array('newfile'),false,null,0,$assignment->maxbytes,false);
9a58f7cb 686 echo " <input type=submit name=save value=\"".get_string("uploadthisfile")."\" />";
687 echo "</form>";
688 echo "</div>";
d699cd1e 689}
04eba58f 690
ffeca120 691function assignment_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $assignment="0", $user="", $groupid="") {
f466c9ed 692// Returns all assignments since a given time. If assignment is specified then
26b90e70 693// this restricts the results
ffeca120 694
26b90e70 695 global $CFG;
696
697 if ($assignment) {
f466c9ed 698 $assignmentselect = " AND cm.id = '$assignment'";
26b90e70 699 } else {
700 $assignmentselect = "";
701 }
702 if ($user) {
703 $userselect = " AND u.id = '$user'";
ffeca120 704 } else {
26b90e70 705 $userselect = "";
706 }
707
f466c9ed 708 $assignments = get_records_sql("SELECT asub.*, u.firstname, u.lastname, u.picture, u.id as userid,
ffeca120 709 a.grade as maxgrade, name, cm.instance, cm.section, a.type
f466c9ed 710 FROM {$CFG->prefix}assignment_submissions asub,
711 {$CFG->prefix}user u,
712 {$CFG->prefix}assignment a,
713 {$CFG->prefix}course_modules cm
714 WHERE asub.timemodified > '$sincetime'
715 AND asub.userid = u.id $userselect
716 AND a.id = asub.assignment $assignmentselect
717 AND cm.course = '$courseid'
718 AND cm.instance = a.id
719 ORDER BY asub.timemodified ASC");
720
ffeca120 721 if (empty($assignments))
722 return;
f466c9ed 723
ffeca120 724 foreach ($assignments as $assignment) {
725 if (empty($groupid) || ismember($groupid, $assignment->userid)) {
726
727 $tmpactivity->type = "assignment";
728 $tmpactivity->defaultindex = $index;
729 $tmpactivity->instance = $assignment->instance;
730 $tmpactivity->name = $assignment->name;
731 $tmpactivity->section = $assignment->section;
26b90e70 732
ffeca120 733 $tmpactivity->content->grade = $assignment->grade;
734 $tmpactivity->content->maxgrade = $assignment->maxgrade;
735 $tmpactivity->content->type = $assignment->type;
26b90e70 736
ffeca120 737 $tmpactivity->user->userid = $assignment->userid;
738 $tmpactivity->user->fullname = fullname($assignment);
739 $tmpactivity->user->picture = $assignment->picture;
26b90e70 740
ffeca120 741 $tmpactivity->timestamp = $assignment->timemodified;
f466c9ed 742
ffeca120 743 $activities[] = $tmpactivity;
f466c9ed 744
ffeca120 745 $index++;
746 }
747 }
f466c9ed 748
ffeca120 749 return;
f466c9ed 750}
751
ffeca120 752function assignment_print_recent_mod_activity($activity, $course, $detail=false) {
26b90e70 753 global $CFG, $THEME;
754
f466c9ed 755 echo '<table border="0" cellpadding="3" cellspacing="0">';
26b90e70 756
f466c9ed 757 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
758 print_user_picture($activity->user->userid, $course, $activity->user->picture);
759 echo "</td><td width=\"100%\"><font size=2>";
26b90e70 760
26b90e70 761
f466c9ed 762 if ($detail) {
763 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
764 "height=16 width=16 alt=\"$activity->type\"> ";
765 echo "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=" . $activity->instance . "\">"
766 . $activity->name . "</a> - ";
26b90e70 767
f466c9ed 768 }
26b90e70 769
f466c9ed 770 if (isteacher($USER)) {
771 $grades = "(" . $activity->content->grade . " / " . $activity->content->maxgrade . ") ";
26b90e70 772
f466c9ed 773 $assignment->id = $activity->instance;
774 $assignment->course = $course;
775 $user->id = $activity->user->userid;
26b90e70 776
ffeca120 777 echo $grades;
778 if ($activity->content->type == UPLOADSINGLE) {
779 $file = assignment_get_user_file($assignment, $user);
780 echo "<img src=\"$CFG->pixpath/f/$file->icon\" height=16 width=16 border=0 alt=\"file\">";
9a58f7cb 781 echo "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$file->url\">$file->name</a>";
ffeca120 782 }
a9ef4a63 783 echo "<br />";
f466c9ed 784 }
785 echo "<a href=\"$CFG->wwwroot/user/view.php?id="
839f2456 786 . $activity->user->userid . "&amp;course=$course\">"
f466c9ed 787 . $activity->user->fullname . "</a> ";
26b90e70 788
f466c9ed 789 echo " - " . userdate($activity->timestamp);
26b90e70 790
f466c9ed 791 echo "</font></td></tr>";
792 echo "</table>";
26b90e70 793
f466c9ed 794 return;
795}
26b90e70 796
f466c9ed 797function assignment_get_user_file($assignment, $user) {
798 global $CFG;
799
800 $tmpfile = "";
801
802 $filearea = assignment_file_area_name($assignment, $user);
803
804 if ($basedir = assignment_file_area($assignment, $user)) {
805 if ($files = get_directory_list($basedir)) {
806 foreach ($files as $file) {
807 $icon = mimeinfo("icon", $file);
808 if ($CFG->slasharguments) {
809 $ffurl = "file.php/$filearea/$file";
810 } else {
811 $ffurl = "file.php?file=/$filearea/$file";
812 }
813 $tmpfile->url = $ffurl;
814 $tmpfile->name = $file;
815 $tmpfile->icon = $icon;
816 }
817 }
26b90e70 818 }
f466c9ed 819 return $tmpfile;
26b90e70 820}
f466c9ed 821
04eba58f 822?>