*** empty log message ***
[moodle.git] / mod / assignment / lib.php
CommitLineData
b0f2597e 1<?PHP // $Id$
2/**
3 * assignment_base is the base class for assignment types
4 *
5 * This class provides all the functionality for an assignment
6 */
04eba58f 7
d699cd1e 8
4909e176 9if (!isset($CFG->assignment_maxbytes)) {
10 set_config("assignment_maxbytes", 1024000); // Default maximum size for all assignments
04eba58f 11}
12
13
b0f2597e 14/*
15 * Standard base class for all assignment submodules (assignment types).
16 *
17 *
18 */
19class assignment_base {
20
21 var $cm;
22 var $course;
23 var $assignment;
24
25 /**
26 * Constructor for the base assignment class
27 *
28 * Constructor for the base assignment class.
29 * If cmid is set create the cm, course, assignment objects.
30 *
31 * @param cmid integer, the current course module id - not set for new assignments
73097f07 32 * @param assignment object, usually null, but if we have it we pass it to save db access
b0f2597e 33 */
73097f07 34 function assignment_base($cmid=0, $assignment=NULL, $cm=NULL, $course=NULL) {
b0f2597e 35
36 global $CFG;
37
38 if ($cmid) {
73097f07 39 if ($cm) {
40 $this->cm = $cm;
41 } else if (! $this->cm = get_record('course_modules', 'id', $cmid)) {
42 error('Course Module ID was incorrect');
b0f2597e 43 }
04eba58f 44
73097f07 45 if ($course) {
46 $this->course = $course;
47 } else if (! $this->course = get_record('course', 'id', $this->cm->course)) {
48 error('Course is misconfigured');
b0f2597e 49 }
04eba58f 50
73097f07 51 if ($assignment) {
52 $this->assignment = $assignment;
53 } else if (! $this->assignment = get_record('assignment', 'id', $this->cm->instance)) {
54 error('assignment ID was incorrect');
b0f2597e 55 }
e6a4906b 56
b0f2597e 57 $this->strassignment = get_string('modulename', 'assignment');
58 $this->strassignments = get_string('modulenameplural', 'assignment');
59 $this->strsubmissions = get_string('submissions', 'assignment');
60 $this->strlastmodified = get_string('lastmodified');
e6a4906b 61
b0f2597e 62 if ($this->course->category) {
63 $this->navigation = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id={$this->course->id}\">{$this->course->shortname}</a> -> ".
64 "<a target=\"{$CFG->framename}\" href=\"index.php?id={$this->course->id}\">$this->strassignments</a> ->";
65 } else {
66 $this->navigation = "<a target=\"{$CFG->framename}\" href=\"index.php?id={$this->course->id}\">$this->strassignments</a> ->";
67 }
e6a4906b 68
3898bc33 69 $this->pagetitle = strip_tags($this->course->shortname.': '.$this->strassignment.': '.format_string($this->assignment->name,true));
e6a4906b 70
b0f2597e 71 if (!$this->cm->visible and !isteacher($this->course->id)) {
72 $pagetitle = strip_tags($this->course->shortname.': '.$this->strassignment);
73 print_header($pagetitle, $this->course->fullname, "$this->navigation $this->strassignment",
74 "", "", true, '', navmenu($this->course, $this->cm));
75 notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
76 }
77
78 $this->currentgroup = get_current_group($this->course->id);
73097f07 79 }
e6a4906b 80
73097f07 81 /// Set up things for a HTML editor if it's needed
82 if ($this->usehtmleditor = can_use_html_editor()) {
83 $this->defaultformat = FORMAT_HTML;
84 } else {
85 $this->defaultformat = FORMAT_MOODLE;
e6a4906b 86 }
73097f07 87
e6a4906b 88 }
89
b0f2597e 90 /*
91 * Display the assignment to students (sub-modules will most likely override this)
92 */
04eba58f 93
b0f2597e 94 function view() {
04eba58f 95
b0f2597e 96 add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}",
97 $this->assignment->id, $this->cm->id);
04eba58f 98
73097f07 99 $this->view_header();
04eba58f 100
f77cfb73 101 $this->view_intro();
04eba58f 102
f77cfb73 103 $this->view_dates();
04eba58f 104
b0f2597e 105 $this->view_feedback();
106
f77cfb73 107 $this->view_footer();
36eb856f 108 }
109
73097f07 110 /*
111 * Display the top of the view.php page, this doesn't change much for submodules
112 */
113 function view_header($subpage='') {
114
115 global $CFG;
116
117 if ($subpage) {
118 $extranav = '<a target="'.$CFG->framename.'" href="view.php?id='.$this->cm->id.'">'.
3898bc33 119 format_string($this->assignment->name,true).'</a> -> '.$subpage;
73097f07 120 } else {
3898bc33 121 $extranav = ' '.format_string($this->assignment->name,true);
73097f07 122 }
123
124 print_header($this->pagetitle, $this->course->fullname, $this->navigation.$extranav, '', '',
125 true, update_module_button($this->cm->id, $this->course->id, $this->strassignment),
126 navmenu($this->course, $this->cm));
127
128 echo '<div class="reportlink">'.$this->submittedlink().'</div>';
129 }
130
131
f77cfb73 132 /*
133 * Display the assignment intro
134 */
135 function view_intro() {
136 print_simple_box_start('center', '', '', '', 'generalbox', 'intro');
137 echo format_text($this->assignment->description, $this->assignment->format);
138 print_simple_box_end();
139 }
140
141 /*
142 * Display the assignment dates
143 */
144 function view_dates() {
145 if (!$this->assignment->timeavailable && !$this->assignment->timedue) {
146 return;
147 }
148
149 print_simple_box_start('center', '', '', '', 'generalbox', 'dates');
150 echo '<table>';
151 if ($this->assignment->timeavailable) {
152 echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>';
153 echo ' <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>';
154 }
155 if ($this->assignment->timedue) {
156 echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>';
157 echo ' <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>';
158 }
159 echo '</table>';
160 print_simple_box_end();
161 }
162
163
73097f07 164 /*
165 * Display the bottom of the view.php page, this doesn't change much for submodules
166 */
167 function view_footer() {
168 print_footer($this->course);
169 }
170
04eba58f 171
73097f07 172 function view_feedback($submission=NULL) {
b0f2597e 173 global $USER;
e6a4906b 174
73097f07 175 if (!$submission) { /// Get submission for this assignment
176 $submission = $this->get_submission($USER->id);
70b2c772 177 }
178
179 if (empty($submission->timemarked)) { /// Nothing to show, so print nothing
180 return;
9c48354d 181 }
e6a4906b 182
b0f2597e 183 /// We need the teacher info
184 if (! $teacher = get_record('user', 'id', $submission->teacher)) {
185 print_object($submission);
186 error('Could not find the teacher');
187 }
e6a4906b 188
b0f2597e 189 /// Print the feedback
6d4ecaec 190 print_heading(get_string('feedbackfromteacher', 'assignment', $this->course->teacher));
191
b0f2597e 192 echo '<table cellspacing="0" class="feedback">';
193
194 echo '<tr>';
195 echo '<td class="left picture">';
196 print_user_picture($teacher->id, $this->course->id, $teacher->picture);
197 echo '</td>';
6d4ecaec 198 echo '<td class="topic">';
70b2c772 199 echo '<div class="from">';
73097f07 200 echo '<div class="fullname">'.fullname($teacher).'</div>';
6d4ecaec 201 echo '<div class="time">'.userdate($submission->timemarked).'</div>';
70b2c772 202 echo '</div>';
203 $this->print_user_files($submission->userid);
204 echo '</td>';
b0f2597e 205 echo '</td>';
206 echo '</tr>';
207
208 echo '<tr>';
209 echo '<td class="left side">&nbsp;</td>';
6d4ecaec 210 echo '<td class="content">';
b0f2597e 211 if ($this->assignment->grade) {
6d4ecaec 212 echo '<div class="grade">';
70b2c772 213 echo get_string("grade").': '.$this->display_grade($submission->grade);
6d4ecaec 214 echo '</div>';
52436fe1 215 echo '<div class="clearer"></div>';
e6a4906b 216 }
dcd338ff 217
6d4ecaec 218 echo '<div class="comment">';
ff8f7015 219 echo format_text($submission->comment, $submission->format);
6d4ecaec 220 echo '</div>';
b0f2597e 221 echo '</tr>';
222
223 echo '</table>';
e6a4906b 224 }
e6a4906b 225
226
77db7e4c 227
ba16713f 228 /*
229 * Returns a link with info about the state of the assignment submissions
230 */
231 function submittedlink() {
232 global $USER;
233
234 $submitted = '';
235
236 if (isteacher($this->course->id)) {
237 if (!isteacheredit($this->course->id) and user_group($this->course->id, $USER->id)) {
238 $count = $this->count_real_submissions($this->currentgroup); // Only their group
239 } else {
240 $count = $this->count_real_submissions(); // Everyone
241 }
242 $submitted = '<a href="submissions.php?id='.$this->cm->id.'">'.
243 get_string('viewsubmissions', 'assignment', $count).'</a>';
244 } else {
245 if (isset($USER->id)) {
246 if ($submission = $this->get_submission($USER->id)) {
247 if ($submission->timemodified) {
248 if ($submission->timemodified <= $this->assignment->timedue) {
249 $submitted = '<span class="early">'.userdate($submission->timemodified).'</span>';
250 } else {
251 $submitted = '<span class="late">'.userdate($submission->timemodified).'</span>';
252 }
253 }
254 }
255 }
256 }
257
258 return $submitted;
259 }
260
261
b0f2597e 262 /*
263 * Print the start of the setup form for the current assignment type
264 */
265 function setup(&$form, $action='') {
266 global $CFG, $THEME;
267
268 if (empty($this->course)) {
269 if (! $this->course = get_record("course", "id", $form->course)) {
270 error("Course is misconfigured");
77db7e4c 271 }
272 }
b0f2597e 273 if (empty($action)) { // Default destination for this form
274 $action = $CFG->wwwroot.'/course/mod.php';
275 }
77db7e4c 276
b0f2597e 277 if (empty($form->name)) {
278 $form->name = "";
279 }
280 if (empty($form->assignmenttype)) {
281 $form->assignmenttype = "";
282 }
283 if (empty($form->description)) {
284 $form->description = "";
285 }
77db7e4c 286
b0f2597e 287 $strname = get_string('name');
288 $strassignments = get_string('modulenameplural', 'assignment');
3898bc33 289 $strheading = empty($form->name) ? get_string("type$form->assignmenttype",'assignment') : s(format_string(stripslashes($form->name),true));
77db7e4c 290
b0f2597e 291 print_header($this->course->shortname.': '.$strheading, "$strheading",
292 "<a href=\"$CFG->wwwroot/course/view.php?id={$this->course->id}\">{$this->course->shortname} </a> -> ".
293 "<a href=\"$CFG->wwwroot/mod/assignment/index.php?id={$this->course->id}\">$strassignments</a> -> $strheading");
77db7e4c 294
f77cfb73 295 print_simple_box_start('center', '70%');
296 print_heading(get_string('type'.$form->assignmenttype,'assignment'));
297 print_simple_box(get_string('help'.$form->assignmenttype, 'assignment'), 'center');
b0f2597e 298 include("$CFG->dirroot/mod/assignment/type/common.html");
f77cfb73 299
300 include("$CFG->dirroot/mod/assignment/type/".$form->assignmenttype."/mod.html");
301 $this->setup_end();
b0f2597e 302 }
303
304 /*
305 * Print the end of the setup form for the current assignment type
306 */
307 function setup_end() {
73097f07 308 global $CFG;
b0f2597e 309
310 include($CFG->dirroot.'/mod/assignment/type/common_end.html');
77db7e4c 311
312 print_simple_box_end();
313
73097f07 314 if ($this->usehtmleditor) {
b0f2597e 315 use_html_editor();
316 }
317
318 print_footer($this->course);
77db7e4c 319 }
77db7e4c 320
321
b0f2597e 322 function add_instance($assignment) {
323 // Given an object containing all the necessary data,
324 // (defined by the form in mod.html) this function
325 // will create a new instance and return the id number
326 // of the new instance.
327
328 $assignment->timemodified = time();
38147229 329 if (empty($assignment->dueenable)) {
330 $assignment->timedue = 0;
331 } else {
332 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth,
333 $assignment->dueday, $assignment->duehour,
334 $assignment->dueminute);
335 }
336 if (empty($assignment->availableenable)) {
337 $assignment->timeavailable = 0;
338 $assignment->preventlate = 0;
339 } else {
340 $assignment->timeavailable = make_timestamp($assignment->availableyear, $assignment->availablemonth,
341 $assignment->availableday, $assignment->availablehour,
342 $assignment->availableminute);
343 }
344
345 return insert_record('assignment', $assignment);
b0f2597e 346 }
d699cd1e 347
b0f2597e 348 function delete_instance($assignment) {
12acb98c 349 return delete_records('assignment', 'id', $assignment->id);
b0f2597e 350 }
d699cd1e 351
b0f2597e 352 function update_instance($assignment) {
353 // Given an object containing all the necessary data,
354 // (defined by the form in mod.html) this function
355 // will create a new instance and return the id number
356 // of the new instance.
357
358 $assignment->timemodified = time();
38147229 359 $assignment->timemodified = time();
360 if (empty($assignment->dueenable)) {
361 $assignment->timedue = 0;
362 } else {
363 $assignment->timedue = make_timestamp($assignment->dueyear, $assignment->duemonth,
364 $assignment->dueday, $assignment->duehour,
365 $assignment->dueminute);
366 }
367 if (empty($assignment->availableenable)) {
368 $assignment->timeavailable = 0;
369 $assignment->preventlate = 0;
370 } else {
371 $assignment->timeavailable = make_timestamp($assignment->availableyear, $assignment->availablemonth,
372 $assignment->availableday, $assignment->availablehour,
373 $assignment->availableminute);
374 }
375
b0f2597e 376 $assignment->id = $assignment->instance;
38147229 377 return update_record('assignment', $assignment);
b0f2597e 378 }
379
380
381
382 /*
383 * Top-level function for handling of submissions called by submissions.php
384 *
385 */
386 function submissions($mode) {
387 switch ($mode) {
388 case 'grade': // We are in a popup window grading
389 if ($submission = $this->process_feedback()) {
390 print_heading(get_string('changessaved'));
be86672d 391 $this->update_main_listing($submission);
b0f2597e 392 }
393 close_window();
394 break;
9cc9b7c1 395
b0f2597e 396 case 'single': // We are in a popup window displaying submission
397 $this->display_submission();
398 break;
a56d79cd 399
b0f2597e 400 case 'all': // Main window, display everything
401 $this->display_submissions();
402 break;
a56d79cd 403 }
b0f2597e 404 }
a56d79cd 405
be86672d 406 function update_main_listing($submission) {
407 global $SESSION;
408
409 /// Run some Javascript to try and update the parent page
410 echo '<script type="text/javascript">'."\n<!--\n";
411 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['grade'])) {
412 echo 'opener.document.getElementById("g'.$submission->userid.
413 '").innerHTML="'.$this->display_grade($submission->grade)."\";\n";
414 }
415 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['comment'])) {
416 echo 'opener.document.getElementById("com'.$submission->userid.
86365d20 417 '").innerHTML="'.shorten_text(strip_tags($submission->comment), 15)."\";\n";
be86672d 418 }
73097f07 419 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['timemodified']) &&
420 $submission->timemodified) {
be86672d 421 echo 'opener.document.getElementById("ts'.$submission->userid.
422 '").innerHTML="'.userdate($submission->timemodified)."\";\n";
423 }
73097f07 424 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['timemarked']) &&
425 $submission->timemarked) {
be86672d 426 echo 'opener.document.getElementById("tt'.$submission->userid.
427 '").innerHTML="'.userdate($submission->timemarked)."\";\n";
428 }
429 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['status'])) {
430 echo 'opener.document.getElementById("up'.$submission->userid.'").className="s1";';
6d4ecaec 431 echo 'opener.document.getElementById("button'.$submission->userid.'").value="'.get_string('update').' ...";';
be86672d 432 }
433 echo "\n-->\n</script>";
434 fflush();
435 }
d699cd1e 436
d59269cf 437 /*
438 * Display a grade in user-friendly form, whether it's a scale or not
439 *
440 */
441 function display_grade($grade) {
442
443 static $scalegrades; // Cached because we only have one per assignment
444
445 if ($this->assignment->grade >= 0) { // Normal number
3a3643df 446 return $grade.' / '.$this->assignment->grade;
d59269cf 447
448 } else { // Scale
449 if (empty($scalegrades)) {
450 if ($scale = get_record('scale', 'id', -($this->assignment->grade))) {
451 $scalegrades = make_menu_from_list($scale->scale);
452 } else {
453 return '-';
454 }
455 }
0f7d4e5e 456 if (isset($scalegrades[$grade])) {
457 return $scalegrades[$grade];
458 }
459 return '';
d59269cf 460 }
461 }
462
b0f2597e 463 /*
464 * Display a single submission, ready for grading on a popup window
465 *
466 */
467 function display_submission() {
b0f2597e 468
469 $userid = required_param('userid');
d699cd1e 470
b0f2597e 471 if (!$user = get_record('user', 'id', $userid)) {
472 error('No such user!');
473 }
d699cd1e 474
0f7d4e5e 475 if (!$submission = $this->get_submission($user->id, true)) { // Get one or make one
b0f2597e 476 error('Could not find submission!');
477 }
a5a4cd60 478
b0f2597e 479 if ($submission->timemodified > $submission->timemarked) {
480 $subtype = 'assignmentnew';
481 } else {
482 $subtype = 'assignmentold';
483 }
d699cd1e 484
3898bc33 485 print_header(get_string('feedback', 'assignment').':'.fullname($user, true).':'.format_string($this->assignment->name));
d699cd1e 486
d699cd1e 487
52436fe1 488 echo '<table cellspacing="0" class="feedback '.$subtype.'" >';
d699cd1e 489
b0f2597e 490 echo '<tr>';
491 echo '<td width="35" valign="top" class="picture user">';
492 print_user_picture($user->id, $this->course->id, $user->picture);
493 echo '</td>';
70b2c772 494 echo '<td class="topic">';
495 echo '<div class="from">';
73097f07 496 echo '<div class="fullname">'.fullname($user, true).'</div>';
70b2c772 497 if ($submission->timemodified) {
498 echo '<div class="time">'.userdate($submission->timemodified).
499 $this->display_lateness($submission->timemodified).'</div>';
500 }
501 echo '</div>';
502 $this->print_user_files($user->id);
73097f07 503 echo '</td>';
b0f2597e 504 echo '</tr>';
c69cb506 505
b0f2597e 506 echo '<tr>';
507 echo '<td width="35" valign="top" class="picture teacher">';
508 if ($submission->teacher) {
509 $teacher = get_record('user', 'id', $submission->teacher);
510 } else {
511 global $USER;
512 $teacher = $USER;
513 }
514 print_user_picture($teacher->id, $this->course->id, $teacher->picture);
515 echo '</td>';
516 echo '<td class="content">';
d699cd1e 517
73097f07 518 echo '<form action="submissions.php" method="post">';
519 echo '<input type="hidden" name="userid" value="'.$userid.'">';
520 echo '<input type="hidden" name="id" value="'.$this->cm->id.'">';
521 echo '<input type="hidden" name="mode" value="grade">';
b0f2597e 522 if (!$submission->grade and !$submission->timemarked) {
523 $submission->grade = -1; /// Hack to stop zero being selected on the menu below (so it shows 'no grade')
524 }
52436fe1 525 if ($submission->timemarked) {
526 echo '<div class="from">';
527 echo '<div class="fullname">'.fullname($teacher, true).'</div>';
528 echo '<div class="time">'.userdate($submission->timemarked).'</div>';
529 echo '</div>';
530 }
531 echo '<div class="grade">'.get_string('grade').':';
b0f2597e 532 choose_from_menu(make_grades_menu($this->assignment->grade), 'grade',
533 $submission->grade, get_string('nograde'));
52436fe1 534 echo '</div>';
535 echo '<div class="clearer"></div>';
b0f2597e 536
01e2fdfe 537 $this->preprocess_submission($submission);
538
b0f2597e 539 echo '<br />';
ff8f7015 540 print_textarea($this->usehtmleditor, 12, 58, 0, 0, 'comment', $submission->comment, $this->course->id);
541
ff8f7015 542 if ($this->usehtmleditor) {
ff8f7015 543 echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
544 } else {
f77cfb73 545 echo '<div align="right" class="format">';
ff8f7015 546 choose_from_menu(format_text_menu(), "format", $submission->format, "");
f77cfb73 547 helpbutton("textformat", get_string("helpformatting"));
548 echo '</div>';
ff8f7015 549 }
b0f2597e 550
73097f07 551 echo '<div class="buttons" align="center">';
b0f2597e 552 echo '<input type="submit" name="submit" value="'.get_string('savechanges').'" />';
553 echo '<input type="submit" name="cancel" value="'.get_string('cancel').'" />';
73097f07 554 echo '</div>';
555
b0f2597e 556 echo '</form>';
73097f07 557
558 echo '</td></tr>';
559 echo '</table>';
560
561
562 if ($this->usehtmleditor) {
563 use_html_editor();
564 }
565
b0f2597e 566 print_footer('none');
d699cd1e 567 }
568
01e2fdfe 569 /*
570 * Preprocess submission before grading
571 */
572 function preprocess_submission(&$submission) {
573 }
d699cd1e 574
b0f2597e 575 /*
576 * Display all the submissions ready for grading
577 */
578 function display_submissions() {
3446205d 579
b0f2597e 580 global $CFG, $db;
3446205d 581
b0f2597e 582 $teacherattempts = true; /// Temporary measure
1b5910c4 583
b0f2597e 584 $page = optional_param('page', 0);
585 $perpage = optional_param('perpage', 10);
d699cd1e 586
b0f2597e 587 $strsaveallfeedback = get_string('saveallfeedback', 'assignment');
d0ac6bc2 588
b0f2597e 589 /// Some shortcuts to make the code read better
590
591 $course = $this->course;
592 $assignment = $this->assignment;
593 $cm = $this->cm;
91719320 594
91719320 595
b0f2597e 596 add_to_log($course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
597
3898bc33 598 print_header_simple(format_string($this->assignment->name,true), "", '<a href="index.php?id='.$course->id.'">'.$this->strassignments.'</a> -> <a href="view.php?a='.$this->assignment->id.'">'.format_string($this->assignment->name,true).'</a> -> '. $this->strsubmissions, '', '', true, update_module_button($cm->id, $course->id, $this->strassignment), navmenu($course, $cm));
91719320 599
91719320 600
9437c854 601 $tablecolumns = array('picture', 'fullname', 'grade', 'comment', 'timemodified', 'timemarked', 'status');
602 $tableheaders = array('', get_string('fullname'), get_string('grade'), get_string('comment', 'assignment'), get_string('lastmodified').' ('.$course->student.')', get_string('lastmodified').' ('.$course->teacher.')', get_string('status'));
91719320 603
d0ac6bc2 604
b0f2597e 605 require_once($CFG->libdir.'/tablelib.php');
606 $table = new flexible_table('mod-assignment-submissions');
607
608 $table->define_columns($tablecolumns);
609 $table->define_headers($tableheaders);
610 $table->define_baseurl($CFG->wwwroot.'/mod/assignment/submissions.php?id='.$this->cm->id);
611
612 $table->sortable(true);
613 $table->collapsible(true);
614 $table->initialbars(true);
615
616 $table->column_suppress('picture');
617 $table->column_suppress('fullname');
618
619 $table->column_class('picture', 'picture');
9437c854 620 $table->column_class('fullname', 'fullname');
621 $table->column_class('grade', 'grade');
622 $table->column_class('comment', 'comment');
623 $table->column_class('timemodified', 'timemodified');
624 $table->column_class('timemarked', 'timemarked');
625 $table->column_class('status', 'status');
b0f2597e 626
627 $table->set_attribute('cellspacing', '0');
628 $table->set_attribute('id', 'attempts');
9437c854 629 $table->set_attribute('class', 'submissions');
b0f2597e 630 $table->set_attribute('width', '90%');
631 $table->set_attribute('align', 'center');
632
633 // Start working -- this is necessary as soon as the niceties are over
634 $table->setup();
635
05855091 636
05855091 637
b0f2597e 638 /// Check to see if groups are being used in this assignment
639 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
306dc7e5 640 $currentgroup = setup_and_print_groups($course, $groupmode, 'submissions.php?id='.$this->cm->id);
b0f2597e 641 } else {
642 $currentgroup = false;
05855091 643 }
05855091 644
0f1a97c2 645
b0f2597e 646 /// Get all teachers and students
647 if ($currentgroup) {
648 $users = get_group_users($currentgroup);
649 } else {
650 $users = get_course_users($course->id);
651 }
652
653 if (!$teacherattempts) {
654 $teachers = get_course_teachers($course->id);
655 if (!empty($teachers)) {
656 $keys = array_keys($teachers);
657 }
658 foreach ($keys as $key) {
659 unset($users[$key]);
660 }
661 }
662
663 if (empty($users)) {
c8dbfa5c 664 print_heading(get_string('noattempts','assignment'));
b0f2597e 665 return true;
666 }
0f1a97c2 667
0f1a97c2 668
b0f2597e 669 /// Construct the SQL
0f1a97c2 670
b0f2597e 671 if ($where = $table->get_sql_where()) {
b0f2597e 672 $where .= ' AND ';
673 }
0f1a97c2 674
b0f2597e 675 if ($sort = $table->get_sql_sort()) {
676 $sortparts = explode(',', $sort);
677 $newsort = array();
678 foreach ($sortparts as $sortpart) {
679 $sortpart = trim($sortpart);
680 $newsort[] = $sortpart;
681 }
682 $sort = ' ORDER BY '.implode(', ', $newsort);
683 }
9fa49e22 684
9fa49e22 685
c5d36203 686 $select = 'SELECT u.id, u.id, u.firstname, u.lastname, u.picture, s.id AS submissionid, s.grade, s.comment, s.timemodified, s.timemarked, ((s.timemarked > 0) && (s.timemarked >= s.timemodified)) AS status ';
b0f2597e 687 $sql = 'FROM '.$CFG->prefix.'user u '.
306dc7e5 688 'LEFT JOIN '.$CFG->prefix.'assignment_submissions s ON u.id = s.userid AND s.assignment = '.$this->assignment->id.' '.
689 'WHERE '.$where.'u.id IN ('.implode(',', array_keys($users)).') ';
8ff79e8c 690
c5d36203 691 $table->pagesize($perpage, count($users));
b0f2597e 692
693 if($table->get_page_start() !== '' && $table->get_page_size() !== '') {
694 $limit = ' '.sql_paging_limit($table->get_page_start(), $table->get_page_size());
695 }
696 else {
697 $limit = '';
698 }
9fa49e22 699
b0f2597e 700 $strupdate = get_string('update');
9437c854 701 $strgrade = get_string('grade');
b0f2597e 702 $grademenu = make_grades_menu($this->assignment->grade);
703
c5d36203 704 if (($ausers = get_records_sql($select.$sql.$sort.$limit)) !== false) {
d59269cf 705
d59269cf 706 foreach ($ausers as $auser) {
707 $picture = print_user_picture($auser->id, $course->id, $auser->picture, false, true);
708 if (!empty($auser->submissionid)) {
709 if ($auser->timemodified > 0) {
710 $studentmodified = '<div id="ts'.$auser->id.'">'.userdate($auser->timemodified).'</div>';
d59269cf 711 } else {
9437c854 712 $studentmodified = '<div id="ts'.$auser->id.'">&nbsp;</div>';
d59269cf 713 }
714 if ($auser->timemarked > 0) {
715 $teachermodified = '<div id="tt'.$auser->id.'">'.userdate($auser->timemarked).'</div>';
f77cfb73 716 $grade = '<div id="g'.$auser->id.'">'.$this->display_grade($auser->grade).'</div>';
b0f2597e 717 } else {
9437c854 718 $teachermodified = '<div id="tt'.$auser->id.'">&nbsp;</div>';
f77cfb73 719 $grade = '<div id="g'.$auser->id.'"></div>';
b0f2597e 720 }
9437c854 721
86365d20 722 $comment = '<div id="com'.$auser->id.'">'.shorten_text(strip_tags($auser->comment), 15).'</div>';
d59269cf 723
b0f2597e 724 } else {
9437c854 725 $studentmodified = '<div id="ts'.$auser->id.'">&nbsp;</div>';
726 $teachermodified = '<div id="tt'.$auser->id.'">&nbsp;</div>';
d59269cf 727 $status = '<div id="st'.$auser->id.'"></div>';
9437c854 728 $grade = '<div id="g'.$auser->id.'">&nbsp;</div>';
729 $comment = '<div id="com'.$auser->id.'">&nbsp;</div>';
b0f2597e 730 }
9fa49e22 731
0f7d4e5e 732 if ($auser->status === NULL) {
733 $auser->status = 0;
734 }
735
9437c854 736 $buttontext = ($auser->status == 1) ? $strupdate : $strgrade;
9fa49e22 737
9437c854 738 $button = button_to_popup_window ('/mod/assignment/submissions.php?id='.$this->cm->id.'&amp;userid='.$auser->id.'&amp;mode=single',
52436fe1 739 'grade'.$auser->id, $buttontext, 450, 700, $buttontext, 'none', true, 'button'.$auser->id);
0f7d4e5e 740
9437c854 741 $status = '<div id="up'.$auser->id.'" class="s'.$auser->status.'">'.$button.'</div>';
306dc7e5 742
9437c854 743 $row = array($picture, fullname($auser), $grade, $comment, $studentmodified, $teachermodified, $status);
d59269cf 744 $table->add_data($row);
745 }
b0f2597e 746 }
d699cd1e 747
b0f2597e 748 $table->print_html();
d699cd1e 749
b0f2597e 750 print_footer($this->course);
d699cd1e 751
8e340cb0 752 }
d699cd1e 753
d699cd1e 754
d699cd1e 755
b0f2597e 756 /*
757 * Display and process the submissions
758 */
759 function process_feedback() {
d699cd1e 760
b0f2597e 761 global $USER;
d699cd1e 762
b0f2597e 763 if (!$feedback = data_submitted()) { // No incoming data?
764 return false;
d699cd1e 765 }
b7b42874 766
b0f2597e 767 if (!empty($feedback->cancel)) { // User hit cancel button
768 return false;
769 }
d699cd1e 770
0f7d4e5e 771 $newsubmission = $this->get_submission($feedback->userid, true); // Get or make one
d699cd1e 772
b0f2597e 773 $newsubmission->grade = $feedback->grade;
774 $newsubmission->comment = $feedback->comment;
ff8f7015 775 $newsubmission->format = $feedback->format;
b0f2597e 776 $newsubmission->teacher = $USER->id;
777 $newsubmission->mailed = 0; // Make sure mail goes out (again, even)
778 $newsubmission->timemarked = time();
d699cd1e 779
b0f2597e 780 if (empty($submission->timemodified)) { // eg for offline assignments
781 $newsubmission->timemodified = time();
782 }
d699cd1e 783
b0f2597e 784 if (! update_record('assignment_submissions', $newsubmission)) {
785 return false;
786 }
d699cd1e 787
b0f2597e 788 add_to_log($this->course->id, 'assignment', 'update grades',
789 'submissions.php?id='.$this->assignment->id.'&user='.$feedback->userid, $feedback->userid, $this->cm->id);
790
791 return $newsubmission;
d699cd1e 792
d699cd1e 793 }
d699cd1e 794
d699cd1e 795
f77cfb73 796 function get_submission($userid=0, $createnew=false) {
797 global $USER;
798
799 if (empty($userid)) {
800 $userid = $USER->id;
801 }
802
b0f2597e 803 $submission = get_record('assignment_submissions', 'assignment', $this->assignment->id, 'userid', $userid);
d699cd1e 804
b0f2597e 805 if ($submission || !$createnew) {
806 return $submission;
807 }
d699cd1e 808
b0f2597e 809 $newsubmission = new Object;
810 $newsubmission->assignment = $this->assignment->id;
811 $newsubmission->userid = $userid;
812 $newsubmission->timecreated = time();
813 if (!insert_record("assignment_submissions", $newsubmission)) {
814 error("Could not insert a new empty submission");
815 }
d699cd1e 816
b0f2597e 817 return get_record('assignment_submissions', 'assignment', $this->assignment->id, 'userid', $userid);
818 }
d699cd1e 819
3f8247c2 820
b0f2597e 821 function get_submissions($sort='', $dir='DESC') {
822 /// Return all assignment submissions by ENROLLED students (even empty)
823 global $CFG;
824
825 if ($sort == "lastname" or $sort == "firstname") {
826 $sort = "u.$sort $dir";
827 } else if (empty($sort)) {
828 $sort = "a.timemodified DESC";
829 } else {
830 $sort = "a.$sort $dir";
d699cd1e 831 }
d699cd1e 832
b0f2597e 833 $select = "s.course = '$this->assignment->course' AND";
834 $site = get_site();
835 if ($this->assignment->course == $site->id) {
836 $select = '';
837 }
838 return get_records_sql("SELECT a.*
839 FROM {$CFG->prefix}assignment_submissions a,
840 {$CFG->prefix}user_students s,
841 {$CFG->prefix}user u
842 WHERE a.userid = s.userid
843 AND u.id = a.userid
844 AND $select a.assignment = '$this->assignment->id'
845 ORDER BY $sort");
846 }
847
848
849 function count_real_submissions($groupid=0) {
850 /// Return all real assignment submissions by ENROLLED students (not empty ones)
851 global $CFG;
852
853 if ($groupid) { /// How many in a particular group?
854 return count_records_sql("SELECT COUNT(DISTINCT g.userid, g.groupid)
855 FROM {$CFG->prefix}assignment_submissions a,
856 {$CFG->prefix}groups_members g
857 WHERE a.assignment = {$this->assignment->id}
858 AND a.timemodified > 0
859 AND g.groupid = '$groupid'
860 AND a.userid = g.userid ");
861 } else {
862 $select = "s.course = '{$this->assignment->course}' AND";
863 if ($this->assignment->course == SITEID) {
864 $select = '';
d699cd1e 865 }
b0f2597e 866 return count_records_sql("SELECT COUNT(*)
867 FROM {$CFG->prefix}assignment_submissions a,
868 {$CFG->prefix}user_students s
869 WHERE a.assignment = '{$this->assignment->id}'
870 AND a.timemodified > 0
871 AND $select a.userid = s.userid ");
872 }
d59269cf 873 }
d699cd1e 874
73097f07 875 function email_teachers($submission) {
876 /// Alerts teachers by email of new or changed assignments that need grading
877
878 global $CFG;
879
d8199f1d 880 if (empty($this->assignment->emailteachers)) { // No need to do anything
73097f07 881 return;
882 }
883
884 $user = get_record('user', 'id', $submission->userid);
885
d8199f1d 886 if (groupmode($this->course, $this->cm) == SEPARATEGROUPS) { // Separate groups are being used
887 if (!$group = user_group($this->course->id, $user->id)) { // Try to find a group
73097f07 888 $group->id = 0; // Not in a group, never mind
889 }
d8199f1d 890 $teachers = get_group_teachers($this->course->id, $group->id); // Works even if not in group
73097f07 891 } else {
d8199f1d 892 $teachers = get_course_teachers($this->course->id);
73097f07 893 }
894
895 if ($teachers) {
896
897 $strassignments = get_string('modulenameplural', 'assignment');
898 $strassignment = get_string('modulename', 'assignment');
899 $strsubmitted = get_string('submitted', 'assignment');
900
901 foreach ($teachers as $teacher) {
902 unset($info);
903 $info->username = fullname($user);
d8199f1d 904 $info->assignment = format_string($this->assignment->name,true);
905 $info->url = $CFG->wwwroot.'/mod/assignment/submissions.php?id='.$this->cm->id;
906
907 $postsubject = $strsubmitted.': '.$info->username.' -> '.$this->assignment->name;
908 $posttext = $this->email_teachers_text($info);
909 $posthtml = ($teacher->mailformat == 1) ? $this->email_teachers_html($info) : '';
73097f07 910
911 @email_to_user($teacher, $user, $postsubject, $posttext, $posthtml); // If it fails, oh well, too bad.
912 }
913 }
914 }
915
d8199f1d 916 function email_teachers_text($info) {
917 $posttext = $this->course->shortname.' -> '.$this->strassignments.' -> '.
918 format_string($this->assignment->name, true)."\n";
919 $posttext .= '---------------------------------------------------------------------'."\n";
920 $posttext .= get_string("emailteachermail", "assignment", $info)."\n";
921 $posttext .= '---------------------------------------------------------------------'."\n";
922 return $posttext;
923 }
924
925
926 function email_teachers_html($info) {
927 $posthtml = '<p><font face="sans-serif">'.
928 '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$this->course->id.'">'.$course->shortname.'</a> ->'.
929 '<a href="'.$CFG->wwwroot.'/mod/assignment/index.php?id='.$this->course->id.'">'.$this->strassignments.'</a> ->'.
930 '<a href="'.$CFG->wwwroot.'/mod/assignment/view.php?id='.$cm->id.'">'.format_string($this->assignment->name,true).'</a></font></p>';
931 $posthtml .= '<hr /><font face="sans-serif">';
932 $posthtml .= '<p>'.get_string('emailteachermailhtml', 'assignment', $info).'</p>';
933 $posthtml .= '</font><hr />';
934 }
935
936
937 function print_user_files($userid=0, $return=false) {
73097f07 938
d8199f1d 939 global $CFG, $USER;
940
941 if (!$userid) {
942 if (!isloggedin()) {
943 return '';
944 }
945 $userid = $USER->id;
946 }
73097f07 947
70b2c772 948 $filearea = $this->file_area_name($userid);
73097f07 949
950 $output = '';
951
70b2c772 952 if ($basedir = $this->file_area($userid)) {
73097f07 953 if ($files = get_directory_list($basedir)) {
954 foreach ($files as $key => $file) {
955 require_once($CFG->libdir.'/filelib.php');
956 $icon = mimeinfo('icon', $file);
957 if ($CFG->slasharguments) {
958 $ffurl = "file.php/$filearea/$file";
959 } else {
960 $ffurl = "file.php?file=/$filearea/$file";
961 }
962
963 $output = '<img align="middle" src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" />'.
964 link_to_popup_window ('/'.$ffurl, 'file'.$key, $file, 450, 580, $file, 'none', true).
965 '<br />';
966 }
967 }
968 }
969
970 $output = '<div class="files">'.$output.'</div>';
971
972 if ($return) {
973 return $output;
974 }
975 echo $output;
976 }
977
70b2c772 978 function count_user_files($userid) {
979 global $CFG;
980
981 $filearea = $this->file_area_name($userid);
982
983 if ($basedir = $this->file_area($userid)) {
984 if ($files = get_directory_list($basedir)) {
985 return count($files);
986 }
987 }
988 return 0;
989 }
73097f07 990
70b2c772 991 function file_area_name($userid) {
73097f07 992 // Creates a directory file name, suitable for make_upload_directory()
993 global $CFG;
994
70b2c772 995 return $this->course->id.'/'.$CFG->moddata.'/assignment/'.$this->assignment->id.'/'.$userid;
73097f07 996 }
997
70b2c772 998 function file_area($userid) {
999 return make_upload_directory( $this->file_area_name($userid) );
73097f07 1000 }
1001
f77cfb73 1002 function isopen() {
1003 $time = time();
1004 if ($this->assignment->preventlate) {
1005 return ($this->assignment->timeavailable <= $time && $time <= $this->assignment->timedue);
1006 } else {
1007 return ($this->assignment->timeavailable <= $time);
1008 }
1009 }
1010
73097f07 1011 function user_outline($user) {
1012 if ($submission = $this->get_submission($user->id)) {
1013
1014 if ($submission->grade) {
f1893f44 1015 $result->info = get_string('grade').': '.$this->display_grade($submission->grade);
73097f07 1016 }
1017 $result->time = $submission->timemodified;
1018 return $result;
1019 }
1020 return NULL;
1021 }
1022
1023 function user_complete($user) {
1024 if ($submission = $this->get_submission($user->id)) {
70b2c772 1025 if ($basedir = $this->file_area($user->id)) {
73097f07 1026 if ($files = get_directory_list($basedir)) {
1027 $countfiles = count($files)." ".get_string("uploadedfiles", "assignment");
1028 foreach ($files as $file) {
1029 $countfiles .= "; $file";
1030 }
1031 }
1032 }
1033
1034 print_simple_box_start();
1035 echo get_string("lastmodified").": ";
1036 echo userdate($submission->timemodified);
70b2c772 1037 echo $this->display_lateness($submission->timemodified);
73097f07 1038
70b2c772 1039 $this->print_user_files($user->id);
73097f07 1040
1041 echo '<br />';
1042
1043 if (empty($submission->timemarked)) {
1044 print_string("notgradedyet", "assignment");
1045 } else {
1046 $this->view_feedback($submission);
1047 }
1048
1049 print_simple_box_end();
1050
1051 } else {
1052 print_string("notsubmittedyet", "assignment");
1053 }
1054 }
1055
70b2c772 1056 function display_lateness($timesubmitted) {
1057 $time = $this->assignment->timedue - $timesubmitted;
73097f07 1058 if ($time < 0) {
1059 $timetext = get_string('late', 'assignment', format_time($time));
70b2c772 1060 return ' (<span class="late">'.$timetext.'</span>)';
73097f07 1061 } else {
1062 $timetext = get_string('early', 'assignment', format_time($time));
70b2c772 1063 return ' (<span class="early">'.$timetext.'</span>)';
73097f07 1064 }
1065 }
1066
1067
1068
1069
1070
b0f2597e 1071} ////// End of the assignment_base class
d699cd1e 1072
18b8fbfa 1073
04eba58f 1074
b0f2597e 1075/// OTHER STANDARD FUNCTIONS ////////////////////////////////////////////////////////
1076
1077
1078function assignment_delete_instance($id){
26b90e70 1079 global $CFG;
1080
b0f2597e 1081 if (! $assignment = get_record('assignment', 'id', $id)) {
1082 return false;
26b90e70 1083 }
1084
b0f2597e 1085 require_once("$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php");
1086 $assignmentclass = "assignment_$assignment->assignmenttype";
1087 $ass = new $assignmentclass();
1088 return $ass->delete_instance($assignment);
1089}
f466c9ed 1090
ac21ad39 1091
b0f2597e 1092function assignment_update_instance($assignment){
1093 global $CFG;
26b90e70 1094
b0f2597e 1095 require_once("$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php");
1096 $assignmentclass = "assignment_$assignment->assignmenttype";
1097 $ass = new $assignmentclass();
1098 return $ass->update_instance($assignment);
1099}
26b90e70 1100
26b90e70 1101
b0f2597e 1102function assignment_add_instance($assignment) {
1103 global $CFG;
f466c9ed 1104
b0f2597e 1105 require_once("$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php");
1106 $assignmentclass = "assignment_$assignment->assignmenttype";
1107 $ass = new $assignmentclass();
1108 return $ass->add_instance($assignment);
1109}
f466c9ed 1110
73097f07 1111
1112function assignment_user_outline($course, $user, $mod, $assignment) {
1113 global $CFG;
1114
1115 require_once("$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php");
1116 $assignmentclass = "assignment_$assignment->assignmenttype";
1117 $ass = new $assignmentclass($mod->id, $assignment, $mod, $course);
1118 return $ass->user_outline($user);
1119}
1120
1121function assignment_user_complete($course, $user, $mod, $assignment) {
1122 global $CFG;
1123
1124 require_once("$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php");
1125 $assignmentclass = "assignment_$assignment->assignmenttype";
1126 $ass = new $assignmentclass($mod->id, $assignment, $mod, $course);
1127 return $ass->user_complete($user);
1128}
1129
1130
1131function assignment_cron () {
1132// Function to be run periodically according to the moodle cron
1133// Finds all assignment notifications that have yet to be mailed out, and mails them
1134
1135 global $CFG, $USER;
1136
1137 /// Notices older than 1 day will not be mailed. This is to avoid the problem where
1138 /// cron has not been running for a long time, and then suddenly people are flooded
1139 /// with mail from the past few weeks or months
1140
1141 $timenow = time();
1142 $endtime = $timenow - $CFG->maxeditingtime;
1143 $starttime = $endtime - 24 * 3600; /// One day earlier
1144
1145 if ($submissions = assignment_get_unmailed_submissions($starttime, $endtime)) {
1146
1147 foreach ($submissions as $key => $submission) {
1148 if (! set_field("assignment_submissions", "mailed", "1", "id", "$submission->id")) {
1149 echo "Could not update the mailed field for id $submission->id. Not mailed.\n";
1150 unset($submissions[$key]);
1151 }
1152 }
1153
1154 $timenow = time();
1155
1156 foreach ($submissions as $submission) {
1157
1158 echo "Processing assignment submission $submission->id\n";
1159
1160 if (! $user = get_record("user", "id", "$submission->userid")) {
1161 echo "Could not find user $post->userid\n";
1162 continue;
1163 }
1164
1165 $USER->lang = $user->lang;
1166
1167 if (! $course = get_record("course", "id", "$submission->course")) {
1168 echo "Could not find course $submission->course\n";
1169 continue;
1170 }
1171
1172 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
1173 echo fullname($user)." not an active participant in $course->shortname\n";
1174 continue;
1175 }
1176
1177 if (! $teacher = get_record("user", "id", "$submission->teacher")) {
1178 echo "Could not find teacher $submission->teacher\n";
1179 continue;
1180 }
1181
1182 if (! $mod = get_coursemodule_from_instance("assignment", $submission->assignment, $course->id)) {
1183 echo "Could not find course module for assignment id $submission->assignment\n";
1184 continue;
1185 }
1186
1187 if (! $mod->visible) { /// Hold mail notification for hidden assignments until later
1188 continue;
1189 }
1190
1191 $strassignments = get_string("modulenameplural", "assignment");
1192 $strassignment = get_string("modulename", "assignment");
1193
1194 unset($assignmentinfo);
1195 $assignmentinfo->teacher = fullname($teacher);
1196 $assignmentinfo->assignment = format_string($submission->name,true);
1197 $assignmentinfo->url = "$CFG->wwwroot/mod/assignment/view.php?id=$mod->id";
1198
1199 $postsubject = "$course->shortname: $strassignments: ".format_string($submission->name,true);
1200 $posttext = "$course->shortname -> $strassignments -> ".format_string($submission->name,true)."\n";
1201 $posttext .= "---------------------------------------------------------------------\n";
1202 $posttext .= get_string("assignmentmail", "assignment", $assignmentinfo);
1203 $posttext .= "---------------------------------------------------------------------\n";
1204
1205 if ($user->mailformat == 1) { // HTML
1206 $posthtml = "<p><font face=\"sans-serif\">".
1207 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
1208 "<a href=\"$CFG->wwwroot/mod/assignment/index.php?id=$course->id\">$strassignments</a> ->".
1209 "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=$mod->id\">".format_string($submission->name,true)."</a></font></p>";
1210 $posthtml .= "<hr /><font face=\"sans-serif\">";
1211 $posthtml .= "<p>".get_string("assignmentmailhtml", "assignment", $assignmentinfo)."</p>";
1212 $posthtml .= "</font><hr />";
1213 } else {
1214 $posthtml = "";
1215 }
1216
1217 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
1218 echo "Error: assignment cron: Could not send out mail for id $submission->id to user $user->id ($user->email)\n";
1219 }
1220 }
1221 }
1222
1223 return true;
1224}
1225
1226function assignment_grades($assignmentid) {
1227/// Must return an array of grades, indexed by user, and a max grade.
1228
1229
1230 if (!$assignment = get_record("assignment", "id", $assignmentid)) {
1231 return NULL;
1232 }
1233
1234 $grades = get_records_menu("assignment_submissions", "assignment",
1235 $assignment->id, "", "userid,grade");
1236
1237 if ($assignment->grade >= 0) {
1238 $return->grades = $grades;
1239 $return->maxgrade = $assignment->grade;
1240
1241 } else { // Scale
1242 if ($grades) {
1243 $scaleid = - ($assignment->grade);
1244 if ($scale = get_record('scale', 'id', $scaleid)) {
1245 $scalegrades = make_menu_from_list($scale->scale);
1246 foreach ($grades as $key => $grade) {
1247 $grades[$key] = $scalegrades[$grade];
1248 }
1249 }
1250 }
1251 $return->grades = $grades;
1252 $return->maxgrade = "";
1253 }
1254
1255 return $return;
1256}
1257
1258function assignment_get_participants($assignmentid) {
1259//Returns the users with data in one assignment
1260//(users with records in assignment_submissions, students and teachers)
1261
1262 global $CFG;
1263
1264 //Get students
1265 $students = get_records_sql("SELECT DISTINCT u.id, u.id
1266 FROM {$CFG->prefix}user u,
1267 {$CFG->prefix}assignment_submissions a
1268 WHERE a.assignment = '$assignmentid' and
1269 u.id = a.userid");
1270 //Get teachers
1271 $teachers = get_records_sql("SELECT DISTINCT u.id, u.id
1272 FROM {$CFG->prefix}user u,
1273 {$CFG->prefix}assignment_submissions a
1274 WHERE a.assignment = '$assignmentid' and
1275 u.id = a.teacher");
1276
1277 //Add teachers to students
1278 if ($teachers) {
1279 foreach ($teachers as $teacher) {
1280 $students[$teacher->id] = $teacher;
1281 }
1282 }
1283 //Return students array (it contains an array of unique users)
1284 return ($students);
1285}
1286
1287
1288function assignment_scale_used ($assignmentid,$scaleid) {
1289//This function returns if a scale is being used by one assignment
1290
1291 $return = false;
1292
1293 $rec = get_record('assignment','id',$assignmentid,'grade',-$scaleid);
1294
1295 if (!empty($rec) && !empty($scaleid)) {
1296 $return = true;
1297 }
1298
1299 return $return;
1300}
1301
1302
1303function assignment_refresh_events($courseid = 0) {
1304// This standard function will check all instances of this module
1305// and make sure there are up-to-date events created for each of them.
1306// If courseid = 0, then every assignment event in the site is checked, else
1307// only assignment events belonging to the course specified are checked.
1308// This function is used, in its new format, by restore_refresh_events()
1309
1310 if ($courseid == 0) {
1311 if (! $assignments = get_records("assignment")) {
1312 return true;
1313 }
1314 } else {
1315 if (! $assignments = get_records("assignment", "course", $courseid)) {
1316 return true;
1317 }
1318 }
1319 $moduleid = get_field('modules', 'id', 'name', 'assignment');
1320
1321 foreach ($assignments as $assignment) {
1322 $event = NULL;
1323 $event->name = addslashes($assignment->name);
1324 $event->description = addslashes($assignment->description);
1325 $event->timestart = $assignment->timedue;
1326
1327 if ($event->id = get_field('event', 'id', 'modulename', 'assignment', 'instance', $assignment->id)) {
1328 update_event($event);
1329
1330 } else {
1331 $event->courseid = $assignment->course;
1332 $event->groupid = 0;
1333 $event->userid = 0;
1334 $event->modulename = 'assignment';
1335 $event->instance = $assignment->id;
1336 $event->eventtype = 'due';
1337 $event->timeduration = 0;
1338 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $assignment->id);
1339 add_event($event);
1340 }
1341
1342 }
1343 return true;
1344}
1345
1346
1347function assignment_print_recent_activity($course, $isteacher, $timestart) {
1348 global $CFG;
1349
1350 $content = false;
1351 $assignments = NULL;
1352
1353 if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
1354 'course = \''.$course->id.'\' AND '.
1355 'module = \'assignment\' AND '.
1356 'action = \'upload\' ', 'time ASC')) {
1357 return false;
1358 }
1359
1360 foreach ($logs as $log) {
1361 //Create a temp valid module structure (course,id)
1362 $tempmod->course = $log->course;
1363 $tempmod->id = $log->info;
1364 //Obtain the visible property from the instance
1365 $modvisible = instance_is_visible($log->module,$tempmod);
1366
1367 //Only if the mod is visible
1368 if ($modvisible) {
1369 $assignments[$log->info] = assignment_log_info($log);
1370 $assignments[$log->info]->time = $log->time;
1371 $assignments[$log->info]->url = str_replace('&', '&amp;', $log->url);
1372 }
1373 }
1374
1375 if ($assignments) {
1376 print_headline(get_string('newsubmissions', 'assignment').':');
1377 foreach ($assignments as $assignment) {
1378 print_recent_activity_note($assignment->time, $assignment, $isteacher, $assignment->name,
1379 $CFG->wwwroot.'/mod/assignment/'.$assignment->url);
1380 }
1381 $content = true;
1382 }
1383
1384 return $content;
1385}
1386
1387
1388
1389function assignment_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $assignment="0", $user="", $groupid="") {
1390// Returns all assignments since a given time. If assignment is specified then
1391// this restricts the results
1392
1393 global $CFG;
1394
1395 if ($assignment) {
1396 $assignmentselect = " AND cm.id = '$assignment'";
1397 } else {
1398 $assignmentselect = "";
1399 }
1400 if ($user) {
1401 $userselect = " AND u.id = '$user'";
1402 } else {
1403 $userselect = "";
1404 }
1405
1406 $assignments = get_records_sql("SELECT asub.*, u.firstname, u.lastname, u.picture, u.id as userid,
1407 a.grade as maxgrade, name, cm.instance, cm.section, a.assignmenttype
1408 FROM {$CFG->prefix}assignment_submissions asub,
1409 {$CFG->prefix}user u,
1410 {$CFG->prefix}assignment a,
1411 {$CFG->prefix}course_modules cm
1412 WHERE asub.timemodified > '$sincetime'
1413 AND asub.userid = u.id $userselect
1414 AND a.id = asub.assignment $assignmentselect
1415 AND cm.course = '$courseid'
1416 AND cm.instance = a.id
1417 ORDER BY asub.timemodified ASC");
1418
1419 if (empty($assignments))
1420 return;
1421
1422 foreach ($assignments as $assignment) {
1423 if (empty($groupid) || ismember($groupid, $assignment->userid)) {
1424
1425 $tmpactivity = new Object;
1426
1427 $tmpactivity->type = "assignment";
1428 $tmpactivity->defaultindex = $index;
1429 $tmpactivity->instance = $assignment->instance;
1430 $tmpactivity->name = $assignment->name;
1431 $tmpactivity->section = $assignment->section;
1432
1433 $tmpactivity->content->grade = $assignment->grade;
1434 $tmpactivity->content->maxgrade = $assignment->maxgrade;
1435 $tmpactivity->content->type = $assignment->assignmenttype;
1436
1437 $tmpactivity->user->userid = $assignment->userid;
1438 $tmpactivity->user->fullname = fullname($assignment);
1439 $tmpactivity->user->picture = $assignment->picture;
1440
1441 $tmpactivity->timestamp = $assignment->timemodified;
1442
1443 $activities[] = $tmpactivity;
1444
1445 $index++;
1446 }
1447 }
1448
1449 return;
1450}
1451
1452
1453function assignment_print_recent_mod_activity($activity, $course, $detail=false) {
1454 global $CFG;
1455
1456 echo '<table border="0" cellpadding="3" cellspacing="0">';
1457
1458 echo "<tr><td class=\"userpicture\" width=\"35\" valign=\"top\">";
1459 print_user_picture($activity->user->userid, $course, $activity->user->picture);
1460 echo "</td><td width=\"100%\"><font size=2>";
1461
1462 if ($detail) {
1463 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
1464 "height=16 width=16 alt=\"$activity->type\"> ";
1465 echo "<a href=\"$CFG->wwwroot/mod/assignment/view.php?id=" . $activity->instance . "\">"
1466 . format_string($activity->name,true) . "</a> - ";
1467
1468 }
1469
1470 if (isteacher($course)) {
1471 $grades = "(" . $activity->content->grade . " / " . $activity->content->maxgrade . ") ";
1472
1473 $assignment->id = $activity->instance;
1474 $assignment->course = $course;
1475 $user->id = $activity->user->userid;
1476
1477 echo $grades;
1478 echo "<br />";
1479 }
1480 echo "<a href=\"$CFG->wwwroot/user/view.php?id="
1481 . $activity->user->userid . "&amp;course=$course\">"
1482 . $activity->user->fullname . "</a> ";
1483
1484 echo " - " . userdate($activity->timestamp);
1485
1486 echo "</font></td></tr>";
1487 echo "</table>";
1488
1489 return;
1490}
1491
1492/// GENERIC SQL FUNCTIONS
1493
1494function assignment_log_info($log) {
1495 global $CFG;
1496 return get_record_sql("SELECT a.name, u.firstname, u.lastname
1497 FROM {$CFG->prefix}assignment a,
1498 {$CFG->prefix}user u
1499 WHERE a.id = '$log->info'
1500 AND u.id = '$log->userid'");
1501}
1502
1503function assignment_get_unmailed_submissions($starttime, $endtime) {
1504/// Return list of marked submissions that have not been mailed out for currently enrolled students
1505 global $CFG;
1506 return get_records_sql("SELECT s.*, a.course, a.name
1507 FROM {$CFG->prefix}assignment_submissions s,
1508 {$CFG->prefix}assignment a,
1509 {$CFG->prefix}user_students us
1510 WHERE s.mailed = 0
1511 AND s.timemarked <= $endtime
1512 AND s.timemarked >= $starttime
1513 AND s.assignment = a.id
1514 AND s.userid = us.userid
1515 AND a.course = us.course");
1516}
1517
1518function assignment_count_real_submissions($assignment, $groupid=0) {
1519/// Return all real assignment submissions by ENROLLED students (not empty ones)
1520 global $CFG;
1521
1522 if ($groupid) { /// How many in a particular group?
1523 return count_records_sql("SELECT COUNT(DISTINCT g.userid, g.groupid)
1524 FROM {$CFG->prefix}assignment_submissions a,
1525 {$CFG->prefix}groups_members g
1526 WHERE a.assignment = $assignment->id
1527 AND a.timemodified > 0
1528 AND g.groupid = '$groupid'
1529 AND a.userid = g.userid ");
1530 } else {
1531 $select = "s.course = '$assignment->course' AND";
1532 if ($assignment->course == SITEID) {
1533 $select = '';
1534 }
1535 return count_records_sql("SELECT COUNT(*)
1536 FROM {$CFG->prefix}assignment_submissions a,
1537 {$CFG->prefix}user_students s
1538 WHERE a.assignment = '$assignment->id'
1539 AND a.timemodified > 0
1540 AND $select a.userid = s.userid ");
1541 }
1542}
1543
1544function assignment_get_all_submissions($assignment, $sort="", $dir="DESC") {
1545/// Return all assignment submissions by ENROLLED students (even empty)
1546 global $CFG;
1547
1548 if ($sort == "lastname" or $sort == "firstname") {
1549 $sort = "u.$sort $dir";
1550 } else if (empty($sort)) {
1551 $sort = "a.timemodified DESC";
1552 } else {
1553 $sort = "a.$sort $dir";
1554 }
1555
1556 $select = "s.course = '$assignment->course' AND";
1557 if ($assignment->course == SITEID) {
1558 $select = '';
1559 }
1560 return get_records_sql("SELECT a.*
1561 FROM {$CFG->prefix}assignment_submissions a,
1562 {$CFG->prefix}user_students s,
1563 {$CFG->prefix}user u
1564 WHERE a.userid = s.userid
1565 AND u.id = a.userid
1566 AND $select a.assignment = '$assignment->id'
1567 ORDER BY $sort");
1568}
1569
1570
1571
1572
1573/// OTHER GENERAL FUNCTIONS FOR ASSIGNMENTS ///////////////////////////////////////
1574
1575
b0f2597e 1576function assignment_types() {
1577 $types = array();
1578 $names = get_list_of_plugins('mod/assignment/type');
1579 foreach ($names as $name) {
1580 $types[$name] = get_string('type'.$name, 'assignment');
ffeca120 1581 }
b0f2597e 1582 asort($types);
1583 return $types;
f466c9ed 1584}
1585
b0f2597e 1586function assignment_upgrade_submodules() {
f1c1d2f1 1587 global $CFG;
26b90e70 1588
b0f2597e 1589 $types = assignment_types();
26b90e70 1590
d175966b 1591 include($CFG->dirroot.'/mod/assignment/version.php'); // defines $module with version etc
26b90e70 1592
d175966b 1593 foreach ($types as $type => $typename) {
26b90e70 1594
b0f2597e 1595 $fullpath = $CFG->dirroot.'/mod/assignment/type/'.$type;
26b90e70 1596
b0f2597e 1597 /// Check for an external version file (defines $submodule)
26b90e70 1598
b0f2597e 1599 if (!is_readable($fullpath .'/version.php')) {
1600 continue;
ffeca120 1601 }
b0f2597e 1602 include_once($fullpath .'/version.php');
26b90e70 1603
b0f2597e 1604 /// Check whether we need to upgrade
26b90e70 1605
b0f2597e 1606 if (!isset($submodule->version)) {
1607 continue;
1608 }
26b90e70 1609
b0f2597e 1610 /// Make sure this submodule will work with this assignment version
26b90e70 1611
d175966b 1612 if (isset($submodule->requires) and ($submodule->requires > $module->version)) {
b0f2597e 1613 notify("Assignment submodule '$type' is too new for your assignment");
1614 continue;
1615 }
f466c9ed 1616
b0f2597e 1617 /// If we use versions, make sure an internal record exists
f466c9ed 1618
b0f2597e 1619 $currentversion = 'assignment_'.$type.'_version';
f466c9ed 1620
b0f2597e 1621 if (!isset($CFG->$currentversion)) {
1622 set_config($currentversion, 0);
f466c9ed 1623 }
f466c9ed 1624
b0f2597e 1625 /// See if we need to upgrade
1626
1627 if ($submodule->version <= $CFG->$currentversion) {
1628 continue;
59c005b7 1629 }
59c005b7 1630
b0f2597e 1631 /// Look for the upgrade file
59c005b7 1632
b0f2597e 1633 if (!is_readable($fullpath .'/db/'.$CFG->dbtype.'.php')) {
1634 continue;
1635 }
59c005b7 1636
b0f2597e 1637 include_once($fullpath .'/db/'. $CFG->dbtype .'.php'); // defines assignment_xxx_upgrade
59c005b7 1638
b0f2597e 1639 /// Perform the upgrade
59c005b7 1640
b0f2597e 1641 $upgrade_function = 'assignment_'.$type.'_upgrade';
1642 if (function_exists($upgrade_function)) {
1643 $db->debug=true;
1644 if ($upgrade_function($CFG->$currentversion)) {
1645 $db->debug=false;
1646 set_config($currentversion, $submodule->version);
59c005b7 1647 }
b0f2597e 1648 $db->debug=false;
59c005b7 1649 }
1650 }
9437c854 1651
1652
59c005b7 1653}
1654
04eba58f 1655?>