Example #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();
 }
Example #2
0
 /**
  * Adds a new marking.
  *
  * Called when this component receives an HTTP POST request to
  * /marking(/).
  * The request body should contain a JSON object representing the new marking.
  *
  * @author Till Uhlig
  * @date 2014
  */
 public function addMarking()
 {
     $header = $this->app->request->headers->all();
     $body = $this->app->request->getBody();
     $markings = Marking::decodeMarking($body);
     $this->app->response->setStatus(201);
     // always been an array
     $arr = true;
     if (!is_array($markings)) {
         $markings = array($markings);
         $arr = false;
     }
     // this array contains the inserted objects
     $res = array();
     foreach ($markings as $marking) {
         if ($marking->getDate() === null) {
             $marking->setDate(time());
         }
         $file = $marking->getFile();
         if (!isset($file)) {
             $file = new File();
         }
         if ($file->getTimeStamp() === null) {
             $file->setTimeStamp(time());
         }
         // upload file to filesystem
         $result = Request::routeRequest('POST', '/file', $this->app->request->headers->all(), File::encodeFile($file), $this->_file, 'file');
         // checks the correctness of the query
         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);
             $marking->setFile($file);
             // upload marking to database
             if ($marking->getId() === null) {
                 $result = Request::routeRequest('POST', '/marking', $this->app->request->headers->all(), Marking::encodeMarking($marking), $this->_marking, 'marking');
             } else {
                 $result = Request::routeRequest('PUT', '/marking/marking/' . $marking->getId(), $this->app->request->headers->all(), Marking::encodeMarking($marking), $this->_marking, 'marking');
             }
             if ($result['status'] >= 200 && $result['status'] <= 299) {
                 // marking is uploaded
                 $newmarking = Marking::decodeMarking($result['content']);
                 if (is_array($newmarking)) {
                     $newmarking = $newmarking[0];
                 }
                 if ($newmarking->getId() !== null) {
                     $marking->setId($newmarking->getId());
                 }
                 $res[] = $marking;
                 if (isset($result['headers']['Content-Type'])) {
                     $this->app->response->headers->set('Content-Type', $result['headers']['Content-Type']);
                 }
             } else {
                 Logger::Log('POST AddMarking failed', LogLevel::ERROR);
                 $this->app->response->setStatus(isset($result['status']) ? $result['status'] : 409);
                 $this->app->response->setBody(Marking::encodeMarking($res));
                 $this->app->stop();
             }
         } else {
             Logger::Log('POST AddMarking failed', LogLevel::ERROR);
             $this->app->response->setStatus(isset($result['status']) ? $result['status'] : 409);
             $this->app->response->setBody(Marking::encodeMarking($res));
             $this->app->stop();
         }
     }
     if (!$arr && count($res) == 1) {
         $this->app->response->setBody(Marking::encodeMarking($res[0]));
     } else {
         $this->app->response->setBody(Marking::encodeMarking($res));
     }
 }