[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/assignment/type/upload/ -> assignment.class.php (source)

   1  <?php // $Id: assignment.class.php,v 1.32.2.15 2008/10/09 11:22:14 poltawski Exp $
   2  require_once($CFG->libdir.'/formslib.php');
   3  
   4  define('ASSIGNMENT_STATUS_SUBMITTED', 'submitted'); // student thinks it is finished
   5  define('ASSIGNMENT_STATUS_CLOSED', 'closed');       // teacher prevents more submissions
   6  
   7  /**
   8   * Extend the base assignment class for assignments where you upload a single file
   9   *
  10   */
  11  class assignment_upload extends assignment_base {
  12  
  13      function assignment_upload($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
  14          parent::assignment_base($cmid, $assignment, $cm, $course);
  15          $this->type = 'upload';
  16      }
  17  
  18      function view() {
  19          global $USER;
  20  
  21          require_capability('mod/assignment:view', $this->context);
  22  
  23          add_to_log($this->course->id, 'assignment', 'view', "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
  24  
  25          $this->view_header();
  26  
  27          if ($this->assignment->timeavailable > time()
  28            and !has_capability('mod/assignment:grade', $this->context)      // grading user can see it anytime
  29            and $this->assignment->var3) {                                   // force hiding before available date
  30              print_simple_box_start('center', '', '', 0, 'generalbox', 'intro');
  31              print_string('notavailableyet', 'assignment');
  32              print_simple_box_end();
  33          } else {
  34              $this->view_intro();
  35          }
  36  
  37          $this->view_dates();
  38  
  39          if (has_capability('mod/assignment:submit', $this->context)) {
  40              $filecount = $this->count_user_files($USER->id);
  41              $submission = $this->get_submission($USER->id);
  42  
  43              $this->view_feedback();
  44  
  45              if (!$this->drafts_tracked() or !$this->isopen() or $this->is_finalized($submission)) {
  46                  print_heading(get_string('submission', 'assignment'), '', 3);
  47              } else {
  48                  print_heading(get_string('submissiondraft', 'assignment'), '', 3);
  49              }
  50  
  51              if ($filecount and $submission) {
  52                  print_simple_box($this->print_user_files($USER->id, true), 'center');
  53              } else {
  54                  if (!$this->isopen() or $this->is_finalized($submission)) {
  55                      print_simple_box(get_string('nofiles', 'assignment'), 'center');
  56                  } else {
  57                      print_simple_box(get_string('nofilesyet', 'assignment'), 'center');
  58                  }
  59              }
  60  
  61              $this->view_upload_form();
  62  
  63              if ($this->notes_allowed()) {
  64                  print_heading(get_string('notes', 'assignment'), '', 3);
  65                  $this->view_notes();
  66              }
  67  
  68              $this->view_final_submission();
  69          }
  70          $this->view_footer();
  71      }
  72  
  73  
  74      function view_feedback($submission=NULL) {
  75          global $USER, $CFG;
  76          require_once($CFG->libdir.'/gradelib.php');
  77  
  78          if (!$submission) { /// Get submission for this assignment
  79              $submission = $this->get_submission($USER->id);
  80          }
  81  
  82          if (empty($submission->timemarked)) {   /// Nothing to show, so print nothing
  83              if ($this->count_responsefiles($USER->id)) {
  84                  print_heading(get_string('responsefiles', 'assignment', $this->course->teacher), '', 3);
  85                  $responsefiles = $this->print_responsefiles($USER->id, true);
  86                  print_simple_box($responsefiles, 'center');
  87              }
  88              return;
  89          }
  90  
  91          $grading_info = grade_get_grades($this->course->id, 'mod', 'assignment', $this->assignment->id, $USER->id);
  92          $item = $grading_info->items[0];
  93          $grade = $item->grades[$USER->id];
  94  
  95          if ($grade->hidden or $grade->grade === false) { // hidden or error
  96              return;
  97          }
  98  
  99          if ($grade->grade === null and empty($grade->str_feedback)) {   /// Nothing to show yet
 100              return;
 101          }
 102  
 103          $graded_date = $grade->dategraded;
 104          $graded_by   = $grade->usermodified;
 105  
 106      /// We need the teacher info
 107          if (!$teacher = get_record('user', 'id', $graded_by)) {
 108              error('Could not find the teacher');
 109          }
 110  
 111      /// Print the feedback
 112          print_heading(get_string('submissionfeedback', 'assignment'), '', 3);
 113  
 114          echo '<table cellspacing="0" class="feedback">';
 115  
 116          echo '<tr>';
 117          echo '<td class="left picture">';
 118          print_user_picture($teacher, $this->course->id, $teacher->picture);
 119          echo '</td>';
 120          echo '<td class="topic">';
 121          echo '<div class="from">';
 122          echo '<div class="fullname">'.fullname($teacher).'</div>';
 123          echo '<div class="time">'.userdate($graded_date).'</div>';
 124          echo '</div>';
 125          echo '</td>';
 126          echo '</tr>';
 127  
 128          echo '<tr>';
 129          echo '<td class="left side">&nbsp;</td>';
 130          echo '<td class="content">';
 131          if ($this->assignment->grade) {
 132              echo '<div class="grade">';
 133              echo get_string("grade").': '.$grade->str_long_grade;
 134              echo '</div>';
 135              echo '<div class="clearer"></div>';
 136          }
 137  
 138          echo '<div class="comment">';
 139          echo $grade->str_feedback;
 140          echo '</div>';
 141          echo '</tr>';
 142  
 143          echo '<tr>';
 144          echo '<td class="left side">&nbsp;</td>';
 145          echo '<td class="content">';
 146          echo $this->print_responsefiles($USER->id, true);
 147          echo '</tr>';
 148  
 149          echo '</table>';
 150      }
 151  
 152  
 153      function view_upload_form() {
 154          global $CFG, $USER;
 155  
 156          $submission = $this->get_submission($USER->id);
 157  
 158          $struploadafile = get_string('uploadafile');
 159          $maxbytes = $this->assignment->maxbytes == 0 ? $this->course->maxbytes : $this->assignment->maxbytes;
 160          $strmaxsize = get_string('maxsize', '', display_size($maxbytes));
 161  
 162          if ($this->is_finalized($submission)) {
 163              // no uploading
 164              return;
 165          }
 166  
 167          if ($this->can_upload_file($submission)) {
 168              echo '<div style="text-align:center">';
 169              echo '<form enctype="multipart/form-data" method="post" action="upload.php">';
 170              echo '<fieldset class="invisiblefieldset">';
 171              echo "<p>$struploadafile ($strmaxsize)</p>";
 172              echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
 173              echo '<input type="hidden" name="action" value="uploadfile" />';
 174              require_once($CFG->libdir.'/uploadlib.php');
 175              upload_print_form_fragment(1,array('newfile'),null,false,null,0,$this->assignment->maxbytes,false);
 176              echo '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
 177              echo '</fieldset>';
 178              echo '</form>';
 179              echo '</div>';
 180              echo '<br />';
 181          }
 182  
 183      }
 184  
 185      function view_notes() {
 186          global $USER;
 187  
 188          if ($submission = $this->get_submission($USER->id)
 189            and !empty($submission->data1)) {
 190              print_simple_box(format_text($submission->data1, FORMAT_HTML), 'center', '630px');
 191          } else {
 192              print_simple_box(get_string('notesempty', 'assignment'), 'center');
 193          }
 194          if ($this->can_update_notes($submission)) {
 195              $options = array ('id'=>$this->cm->id, 'action'=>'editnotes');
 196              echo '<div style="text-align:center">';
 197              print_single_button('upload.php', $options, get_string('edit'), 'post', '_self', false);
 198              echo '</div>';
 199          }
 200      }
 201  
 202      function view_final_submission() {
 203          global $CFG, $USER;
 204  
 205          $submission = $this->get_submission($USER->id);
 206  
 207          if ($this->isopen() and $this->can_finalize($submission)) {
 208              //print final submit button
 209              print_heading(get_string('submitformarking','assignment'), '', 3);
 210              echo '<div style="text-align:center">';
 211              echo '<form method="post" action="upload.php">';
 212              echo '<fieldset class="invisiblefieldset">';
 213              echo '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
 214              echo '<input type="hidden" name="action" value="finalize" />';
 215              echo '<input type="submit" name="formarking" value="'.get_string('sendformarking', 'assignment').'" />';
 216              echo '</fieldset>';
 217              echo '</form>';
 218              echo '</div>';
 219          } else if (!$this->isopen()) {
 220              print_heading(get_string('nomoresubmissions','assignment'), '', 3);
 221  
 222          } else if ($this->drafts_tracked() and $state = $this->is_finalized($submission)) {
 223              if ($state == ASSIGNMENT_STATUS_SUBMITTED) {
 224                  print_heading(get_string('submitedformarking','assignment'), '', 3);
 225              } else {
 226                  print_heading(get_string('nomoresubmissions','assignment'), '', 3);
 227              }
 228          } else {
 229              //no submission yet
 230          }
 231      }
 232  
 233  
 234      /**
 235       * Return true if var3 == hide description till available day
 236       *
 237       *@return boolean
 238       */
 239      function description_is_hidden() {
 240          return ($this->assignment->var3 && (time() <= $this->assignment->timeavailable));
 241      }
 242  
 243      function custom_feedbackform($submission, $return=false) {
 244          global $CFG;
 245  
 246          $mode         = optional_param('mode', '', PARAM_ALPHA);
 247          $offset       = optional_param('offset', 0, PARAM_INT);
 248          $forcerefresh = optional_param('forcerefresh', 0, PARAM_BOOL);
 249  
 250          $output = get_string('responsefiles', 'assignment').': ';
 251  
 252          $output .= '<form enctype="multipart/form-data" method="post" '.
 253               "action=\"$CFG->wwwroot/mod/assignment/upload.php\">";
 254          $output .= '<div>';
 255          $output .= '<input type="hidden" name="id" value="'.$this->cm->id.'" />';
 256          $output .= '<input type="hidden" name="action" value="uploadresponse" />';
 257          $output .= '<input type="hidden" name="mode" value="'.$mode.'" />';
 258          $output .= '<input type="hidden" name="offset" value="'.$offset.'" />';
 259          $output .= '<input type="hidden" name="userid" value="'.$submission->userid.'" />';
 260          require_once($CFG->libdir.'/uploadlib.php');
 261          $output .= upload_print_form_fragment(1,array('newfile'),null,false,null,0,0,true);
 262          $output .= '<input type="submit" name="save" value="'.get_string('uploadthisfile').'" />';
 263          $output .= '</div>';
 264          $output .= '</form>';
 265  
 266          if ($forcerefresh) {
 267              $output .= $this->update_main_listing($submission);
 268          }
 269  
 270          $responsefiles = $this->print_responsefiles($submission->userid, true);
 271          if (!empty($responsefiles)) {
 272              $output .= $responsefiles;
 273          }
 274  
 275          if ($return) {
 276              return $output;
 277          }
 278          echo $output;
 279          return;
 280      }
 281  
 282  
 283      function print_student_answer($userid, $return=false){
 284          global $CFG;
 285  
 286          $filearea = $this->file_area_name($userid);
 287          $submission = $this->get_submission($userid);
 288  
 289          $output = '';
 290  
 291          if ($basedir = $this->file_area($userid)) {
 292              if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission)) {
 293                  $output .= '<strong>'.get_string('draft', 'assignment').':</strong> ';
 294              }
 295  
 296              if ($this->notes_allowed() and !empty($submission->data1)) {
 297                  $output .= link_to_popup_window ('/mod/assignment/type/upload/notes.php?id='.$this->cm->id.'&amp;userid='.$userid,
 298                                                  'notes'.$userid, get_string('notes', 'assignment'), 500, 780, get_string('notes', 'assignment'), 'none', true, 'notesbutton'.$userid);
 299                  $output .= '&nbsp;';
 300              }
 301  
 302              if ($files = get_directory_list($basedir, 'responses')) {
 303                  require_once($CFG->libdir.'/filelib.php');
 304                  foreach ($files as $key => $file) {
 305                      $icon = mimeinfo('icon', $file);
 306                      $ffurl = get_file_url("$filearea/$file");
 307                      $output .= '<a href="'.$ffurl.'" ><img class="icon" src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.$file.'</a>&nbsp;';
 308                  }
 309              }
 310          }
 311          $output = '<div class="files">'.$output.'</div>';
 312          $output .= '<br />';
 313  
 314          return $output;
 315      }
 316  
 317  
 318      /**
 319       * Produces a list of links to the files uploaded by a user
 320       *
 321       * @param $userid int optional id of the user. If 0 then $USER->id is used.
 322       * @param $return boolean optional defaults to false. If true the list is returned rather than printed
 323       * @return string optional
 324       */
 325      function print_user_files($userid=0, $return=false) {
 326          global $CFG, $USER;
 327  
 328          $mode    = optional_param('mode', '', PARAM_ALPHA);
 329          $offset  = optional_param('offset', 0, PARAM_INT);
 330  
 331          if (!$userid) {
 332              if (!isloggedin()) {
 333                  return '';
 334              }
 335              $userid = $USER->id;
 336          }
 337  
 338          $filearea = $this->file_area_name($userid);
 339  
 340          $output = '';
 341  
 342          $submission = $this->get_submission($userid);
 343  
 344          $candelete = $this->can_delete_files($submission);
 345          $strdelete   = get_string('delete');
 346  
 347          if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) {                 // only during grading
 348              $output .= '<strong>'.get_string('draft', 'assignment').':</strong><br />';
 349          }
 350  
 351          if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading
 352  
 353              $npurl = $CFG->wwwroot."/mod/assignment/type/upload/notes.php?id={$this->cm->id}&amp;userid=$userid&amp;offset=$offset&amp;mode=single";
 354              $output .= '<a href="'.$npurl.'">'.get_string('notes', 'assignment').'</a><br />';
 355  
 356          }
 357  
 358          if ($basedir = $this->file_area($userid)) {
 359              if ($files = get_directory_list($basedir, 'responses')) {
 360                  require_once($CFG->libdir.'/filelib.php');
 361                  foreach ($files as $key => $file) {
 362  
 363                      $icon = mimeinfo('icon', $file);
 364                      $ffurl = get_file_url("$filearea/$file");
 365  
 366                      $output .= '<a href="'.$ffurl.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.$file.'</a>';
 367  
 368                      if ($candelete) {
 369                          $delurl  = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=$file&amp;userid={$submission->userid}&amp;mode=$mode&amp;offset=$offset";
 370  
 371                          $output .= '<a href="'.$delurl.'">&nbsp;'
 372                                    .'<img title="'.$strdelete.'" src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="" /></a> ';
 373                      }
 374  
 375                      $output .= '<br />';
 376                  }
 377              }
 378          }
 379  
 380          if ($this->drafts_tracked() and $this->isopen() and has_capability('mod/assignment:grade', $this->context) and $mode != '') { // we do not want it on view.php page
 381              if ($this->can_unfinalize($submission)) {
 382                  $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'unfinalize', 'mode'=>$mode, 'offset'=>$offset);
 383                  $output .= print_single_button('upload.php', $options, get_string('unfinalize', 'assignment'), 'post', '_self', true);
 384              } else if ($this->can_finalize($submission)) {
 385                  $options = array ('id'=>$this->cm->id, 'userid'=>$userid, 'action'=>'finalizeclose', 'mode'=>$mode, 'offset'=>$offset);
 386                  $output .= print_single_button('upload.php', $options, get_string('finalize', 'assignment'), 'post', '_self', true);
 387              }
 388          }
 389  
 390          $output = '<div class="files">'.$output.'</div>';
 391  
 392          if ($return) {
 393              return $output;
 394          }
 395          echo $output;
 396      }
 397  
 398      function print_responsefiles($userid, $return=false) {
 399          global $CFG, $USER;
 400  
 401          $mode    = optional_param('mode', '', PARAM_ALPHA);
 402          $offset  = optional_param('offset', 0, PARAM_INT);
 403  
 404          $filearea = $this->file_area_name($userid).'/responses';
 405  
 406          $output = '';
 407  
 408          $candelete = $this->can_manage_responsefiles();
 409          $strdelete   = get_string('delete');
 410  
 411          if ($basedir = $this->file_area($userid)) {
 412              $basedir .= '/responses';
 413  
 414              if ($files = get_directory_list($basedir)) {
 415                  require_once($CFG->libdir.'/filelib.php');
 416                  foreach ($files as $key => $file) {
 417  
 418                      $icon = mimeinfo('icon', $file);
 419  
 420                      $ffurl = get_file_url("$filearea/$file");
 421  
 422                      $output .= '<a href="'.$ffurl.'" ><img src="'.$CFG->pixpath.'/f/'.$icon.'" alt="'.$icon.'" />'.$file.'</a>';
 423  
 424                      if ($candelete) {
 425                          $delurl  = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=$file&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;action=response";
 426  
 427                          $output .= '<a href="'.$delurl.'">&nbsp;'
 428                                    .'<img title="'.$strdelete.'" src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt=""/></a> ';
 429                      }
 430  
 431                      $output .= '&nbsp;';
 432                  }
 433              }
 434  
 435  
 436              $output = '<div class="responsefiles">'.$output.'</div>';
 437  
 438          }
 439  
 440          if ($return) {
 441              return $output;
 442          }
 443          echo $output;
 444      }
 445  
 446  
 447      function upload() {
 448          $action = required_param('action', PARAM_ALPHA);
 449  
 450          switch ($action) {
 451              case 'finalize':
 452                  $this->finalize();
 453                  break;
 454              case 'finalizeclose':
 455                  $this->finalizeclose();
 456                  break;
 457              case 'unfinalize':
 458                  $this->unfinalize();
 459                  break;
 460              case 'uploadresponse':
 461                  $this->upload_responsefile();
 462                  break;
 463              case 'uploadfile':
 464                  $this->upload_file();
 465              case 'savenotes':
 466              case 'editnotes':
 467                  $this->upload_notes();
 468              default:
 469                  error('Error: Unknow upload action ('.$action.').');
 470          }
 471      }
 472  
 473      function upload_notes() {
 474          global $CFG, $USER;
 475  
 476          $action = required_param('action', PARAM_ALPHA);
 477  
 478          $returnurl = 'view.php?id='.$this->cm->id;
 479  
 480          $mform = new mod_assignment_upload_notes_form();
 481  
 482          $defaults = new object();
 483          $defaults->id = $this->cm->id;
 484  
 485          if ($submission = $this->get_submission($USER->id)) {
 486              $defaults->text = $submission->data1;
 487          } else {
 488              $defaults->text = '';
 489          }
 490  
 491          $mform->set_data($defaults);
 492  
 493          if ($mform->is_cancelled()) {
 494              redirect('view.php?id='.$this->cm->id);
 495          }
 496  
 497          if (!$this->can_update_notes($submission)) {
 498              $this->view_header(get_string('upload'));
 499              notify(get_string('uploaderror', 'assignment'));
 500              print_continue($returnurl);
 501              $this->view_footer();
 502              die;
 503          }
 504  
 505          if ($data = $mform->get_data() and $action == 'savenotes') {
 506              $submission = $this->get_submission($USER->id, true); // get or create submission
 507              $updated = new object();
 508              $updated->id           = $submission->id;
 509              $updated->timemodified = time();
 510              $updated->data1        = $data->text;
 511  
 512              if (update_record('assignment_submissions', $updated)) {
 513                  add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
 514                  redirect($returnurl);
 515                  $submission = $this->get_submission($USER->id);
 516                  $this->update_grade($submission);
 517  
 518              } else {
 519                  $this->view_header(get_string('notes', 'assignment'));
 520                  notify(get_string('notesupdateerror', 'assignment'));
 521                  print_continue($returnurl);
 522                  $this->view_footer();
 523                  die;
 524              }
 525          }
 526  
 527          /// show notes edit form
 528          $this->view_header(get_string('notes', 'assignment'));
 529  
 530          print_heading(get_string('notes', 'assignment'), '');
 531  
 532          $mform->display();
 533  
 534          $this->view_footer();
 535          die;
 536      }
 537  
 538      function upload_responsefile() {
 539          global $CFG;
 540  
 541          $userid = required_param('userid', PARAM_INT);
 542          $mode   = required_param('mode', PARAM_ALPHA);
 543          $offset = required_param('offset', PARAM_INT);
 544  
 545          $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
 546  
 547          if (data_submitted('nomatch') and $this->can_manage_responsefiles()) {
 548              $dir = $this->file_area_name($userid).'/responses';
 549              check_dir_exists($CFG->dataroot.'/'.$dir, true, true);
 550  
 551              require_once($CFG->dirroot.'/lib/uploadlib.php');
 552              $um = new upload_manager('newfile',false,true,$this->course,false,0,true);
 553  
 554              if (!$um->process_file_uploads($dir)) {
 555                  print_header(get_string('upload'));
 556                  notify(get_string('uploaderror', 'assignment'));
 557                  echo $um->get_errors();
 558                  print_continue($returnurl);
 559                  print_footer('none');
 560                  die;
 561              }
 562          }
 563          redirect($returnurl);
 564      }
 565  
 566      function upload_file() {
 567          global $CFG, $USER;
 568  
 569          $mode   = optional_param('mode', '', PARAM_ALPHA);
 570          $offset = optional_param('offset', 0, PARAM_INT);
 571  
 572          $returnurl = 'view.php?id='.$this->cm->id;
 573  
 574          $filecount = $this->count_user_files($USER->id);
 575          $submission = $this->get_submission($USER->id);
 576  
 577          if (!$this->can_upload_file($submission)) {
 578              $this->view_header(get_string('upload'));
 579              notify(get_string('uploaderror', 'assignment'));
 580              print_continue($returnurl);
 581              $this->view_footer();
 582              die;
 583          }
 584  
 585          $dir = $this->file_area_name($USER->id);
 586          check_dir_exists($CFG->dataroot.'/'.$dir, true, true); // better to create now so that student submissions do not block it later
 587  
 588          require_once($CFG->dirroot.'/lib/uploadlib.php');
 589          $um = new upload_manager('newfile',false,true,$this->course,false,$this->assignment->maxbytes,true);
 590  
 591          if ($um->process_file_uploads($dir)) {
 592              $submission = $this->get_submission($USER->id, true); //create new submission if needed
 593              $updated = new object();
 594              $updated->id           = $submission->id;
 595              $updated->timemodified = time();
 596  
 597              if (update_record('assignment_submissions', $updated)) {
 598                  add_to_log($this->course->id, 'assignment', 'upload',
 599                          'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
 600                  $submission = $this->get_submission($USER->id);
 601                  $this->update_grade($submission);
 602                  if (!$this->drafts_tracked()) {
 603                      $this->email_teachers($submission);
 604                  }
 605              } else {
 606                  $new_filename = $um->get_new_filename();
 607                  $this->view_header(get_string('upload'));
 608                  notify(get_string('uploadnotregistered', 'assignment', $new_filename));
 609                  print_continue($returnurl);
 610                  $this->view_footer();
 611                  die;
 612              }
 613              redirect('view.php?id='.$this->cm->id);
 614          }
 615          $this->view_header(get_string('upload'));
 616          notify(get_string('uploaderror', 'assignment'));
 617          echo $um->get_errors();
 618          print_continue($returnurl);
 619          $this->view_footer();
 620          die;
 621      }
 622  
 623      function finalize() {
 624          global $USER;
 625  
 626          $confirm    = optional_param('confirm', 0, PARAM_BOOL);
 627          $returnurl  = 'view.php?id='.$this->cm->id;
 628          $submission = $this->get_submission($USER->id);
 629  
 630          if (!$this->can_finalize($submission)) {
 631              redirect($returnurl); // probably already graded, redirect to assignment page, the reason should be obvious
 632          }
 633  
 634          if (!data_submitted() or !$confirm) {
 635              $optionsno = array('id'=>$this->cm->id);
 636              $optionsyes = array ('id'=>$this->cm->id, 'confirm'=>1, 'action'=>'finalize');
 637              $this->view_header(get_string('submitformarking', 'assignment'));
 638              print_heading(get_string('submitformarking', 'assignment'));
 639              notice_yesno(get_string('onceassignmentsent', 'assignment'), 'upload.php', 'view.php', $optionsyes, $optionsno, 'post', 'get');
 640              $this->view_footer();
 641              die;
 642  
 643          }
 644          $updated = new object();
 645          $updated->id           = $submission->id;
 646          $updated->data2        = ASSIGNMENT_STATUS_SUBMITTED;
 647          $updated->timemodified = time();
 648  
 649          if (update_record('assignment_submissions', $updated)) {
 650              add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
 651                      'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
 652              $submission = $this->get_submission($USER->id);
 653              $this->update_grade($submission);
 654              $this->email_teachers($submission);
 655          } else {
 656              $this->view_header(get_string('submitformarking', 'assignment'));
 657              notify(get_string('finalizeerror', 'assignment'));
 658              print_continue($returnurl);
 659              $this->view_footer();
 660              die;
 661          }
 662          redirect($returnurl);
 663      }
 664  
 665      function finalizeclose() {
 666          $userid    = optional_param('userid', 0, PARAM_INT);
 667          $mode      = required_param('mode', PARAM_ALPHA);
 668          $offset    = required_param('offset', PARAM_INT);
 669          $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
 670  
 671          // create but do not add student submission date
 672          $submission = $this->get_submission($userid, true, true);
 673  
 674          if (!data_submitted() or !$this->can_finalize($submission)) {
 675              redirect($returnurl); // probably closed already
 676          }
 677  
 678          $updated = new object();
 679          $updated->id    = $submission->id;
 680          $updated->data2 = ASSIGNMENT_STATUS_CLOSED;
 681  
 682          if (update_record('assignment_submissions', $updated)) {
 683              add_to_log($this->course->id, 'assignment', 'upload', //TODO: add finalize action to log
 684                      'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
 685              $submission = $this->get_submission($userid, false, true);
 686              $this->update_grade($submission);
 687          }
 688          redirect($returnurl);
 689      }
 690  
 691      function unfinalize() {
 692  
 693          $userid = required_param('userid', PARAM_INT);
 694          $mode   = required_param('mode', PARAM_ALPHA);
 695          $offset = required_param('offset', PARAM_INT);
 696  
 697          $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset&amp;forcerefresh=1";
 698  
 699          if (data_submitted('nomatch')
 700            and $submission = $this->get_submission($userid)
 701            and $this->can_unfinalize($submission)) {
 702  
 703              $updated = new object();
 704              $updated->id = $submission->id;
 705              $updated->data2 = '';
 706              if (update_record('assignment_submissions', $updated)) {
 707                  //TODO: add unfinalize action to log
 708                  add_to_log($this->course->id, 'assignment', 'view submission', 'submissions.php?id='.$this->assignment->id, $this->assignment->id, $this->cm->id);
 709                  $submission = $this->get_submission($userid);
 710                  $this->update_grade($submission);
 711              } else {
 712                  $this->view_header(get_string('submitformarking', 'assignment'));
 713                  notify(get_string('unfinalizeerror', 'assignment'));
 714                  print_continue($returnurl);
 715                  $this->view_footer();
 716                  die;
 717              }
 718          }
 719          redirect($returnurl);
 720      }
 721  
 722  
 723      function delete() {
 724          $action   = optional_param('action', '', PARAM_ALPHA);
 725  
 726          switch ($action) {
 727              case 'response':
 728                  $this->delete_responsefile();
 729                  break;
 730              default:
 731                  $this->delete_file();
 732          }
 733          die;
 734      }
 735  
 736  
 737      function delete_responsefile() {
 738          global $CFG;
 739  
 740          $file     = required_param('file', PARAM_FILE);
 741          $userid   = required_param('userid', PARAM_INT);
 742          $mode     = required_param('mode', PARAM_ALPHA);
 743          $offset   = required_param('offset', PARAM_INT);
 744          $confirm  = optional_param('confirm', 0, PARAM_BOOL);
 745  
 746          $returnurl = "submissions.php?id={$this->cm->id}&amp;userid=$userid&amp;mode=$mode&amp;offset=$offset";
 747  
 748          if (!$this->can_manage_responsefiles()) {
 749             redirect($returnurl);
 750          }
 751  
 752          $urlreturn = 'submissions.php';
 753          $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
 754  
 755          if (!data_submitted('nomatch') or !$confirm) {
 756              $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'action'=>'response', 'mode'=>$mode, 'offset'=>$offset);
 757              print_header(get_string('delete'));
 758              print_heading(get_string('delete'));
 759              notice_yesno(get_string('confirmdeletefile', 'assignment', $file), 'delete.php', $urlreturn, $optionsyes, $optionsreturn, 'post', 'get');
 760              print_footer('none');
 761              die;
 762          }
 763  
 764          $dir = $this->file_area_name($userid).'/responses';
 765          $filepath = $CFG->dataroot.'/'.$dir.'/'.$file;
 766          if (file_exists($filepath)) {
 767              if (@unlink($filepath)) {
 768                  redirect($returnurl);
 769              }
 770          }
 771  
 772          // print delete error
 773          print_header(get_string('delete'));
 774          notify(get_string('deletefilefailed', 'assignment'));
 775          print_continue($returnurl);
 776          print_footer('none');
 777          die;
 778  
 779      }
 780  
 781  
 782      function delete_file() {
 783          global $CFG;
 784  
 785          $file     = required_param('file', PARAM_FILE);
 786          $userid   = required_param('userid', PARAM_INT);
 787          $confirm  = optional_param('confirm', 0, PARAM_BOOL);
 788          $mode     = optional_param('mode', '', PARAM_ALPHA);
 789          $offset   = optional_param('offset', 0, PARAM_INT);
 790  
 791          require_login($this->course->id, false, $this->cm);
 792  
 793          if (empty($mode)) {
 794              $urlreturn = 'view.php';
 795              $optionsreturn = array('id'=>$this->cm->id);
 796              $returnurl = 'view.php?id='.$this->cm->id;
 797          } else {
 798              $urlreturn = 'submissions.php';
 799              $optionsreturn = array('id'=>$this->cm->id, 'offset'=>$offset, 'mode'=>$mode, 'userid'=>$userid);
 800              $returnurl = "submissions.php?id={$this->cm->id}&amp;offset=$offset&amp;mode=$mode&amp;userid=$userid";
 801          }
 802  
 803          if (!$submission = $this->get_submission($userid) // incorrect submission
 804            or !$this->can_delete_files($submission)) {     // can not delete
 805              $this->view_header(get_string('delete'));
 806              notify(get_string('cannotdeletefiles', 'assignment'));
 807              print_continue($returnurl);
 808              $this->view_footer();
 809              die;
 810          }
 811          $dir = $this->file_area_name($userid);
 812  
 813          if (!data_submitted('nomatch') or !$confirm) {
 814              $optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset);
 815              if (empty($mode)) {
 816                  $this->view_header(get_string('delete'));
 817              } else {
 818                  print_header(get_string('delete'));
 819              }
 820              print_heading(get_string('delete'));
 821              notice_yesno(get_string('confirmdeletefile', 'assignment', $file), 'delete.php', $urlreturn, $optionsyes, $optionsreturn, 'post', 'get');
 822              if (empty($mode)) {
 823                  $this->view_footer();
 824              } else {
 825                  print_footer('none');
 826              }
 827              die;
 828          }
 829  
 830          $filepath = $CFG->dataroot.'/'.$dir.'/'.$file;
 831          if (file_exists($filepath)) {
 832              if (@unlink($filepath)) {
 833                  $updated = new object();
 834                  $updated->id = $submission->id;
 835                  $updated->timemodified = time();
 836                  if (update_record('assignment_submissions', $updated)) {
 837                      add_to_log($this->course->id, 'assignment', 'upload', //TODO: add delete action to log
 838                              'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
 839                      $submission = $this->get_submission($userid);
 840                      $this->update_grade($submission);
 841                  }
 842                  redirect($returnurl);
 843              }
 844          }
 845  
 846          // print delete error
 847          if (empty($mode)) {
 848              $this->view_header(get_string('delete'));
 849          } else {
 850              print_header(get_string('delete'));
 851          }
 852          notify(get_string('deletefilefailed', 'assignment'));
 853          print_continue($returnurl);
 854          if (empty($mode)) {
 855              $this->view_footer();
 856          } else {
 857              print_footer('none');
 858          }
 859          die;
 860      }
 861  
 862  
 863      function can_upload_file($submission) {
 864          global $USER;
 865  
 866          if (has_capability('mod/assignment:submit', $this->context)           // can submit
 867            and $this->isopen()                                                 // assignment not closed yet
 868            and (empty($submission) or $submission->userid == $USER->id)        // his/her own submission
 869            and $this->count_user_files($USER->id) < $this->assignment->var1    // file limit not reached
 870            and !$this->is_finalized($submission)) {                            // no uploading after final submission
 871              return true;
 872          } else {
 873              return false;
 874          }
 875      }
 876  
 877      function can_manage_responsefiles() {
 878          if (has_capability('mod/assignment:grade', $this->context)) {
 879              return true;
 880          } else {
 881              return false;
 882          }
 883      }
 884  
 885      function can_delete_files($submission) {
 886          global $USER;
 887  
 888          if (has_capability('mod/assignment:grade', $this->context)) {
 889              return true;
 890          }
 891  
 892          if (has_capability('mod/assignment:submit', $this->context)
 893            and $this->isopen()                                      // assignment not closed yet
 894            and $this->assignment->resubmit                          // deleting allowed
 895            and $USER->id == $submission->userid                     // his/her own submission
 896            and !$this->is_finalized($submission)) {                 // no deleting after final submission
 897              return true;
 898          } else {
 899              return false;
 900          }
 901      }
 902  
 903      function drafts_tracked() {
 904          return !empty($this->assignment->var4);
 905      }
 906  
 907      /**
 908       * Returns submission status
 909       * @param object $submission - may be empty
 910       * @return string submission state - empty, ASSIGNMENT_STATUS_SUBMITTED or ASSIGNMENT_STATUS_CLOSED
 911       */
 912      function is_finalized($submission) {
 913          if (!$this->drafts_tracked()) {
 914              return '';
 915  
 916          } else if (empty($submission)) {
 917              return '';
 918  
 919          } else if ($submission->data2 == ASSIGNMENT_STATUS_SUBMITTED or $submission->data2 == ASSIGNMENT_STATUS_CLOSED) {
 920              return $submission->data2;
 921  
 922          } else {
 923              return '';
 924          }
 925      }
 926  
 927      function can_unfinalize($submission) {
 928          if (!$this->drafts_tracked()) {
 929              return false;
 930          }
 931          if (has_capability('mod/assignment:grade', $this->context)
 932            and $this->isopen()
 933            and $this->is_finalized($submission)) {
 934              return true;
 935          } else {
 936              return false;
 937          }
 938      }
 939  
 940      function can_finalize($submission) {
 941          global $USER;
 942          if (!$this->drafts_tracked()) {
 943              return false;
 944          }
 945  
 946          if ($this->is_finalized($submission)) {
 947              return false;
 948          }
 949  
 950          if (has_capability('mod/assignment:grade', $this->context)) {
 951              return true;
 952  
 953          } else if (has_capability('mod/assignment:submit', $this->context)    // can submit
 954            and $this->isopen()                                                 // assignment not closed yet
 955            and !empty($submission)                                             // submission must exist
 956            and $submission->userid == $USER->id                                // his/her own submission
 957            and ($this->count_user_files($USER->id)
 958              or ($this->notes_allowed() and !empty($submission->data1)))) {    // something must be submitted
 959  
 960              return true;
 961          } else {
 962              return false;
 963          }
 964      }
 965  
 966      function can_update_notes($submission) {
 967          global $USER;
 968  
 969          if (has_capability('mod/assignment:submit', $this->context)
 970            and $this->notes_allowed()                                          // notesd must be allowed
 971            and $this->isopen()                                                 // assignment not closed yet
 972            and (empty($submission) or $USER->id == $submission->userid)        // his/her own submission
 973            and !$this->is_finalized($submission)) {                            // no updateingafter final submission
 974              return true;
 975          } else {
 976              return false;
 977          }
 978      }
 979  
 980      function notes_allowed() {
 981          return (boolean)$this->assignment->var2;
 982      }
 983  
 984      /**
 985       * Count the files uploaded by a given user
 986       *
 987       * @param $userid int The user id
 988       * @return int
 989       */
 990      function count_user_files($userid) {
 991          global $CFG;
 992  
 993          $filearea = $this->file_area_name($userid);
 994  
 995          if ( is_dir($CFG->dataroot.'/'.$filearea) && $basedir = $this->file_area($userid)) {
 996              if ($files = get_directory_list($basedir, 'responses')) {
 997                  return count($files);
 998              }
 999          }
1000          return 0;
1001      }
1002  
1003      function count_responsefiles($userid) {
1004          global $CFG;
1005  
1006          $filearea = $this->file_area_name($userid).'/responses';
1007  
1008          if ( is_dir($CFG->dataroot.'/'.$filearea) && $basedir = $this->file_area($userid)) {
1009              $basedir .= '/responses';
1010              if ($files = get_directory_list($basedir)) {
1011                  return count($files);
1012              }
1013          }
1014          return 0;
1015      }
1016  
1017      function setup_elements(&$mform) {
1018          global $CFG, $COURSE;
1019  
1020          $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
1021  
1022          $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
1023          $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
1024          $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
1025          $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
1026  
1027          $mform->addElement('select', 'resubmit', get_string("allowdeleting", "assignment"), $ynoptions);
1028          $mform->setHelpButton('resubmit', array('allowdeleting', get_string('allowdeleting', 'assignment'), 'assignment'));
1029          $mform->setDefault('resubmit', 1);
1030  
1031          $options = array();
1032          for($i = 1; $i <= 20; $i++) {
1033              $options[$i] = $i;
1034          }
1035          $mform->addElement('select', 'var1', get_string("allowmaxfiles", "assignment"), $options);
1036          $mform->setHelpButton('var1', array('allowmaxfiles', get_string('allowmaxfiles', 'assignment'), 'assignment'));
1037          $mform->setDefault('var1', 3);
1038  
1039          $mform->addElement('select', 'var2', get_string("allownotes", "assignment"), $ynoptions);
1040          $mform->setHelpButton('var2', array('allownotes', get_string('allownotes', 'assignment'), 'assignment'));
1041          $mform->setDefault('var2', 0);
1042  
1043          $mform->addElement('select', 'var3', get_string("hideintro", "assignment"), $ynoptions);
1044          $mform->setHelpButton('var3', array('hideintro', get_string('hideintro', 'assignment'), 'assignment'));
1045          $mform->setDefault('var3', 0);
1046  
1047          $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
1048          $mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
1049          $mform->setDefault('emailteachers', 0);
1050  
1051          $mform->addElement('select', 'var4', get_string("trackdrafts", "assignment"), $ynoptions);
1052          $mform->setHelpButton('var4', array('trackdrafts', get_string('trackdrafts', 'assignment'), 'assignment'));
1053          $mform->setDefault('var4', 1);
1054  
1055      }
1056  
1057  }
1058  
1059  class mod_assignment_upload_notes_form extends moodleform {
1060      function definition() {
1061          $mform =& $this->_form;
1062  
1063          // visible elements
1064          $mform->addElement('htmleditor', 'text', get_string('notes', 'assignment'), array('cols'=>85, 'rows'=>30));
1065          $mform->setType('text', PARAM_RAW); // to be cleaned before display
1066          $mform->setHelpButton('text', array('reading', 'writing'), false, 'editorhelpbutton');
1067  
1068          // hidden params
1069          $mform->addElement('hidden', 'id', 0);
1070          $mform->setType('id', PARAM_INT);
1071          $mform->addElement('hidden', 'action', 'savenotes');
1072          $mform->setType('id', PARAM_ALPHA);
1073  
1074          // buttons
1075          $this->add_action_buttons();
1076      }
1077  }
1078  
1079  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7