More styles added as the THEME variable is removed from modules
[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();
77db7e4c 171 echo get_string("lastmodified").": ";
172 echo userdate($submission->timemodified);
173 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
77db7e4c 174
175 assignment_print_user_files($assignment, $user);
176
0927b54b 177 echo "<br />";
77db7e4c 178
0927b54b 179 if (empty($submission->timemarked)) {
180 print_string("notgradedyet", "assignment");
181 } else {
31a5ee99 182 assignment_print_feedback($course, $submission, $assignment);
0927b54b 183 }
77db7e4c 184
185 print_simple_box_end();
186
187 } else {
188 print_string("notsubmittedyet", "assignment");
189 }
190}
191
192
d699cd1e 193function assignment_cron () {
194// Function to be run periodically according to the moodle cron
195// Finds all assignment notifications that have yet to be mailed out, and mails them
196
a16c2180 197 global $CFG, $USER;
d699cd1e 198
9cc9b7c1 199 /// Notices older than 1 day will not be mailed. This is to avoid the problem where
200 /// cron has not been running for a long time, and then suddenly people are flooded
201 /// with mail from the past few weeks or months
d699cd1e 202
9cc9b7c1 203 $timenow = time();
204 $endtime = $timenow - $CFG->maxeditingtime;
205 $starttime = $endtime - 24 * 3600; /// One day earlier
206
207 if ($submissions = assignment_get_unmailed_submissions($starttime, $endtime)) {
a56d79cd 208
209 foreach ($submissions as $key => $submission) {
210 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
211 echo "Could not update the mailed field for id $submission->id. Not mailed.\n";
212 unset($submissions[$key]);
213 }
214 }
215
d699cd1e 216 $timenow = time();
217
218 foreach ($submissions as $submission) {
219
220 echo "Processing assignment submission $submission->id\n";
221
ebc3bd2b 222 if (! $user = get_record("user", "id", "$submission->userid")) {
223 echo "Could not find user $post->userid\n";
d699cd1e 224 continue;
225 }
226
a5a4cd60 227 $USER->lang = $user->lang;
228
d699cd1e 229 if (! $course = get_record("course", "id", "$submission->course")) {
230 echo "Could not find course $submission->course\n";
231 continue;
232 }
233
234 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
6f2e07e8 235 echo fullname($user)." not an active participant in $course->shortname\n";
b9287b2f 236 continue;
d699cd1e 237 }
238
239 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
240 echo "Could not find teacher $submission->teacher\n";
241 continue;
242 }
243
244 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
245 echo "Could not find course module for assignment id $submission->assignment\n";
246 continue;
247 }
248
c69cb506 249 if (! $mod->visible) { /// Hold mail notification for hidden assignments until later
250 continue;
251 }
252
d699cd1e 253 $strassignments = get_string("modulenameplural", "assignment");
254 $strassignment = get_string("modulename", "assignment");
255
ae078a98 256 unset($assignmentinfo);
6f2e07e8 257 $assignmentinfo->teacher = fullname($teacher);
ae078a98 258 $assignmentinfo->assignment = "$submission->name";
259 $assignmentinfo->url = "$CFG->wwwroot/mod/assignment/view.php?id=$mod->id";
260
d699cd1e 261 $postsubject = "$course->shortname: $strassignments: $submission->name";
262 $posttext = "$course->shortname -> $strassignments -> $submission->name\n";
263 $posttext .= "---------------------------------------------------------------------\n";
ae078a98 264 $posttext .= get_string("assignmentmail", "assignment", $assignmentinfo);
d699cd1e 265 $posttext .= "---------------------------------------------------------------------\n";
ae078a98 266
d699cd1e 267 if ($user->mailformat == 1) { // HTML
ae078a98 268 $posthtml = "<p><font face=\"sans-serif\">".
269 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
270 "<a href=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</a> ->".
271 "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">$submission->name</a></font></p>";
a9ef4a63 272 $posthtml .= "<hr /><font face=\"sans-serif\">";
ae078a98 273 $posthtml .= "<p>".get_string("assignmentmailhtml", "assignment", $assignmentinfo)."</p>";
a9ef4a63 274 $posthtml .= "</font><hr />";
d699cd1e 275 } else {
ae078a98 276 $posthtml = "";
d699cd1e 277 }
278
279 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
280 echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
281 }
d699cd1e 282 }
283 }
284
285 return true;
286}
287
1b5910c4 288function assignment_print_recent_activity($course, $isteacher, $timestart) {
3d891989 289 global $CFG;
3446205d 290
291 $content = false;
292 $assignments = NULL;
293
1b5910c4 294 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
295 "course = '$course->id' AND ".
296 "module = 'assignment' AND ".
297 "action = 'upload' ", "time ASC")) {
298 return false;
299 }
300
301 foreach ($logs as $log) {
302 //Create a temp valid module structure (course,id)
303 $tempmod->course = $log->course;
304 $tempmod->id = $log->info;
305 //Obtain the visible property from the instance
306 $modvisible = instance_is_visible($log->module,$tempmod);
307
308 //Only if the mod is visible
309 if ($modvisible) {
310 $assignments[$log->info] = assignment_log_info($log);
311 $assignments[$log->info]->time = $log->time;
de785682 312 $assignments[$log->info]->url = str_replace('&', '&amp;', $log->url);
3446205d 313 }
314 }
315
316 if ($assignments) {
dcde9f02 317 $strftimerecent = get_string("strftimerecent");
3446205d 318 $content = true;
319 print_headline(get_string("newsubmissions", "assignment").":");
320 foreach ($assignments as $assignment) {
dcde9f02 321 $date = userdate($assignment->time, $strftimerecent);
d3c80562 322 echo "$date - ".fullname($assignment)."<br />";
1b5910c4 323 echo "\"<a href=\"$CFG->wwwroot/mod/assignment/$assignment->url\">";
3446205d 324 echo "$assignment->name";
d3c80562 325 echo '</a>"';
3446205d 326 }
327 }
328
329 return $content;
330}
d699cd1e 331
d0ac6bc2 332function assignment_grades($assignmentid) {
858deff0 333/// Must return an array of grades, indexed by user, and a max grade.
d0ac6bc2 334
91719320 335
336 if (!$assignment = get_record("assignment", "id", $assignmentid)) {
337 return NULL;
338 }
339
340 $grades = get_records_menu("assignment_submissions", "assignment",
341 $assignment->id, "", "userid,grade");
342
343 if ($assignment->grade >= 0) {
344 $return->grades = $grades;
734bec5d 345 $return->maxgrade = $assignment->grade;
91719320 346
347 } else {
348 $scaleid = - ($assignment->grade);
349 if ($scale = get_record("scale", "id", $scaleid)) {
350 $scalegrades = make_menu_from_list($scale->scale);
f7c225fb 351 if ($grades) {
352 foreach ($grades as $key => $grade) {
353 $grades[$key] = $scalegrades[$grade];
354 }
91719320 355 }
356 }
357 $return->grades = $grades;
358 $return->maxgrade = "";
359 }
360
858deff0 361 return $return;
d0ac6bc2 362}
363
05855091 364function assignment_get_participants($assignmentid) {
365//Returns the users with data in one assignment
366//(users with records in assignment_submissions, students and teachers)
367
368 global $CFG;
369
370 //Get students
371 $students = get_records_sql("SELECT DISTINCT u.*
372 FROM {$CFG->prefix}user u,
373 {$CFG->prefix}assignment_submissions a
374 WHERE a.assignment = '$assignmentid' and
375 u.id = a.userid");
376 //Get teachers
377 $teachers = get_records_sql("SELECT DISTINCT u.*
378 FROM {$CFG->prefix}user u,
379 {$CFG->prefix}assignment_submissions a
380 WHERE a.assignment = '$assignmentid' and
381 u.id = a.teacher");
382
383 //Add teachers to students
384 if ($teachers) {
385 foreach ($teachers as $teacher) {
386 $students[$teacher->id] = $teacher;
387 }
388 }
389 //Return students array (it contains an array of unique users)
390 return ($students);
391}
392
0f1a97c2 393function assignment_scale_used ($assignmentid,$scaleid) {
394//This function returns if a scale is being used by one assignment
395
396 $return = false;
397
398 $rec = get_record("assignment","id","$assignmentid","grade","-$scaleid");
399
2127fedd 400 if (!empty($rec) && !empty($scaleid)) {
0f1a97c2 401 $return = true;
402 }
403
404 return $return;
405}
406
9fa49e22 407/// SQL STATEMENTS //////////////////////////////////////////////////////////////////
408
409function assignment_log_info($log) {
410 global $CFG;
411 return get_record_sql("SELECT a.name, u.firstname, u.lastname
412 FROM {$CFG->prefix}assignment a,
413 {$CFG->prefix}user u
414 WHERE a.id = '$log->info'
ebc3bd2b 415 AND u.id = '$log->userid'");
9fa49e22 416}
417
9fa82712 418function assignment_count_real_submissions($assignment, $groupid=0) {
8ff79e8c 419/// Return all real assignment submissions by ENROLLED students (not empty ones)
420 global $CFG;
421
9fa82712 422 if ($groupid) { /// How many in a particular group?
08cfe980 423 return count_records_sql("SELECT COUNT(DISTINCT g.userid, g.groupid)
9fa82712 424 FROM {$CFG->prefix}assignment_submissions a,
425 {$CFG->prefix}groups_members g
426 WHERE a.assignment = $assignment->id
427 AND a.timemodified > 0
428 AND g.groupid = '$groupid'
429 AND a.userid = g.userid ");
f950af3c 430 } else {
431 $select = "s.course = '$assignment->course' AND";
222ac91b 432 if ($assignment->course == SITEID) {
f950af3c 433 $select = '';
434 }
9fa82712 435 return count_records_sql("SELECT COUNT(*)
436 FROM {$CFG->prefix}assignment_submissions a,
437 {$CFG->prefix}user_students s
438 WHERE a.assignment = '$assignment->id'
439 AND a.timemodified > 0
f950af3c 440 AND $select a.userid = s.userid ");
9fa82712 441 }
8ff79e8c 442}
443
df48be74 444function assignment_get_all_submissions($assignment, $sort="", $dir="DESC") {
8ff79e8c 445/// Return all assignment submissions by ENROLLED students (even empty)
9fa49e22 446 global $CFG;
000cc405 447
448 if ($sort == "lastname" or $sort == "firstname") {
449 $sort = "u.$sort $dir";
450 } else if (empty($sort)) {
451 $sort = "a.timemodified DESC";
452 } else {
453 $sort = "a.$sort $dir";
454 }
f950af3c 455
456 $select = "s.course = '$assignment->course' AND";
222ac91b 457 if ($assignment->course == SITEID) {
f950af3c 458 $select = '';
459 }
9fa49e22 460 return get_records_sql("SELECT a.*
461 FROM {$CFG->prefix}assignment_submissions a,
000cc405 462 {$CFG->prefix}user_students s,
463 {$CFG->prefix}user u
ebc3bd2b 464 WHERE a.userid = s.userid
b2d0ca3b 465 AND u.id = a.userid
f950af3c 466 AND $select a.assignment = '$assignment->id'
000cc405 467 ORDER BY $sort");
9fa49e22 468}
469
470function assignment_get_users_done($assignment) {
471/// Return list of users who have done an assignment
472 global $CFG;
f950af3c 473
474 $select = "s.course = '$assignment->course' AND";
222ac91b 475 if ($assignment->course == SITEID) {
f950af3c 476 $select = '';
477 }
9fa49e22 478 return get_records_sql("SELECT u.*
479 FROM {$CFG->prefix}user u,
480 {$CFG->prefix}user_students s,
481 {$CFG->prefix}assignment_submissions a
f950af3c 482 WHERE $select s.userid = u.id
ebc3bd2b 483 AND u.id = a.userid
9fa49e22 484 AND a.assignment = '$assignment->id'
485 ORDER BY a.timemodified DESC");
486}
487
9cc9b7c1 488function assignment_get_unmailed_submissions($starttime, $endtime) {
a2631c99 489/// Return list of marked submissions that have not been mailed out for currently enrolled students
9fa49e22 490 global $CFG;
d4eec243 491 return get_records_sql("SELECT s.*, a.course, a.name
9fa49e22 492 FROM {$CFG->prefix}assignment_submissions s,
a2631c99 493 {$CFG->prefix}assignment a,
494 {$CFG->prefix}user_students us
9fa49e22 495 WHERE s.mailed = 0
9cc9b7c1 496 AND s.timemarked <= $endtime
497 AND s.timemarked >= $starttime
a2631c99 498 AND s.assignment = a.id
499 AND s.userid = us.userid
500 AND a.course = us.course");
9fa49e22 501}
502
503
d699cd1e 504//////////////////////////////////////////////////////////////////////////////////////
505
506function assignment_file_area_name($assignment, $user) {
507// Creates a directory file name, suitable for make_upload_directory()
ca4f8eb8 508 global $CFG;
509
510 return "$assignment->course/$CFG->moddata/assignment/$assignment->id/$user->id";
d699cd1e 511}
512
513function assignment_file_area($assignment, $user) {
514 return make_upload_directory( assignment_file_area_name($assignment, $user) );
515}
516
517function assignment_get_submission($assignment, $user) {
21a15d9f 518 $submission = get_record("assignment_submissions", "assignment", $assignment->id, "userid", $user->id);
519 if (!empty($submission->timemodified)) {
520 return $submission;
8e340cb0 521 }
522 return NULL;
d699cd1e 523}
524
525function assignment_print_difference($time) {
526 if ($time < 0) {
527 $timetext = get_string("late", "assignment", format_time($time));
9a58f7cb 528 return " (<font COLOR=RED>$timetext</font>)";
d699cd1e 529 } else {
530 $timetext = get_string("early", "assignment", format_time($time));
531 return " ($timetext)";
532 }
533}
534
535function assignment_print_submission($assignment, $user, $submission, $teachers, $grades) {
ce78926d 536 global $THEME, $USER;
d699cd1e 537
4ac21574 538 echo "\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"10\" align=\"center\">";
d699cd1e 539
9a58f7cb 540 echo "\n<tr>";
b7b42874 541 if ($assignment->type == OFFLINE) {
4ac21574 542 echo "\n<td bgcolor=\"$THEME->body\" width=\"35\" valign=\"top\">";
b7b42874 543 } else {
d3c80562 544 echo "\n<td rowspan=\"2\" bgcolor=\"$THEME->body\" width=\"35\" valign=\"top\">";
b7b42874 545 }
d699cd1e 546 print_user_picture($user->id, $assignment->course, $user->picture);
9a58f7cb 547 echo "</td>";
4ac21574 548 echo "<td nowrap=\"nowrap\" bgcolor=\"$THEME->cellheading\">".fullname($user, true);
c4df6e17 549 if ($assignment->type != OFFLINE and $submission->timemodified) {
d3c80562 550 echo "&nbsp;&nbsp;".get_string("lastmodified").": ";
d699cd1e 551 echo userdate($submission->timemodified);
552 echo assignment_print_difference($assignment->timedue - $submission->timemodified);
d699cd1e 553 }
4ac21574 554 echo "</td>\n";
9a58f7cb 555 echo "</tr>";
d699cd1e 556
b7b42874 557 if ($assignment->type != OFFLINE) {
4ac21574 558 echo "\n<tr><td bgcolor=\"$THEME->cellcontent\">";
ff446576 559 if ($submission->timemodified) {
b7b42874 560 assignment_print_user_files($assignment, $user);
561 } else {
562 print_string("notsubmittedyet", "assignment");
d699cd1e 563 }
9a58f7cb 564 echo "</td></tr>";
d699cd1e 565 }
b7b42874 566
9a58f7cb 567 echo "\n<tr>";
4ac21574 568 echo "<td width=\"35\" valign=\"top\">";
b7b42874 569 if (!$submission->teacher) {
570 $submission->teacher = $USER->id;
571 }
572 print_user_picture($submission->teacher, $assignment->course, $teachers[$submission->teacher]->picture);
4ac21574 573 echo "</td>\n";
b7b42874 574 if ($submission->timemodified > $submission->timemarked) {
9a58f7cb 575 echo "<td bgcolor=\"$THEME->cellheading2\">";
b7b42874 576 } else {
9a58f7cb 577 echo "<td bgcolor=\"$THEME->cellheading\">";
b7b42874 578 }
0acd6863 579 if (!$submission->grade and !$submission->timemarked) {
580 $submission->grade = -1; /// Hack to stop zero being selected on the menu below (so it shows 'no grade')
581 }
f830b324 582 echo get_string("feedback", "assignment").":";
94d0cea5 583 choose_from_menu($grades, "g$submission->id", $submission->grade, get_string("nograde"));
b7b42874 584 if ($submission->timemarked) {
d3c80562 585 echo "&nbsp;&nbsp;".userdate($submission->timemarked);
b7b42874 586 }
4ac21574 587 echo "<br /><textarea name=\"c$submission->id\" rows=\"6\" cols=\"60\">";
b7b42874 588 p($submission->comment);
1c5d9bbb 589 echo "</textarea><br />";
9a58f7cb 590 echo "</td></tr>";
b7b42874 591
1c5d9bbb 592 echo "</table><br clear=\"all\" />\n";
d699cd1e 593}
594
31a5ee99 595function assignment_print_feedback($course, $submission, $assignment) {
d699cd1e 596 global $CFG, $THEME, $RATING;
597
598 if (! $teacher = get_record("user", "id", $submission->teacher)) {
599 error("Weird assignment error");
600 }
601
4ac21574 602 echo "\n<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" align=\"center\"><tr><td bgcolor=\"#888888\">";
603 echo "\n<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" valign=\"top\">";
d699cd1e 604
9a58f7cb 605 echo "\n<tr>";
4ac21574 606 echo "\n<td rowspan=\"3\" bgcolor=\"$THEME->body\" width=\"35\" valign=\"top\">";
d699cd1e 607 print_user_picture($teacher->id, $course->id, $teacher->picture);
9a58f7cb 608 echo "</td>";
4ac21574 609 echo "<td nowrap=\"nowrap\" width=\"100%\" bgcolor=\"$THEME->cellheading\">".fullname($teacher);
610 echo "&nbsp;&nbsp;<font size=\"2\"><i>".userdate($submission->timemarked)."</i>";
9a58f7cb 611 echo "</tr>";
d699cd1e 612
4ac21574 613 echo "\n<tr><td width=\"100%\" bgcolor=\"$THEME->cellcontent\">";
d699cd1e 614
4ac21574 615 echo "<p align=\"right\"><font size=\"-1\"><i>";
31a5ee99 616 if ($assignment->grade) {
617 if ($submission->grade or $submission->timemarked) {
618 echo get_string("grade").": $submission->grade";
619 } else {
620 echo get_string("nograde");
621 }
d699cd1e 622 }
9a58f7cb 623 echo "</i></font></p>";
d699cd1e 624
625 echo text_to_html($submission->comment);
9a58f7cb 626 echo "</td></tr></table>";
627 echo "</td></tr></table>";
d699cd1e 628}
629
630
631function assignment_print_user_files($assignment, $user) {
632// Arguments are objects
633
634 global $CFG;
635
636 $filearea = assignment_file_area_name($assignment, $user);
637
638 if ($basedir = assignment_file_area($assignment, $user)) {
639 if ($files = get_directory_list($basedir)) {
640 foreach ($files as $file) {
641 $icon = mimeinfo("icon", $file);
3f8247c2 642 if ($CFG->slasharguments) {
643 $ffurl = "file.php/$filearea/$file";
644 } else {
645 $ffurl = "file.php?file=/$filearea/$file";
646 }
647
d3c80562 648 echo "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" border=\"0\" alt=\"file\" />";
096b5432 649 echo "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$ffurl\">$file</a>";
650 echo "<br />";
d699cd1e 651 }
652 }
653 }
654}
655
18b8fbfa 656// this function should be defunct now that we're using uploadlib.php
d699cd1e 657function assignment_delete_user_files($assignment, $user, $exception) {
658// Deletes all the user files in the assignment area for a user
659// EXCEPT for any file named $exception
660
661 if ($basedir = assignment_file_area($assignment, $user)) {
662 if ($files = get_directory_list($basedir)) {
663 foreach ($files as $file) {
664 if ($file != $exception) {
665 unlink("$basedir/$file");
fed7c5ba 666 notify(get_string("existingfiledeleted", "assignment", $file));
d699cd1e 667 }
668 }
669 }
670 }
671}
672
673function assignment_print_upload_form($assignment) {
674// Arguments are objects
675
18b8fbfa 676 global $CFG;
677
d3c80562 678 echo '<center>';
679 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"upload.php?id=$assignment->id\">";
680 echo '<input type="hidden" name="id" value="'.$assignment->id.'" />';
18b8fbfa 681 require_once($CFG->dirroot.'/lib/uploadlib.php');
682 upload_print_form_fragment(1,array('newfile'),false,null,0,$assignment->maxbytes,false);
d3c80562 683 echo '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
684 echo '</form>';
685 echo '</center>';
d699cd1e 686}
04eba58f 687
ffeca120 688function assignment_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $assignment="0", $user="", $groupid="") {
f466c9ed 689// Returns all assignments since a given time. If assignment is specified then
26b90e70 690// this restricts the results
ffeca120 691
26b90e70 692 global $CFG;
693
694 if ($assignment) {
f466c9ed 695 $assignmentselect = " AND cm.id = '$assignment'";
26b90e70 696 } else {
697 $assignmentselect = "";
698 }
699 if ($user) {
700 $userselect = " AND u.id = '$user'";
ffeca120 701 } else {
26b90e70 702 $userselect = "";
703 }
704
f466c9ed 705 $assignments = get_records_sql("SELECT asub.*, u.firstname, u.lastname, u.picture, u.id as userid,
ffeca120 706 a.grade as maxgrade, name, cm.instance, cm.section, a.type
f466c9ed 707 FROM {$CFG->prefix}assignment_submissions asub,
708 {$CFG->prefix}user u,
709 {$CFG->prefix}assignment a,
710 {$CFG->prefix}course_modules cm
711 WHERE asub.timemodified > '$sincetime'
712 AND asub.userid = u.id $userselect
713 AND a.id = asub.assignment $assignmentselect
714 AND cm.course = '$courseid'
715 AND cm.instance = a.id
716 ORDER BY asub.timemodified ASC");
717
ffeca120 718 if (empty($assignments))
719 return;
f466c9ed 720
ffeca120 721 foreach ($assignments as $assignment) {
722 if (empty($groupid) || ismember($groupid, $assignment->userid)) {
723
724 $tmpactivity->type = "assignment";
725 $tmpactivity->defaultindex = $index;
726 $tmpactivity->instance = $assignment->instance;
727 $tmpactivity->name = $assignment->name;
728 $tmpactivity->section = $assignment->section;
26b90e70 729
ffeca120 730 $tmpactivity->content->grade = $assignment->grade;
731 $tmpactivity->content->maxgrade = $assignment->maxgrade;
732 $tmpactivity->content->type = $assignment->type;
26b90e70 733
ffeca120 734 $tmpactivity->user->userid = $assignment->userid;
735 $tmpactivity->user->fullname = fullname($assignment);
736 $tmpactivity->user->picture = $assignment->picture;
26b90e70 737
ffeca120 738 $tmpactivity->timestamp = $assignment->timemodified;
f466c9ed 739
ffeca120 740 $activities[] = $tmpactivity;
f466c9ed 741
ffeca120 742 $index++;
743 }
744 }
f466c9ed 745
ffeca120 746 return;
f466c9ed 747}
748
ffeca120 749function assignment_print_recent_mod_activity($activity, $course, $detail=false) {
26b90e70 750 global $CFG, $THEME;
751
f466c9ed 752 echo '<table border="0" cellpadding="3" cellspacing="0">';
26b90e70 753
f466c9ed 754 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
755 print_user_picture($activity->user->userid, $course, $activity->user->picture);
756 echo "</td><td width=\"100%\"><font size=2>";
26b90e70 757
26b90e70 758
f466c9ed 759 if ($detail) {
760 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
761 "height=16 width=16 alt=\"$activity->type\"> ";
762 echo "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=" . $activity->instance . "\">"
763 . $activity->name . "</a> - ";
26b90e70 764
f466c9ed 765 }
26b90e70 766
32503db1 767 if (isteacher($course)) {
f466c9ed 768 $grades = "(" . $activity->content->grade . " / " . $activity->content->maxgrade . ") ";
26b90e70 769
f466c9ed 770 $assignment->id = $activity->instance;
771 $assignment->course = $course;
772 $user->id = $activity->user->userid;
26b90e70 773
ffeca120 774 echo $grades;
775 if ($activity->content->type == UPLOADSINGLE) {
776 $file = assignment_get_user_file($assignment, $user);
777 echo "<img src=\"$CFG->pixpath/f/$file->icon\" height=16 width=16 border=0 alt=\"file\">";
9a58f7cb 778 echo "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$file->url\">$file->name</a>";
ffeca120 779 }
a9ef4a63 780 echo "<br />";
f466c9ed 781 }
782 echo "<a href=\"$CFG->wwwroot/user/view.php?id="
839f2456 783 . $activity->user->userid . "&amp;course=$course\">"
f466c9ed 784 . $activity->user->fullname . "</a> ";
26b90e70 785
f466c9ed 786 echo " - " . userdate($activity->timestamp);
26b90e70 787
f466c9ed 788 echo "</font></td></tr>";
789 echo "</table>";
26b90e70 790
f466c9ed 791 return;
792}
26b90e70 793
f466c9ed 794function assignment_get_user_file($assignment, $user) {
795 global $CFG;
796
797 $tmpfile = "";
798
799 $filearea = assignment_file_area_name($assignment, $user);
800
801 if ($basedir = assignment_file_area($assignment, $user)) {
802 if ($files = get_directory_list($basedir)) {
803 foreach ($files as $file) {
804 $icon = mimeinfo("icon", $file);
805 if ($CFG->slasharguments) {
806 $ffurl = "file.php/$filearea/$file";
807 } else {
808 $ffurl = "file.php?file=/$filearea/$file";
809 }
810 $tmpfile->url = $ffurl;
811 $tmpfile->name = $file;
812 $tmpfile->icon = $icon;
813 }
814 }
26b90e70 815 }
f466c9ed 816 return $tmpfile;
26b90e70 817}
f466c9ed 818
59c005b7 819if (!function_exists('get_group_teachers')) { // Will be in datalib.php later
820 function get_group_teachers($courseid, $groupid) {
821 /// Returns a list of all the teachers who can access a group
822 if ($teachers = get_course_teachers($courseid)) {
823 foreach ($teachers as $key => $teacher) {
824 if ($teacher->editall) { // These can access anything
825 continue;
826 }
827 if (($teacher->authority > 0) and ismember($groupid, $teacher->id)) { // Specific group teachers
828 continue;
829 }
830 unset($teacher[$key]);
831 }
832 }
833 return $teachers;
834 }
835}
836
837function assignment_email_teachers($course, $cm, $assignment, $submission) {
838/// Alerts teachers by email of new or changed assignments that need grading
839
840 global $CFG;
841
842 if (empty($assignment->emailteachers)) { // No need to do anything
843 return;
844 }
845
846 $user = get_record('user', 'id', $submission->userid);
847
848 if (groupmode($course, $cm) == SEPARATEGROUPS) { // Separate groups are being used
849 if (!$group = user_group($course->id, $user->id)) { // Try to find a group
850 $group->id = 0; // Not in a group, never mind
851 }
852 $teachers = get_group_teachers($course->id, $group->id); // Works even if not in group
853 } else {
854 $teachers = get_course_teachers($course->id);
855 }
856
c30b1bc0 857 if ($teachers) {
59c005b7 858
859 $strassignments = get_string('modulenameplural', 'assignment');
860 $strassignment = get_string('modulename', 'assignment');
861 $strsubmitted = get_string('submitted', 'assignment');
862
863 foreach ($teachers as $teacher) {
864 unset($info);
865 $info->username = fullname($user);
c30b1bc0 866 $info->assignment = $assignment->name;
867 $info->url = "$CFG->wwwroot/mod/assignment/submissions.php?id=$assignment->id";
59c005b7 868
869 $postsubject = "$strsubmitted: $info->username -> $assignment->name";
870 $posttext = "$course->shortname -> $strassignments -> $assignment->name\n";
871 $posttext .= "---------------------------------------------------------------------\n";
872 $posttext .= get_string("emailteachermail", "assignment", $info);
c30b1bc0 873 $posttext .= "\n---------------------------------------------------------------------\n";
59c005b7 874
875 if ($user->mailformat == 1) { // HTML
876 $posthtml = "<p><font face=\"sans-serif\">".
877 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
878 "<a href=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</a> ->".
879 "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=$cm->id\">$assignment->name</a></font></p>";
880 $posthtml .= "<hr /><font face=\"sans-serif\">";
881 $posthtml .= "<p>".get_string("emailteachermailhtml", "assignment", $info)."</p>";
882 $posthtml .= "</font><hr />";
883 } else {
884 $posthtml = "";
885 }
886
c30b1bc0 887 @email_to_user($teacher, $user, $postsubject, $posttext, $posthtml); // If it fails, oh well, too bad.
59c005b7 888 }
889 }
890}
891
892
04eba58f 893?>