New function field_exists() to detect if one field exists
[moodle.git] / mod / assignment / type / upload / assignment.class.php
CommitLineData
fcdcc372 1<?php // $Id$
2
3/**
4 * Extend the base assignment class for assignments where you upload one or more files and where teacher can upload one or more response files
5 *
6 */
7class assignment_upload extends assignment_base {
8ba4cbd2 8
fcdcc372 9 function print_student_answer($userid, $return=false){
10 global $CFG, $USER;
8ba4cbd2 11
6fc73d59 12 $filearea = $this->file_area_name($userid);
6fc73d59 13
8ba4cbd2 14 $output = '';
15
fcdcc372 16 if ($basedir = $this->file_area($userid)) {
17 if ($files = get_directory_list($basedir)) {
18 require_once($CFG->libdir.'/filelib.php');
19 foreach ($files as $key => $file) {
8ba4cbd2 20
fcdcc372 21 $icon = mimeinfo('icon', $file);
8ba4cbd2 22
fcdcc372 23 if ($CFG->slasharguments) {
8ba4cbd2 24 $ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
25 } else {
26 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
fcdcc372 27 }
8ba4cbd2 28 //died right here
29 //require_once($ffurl);
fcdcc372 30 $output .= '<img align="middle" src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" />'.
31 '<a href="'.$ffurl.'" >'.$file.'</a><br />';
32 }
33 }
34 }
35
36 $output = '<div class="files">'.$output.'</div>';
8ba4cbd2 37 return $output;
fcdcc372 38 }
39
40 function assignment_upload($cmid=0) {
41 parent::assignment_base($cmid);
8ba4cbd2 42
fcdcc372 43 }
44
45 //generates page for file upload, list of uploaded files, feedbacks from teachers, etc
46 function view() {
fcdcc372 47
8ba4cbd2 48 global $USER;
fcdcc372 49
8ba4cbd2 50 $this->view_header();
fcdcc372 51
8ba4cbd2 52 $this->view_intro();
fcdcc372 53
8ba4cbd2 54 $this->view_dates();
6fc73d59 55
8ba4cbd2 56 $filecount = $this->count_user_files($USER->id);
6fc73d59 57
8ba4cbd2 58 if ($submission = $this->get_submission()) {
59 if ($submission->timemarked) {
60 $this->view_feedback();
61 }else if ($filecount) {
62 print_simple_box($this->print_user_files($USER->id, true), 'center');
6fc73d59 63 }
8ba4cbd2 64 //from upload&review. check if there are responses from teacher
65 if ($submission->data2 != NULL) {
66 print_heading(get_string('responsesfromteacher', 'assignment'));
67 $this->print_response_students(true);
fcdcc372 68 }
8ba4cbd2 69 }
fcdcc372 70
8ba4cbd2 71 //display Blank if there were no files uploaded yet, otherwithe display submission status
72 if (!$submission->data1) {
73 $submission_status = get_string('submissionstatusblank','assignment');
74 }
75 else {
76 $submission_status = $submission->data1;
38ac07d2 77 }
6fc73d59 78
8ba4cbd2 79 //display submisison status
80 notify("<b>".get_string('submissionstatus', 'assignment')." </b> ".$submission_status);
81
82 if ($submission->data1 != get_string("submissionstatussubmitted", "assignment") && has_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE, $this->cm->id)) && $this->isopen() &&
83 ((!$this->assignment->var1 && $submission->data1 == get_string("submissionstatusreturned", "assignment")) ||
84 // $this->assignment->var1 ||
85 !$filecount ||
86 ($this->assignment->resubmit && $submission->data1 == get_string("submissionstatusmarked", "assignment")) ||
87 $submission->data1 == get_string("submissionstatusreturned", "assignment") ||
88 !$submission->timemarked)
89 ) {
90 $this->view_upload_form();
91 }
92 $this->view_footer();
fcdcc372 93 }
94
95 function view_upload_form() {
96 global $CFG;
97 $struploadafile = get_string("uploadafile");
98
99 $strmaxsize = get_string("maxsize", "", display_size($this->assignment->maxbytes));
100
101 echo '<center>';
8ba4cbd2 102 echo '<form enctype="multipart/form-data" method="post" '.
103 "action=\"$CFG->wwwroot/mod/assignment/upload.php\">";
104 echo "<p><b>".get_string('step1','assignment')."</b>".get_string('attachfiletoassignment','assignment')." ($strmaxsize)</p>";//$struploadafile
fcdcc372 105 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
8ba4cbd2 106 require_once($CFG->libdir.'/uploadlib.php');
fcdcc372 107 upload_print_form_fragment(1,array('newfile'),false,null,0,$this->assignment->maxbytes,false);
108 //upload files
109 echo '<input type="submit" name="save" value="'.get_string('attachfile','assignment').'" />';
61240489 110 echo "<p><b>".get_string('step2','assignment')."</b>".get_string('submitformarking','assignment')."</p>";
fcdcc372 111 //final submit
112 echo '<input type="submit" name="save" value="'.get_string('sendformarking','assignment').'" />';
113 echo "<p>".get_string('onceassignmentsent','assignment')."</p>";
8ba4cbd2 114
fcdcc372 115 echo '</form>';
8ba4cbd2 116
fcdcc372 117 echo '</center>';
118 }
119
120 //from upload&review. generates upload form for markers response
121 function view_upload_response_form($userid,$offset) {
122 global $CFG;
123
8ba4cbd2 124 $userid = required_param('userid');
125
fcdcc372 126 echo '<center>';
61240489 127 echo get_string('choosereviewfile','assignment').'<br>';
fcdcc372 128
8ba4cbd2 129 echo '<form enctype="multipart/form-data" method="post" '.
130 "action=\"$CFG->wwwroot/mod/assignment/upload.php\">";
fcdcc372 131 echo '<input type="hidden" name="userid" value="'.$userid.'" />';
132 echo '<input type="hidden" name="offset" value="'.$offset.'" />';
133 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
8ba4cbd2 134 require_once($CFG->libdir.'/uploadlib.php');
135 upload_print_form_fragment(1,array('newfile'),false,null,0,$this->assignment->maxbytes,false);
fcdcc372 136 echo '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
137 echo '</form>';
138 echo '</center>';
139 }
140
8ba4cbd2 141 //general function which calls function for drafts upload, final upload, teachers responce upload
fcdcc372 142 function upload(){
8ba4cbd2 143 global $offset;
144
fcdcc372 145 //if this is final submit
8ba4cbd2 146 $savestr = optional_param('save', '', PARAM_ALPHA);
147 if ($savestr === get_string('sendformarking','assignment')) {
148
149 $this->final_upload();
150
151 } else {
152 //if this is draft upload
153 if ($_POST['save']==get_string('attachfile','assignment') && !isset($_POST['userid'])){
154
155 $this->submission_upload();
156
157 } else {
158 //if this is upload of teacher's response
159 $id = optional_param('id', 0, PARAM_INT); // Course module ID
160 $a = optional_param('a', 0, PARAM_INT); // Assignment ID
161 $userid = required_param('userid', 0, PARAM_INT); // Stores student id for uploading a review file to
162 $this->response_upload($userid); // Upload files
163 echo "<form action=\"submissions.php\">";
164 echo "<input type=\"hidden\" value=\"$userid\" name=\"userid\">";
165 echo "<input type=\"hidden\" value=\"$id\" name=\"id\"><input type=\"hidden\" value=\"$a\" name=\"a\"><input type=\"hidden\" value=\"single\" name=\"mode\">";
166 echo '<input type="hidden" name="offset" value="'.$offset.'" />';
167 echo "<center><input type=\"submit\" value=\"Back to Feedback\" name=\"submit\"></center></form>";
168 }
169 }
fcdcc372 170 }
171
172 //implements upload regular submissuion - draft files
8ba4cbd2 173 function submission_upload() {
fcdcc372 174 global $CFG, $USER, $counter;
175
8ba4cbd2 176 require_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE, $this->cm->id));
fcdcc372 177
8ba4cbd2 178 $this->view_header(get_string('upload'));
fcdcc372 179
8ba4cbd2 180 $filecount = $this->count_user_files($USER->id);
181 $submission = $this->get_submission($USER->id);
fcdcc372 182
8ba4cbd2 183 //need to get student's directory and all previously uploaded files
184 $basedir = $this->file_area($USER->id);
185 $files = get_directory_list($basedir);
fcdcc372 186
8ba4cbd2 187 if ($this->isopen()) {//&& ($this->assignment->var1 || !$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
188 if ($submission) {
189 //TODO: change later to ">= 0", to prevent resubmission when graded 0
190 if (($submission->grade > 0) and !$this->assignment->resubmit) {
191 notify(get_string('alreadygraded', 'assignment'));
6fc73d59 192 }
8ba4cbd2 193 }
6fc73d59 194
8ba4cbd2 195 $dir = $this->file_area_name($USER->id);
196
197 require_once($CFG->dirroot.'/lib/uploadlib.php');
198 $um = new upload_manager('newfile',!$this->assignment->var1,false,$course,false,$this->assignment->maxbytes);
199
200 if ($um->process_file_uploads($dir)) {
201 $newfile_name = $um->get_new_filename();
202 //if student already submitted smth before
203 if ($submission) {
204 // assignment is not submited for marking
205 $newsubmission->timemodified = '0';
206 //$submission->timemodified = time();
207 $flag=false;
208 foreach ($files as $key => $file) {
209 if ($file == $newfile_name) {
210 $flag = true;
38ac07d2 211 }
8ba4cbd2 212 }
213 //if this is an assignment for single upload
214 if (!$this->assignment->var1) {
215 //if numfiles=1
216 if ($submission->numfiles==0 && !$flag) {
217 $submission->numfiles ++;
6fc73d59 218 }
8ba4cbd2 219 } else {
220 //if file with the same name has not been uploaded before
221 if (!$flag) {
222 $submission->numfiles ++;
38ac07d2 223 }
8ba4cbd2 224 }
38ac07d2 225
8ba4cbd2 226 $submission->submissioncomment = addslashes($submission->submissioncomment);
227 unset($submission->data1); // Don't need to update this.
228 //unset($submission->data2); // Don't need to update this.
229 if (update_record("assignment_submissions", $submission)) {
230 add_to_log($this->course->id, 'assignment', 'upload',
231 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
232 //we email teachers on final upload
233 //$this->email_teachers($submission);
234 print_heading(get_string('uploadedfile'));
235 } else {
236 notify(get_string("uploadfailnoupdate", "assignment"));
237 }
238 //if it's first student's submission
239 } else {
240 $newsubmission = $this->prepare_new_submission($USER->id);
241 //submissions has been created, but not submitted for marking
242 $newsubmission->timecreated = time();
243 $newsubmission->timemodified = '0';
244 $newsubmission->data1 = get_string("submissionstatusdraft", "assignment");
245 $newsubmission->numfiles = 1;
246 if (insert_record('assignment_submissions', $newsubmission)) {
247 add_to_log($this->course->id, 'assignment', 'upload',
248 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
249 //we email teachers on final upload
250 //$this->email_teachers($newsubmission);
251 print_heading(get_string('uploadedfile'));
252 } else {
253 notify(get_string("uploadnotregistered", "assignment", $newfile_name));
fcdcc372 254 }
255 }
256 }
8ba4cbd2 257 } else {
258 notify(get_string("uploaderror", "assignment")); //submitting not allowed!
fcdcc372 259 }
8ba4cbd2 260
261 print_continue('view.php?id='.$this->cm->id);
262
263 $this->view_footer();
fcdcc372 264 }
265
266 //implements final upload (submitting for marking)
8ba4cbd2 267 function final_upload() {
fcdcc372 268 global $CFG, $USER;
269
8ba4cbd2 270 require_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE, $this->cm->id));
fcdcc372 271
8ba4cbd2 272 $this->view_header(get_string('upload'));
273 $filecount = $this->count_user_files($USER->id);
274 $submission = $this->get_submission($USER->id);
fcdcc372 275
8ba4cbd2 276 if ($this->isopen() && ($this->assignment->var1 || $filecount || $this->assignment->resubmit || !$submission->timemarked)) {
277 if ($submission) {
278 //TODO: change later to ">= 0", to prevent resubmission when graded 0
279 if (($submission->grade > 0) and !$this->assignment->resubmit) {
280 notify(get_string('alreadygraded', 'assignment'));
fcdcc372 281 }
8ba4cbd2 282 }
fcdcc372 283
8ba4cbd2 284 $dir = $this->file_area_name($USER->id);
285
286 require_once($CFG->dirroot.'/lib/uploadlib.php');
287 $um = new upload_manager('newfile',!$this->assignment->var1,false,$course,false,$this->assignment->maxbytes);
288
289 //files hass been preprocessed i saved already, we don't need to do it again
290 //if ($um->process_file_uploads($dir)) {
291 $newfile_name = $um->get_new_filename();
292
293 //if student already submitted smth before
294 if ($submission) {
295 //if there is no files uploaded we can't do final submit
296 if ( $submission->numfiles == 0){
297 notify(get_string("nofilesforsubmit","assignment"));//'uploadnofilefound'));
298 }else{
299 $submission->timemodified = time();
300 //$submission->numfiles ++;
301 //$submission->comment = addslashes($submission->comment);
302 $submission->data1 = get_string("submissionstatussubmitted", "assignment");
303 //unset($submission->data2); // Don't need to update this.
304 if (update_record("assignment_submissions", $submission)) {
305 add_to_log($this->course->id, 'assignment', 'upload',
306 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
307 $this->email_teachers($submission);
308 print_heading(get_string('markingsubmitnotification','assignment'));
fcdcc372 309
8ba4cbd2 310 } else {
311 notify(get_string("uploadfailnoupdate", "assignment"));
312 }
313 }
314 //if it's first student's submission
315 } else {/*//probably this block can be removed.
316 $newsubmission = $this->prepare_new_submission($USER->id);
317 $newsubmission->timecreated = '0';
318 $newsubmission->timemodified = '1';
319 $newsubmission->data1 = get_string("submissionstatussubmitted", "assignment");
320 $newsubmission->numfiles = 1;
321 if (insert_record('assignment_submissions', $newsubmission)) {
322 add_to_log($this->course->id, 'assignment', 'upload',
323 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
324 $this->email_teachers($newsubmission);
325 print_heading(get_string('markingsubmitnotification','assignment'));
326 } else {
327 notify(get_string("uploadnotregistered", "assignment", $newfile_name) );
328 }*/
329 notify(get_string("nofilesforsubmit","assignment"));
6fc73d59 330 }
8ba4cbd2 331 // }
332 } else {
333 notify(get_string("uploaderror", "assignment")); //submitting not allowed!
6fc73d59 334 }
8ba4cbd2 335
336 print_continue('view.php?id='.$this->cm->id);
337
338 $this->view_footer();
fcdcc372 339 }
340
341
8ba4cbd2 342 //from upload&review
fcdcc372 343 function response_file_area_name($userid, $teachid = 0) {
8ba4cbd2 344 // Creates a directory file name, suitable for make_upload_directory()
fcdcc372 345 global $CFG, $USER;
38ac07d2 346 $fileloc = "";
fcdcc372 347 if ($teachid == 0) {
38ac07d2 348 $fileloc = "$USER->id/$userid";
8ba4cbd2 349 } else {
38ac07d2 350 $fileloc = "$teachid/$USER->id";
351 }
352 return $this->course->id.'/'.$CFG->moddata.'/assignment/'.$this->assignment->id.'/responses/'.$fileloc;
fcdcc372 353 }
354
8ba4cbd2 355 //from upload&review
356 //make the folder which going to hold response files
357 function response_file_area($userid, $teachid = 0) {
38ac07d2 358 if ($teachid == 0) {
359 return make_upload_directory( $this->response_file_area_name($userid) );
8ba4cbd2 360 } else {
38ac07d2 361 return make_upload_directory( $this->response_file_area_name($userid, $teachid) );
362 }
8ba4cbd2 363 }
fcdcc372 364
8ba4cbd2 365 //from upload&review
366 //upload responce file
367 function response_upload($userid) {
fcdcc372 368 global $CFG, $USER;
369
8ba4cbd2 370 // $this->view_header(get_string('upload'));
fcdcc372 371
372 if (!$this->isopen()) {
8ba4cbd2 373 notify(get_string("uploadfailnoupdate", "assignment"));
374 } else {
38ac07d2 375 $submission = $this->get_submission($userid);
8ba4cbd2 376
fcdcc372 377 $dir = $this->response_file_area_name($userid);
8ba4cbd2 378
379 require_once($CFG->dirroot.'/lib/uploadlib.php');
380 //$um = new upload_manager('newfile',true,false,$course,false,$this->assignment->maxbytes);
381 //set up $deletothers=false to allow multiple feedback uploads
38ac07d2 382 $um = new upload_manager('newfile',false,false,$course,false,$this->assignment->maxbytes);
fcdcc372 383
384 if ($um->process_file_uploads($dir)) {
385 $newfile_name = $um->get_new_filename();
386 if ($submission) {
8ba4cbd2 387 // stores teacher id's in data2 in comma-separated list so students can view all responses from all teachers
fcdcc372 388 if ($teachids = $submission->data2) {
38ac07d2 389 $teachidarr = explode(',', $teachids);
390 $teachidexists = false;
391 foreach($teachidarr as $t) {
392 if ($t == $USER->id) {
393 $teachidexists = true;
394 }
395 }
396 if ($teachidexists == false) {
397 $teachids .= ",$USER->id";
398 }
399 $submission->data2 = $teachids;
8ba4cbd2 400 } else {
fcdcc372 401 $submission->data2 = $USER->id;
402 }
8ba4cbd2 403 //$submission->timemodified = time();
404 //$submission->numfiles = 1;
405 //$submission->comment = addslashes($submission->comment);
fcdcc372 406
8ba4cbd2 407 if (update_record("assignment_submissions", $submission)) {
408 $this->email_students($submission);
409 //print_heading(get_string('uploadedfile'));
410 } else {
411 notify(get_string("uploadfailnoupdate", "assignment"));
412 }
413 } else {
414 notify(get_string("studentrecorderror", "assignment"));
fcdcc372 415 }
416 notify(get_string("uploadsuccessresponse", "assignment"));
417 }
418 }
8ba4cbd2 419
420 // print_continue('view.php?id='.$this->cm->id);
421
422 // $this->view_footer();
fcdcc372 423 }
424
8ba4cbd2 425 //from upload&review
426 function email_students($submission) {
427 /// Alerts students by email of assignments that recieve a new response
428// Email students when uploaded & when grade changed?
429 global $CFG;
fcdcc372 430
8ba4cbd2 431 //changed from var1 to var2 becasue of the merging two assignment types
432 if (empty($this->assignment->var2)) { // No need to do anything
433 return;
fcdcc372 434 }
8ba4cbd2 435/*
436 $user = get_record('user', 'id', $submission->userid);
fcdcc372 437
8ba4cbd2 438 if (groupmode($this->course, $this->cm) == SEPARATEGROUPS) { // Separate groups are being used
439 if (!$group = user_group($this->course->id, $user->id)) { // Try to find a group
440 $group->id = 0; // Not in a group, never mind
441 }
442 $teachers = get_group_teachers($this->course->id, $group->id); // Works even if not in group
443 } else {
444 $teachers = get_course_teachers($this->course->id);
445 }
446
447 if ($teachers) {
448
449 $strassignments = get_string('modulenameplural', 'assignment');
450 $strassignment = get_string('modulename', 'assignment');
451 $strsubmitted = get_string('submitted', 'assignment');
452
453 foreach ($teachers as $teacher) {
454 unset($info);
455 $info->username = fullname($user);
456 $info->assignment = format_string($this->assignment->name,true);
457 $info->url = $CFG->wwwroot.'/mod/assignment/submissions.php?id='.$this->cm->id;
458
459 $postsubject = $strsubmitted.': '.$info->username.' -> '.$this->assignment->name;
460 $posttext = $this->email_teachers_text($info);
461 $posthtml = ($teacher->mailformat == 1) ? $this->email_teachers_html($info) : '';
462
463 @email_to_user($teacher, $user, $postsubject, $posttext, $posthtml); // If it fails, oh well, too bad.
464 }
fcdcc372 465 }
8ba4cbd2 466*/
467 }
468
fcdcc372 469
8ba4cbd2 470 /*
471 * Display and process the submissions
472 */
473 function process_feedback() {
474
475 global $USER;
476
477 if (!$feedback = data_submitted()) { // No incoming data?
478 return false;
479 }
480
fcdcc372 481 ///For save and next, we need to know the userid to save, and the userid to go...
482 ///We use a new hidden field in the form, and set it to -1. If it's set, we use this
483 ///as the userid to store...
484 //removed by Oksana. it was braking functionality and submitting teacher's feedback to.. teacher-user
8ba4cbd2 485 //this was inherited from upload type. check if nothing brackes???????????????
486 if ((int)$feedback->saveuserid !== -1) {
fcdcc372 487 $feedback->userid = $feedback->saveuserid;
8ba4cbd2 488 }
489 if (!empty($feedback->cancel)) { // User hit cancel button
fcdcc372 490 return false;
491 }
8ba4cbd2 492
493 $newsubmission = $this->get_submission($feedback->userid, true); // Get or make one
61240489 494 $newsubmission->grade = $feedback->grade;
8ba4cbd2 495 $newsubmission->submissioncomment = $feedback->submissioncomment;
fcdcc372 496 $newsubmission->format = $feedback->format;
497 $newsubmission->teacher = $USER->id;
8ba4cbd2 498 $newsubmission->mailed = 0; // Make sure mail goes out (again, even)
fcdcc372 499 $newsubmission->timemarked = time();
8ba4cbd2 500 //marker graded assignment then status set into Marked; if marker didn't grade it then status set into Returned
501 if (/*$feedback->grade != 0 && */ $feedback->grade != -1 ) {
502 $newsubmission->data1 = get_string("submissionstatusmarked", "assignment");
38ac07d2 503 }
504 else {
8ba4cbd2 505 $newsubmission->data1 = get_string("submissionstatusreturned", "assignment");
38ac07d2 506 }
8ba4cbd2 507 //unset($newsubmission->data1); // Don't need to update this.
508 //unset($newsubmission->data2); // Don't need to update this.
fcdcc372 509
8ba4cbd2 510 if (! update_record('assignment_submissions', $newsubmission)) {
fcdcc372 511 return false;
512 }
8ba4cbd2 513
514 add_to_log($this->course->id, 'assignment', 'update grades',
515 'submissions.php?id='.$this->assignment->id.'&user='.$feedback->userid, $feedback->userid, $this->cm->id);
516
fcdcc372 517 return $newsubmission;
8ba4cbd2 518
519 }
520
fcdcc372 521 /*
8ba4cbd2 522 * Top-level function for handling of submissions called by submissions.php
fcdcc372 523 *
524 */
525 //from lib.php
526 //needed to update case of fastgrading. upgrade upload_statuses
527 function submissions($mode) {
8ba4cbd2 528 ///The main switch is changed to facilitate
529 ///1) Batch fast grading
530 ///2) Skip to the next one on the popup
531 ///3) Save and Skip to the next one on the popup
532
fcdcc372 533 //make user global so we can use the id
534 global $USER;
535
8ba4cbd2 536 switch ($mode) {
537 case 'grade': // We are in a popup window grading
538 if ($submission = $this->process_feedback()) {
539 //IE needs proper header with encoding
540 print_header(get_string('feedback', 'assignment').':'.format_string($this->assignment->name));
541 print_heading(get_string('changessaved'));
542 $this->update_main_listing($submission);
543 }
544 close_window();
545
546 break;
547
548 case 'single': // We are in a popup window displaying submission
549 $this->display_submission();
550 break;
551
552 case 'all': // Main window, display everything
553 $this->display_submissions();
554 break;
555
556 case 'fastgrade':
557 ///do the fast grading stuff - this process should work for all 3 subclasses
558 $grading = false;
559 $commenting = false;
560 $col = false;
561 if (isset($_POST['submissioncomment'])) {
562 $col = 'submissioncomment';
563 $commenting = true;
fcdcc372 564 }
8ba4cbd2 565 if (isset($_POST['menu'])) {
566 $col = 'menu';
567 $grading = true;
fcdcc372 568 }
8ba4cbd2 569 if (!$col) {
570 //both submissioncomment and grade columns collapsed..
571 $this->display_submissions();
572 break;
fcdcc372 573 }
574
8ba4cbd2 575 foreach ($_POST[$col] as $id => $unusedvalue) {
576
577 $id = (int)$id; //clean parameter name
578 if (!$submission = $this->get_submission($id)) {
579 $submission = $this->prepare_new_submission($id);
580 $newsubmission = true;
581 } else {
582 $newsubmission = false;
fcdcc372 583 }
8ba4cbd2 584 unset($submission->data1); // Don't need to update this.
585 unset($submission->data2); // Don't need to update this.
586
587 //for fast grade, we need to check if any changes take place
588 $updatedb = false;
fcdcc372 589
8ba4cbd2 590 if ($grading) {
591 $grade = $_POST['menu'][$id];
592 $updatedb = $updatedb || ($submission->grade != $grade);
593 $submission->grade = $grade;
594 } else {
595 if (!$newsubmission) {
596 unset($submission->grade); // Don't need to update this.
597 }
38ac07d2 598 }
8ba4cbd2 599
600 //change status if assignment was graded or returned
601 if ($submission->grade != -1 ) {
602 $submission->data1 = get_string("submissionstatusmarked", "assignment");
38ac07d2 603 }
8ba4cbd2 604 else {
605 $submission->data1 = get_string("submissionstatusreturned", "assignment");
606 }
607 if ($commenting) {
608 $commentvalue = trim($_POST['submissioncomment'][$id]);
609 $updatedb = $updatedb || ($submission->submissioncomment != stripslashes($commentvalue));
610 $submission->submissioncomment = $commentvalue;
611 } else {
612 unset($submission->submissioncomment); // Don't need to update this.
fcdcc372 613 }
614
8ba4cbd2 615 $submission->teacher = $USER->id;
616 $submission->mailed = $updatedb?0:$submission->mailed;//only change if it's an update
617 $submission->timemarked = time();
fcdcc372 618
8ba4cbd2 619 //if it is not an update, we don't change the last modified time etc.
620 //this will also not write into database if no submissioncomment and grade is entered.
fcdcc372 621
8ba4cbd2 622 if ($updatedb) {
623 if ($newsubmission) {
624 if (!insert_record('assignment_submissions', $submission)) {
625 return false;
fcdcc372 626 }
627 } else {
8ba4cbd2 628 if (!update_record('assignment_submissions', $submission)) {
629 return false;
6fc73d59 630 }
6fc73d59 631 }
8ba4cbd2 632 //add to log only if updating
633 add_to_log($this->course->id, 'assignment', 'update grades',
634 'submissions.php?id='.$this->assignment->id.'&user='.$submission->userid,
635 $submission->userid, $this->cm->id);
6fc73d59 636 }
fcdcc372 637
8ba4cbd2 638 }
639 $this->display_submissions();
640 break;
fcdcc372 641
fcdcc372 642
8ba4cbd2 643 case 'next':
644 /// We are currently in pop up, but we want to skip to next one without saving.
645 /// This turns out to be similar to a single case
646 /// The URL used is for the next submission.
647
648 $this->display_submission();
649 break;
650
651 case 'saveandnext':
652 ///We are in pop up. save the current one and go to the next one.
653 //first we save the current changes
654 if ($submission = $this->process_feedback()) {
655 //print_heading(get_string('changessaved'));
656 $this->update_main_listing($submission);
657 }
658
659 //then we display the next submission
660 $this->display_submission();
661 break;
662
663 default:
664 echo "something seriously is wrong!!";
665 break;
fcdcc372 666 }
667 }
668
8ba4cbd2 669 //function that updates the listing on the main script from popup using javascript
670 //from lib.php
671 //needed to display teachers responce files and upload_statuses
fcdcc372 672 function update_main_listing($submission) {
673 global $SESSION;
674
675 $perpage = get_user_preferences('assignment_perpage', 10);
8ba4cbd2 676
fcdcc372 677 $quickgrade = get_user_preferences('assignment_quickgrade', 0);
678
679 /// Run some Javascript to try and update the parent page
680 echo '<script type="text/javascript">'."\n<!--\n";
8ba4cbd2 681 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['submissioncomment'])) {
fcdcc372 682 if ($quickgrade){
8ba4cbd2 683 echo 'opener.document.getElementById("submissioncomment['.$submission->userid.']").value="'
684 .trim($submission->submissioncomment).'";'."\n";
685 } else {
686 echo 'opener.document.getElementById("com'.$submission->userid.
687 '").innerHTML="'.shorten_text(trim(strip_tags($submission->submissioncomment)), 15)."\";\n";
fcdcc372 688 }
689 }
690
691 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['grade'])) {
8ba4cbd2 692 //echo optional_param('menuindex');
fcdcc372 693 if ($quickgrade){
8ba4cbd2 694 echo 'opener.document.getElementById("menumenu['.$submission->userid.
695 ']").selectedIndex="'.required_param('menuindex', 0, PARAM_INT).'";'."\n";
fcdcc372 696 } else {
8ba4cbd2 697 echo 'opener.document.getElementById("g'.$submission->userid.'").innerHTML="'.
698 $this->display_grade($submission->grade)."\";\n";
fcdcc372 699 }
700 }
701
702 //need to add student's assignments in there too.
703 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['timemodified']) &&
704 $submission->timemodified) {
8ba4cbd2 705 echo 'opener.document.getElementById("ts'.$submission->userid.
706 '").innerHTML="'.addslashes($this->print_student_answer($submission->userid)).userdate($submission->timemodified)."\";\n";
fcdcc372 707 }
708
709 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['timemarked']) &&
710 $submission->timemarked) {
711 //display teachers feedback files here as well
8ba4cbd2 712 echo 'opener.document.getElementById("tt'.$submission->userid.
713 '").innerHTML="'.addslashes($this->print_user_response_files($submission->userid,false)).userdate($submission->timemarked)."\";\n";
fcdcc372 714 }
715
716 if (empty($SESSION->flextable['mod-assignment-submissions']->collapse['status'])) {
717 echo 'opener.document.getElementById("up'.$submission->userid.'").className="s1";';
718 //replace "Update" by upload_status
8ba4cbd2 719 $buttontext = $submission->data1; //get_string('update');
720 $button = link_to_popup_window ('/mod/assignment/submissions.php?id='.$this->cm->id.'&amp;userid='.$submission->userid.'&amp;mode=single'.'&amp;offset='.optional_param('offset', '', PARAM_INT),
721 'grade'.$submission->userid, $buttontext, 450, 700, $buttontext, 'none', true, 'button'.$submission->userid);
fcdcc372 722 echo 'opener.document.getElementById("up'.$submission->userid.'").innerHTML="'.addslashes($button).'";';
723 }
724 echo "\n-->\n</script>";
725 flush();
726 }
727
8ba4cbd2 728//display student's submission for marking in pop-up window
38ac07d2 729 function display_submission() {
fcdcc372 730
731 global $CFG, $offset;
732
8ba4cbd2 733 $userid = required_param('userid', PARAM_INT);
734 $offset = required_param('offset', PARAM_INT);//offset for where to start looking for student.
fcdcc372 735
8ba4cbd2 736 if (!$user = get_record('user', 'id', $userid)) {
737 error('No such user!');
738 }
fcdcc372 739
8ba4cbd2 740 if (!$submission = $this->get_submission($user->id)) {
741 $submission = $this->prepare_new_submission($userid);
742 }
fcdcc372 743
8ba4cbd2 744 if ($submission->timemodified > $submission->timemarked) {
745 $subtype = 'assignmentnew';
746 } else {
747 $subtype = 'assignmentold';
748 }
fcdcc372 749
8ba4cbd2 750 ///construct SQL, using current offset to find the data of the next student
751 $course = $this->course;
752 $assignment = $this->assignment;
753 $cm = $this->cm;
fcdcc372 754
8ba4cbd2 755 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
756 $currentgroup = setup_and_print_groups($course, $groupmode, 'submissions.php?id='.$this->cm->id);
757 } else {
758 $currentgroup = false;
759 }
fcdcc372 760
8ba4cbd2 761 /// Get all teachers and students
762 if ($currentgroup) {
763 $users = get_group_users($currentgroup);
764 } else {
765 $users = get_course_users($course->id);
766 }
fcdcc372 767
8ba4cbd2 768 $select = 'SELECT u.id, u.id, u.firstname, u.lastname, u.picture,'.
769 's.id AS submissionid, s.grade, s.submissioncomment, s.timemodified, s.timemarked, ((s.timemarked > 0) AND (s.timemarked >= s.timemodified)) AS status ';
770 $sql = 'FROM '.$CFG->prefix.'user u '.
771 'LEFT JOIN '.$CFG->prefix.'assignment_submissions s ON u.id = s.userid AND s.assignment = '.$this->assignment->id.' '.
772 'WHERE u.id IN ('.implode(',', array_keys($users)).') AND s.data1 <> "Draft" AND s.data1 <> ""';
773 //we don't need to grade draft or empty assignments
fcdcc372 774
8ba4cbd2 775 require_once($CFG->libdir.'/tablelib.php');
776 if ($sort = flexible_table::get_sql_sort('mod-assignment-submissions')) {
777 $sort = 'ORDER BY '.$sort.' ';
778 }
fcdcc372 779
8ba4cbd2 780 $limit = sql_paging_limit($offset+1, 1);
781 $nextid = 0;
782 if (($auser = get_record_sql($select.$sql.$sort.$limit, false, true)) !== false) {
783 $nextid = $auser->id;
784 }
785//1111
786//echo "$auser->id $auser->firstname $auser->lastname -next user <br /> $offset -- offset <br /> $userid -- present userid ";
787//echo $offset."-offset on display submission<br>";
788 print_header(get_string('feedback', 'assignment').':'.fullname($user, true).':'.format_string($this->assignment->name));
fcdcc372 789
8ba4cbd2 790 ///SOme javascript to help with setting up >.>
fcdcc372 791
8ba4cbd2 792 echo '<script type="text/javascript">'."\n";
793 echo 'function setNext(){'."\n";
794 echo 'document.submitform.mode.value=\'next\';'."\n";
795 echo 'document.submitform.userid.value="'.$nextid.'";'."\n";
796 echo '}'."\n";
fcdcc372 797
8ba4cbd2 798 echo 'function saveNext(){'."\n";
799 echo 'document.submitform.mode.value=\'saveandnext\';'."\n";
800 echo 'document.submitform.userid.value="'.$nextid.'";'."\n";
801 echo 'document.submitform.saveuserid.value="'.$userid.'";'."\n";
802 echo 'document.submitform.menuindex.value = document.submitform.grade.selectedIndex;'."\n";
803 echo '}'."\n";
fcdcc372 804
8ba4cbd2 805 echo '</script>'."\n";
fcdcc372 806
8ba4cbd2 807//+++
808 // Prints upload form for teachers to upload response file
809 $this->view_upload_response_form($userid,$offset);
810//+++
811 echo '<table border="0" align="center" cellpadding="5" cellspacing="1" class="feedback '.$subtype.'" >';
fcdcc372 812
8ba4cbd2 813 ///Start of teacher info row
fcdcc372 814
8ba4cbd2 815 echo '<tr>';
816 echo '<td width="35" valign="top" class="picture teacher">';
817 if ($submission->teacher) {
818 $teacher = get_record('user', 'id', $submission->teacher);
819 } else {
820 global $USER;
821 $teacher = $USER;
822 }
823 print_user_picture($teacher->id, $this->course->id, $teacher->picture);
824 echo '</td>';
825 echo '<td class="content">';
826 echo '<form name="submitform" action="submissions.php" method="post">';
827 echo '<input type="hidden" name="offset" value="'.++$offset.'">'; //was ++$offset!!!!!!!
828 echo '<input type="hidden" name="userid" value="'.$userid.'" />';
829 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
830 echo '<input type="hidden" name="mode" value="grade" />';
831 echo '<input type="hidden" name="menuindex" value="0" />';//selected menu index
832 //new hidden field, initialized to -1.
833 echo '<input type="hidden" name="saveuserid" value="-1" />';
834//++++++
835 // Shows teacher response files to teacher
836 echo "<div class=\"from\">". get_string('responsefile','assignment')."</div>";
837 echo $this->print_user_response_files($userid,true,--$offset);
838 echo "<br /><br />";
839////++++
840 if ($submission->timemarked) {
841 echo '<div class="from">';
842 echo '<div class="fullname">'.fullname($teacher, true).'</div>';
843 echo '<div class="time">'.userdate($submission->timemarked).'</div>';
fcdcc372 844 echo '</div>';
8ba4cbd2 845 }
846 echo '<div class="grade">'.get_string('grade').':';
847 choose_from_menu(make_grades_menu($this->assignment->grade), 'grade', $submission->grade, get_string('nograde'), '', -1);
848 echo '</div>';
849 echo '<div class="clearer"></div>';
fcdcc372 850
8ba4cbd2 851 $this->preprocess_submission($submission);
6fc73d59 852
8ba4cbd2 853 echo '<br />';
854 print_textarea($this->usehtmleditor, 14, 58, 0, 0, 'submissioncomment', $submission->submissioncomment, $this->course->id);
6fc73d59 855
8ba4cbd2 856 if ($this->usehtmleditor) {
857 echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
858 } else {
859 echo '<div align="right" class="format">';
860 choose_from_menu(format_text_menu(), "format", $submission->format, "");
861 helpbutton("textformat", get_string("helpformatting"));
6fc73d59 862 echo '</div>';
8ba4cbd2 863 }
fcdcc372 864
8ba4cbd2 865 ///Print Buttons in Single View
866 echo '<div class="buttons" align="center">';
867 echo '<input type="submit" name="submit" value="'.get_string('savechanges').'" onclick = "document.submitform.menuindex.value = document.submitform.grade.selectedIndex" />';
868 echo '<input type="submit" name="cancel" value="'.get_string('cancel').'" />';
869 //if there are more to be graded.
870 if ($nextid) {
871 echo '<input type="submit" name="saveandnext" value="'.get_string('saveandnext').'" onclick="saveNext()" />';
872 echo '<input type="submit" name="next" value="'.get_string('next').'" onclick="setNext();" />';
873 }
874 echo '</div>';
875 echo '</form>';
876 echo '</td></tr>';
877 ///End of teacher info row, Start of student info row
878 echo '<tr>';
879 echo '<td width="35" valign="top" class="picture user">';
880 print_user_picture($user->id, $this->course->id, $user->picture);
881 echo '</td>';
882 echo '<td class="topic">';
883 echo '<div class="from">';
884 echo '<div class="fullname">'.fullname($user, true).'</div>';
885 if ($submission->timemodified) {
886 echo '<div class="time">'.userdate($submission->timemodified).
887 $this->display_lateness($submission->timemodified).'</div>';
888 }
889 echo '</div>';
890 $this->print_user_files($user->id);
891 echo '</td>';
892 echo '</tr>';
fcdcc372 893
8ba4cbd2 894 ///End of student info row
fcdcc372 895
8ba4cbd2 896 echo '</table>';
fcdcc372 897
8ba4cbd2 898 if ($this->usehtmleditor) {
899 use_html_editor();
fcdcc372 900 }
8ba4cbd2 901
902 print_footer('none');
fcdcc372 903 }
904
8ba4cbd2 905 //from upload&review
906 // Prints response files to students
fcdcc372 907 function print_response_students($return) {
908 global $CFG, $USER;
8ba4cbd2 909
fcdcc372 910 $stuid = $USER->id;
911
912 echo '<table border="0" align="center" cellpadding="5" cellspacing="1" class="feedback">';
913
914 $submission = $this->get_submission($stuid);
8ba4cbd2 915 if ($teachids = $submission->data2) { // Only will show files if there is a submission
6fc73d59 916 $teachidarr = explode(',', $teachids);
8ba4cbd2 917
38ac07d2 918 foreach ($teachidarr as $t) {
919 if (! $teacher = get_record('user', 'id', $t)) {
920 print_object($submission);
8ba4cbd2 921 error('Could not find the teacher');
38ac07d2 922 }
923 echo '<tr>';
924 echo '<td class="left picture">';
925 print_user_picture($teacher->id, $this->course->id, $teacher->picture);
926 echo '</td>';
927 echo '<td class="topic">';
928 echo '<div class="from">';
8ba4cbd2 929 echo '<div class="fullname">'.fullname($teacher).'</div>';
38ac07d2 930 echo '</div>';
931
932 $filearea = $this->response_file_area_name($stuid, $t);
933 if ($basedir = $this->response_file_area($stuid, $t)) {
934 $output = '';
935 if ($files = get_directory_list($basedir)) {
936 foreach ($files as $key => $file) {
8ba4cbd2 937 require_once($CFG->libdir.'/filelib.php');
38ac07d2 938 $icon = mimeinfo('icon', $file);
939 if ($CFG->slasharguments) {
8ba4cbd2 940 $ffurl = "$CFG->wwwroot/mod/assignment/type/uploadreview/file.php/$filearea/$file";
941 } else {
942 $ffurl = "$CFG->wwwroot/mod/assignment/type/uploadreview/file.php?file=/$filearea/$file";
38ac07d2 943 }
8ba4cbd2 944 /*echo '<div class="files"><center><img align="middle" src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" />'.
945 link_to_popup_window ('/'.$ffurl, 'file'.$key, $file, 450, 580, $file, 'none', true).'</div></center><br />';
946 echo '</td></tr>';*/
947 //displays multiple teachers responces
948 $output .='<img align="middle" src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" />'.
949 link_to_popup_window ('/'.$ffurl, 'file'.$key, $file, 450, 580, $file, 'none', true)."<br />";
38ac07d2 950 }
951 }
952 }
8ba4cbd2 953 echo '<div class="files"><left>'.$output.'</left></div>';
954 echo '</td></tr>';
38ac07d2 955 }
8ba4cbd2 956 echo '</table>';
fcdcc372 957 }
958 }
959
960 //print teacher's files
8ba4cbd2 961 function print_user_response_files($stuid,$display_remove_button=false,$offset=NULL) {//, $return=false) {
fcdcc372 962
963 global $CFG, $USER;
8ba4cbd2 964
fcdcc372 965 $userid = $USER->id;
966
967 $filearea = $this->response_file_area_name($stuid);
8ba4cbd2 968
fcdcc372 969 $output = '';
8ba4cbd2 970
fcdcc372 971 if ($basedir = $this->response_file_area($stuid)) {
972 if ($files = get_directory_list($basedir)) {
973 require_once($CFG->libdir.'/filelib.php');
974 foreach ($files as $key => $file) {
8ba4cbd2 975
fcdcc372 976 $icon = mimeinfo('icon', $file);
977
8ba4cbd2 978 if ($CFG->slasharguments) {
fcdcc372 979 $ffurl = "file.php/$filearea/$file";
8ba4cbd2 980 } else {
fcdcc372 981 $ffurl = "file.php?file=/$filearea/$file";
982 }
8ba4cbd2 983
fcdcc372 984 //get feedback file size, generate and display remove file link
985 $filesize = display_size(filesize($basedir."/".$file));
8ba4cbd2 986 $remove_link='';
987 if ($display_remove_button) {
988 $course_mod_id=$this->cm->id;
989 $deleteurl="$CFG->wwwroot/mod/assignment/type/upload/deleteonesubmission.php?confirm=0&view=teacher&userid=$stuid&id=$course_mod_id&name=$file&file=".$basedir."/".$file."&offset=".$offset;
990 $remove_link='[<a href="'.$deleteurl.'">'.get_string("removelink", "assignment").'</a>]';
fcdcc372 991 }
992 $output .= '<img align="middle" src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" />'.
8ba4cbd2 993 // link_to_popup_window ('/'.$ffurl, 'file'.$key, $file, 450, 580, $file, 'none', true).
994 $file.' ['.$filesize.'] '.$remove_link.'<br />';
fcdcc372 995 }
996 }
997 }
998 $output = '<div class="files">'.$output.'</div>';
8ba4cbd2 999
fcdcc372 1000 return $output;
1001 }
1002
8ba4cbd2 1003 //print student's files.
1004 //if it's teacher view - teacher can view the files, but can't delete them
1005 //if it's student's view - stident can delete files, but can't view them
fcdcc372 1006 function print_user_files($userid=0, $return=false) {
1007 global $CFG, $USER;
1008
1009 if (!$userid) {
1010 if (!isloggedin()) {
1011 return '';
1012 }
1013 $userid = $USER->id;
1014 }
1015
1016 $filearea = $this->file_area_name($userid);
8ba4cbd2 1017
fcdcc372 1018 $output = '';
1019
1020 if ($basedir = $this->file_area($userid)) {
1021 if ($files = get_directory_list($basedir)) {
8ba4cbd2 1022
fcdcc372 1023 foreach ($files as $key => $file) {
8ba4cbd2 1024 require_once($CFG->libdir.'/filelib.php');
fcdcc372 1025
1026 $icon = mimeinfo('icon', $file);
1027 //get filesize for displaying
1028 $filesize = display_size(filesize($basedir."/".$file));
1029
1030 if ($CFG->slasharguments) {
1031 $ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
8ba4cbd2 1032 } else {
fcdcc372 1033 $ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
1034 }
8ba4cbd2 1035 if (has_capability('mod/assignment:grade', get_context_instance(CONTEXT_MODULE, $this->course->id))) {
1036 $output .= '<img align="middle" src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" />'.
1037 '<a href="'.$ffurl.'" >'.$file.'</a> ['.$filesize.'] <br />';
1038 } else {
1039 if (!empty($USER->id)) {
1040 if ($submission = $this->get_submission($USER->id)) {
1041 //i have changed timemodified=0 for Draft assignments, thats' why we remove this condition
1042 //otherwise student's dont' se etheir own submissions
1043// if ($submission->timemodified) {
61240489 1044 if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
8ba4cbd2 1045 //remove link shouldn't be displayed if file was marked or submited for marking
38ac07d2 1046 $remove_link = '';
61240489 1047 if ($submission->data1 == get_string("submissionstatusdraft", "assignment") || $submission->data1 == get_string("submissionstatusreturned", "assignment")) {
38ac07d2 1048 $course_mod_id=$this->cm->id;
8ba4cbd2 1049 $deleteurl="$CFG->wwwroot/mod/assignment/type/upload/deleteonesubmission.php?confirm=0&view=student&userid=$userid&id=$course_mod_id&name=$file&file=".$basedir."/".$file;
38ac07d2 1050 $remove_link= '[<a href="'.$deleteurl.'">'.get_string("removelink", "assignment").'</a>]'; //students of the course
8ba4cbd2 1051 }
1052 $output .= '<img align="middle" src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" />'.$file.' ['.$filesize.']'.$remove_link.'<br />';
1053 } else {
61240489 1054 $output .= '';
38ac07d2 1055 }
8ba4cbd2 1056 }
fcdcc372 1057 }
61240489 1058 }
fcdcc372 1059 }
1060 }
1061 }
1062
1063 $output = '<div class="files">'.$output.'</div>';
1064
1065 if ($return) {
1066 return $output;
1067 }
1068 echo $output;
1069 }
1070
1071 /*
8ba4cbd2 1072 * Display all the submissions ready for grading
1073 */
1074 //from lib.php
1075 //we needed so select more information from database and add upload_statuses display
fcdcc372 1076 function display_submissions() {
8ba4cbd2 1077 global $CFG, $db, $USER;
fcdcc372 1078
8ba4cbd2 1079 /* first we check to see if the form has just been submitted
1080 * to request user_preference updates
1081 */
1082 if (isset($_POST['updatepref'])) {
fcdcc372 1083 $perpage = optional_param('perpage', 10, PARAM_INT);
1084 $perpage = ($perpage <= 0) ? 10 : $perpage ;
1085 set_user_preference('assignment_perpage', $perpage);
1086 set_user_preference('assignment_quickgrade', optional_param('quickgrade',0, PARAM_BOOL));
1087 }
1088
8ba4cbd2 1089 /* next we get perpage and quickgrade (allow quick grade) params
1090 * from database
1091 */
fcdcc372 1092 $perpage = get_user_preferences('assignment_perpage', 10);
1093 $quickgrade = get_user_preferences('assignment_quickgrade', 0);
1094
8ba4cbd2 1095 $teacherattempts = true; /// Temporary measure
fcdcc372 1096 $page = optional_param('page', 0, PARAM_INT);
1097 $strsaveallfeedback = get_string('saveallfeedback', 'assignment');
1098
8ba4cbd2 1099 /// Some shortcuts to make the code read better
1100
fcdcc372 1101 $course = $this->course;
1102 $assignment = $this->assignment;
1103 $cm = $this->cm;
1104
8ba4cbd2 1105 $tabindex = 1; //tabindex for quick grading tabbing; Not working for dropdowns yet
fcdcc372 1106
1107 add_to_log($course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
1108
1109 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));
1110
1111 //change column name to upload_status to allow ordering by upload_statuses
8ba4cbd2 1112 $tablecolumns = array('picture', 'fullname', 'grade', 'submissioncomment', 'timemodified', 'timemarked', 'upload_status');
fcdcc372 1113 $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'));
1114
8ba4cbd2 1115 require_once($CFG->libdir.'/tablelib.php');
fcdcc372 1116 $table = new flexible_table('mod-assignment-submissions');
1117
1118 $table->define_columns($tablecolumns);
1119 $table->define_headers($tableheaders);
1120 $table->define_baseurl($CFG->wwwroot.'/mod/assignment/submissions.php?id='.$this->cm->id);
1121
1122 $table->sortable(true);
1123 $table->collapsible(true);
1124 $table->initialbars(true);
1125
1126 $table->column_suppress('picture');
1127 $table->column_suppress('fullname');
1128
1129 $table->column_class('picture', 'picture');
1130 $table->column_class('fullname', 'fullname');
1131 $table->column_class('grade', 'grade');
8ba4cbd2 1132 $table->column_class('submissioncomment', 'comment');
fcdcc372 1133 $table->column_class('timemodified', 'timemodified');
1134 $table->column_class('timemarked', 'timemarked');
1135 $table->column_class('status', 'status');
1136
1137 $table->set_attribute('cellspacing', '0');
1138 $table->set_attribute('id', 'attempts');
1139 $table->set_attribute('class', 'submissions');
1140 $table->set_attribute('width', '90%');
1141 $table->set_attribute('align', 'center');
1142
1143 // Start working -- this is necessary as soon as the niceties are over
1144 $table->setup();
1145
8ba4cbd2 1146 /// Check to see if groups are being used in this assignment
1147 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
fcdcc372 1148 $currentgroup = setup_and_print_groups($course, $groupmode, 'submissions.php?id='.$this->cm->id);
8ba4cbd2 1149 } else {
fcdcc372 1150 $currentgroup = false;
1151 }
1152
8ba4cbd2 1153 /// Get all teachers and students
fcdcc372 1154 if ($currentgroup) {
1155 $users = get_group_users($currentgroup);
8ba4cbd2 1156 } else {
61240489 1157 $users = get_course_users($course->id);
fcdcc372 1158 }
1159
1160 if (!$teacherattempts) {
61240489 1161 $teachers = get_course_teachers($course->id);
fcdcc372 1162 if (!empty($teachers)) {
1163 $keys = array_keys($teachers);
1164 }
1165 foreach ($keys as $key) {
1166 unset($users[$key]);
1167 }
1168 }
1169
8ba4cbd2 1170 if (empty($users)) {
fcdcc372 1171 print_heading(get_string('noattempts','assignment'));
1172 return true;
1173 }
1174
8ba4cbd2 1175 /// Construct the SQL
1176
1177 if ($where = $table->get_sql_where()) {
1178 $where .= ' AND ';
1179 }
1180
1181 if ($sort = $table->get_sql_sort()) {
1182 $sort = ' ORDER BY '.$sort;
1183 }
1184
1185 //select has been modified. because we also need to get student id & assignment status == upload_status = {Draft, Submitted, Marked, Returned}
1186 $select = 'SELECT u.id, u.id, u.firstname, u.lastname, u.picture, u.idnumber, s.id AS submissionid, s.grade, s.submissioncomment, s.timemodified, s.timemarked, ((s.timemarked > 0) AND (s.timemarked >= s.timemodified)) AS status, s.data1 as upload_status ';
1187
1188 // $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) AND (s.timemarked >= s.timemodified)) AS status ';
1189 $sql = 'FROM '.$CFG->prefix.'user u '.
1190 'LEFT JOIN '.$CFG->prefix.'assignment_submissions s ON u.id = s.userid AND s.assignment = '.$this->assignment->id.' '.
1191 'WHERE '.$where.'u.id IN ('.implode(',', array_keys($users)).') ';
1192
fcdcc372 1193 $table->pagesize($perpage, count($users));
1194
8ba4cbd2 1195 if($table->get_page_start() !== '' && $table->get_page_size() !== '') {
1196 $limit = ' '.sql_paging_limit($table->get_page_start(), $table->get_page_size());
1197 }
1198 else {
1199 $limit = '';
1200 }
fcdcc372 1201
8ba4cbd2 1202 ///offset used to calculate index of student in that particular query, needed for the pop up to know who's next
fcdcc372 1203 $offset = $page * $perpage;
1204
8ba4cbd2 1205 // we have changed the wording.
1206 //$strupdate = get_string('update');
1207 //$strgrade = get_string('grade');
1208 $grademenu = make_grades_menu($this->assignment->grade);
1209
1210 if (($ausers = get_records_sql($select.$sql.$sort.$limit)) !== false) {
fcdcc372 1211
1212 foreach ($ausers as $auser) {
1213 $picture = print_user_picture($auser->id, $course->id, $auser->picture, false, true);
1214
8ba4cbd2 1215 if (empty($auser->submissionid)){
fcdcc372 1216 $auser->grade = -1; //no submission yet
1217 }
1218
1219 //if there is no upload status, then display "blank"
38ac07d2 1220 if (empty($auser->upload_status)) {
fcdcc372 1221 $auser->upload_status=get_string("submissionstatusblank", "assignment");
38ac07d2 1222 }
1223 if (!empty($auser->submissionid)) {
8ba4cbd2 1224 ///Prints student answer and student modified date
1225 ///attach file or print link to student answer, depending on the type of the assignment.
1226 ///Refer to print_student_answer in inherited classes
1227 // do not display draft submissions to marker.
fcdcc372 1228 if ($auser->timemodified > 0 && $auser->upload_status != get_string("submissionstatusdraft", "assignment")) {
1229 $studentmodified = '<div id="ts'.$auser->id.'">'.$this->print_student_answer($auser->id).userdate($auser->timemodified).'</div>';
1230 } else {
1231 $studentmodified = '<div id="ts'.$auser->id.'">&nbsp;</div>';
1232 }
1233 ///Print grade, dropdown or text
1234 if ($auser->timemarked > 0) {
1235 //display teachers feedback files here as well
1236 $teachermodified = '<div id="tt'.$auser->id.'">'.$this->print_user_response_files($auser->id,false).userdate($auser->timemarked).'</div>';
1237 //disable grading ability in case of Blank or Draft assignment
8ba4cbd2 1238 if ($quickgrade) {//&& ($auser->upload_status != get_string("submissionstatusdraft", "assignment") || !$auser->upload_status )){// get_string("submissionstatusblank", "assignment"))){
fcdcc372 1239 $grade = '<div id="g'.$auser->id.'">'.choose_from_menu(make_grades_menu($this->assignment->grade),
1240 'menu['.$auser->id.']', $auser->grade, get_string('nograde'),'',-1,true,false,$tabindex++).'</div>';
1241 } else {
1242 $grade = '<div id="g'.$auser->id.'">'.$this->display_grade($auser->grade).'</div>';
1243 }
1244
1245 } else {
1246 $teachermodified = '<div id="tt'.$auser->id.'">&nbsp;</div>';
1247
8ba4cbd2 1248 if ($quickgrade && $auser->upload_status != get_string("submissionstatusdraft", "assignment") && $auser->upload_status != get_string("submissionstatusblank", "assignment")) {
fcdcc372 1249 $grade = '<div id="g'.$auser->id.'">'.choose_from_menu(make_grades_menu($this->assignment->grade),
1250 'menu['.$auser->id.']', $auser->grade, get_string('nograde'),'',-1,true,false,$tabindex++).'</div>';
1251 } else {
1252 $grade = '<div id="g'.$auser->id.'">'.$this->display_grade($auser->grade).'</div>';
1253 }
1254 }
1255 ///Print Comment
8ba4cbd2 1256 if ($quickgrade && $auser->upload_status != get_string("submissionstatusdraft", "assignment") && $auser->upload_status != get_string("submissionstatusblank", "assignment")) {
1257 $comment = '<div id="com'.$auser->id.'"><textarea tabindex="'.$tabindex++.'" name="submissioncomment['.$auser->id.']" id="submissioncomment['.$auser->id.']">'.($auser->submissioncomment).'</textarea></div>';
fcdcc372 1258 } else {
8ba4cbd2 1259 $comment = '<div id="com'.$auser->id.'">'.shorten_text(strip_tags($auser->submissioncomment),15).'</div>';
fcdcc372 1260 }
1261 } else {
1262 $studentmodified = '<div id="ts'.$auser->id.'">&nbsp;</div>';
1263 $teachermodified = '<div id="tt'.$auser->id.'">&nbsp;</div>';
1264 $status = '<div id="st'.$auser->id.'">&nbsp;</div>';
1265
8ba4cbd2 1266 if ($quickgrade && $auser->upload_status != get_string("submissionstatusdraft", "assignment") && $auser->upload_status != get_string("submissionstatusblank", "assignment")) { // allow editing
fcdcc372 1267 $grade = '<div id="g'.$auser->id.'">'.choose_from_menu(make_grades_menu($this->assignment->grade),
1268 'menu['.$auser->id.']', $auser->grade, get_string('nograde'),'',-1,true,false,$tabindex++).'</div>';
1269 } else {
1270 $grade = '<div id="g'.$auser->id.'">-</div>';
1271 }
1272 if ($quickgrade && $auser->upload_status != get_string("submissionstatusdraft", "assignment") && $auser->upload_status != get_string("submissionstatusblank", "assignment") ){
8ba4cbd2 1273 $comment = '<div id="com'.$auser->id.'"><textarea tabindex="'.$tabindex++.'" name="submissioncomment['.$auser->id.']" id="submissioncomment['.$auser->id.']">'.($auser->submissioncomment).'</textarea></div>';
fcdcc372 1274 } else {
1275 $comment = '<div id="com'.$auser->id.'">&nbsp;</div>';
1276 }
1277 }
1278
1279 //display upload statuses instead of old ones ={Grade, Update}
8ba4cbd2 1280 //if ($auser->status === NULL) {
1281 // $auser->status = 0;
1282 //}
fcdcc372 1283 $buttontext=$auser->upload_status;
8ba4cbd2 1284 //$buttontext = ($auser->status == 1) ? $strupdate : $strgrade;
fcdcc372 1285
8ba4cbd2 1286 //do not display link to the grading pop-up if upload_status={Blank, Draft}
fcdcc372 1287 if ($auser->upload_status == get_string("submissionstatusdraft", "assignment") || $auser->upload_status == get_string("submissionstatusblank", "assignment")){
1288 $button = $buttontext;
8ba4cbd2 1289 } else {
fcdcc372 1290 ///No more buttons, we use popups ;-).
1291 $button = link_to_popup_window ('/mod/assignment/submissions.php?id='.$this->cm->id.'&amp;userid='.$auser->id.'&amp;mode=single'.'&amp;offset='.$offset++,'grade'.$auser->id, $buttontext, 500, 780, $buttontext, 'none', true, 'button'.$auser->id);
1292 }
1293 //changed to $auser->upload_status
1294 $status = '<div id="up'.$auser->id.'" class="s'.$auser->upload_status.'">'.$button.'</div>';
8ba4cbd2 1295 $row = array($picture, $this->fullname($auser), $grade, $comment, $studentmodified, $teachermodified, $status);
fcdcc372 1296 $table->add_data($row);
1297 }
1298 }
1299
1300 /// Print quickgrade form around the table
8ba4cbd2 1301 if ($quickgrade) {
fcdcc372 1302 echo '<form action="submissions.php" name="fastg" method="post">';
1303 echo '<input type="hidden" name="id" value="'.$this->cm->id.'">';
1304 echo '<input type="hidden" name="mode" value="fastgrade">';
1305 echo '<input type="hidden" name="page" value="'.$page.'">';
1306 }
1307
1308 $table->print_html(); /// Print the whole table
1309
8ba4cbd2 1310 if ($quickgrade) {
fcdcc372 1311 echo '<p align="center"><input type="submit" name="fastg" value="'.get_string('saveallfeedback', 'assignment').'" /></p>';
1312 echo '</form>';
1313 }
1314 /// End of fast grading form
1315
1316 /// Mini form for setting user preference
1317 echo '<br />';
1318 echo '<form name="options" action="submissions.php?id='.$this->cm->id.'" method="post">';
1319 echo '<input type="hidden" id="updatepref" name="updatepref" value="1" />';
1320 echo '<table id="optiontable" align="center">';
1321 echo '<tr align="right"><td>';
1322 echo '<label for="perpage">'.get_string('pagesize','assignment').'</label>';
1323 echo ':</td>';
1324 echo '<td align="left">';
1325 echo '<input type="text" id="perpage" name="perpage" size="1" value="'.$perpage.'" />';
1326 helpbutton('pagesize', get_string('pagesize','assignment'), 'assignment');
1327 echo '</td></tr>';
1328 echo '<tr align="right">';
1329 echo '<td>';
1330 print_string('quickgrade','assignment');
1331 echo ':</td>';
1332 echo '<td align="left">';
8ba4cbd2 1333 if ($quickgrade) {
fcdcc372 1334 echo '<input type="checkbox" name="quickgrade" value="1" checked="checked" />';
1335 } else {
1336 echo '<input type="checkbox" name="quickgrade" value="1" />';
1337 }
1338 helpbutton('quickgrade', get_string('quickgrade', 'assignment'), 'assignment').'</p></div>';
1339 echo '</td></tr>';
1340 echo '<tr>';
1341 echo '<td colspan="2" align="right">';
1342 echo '<input type="submit" value="'.get_string('savepreferences').'" />';
1343 echo '</td></tr></table>';
1344 echo '</form>';
1345 ///End of mini form
1346 print_footer($this->course);
1347 }
1348
1349 //deletes submitted file (assignment or response)
8ba4cbd2 1350 function deleteonesubmission () {
1351 global $CFG, $USER;
1352
1353 require_once($CFG->libdir.'/filelib.php');
1354
1355 $id = required_param('id', PARAM_INT); // Course module ID
1356 $a = optional_param('a'); // Assignment ID
1357 $file = optional_param('file', '', PARAM_PATH);
1358 $userid = optional_param('userid');
1359 $confirm = optional_param('confirm');
1360 $name = optional_param('name');
1361 $offset = optional_param('offset');
1362 $view = optional_param('view'); //teacher or student view
1363
1364 $submission = $this->get_submission($USER->id);
1365
1366
1367 if ($view == 'teacher') {
1368 $yes_url = "$CFG->wwwroot/mod/assignment/type/upload/deleteonesubmission.php?confirm=1&view=teacher&userid=$userid&id=$id&name=$name&file=$file&offset=$offset";
fcdcc372 1369 $no_url = "../../submissions.php?userid=$userid&id=$id&mode=single&offset=$offset";
1370 $back_button = get_string("backtofeedback", "assignment");
1371 $action_url = '../../submissions.php';
8ba4cbd2 1372 } else {
1373 $yes_url = "$CFG->wwwroot/mod/assignment/type/upload/deleteonesubmission.php?confirm=1&view=student&userid=$userid&id=$id&name=$name&file=$file&offset=$offset";
fcdcc372 1374 $no_url = "../../view.php?id=$id&offset=$offset";
1375 $back_button = get_string("backtoassignment", "assignment");
1376 $action_url = '../../view.php';
8ba4cbd2 1377 }
1378
1379 if ($view == 'student') {
1380 $this->view_header();
1381 }
1382
1383 if (!empty($confirm)) {
1384
1385 if (!fulldelete($file)) {
1386 notify(get_string("deletefail", "assignment"));
1387 notify($file);
1388 } else {
1389 //if student deletes submitted files then numfiles should be changed
1390 if ($view == 'student'){
1391 $submission->numfiles --;
1392 if (update_record("assignment_submissions", $submission)) {
1393 notify(get_string("deleteednotification", "assignment"));
1394 } else {
1395 notify(get_string("deletefail", "assignment"));
1396 notify($file);
38ac07d2 1397 }
8ba4cbd2 1398 } else {
1399 notify(get_string("deleteednotification", "assignment"));
1400 }
1401
fcdcc372 1402 }
8ba4cbd2 1403/* echo '<form name="submitform" action="submissions.php" method="post">';
1404 echo '<input type="hidden" name="offset" value="'.++$offset.'">';
1405 echo '<input type="hidden" name="userid" value="'.$userid.'" />';
1406 echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
1407 echo '<input type="hidden" name="mode" value="grade" />';
1408 echo '<input type="hidden" name="menuindex" value="0" />';//selected menu index
1409 //new hidden field, initialized to -1.
1410 echo '<input type="hidden" name="saveuserid" value="-1" />';
1411*/
38ac07d2 1412 echo "<form action=\"".$action_url."\">";
1413 echo '<input type="hidden" name="offset" value="'.$offset.'">';
1414 echo "<input type=\"hidden\" value=\"$userid\" name=\"userid\">";
1415 echo "<input type=\"hidden\" value=\"$id\" name=\"id\">";
8ba4cbd2 1416 //echo "<input type=\"hidden\" value=\"$a\" name=\"a\">";
38ac07d2 1417 echo "<input type=\"hidden\" value=\"single\" name=\"mode\">";
1418 echo "<center><input type=\"submit\" value=\"".$back_button."\" name=\"submit\"></center></form>";
8ba4cbd2 1419
1420 } else {
1421 notify (get_string("namedeletefile", "assignment"));
1422 notify($name);
1423 notice_yesno (get_string("deletecheckfile", "assignment"), $yes_url, $no_url);
1424 }
1425
1426 if ($view == 'student') $this->view_footer();
1427
1428 }
1429
1430 //from moodlelib.php
1431 //we need to dispaly studentID along with student name in a grading interface
1432 function fullname($user, $override=false) {
1433
1434 global $CFG, $SESSION;
1435
1436 $user_id='';
1437 if ($user->idnumber) {
1438 $user_id = ' ('. $user->idnumber .') ';
1439 }
1440
1441 if (!isset($user->firstname) and !isset($user->lastname)) {
1442 return '';
1443 }
1444
1445 if (!$override) {
1446 if (!empty($CFG->forcefirstname)) {
1447 $user->firstname = $CFG->forcefirstname;
1448 }
1449 if (!empty($CFG->forcelastname)) {
1450 $user->lastname = $CFG->forcelastname;
38ac07d2 1451 }
fcdcc372 1452 }
8ba4cbd2 1453
1454 if (!empty($SESSION->fullnamedisplay)) {
1455 $CFG->fullnamedisplay = $SESSION->fullnamedisplay;
38ac07d2 1456 }
fcdcc372 1457
8ba4cbd2 1458 if ($CFG->fullnamedisplay == 'firstname lastname') {
1459 return $user->firstname .' '. $user->lastname . $user_id;
1460
1461 } else if ($CFG->fullnamedisplay == 'lastname firstname') {
1462 return $user->lastname .' '. $user->firstname . $user_id;
1463
1464 } else if ($CFG->fullnamedisplay == 'firstname') {
1465 if ($override) {
1466 return get_string('fullnamedisplay', '', $user);
1467 } else {
1468 return $user->firstname . $user_id;;
1469 }
1470 } else if ($CFG->fullnamedisplay == 'textuid') {
1471 if ( $override ) {
1472 return get_string('fullnamedisplay', '', $user) . $user_id;
1473 } else if (isset($user->username)) {
1474 return $user->username . $user_id;
1475 } else {
1476 return $user->firstname . $user_id;
1477 }
fcdcc372 1478 }
fcdcc372 1479
8ba4cbd2 1480 return get_string('fullnamedisplay', '', $user) . $user_id;
38ac07d2 1481 }
8ba4cbd2 1482
fcdcc372 1483}
8ba4cbd2 1484
fcdcc372 1485?>