Пример #1
0
 /**
  * Adds a user's submission to the database and filesystem
  *
  * Called then this component receives an HTTP POST request to
  * /submission(/)
  * The request body should contain a JSON object representing a submission.
  *
  * @author Till Uhlig
  * @date 2014
  */
 public function addSubmission()
 {
     $body = $this->app->request->getBody();
     $submission = Submission::decodeSubmission($body);
     $file = $submission->getFile();
     if (!isset($file) || $file == array()) {
         $file = new File();
     }
     if ($file->getTimeStamp() === null) {
         $file->setTimeStamp(time());
     }
     ///echo File::encodeFile($file);return;
     // upload file to filesystem
     $result = Request::routeRequest('POST', '/file', array(), File::encodeFile($file), $this->_file, 'file');
     ///var_dump($result);
     ///echo $result['content'];return;
     if ($result['status'] >= 200 && $result['status'] <= 299) {
         // file is uploaded
         $newFile = File::decodeFile($result['content']);
         $file->setAddress($newFile->getAddress());
         $file->setHash($newFile->getHash());
         $file->setFileId($newFile->getFileId());
         $file->setBody(null);
         $submission->setFile($file);
         // upload submission to database
         if ($submission->getId() === null) {
             //echo Submission::encodeSubmission($submission);return;
             $result = Request::routeRequest('POST', '/submission', array(), Submission::encodeSubmission($submission), $this->_submission, 'submission');
         } else {
             $result['status'] = 201;
             $result['content'] = Submission::encodeSubmission($submission);
         }
         //var_dump($result);
         if ($result['status'] >= 200 && $result['status'] <= 299) {
             // submission is uploaded
             $newsubmission = Submission::decodeSubmission($result['content']);
             $submission->setId($newsubmission->getId());
             // select new submission
             if ($submission->getSelectedForGroup() == '1') {
                 $selectedSubmission = SelectedSubmission::createSelectedSubmission($submission->getLeaderId(), $submission->getId(), $submission->getExerciseId());
                 $result = Request::routeRequest('POST', '/selectedsubmission', array(), SelectedSubmission::encodeSelectedSubmission($selectedSubmission), $this->_selectedSubmission, 'selectedsubmission');
                 if ($result['status'] >= 200 && $result['status'] <= 299) {
                     $this->app->response->setBody(Submission::encodeSubmission($submission));
                     $this->app->response->setStatus(201);
                     $this->app->stop();
                 } else {
                     $result = Request::routeRequest('PUT', '/selectedsubmission/leader/' . $submission->getLeaderId() . '/exercise/' . $submission->getExerciseId(), $this->app->request->headers->all(), SelectedSubmission::encodeSelectedSubmission($selectedSubmission), $this->_selectedSubmission, 'selectedsubmission');
                     if ($result['status'] >= 200 && $result['status'] <= 299) {
                         $this->app->response->setBody(Submission::encodeSubmission($submission));
                         $this->app->response->setStatus(201);
                         $this->app->stop();
                     }
                 }
             } else {
                 $this->app->response->setBody(Submission::encodeSubmission($submission));
                 $this->app->response->setStatus(201);
                 $this->app->stop();
             }
         }
     }
     Logger::Log('POST AddSubmission failed', LogLevel::ERROR);
     $this->app->response->setBody(Submission::encodeSubmission(new Submission()));
     $this->app->response->setStatus(409);
     $this->app->stop();
 }
Пример #2
0
/**
 * Updates the selected submission of a group.
 *
 * @param string $databaseURI The url at which the database server is running.
 * @param int $leaderId The id of the the group's leader
 * @param int $submissionUd The new selected submission
 * @param int $exerciseId The submission's exercise id.
 * @param string &$message A reference to a variable that will contain the HTTP
 * status code on return.
 *
 * @return string On success rturns a json object, representing the selected
 * submission in the database. NULL otherwise.
 */
function updateSelectedSubmission($databaseURI, $leaderId, $submissionId, $exerciseId, &$message, $newFlag = null)
{
    $selectedSubmission = SelectedSubmission::createSelectedSubmission($leaderId, $submissionId, $exerciseId);
    $URL = $databaseURI . '/selectedsubmission';
    $returnedSubmission = http_post_data($URL, json_encode($selectedSubmission), true, $message);
    if ($message != "201") {
        $URL = $databaseURI . '/selectedsubmission/leader/' . $leaderId . '/exercise/' . $exerciseId;
        $returnedSubmission = http_put_data($URL, json_encode($selectedSubmission), true, $message);
    }
    if ($newFlag !== null) {
        // todo: treat the case if the previous operation failed
        if ($submissionId === null) {
            $ret = Submission::decodeSubmission($returnedSubmission);
            $submissionId = $ret->getId();
        }
        $URL = $databaseURI . '/submission/submission/' . $submissionId;
        $submissionUpdate = Submission::createSubmission($submissionId, null, null, null, null, null, null, $newFlag);
        $returnedSubmission2 = http_put_data($URL, json_encode($submissionUpdate), true, $message2);
    }
    return $returnedSubmission;
}