original_string: public function get_user_submission($userid, $create, $attemptnumber=-1) {
          global $DB, $USER;
  
          if (!$userid) {
              $userid = $USER->id;
          }
          // If the userid is not null then use userid.
          $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid, 'groupid'=>0);
          if ($attemptnumber >= 0) {
              $params['attemptnumber'] = $attemptnumber;
          }
  
          // Only return the row with the highest attemptnumber.
          $submission = null;
          $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
          if ($submissions) {
              $submission = reset($submissions);
          }
  
          if ($submission) {
              return $submission;
          }
          if ($create) {
              $submission = new stdClass();
              $submission->assignment   = $this->get_instance()->id;
              $submission->userid       = $userid;
              $submission->timecreated = time();
              $submission->timemodified = $submission->timecreated;
              $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
              if ($attemptnumber >= 0) {
                  $submission->attemptnumber = $attemptnumber;
              } else {
                  $submission->attemptnumber = 0;
              }
              // Work out if this is the latest submission.
              $submission->latest = 0;
              $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid, 'groupid'=>0);
              if ($attemptnumber == -1) {
                  // This is a new submission so it must be the latest.
                  $submission->latest = 1;
              } else {
                  // We need to work this out.
                  $result = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', 'attemptnumber', 0, 1);
                  $latestsubmission = null;
                  if ($result) {
                      $latestsubmission = reset($result);
                  }
                  if (empty($latestsubmission) || ($attemptnumber > $latestsubmission->attemptnumber)) {
                      $submission->latest = 1;
                  }
              }
              if ($submission->latest) {
                  // This is the case when we need to set latest to 0 for all the other attempts.
                  $DB->set_field('assign_submission', 'latest', 0, $params);
              }
              $sid = $DB->insert_record('assign_submission', $submission);
              return $DB->get_record('assign_submission', array('id' => $sid));
          }
          return false;
      }
code: public function get_user_submission($userid, $create, $attemptnumber=-1) {
          global $DB, $USER;
  
          if (!$userid) {
              $userid = $USER->id;
          }
          // If the userid is not null then use userid.
          $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid, 'groupid'=>0);
          if ($attemptnumber >= 0) {
              $params['attemptnumber'] = $attemptnumber;
          }
  
          // Only return the row with the highest attemptnumber.
          $submission = null;
          $submissions = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', '*', 0, 1);
          if ($submissions) {
              $submission = reset($submissions);
          }
  
          if ($submission) {
              return $submission;
          }
          if ($create) {
              $submission = new stdClass();
              $submission->assignment   = $this->get_instance()->id;
              $submission->userid       = $userid;
              $submission->timecreated = time();
              $submission->timemodified = $submission->timecreated;
              $submission->status = ASSIGN_SUBMISSION_STATUS_NEW;
              if ($attemptnumber >= 0) {
                  $submission->attemptnumber = $attemptnumber;
              } else {
                  $submission->attemptnumber = 0;
              }
              // Work out if this is the latest submission.
              $submission->latest = 0;
              $params = array('assignment'=>$this->get_instance()->id, 'userid'=>$userid, 'groupid'=>0);
              if ($attemptnumber == -1) {
                  // This is a new submission so it must be the latest.
                  $submission->latest = 1;
              } else {
                  // We need to work this out.
                  $result = $DB->get_records('assign_submission', $params, 'attemptnumber DESC', 'attemptnumber', 0, 1);
                  $latestsubmission = null;
                  if ($result) {
                      $latestsubmission = reset($result);
                  }
                  if (empty($latestsubmission) || ($attemptnumber > $latestsubmission->attemptnumber)) {
                      $submission->latest = 1;
                  }
              }
              if ($submission->latest) {
                  // This is the case when we need to set latest to 0 for all the other attempts.
                  $DB->set_field('assign_submission', 'latest', 0, $params);
              }
              $sid = $DB->insert_record('assign_submission', $submission);
              return $DB->get_record('assign_submission', array('id' => $sid));
          }
          return false;
      }
code_tokens: public function get_user_submission ( $ userid , $ create , $ attemptnumber = - 1 ) { global $ DB , $ USER ; if ( ! $ userid ) { $ userid = $ USER -> id ; } // If the userid is not null then use userid. $ params = array ( 'assignment' => $ this -> get_instance ( ) -> id , 'userid' => $ userid , 'groupid' => 0 ) ; if ( $ attemptnumber >= 0 ) { $ params [ 'attemptnumber' ] = $ attemptnumber ; } // Only return the row with the highest attemptnumber. $ submission = null ; $ submissions = $ DB -> get_records ( 'assign_submission' , $ params , 'attemptnumber DESC' , '*' , 0 , 1 ) ; if ( $ submissions ) { $ submission = reset ( $ submissions ) ; } if ( $ submission ) { return $ submission ; } if ( $ create ) { $ submission = new stdClass ( ) ; $ submission -> assignment = $ this -> get_instance ( ) -> id ; $ submission -> userid = $ userid ; $ submission -> timecreated = time ( ) ; $ submission -> timemodified = $ submission -> timecreated ; $ submission -> status = ASSIGN_SUBMISSION_STATUS_NEW ; if ( $ attemptnumber >= 0 ) { $ submission -> attemptnumber = $ attemptnumber ; } else { $ submission -> attemptnumber = 0 ; } // Work out if this is the latest submission. $ submission -> latest = 0 ; $ params = array ( 'assignment' => $ this -> get_instance ( ) -> id , 'userid' => $ userid , 'groupid' => 0 ) ; if ( $ attemptnumber == - 1 ) { // This is a new submission so it must be the latest. $ submission -> latest = 1 ; } else { // We need to work this out. $ result = $ DB -> get_records ( 'assign_submission' , $ params , 'attemptnumber DESC' , 'attemptnumber' , 0 , 1 ) ; $ latestsubmission = null ; if ( $ result ) { $ latestsubmission = reset ( $ result ) ; } if ( empty ( $ latestsubmission ) || ( $ attemptnumber > $ latestsubmission -> attemptnumber ) ) { $ submission -> latest = 1 ; } } if ( $ submission -> latest ) { // This is the case when we need to set latest to 0 for all the other attempts. $ DB -> set_field ( 'assign_submission' , 'latest' , 0 , $ params ) ; } $ sid = $ DB -> insert_record ( 'assign_submission' , $ submission ) ; return $ DB -> get_record ( 'assign_submission' , array ( 'id' => $ sid ) ) ; } return false ; }
docstring: Load the submission object for a particular user, optionally creating it if required.
  
  For team assignments there are 2 submissions - the student submission and the team submission
  All files are associated with the team submission but the status of the students contribution is
  recorded separately.
  
  @param int $userid The id of the user whose submission we want or 0 in which case USER->id is used
  @param bool $create If set to true a new submission object will be created in the database with the status set to "new".
  @param int $attemptnumber - -1 means the latest attempt
  @return stdClass The submission
docstring_tokens: Load the submission object for a particular user optionally creating it if required .