/**
  * Processes a process
  *
  * Called when this component receives an HTTP POST request to
  * /process(/).
  */
 public function postProcess()
 {
     $this->app->response->setStatus(201);
     $header = $this->app->request->headers->all();
     $body = $this->app->request->getBody();
     $process = Process::decodeProcess($body);
     // always been an array
     $arr = true;
     if (!is_array($process)) {
         $process = array($process);
         $arr = false;
     }
     // this array contains the indices of the inserted objects
     $res = array();
     foreach ($process as $pro) {
         $eid = $pro->getExercise()->getId();
         // loads the form from database
         $result = Request::routeRequest('GET', '/form/exercise/' . $eid, $this->app->request->headers->all(), '', $this->_formDb, 'form');
         // checks the correctness of the query
         if ($result['status'] >= 200 && $result['status'] <= 299) {
             // only one form as result
             $forms = Form::decodeForm($result['content']);
             $forms = $forms[0];
             $formdata = $pro->getRawSubmission()->getFile();
             $timestamp = $formdata->getTimeStamp();
             if ($timestamp === null) {
                 $timestamp = time();
             }
             if ($formdata !== null && $forms !== null) {
                 $formdata = Form::decodeForm($formdata->getBody(true));
                 if (is_array($formdata)) {
                     $formdata = $formdata[0];
                 }
                 if ($formdata !== null) {
                     // check the submission
                     $fail = false;
                     $parameter = explode(' ', strtolower($pro->getParameter()));
                     $choices = $formdata->getChoices();
                     if ($forms->getType() == 0) {
                         foreach ($choices as &$choice) {
                             $i = 0;
                             for ($i < 0; $i < count($parameter); $i++) {
                                 $param = $parameter[$i];
                                 if ($param === null || $param === '') {
                                     continue;
                                 }
                                 switch ($param) {
                                     case 'isnumeric':
                                         if (!@preg_match("%^-?([0-9])+([,]([0-9])+)?\$%", DefaultNormalizer::normalizeText($choice->getText()))) {
                                             $fail = true;
                                             $pro->addMessage('"' . $choice->getText() . '" ist keine gültige Zahl. <br>Bsp.: -0,00');
                                         }
                                         break;
                                     case 'isdigit':
                                         if (!ctype_digit(DefaultNormalizer::normalizeText($choice->getText()))) {
                                             $fail = true;
                                             $pro->addMessage('"' . $choice->getText() . '" ist keine gültige Ziffernfolge.');
                                         }
                                         break;
                                     case 'isprintable':
                                         if (!ctype_print(DefaultNormalizer::normalizeText($choice->getText()))) {
                                             $fail = true;
                                             $pro->addMessage('"' . $choice->getText() . '" enthält nicht-druckbare Zeichen.');
                                         }
                                         break;
                                     case 'isalpha':
                                         if (!ctype_alpha(DefaultNormalizer::normalizeText($choice->getText()))) {
                                             $fail = true;
                                             $pro->addMessage('"' . $choice->getText() . '" ist keine gültige Buchstabenfolge.');
                                         }
                                         break;
                                     case 'isalphanum':
                                         if (!ctype_alnum(DefaultNormalizer::normalizeText($choice->getText()))) {
                                             $fail = true;
                                             $pro->addMessage('"' . $choice->getText() . '" ist nicht alphanumerisch.');
                                         }
                                         break;
                                     case 'ishex':
                                         if (!ctype_xdigit(DefaultNormalizer::normalizeText($choice->getText()))) {
                                             $fail = true;
                                             $pro->addMessage('"' . $choice->getText() . '" ist keine gültige Hexadezimalzahl.');
                                         }
                                         break;
                                     default:
                                         $test = $parameter[$i];
                                         $i++;
                                         while ($i < count($parameter)) {
                                             if (@preg_match($test, DefaultNormalizer::normalizeText($choice->getText())) !== false) {
                                                 break;
                                             }
                                             $test .= ' ' . $parameter[$i];
                                             $i++;
                                         }
                                         $match = @preg_match($test, DefaultNormalizer::normalizeText($choice->getText()));
                                         if ($match === false) {
                                             $fail = true;
                                             $pro->addMessage('"' . $test . '" ist kein gültiger regulärer Ausdruck.');
                                         } elseif ($match == false) {
                                             $fail = true;
                                             $pro->addMessage('"' . $choice->getText() . '" entspricht nicht dem regulären Ausdruck "' . $test . '".');
                                         }
                                         break;
                                 }
                             }
                         }
                     }
                     if ($fail) {
                         // received submission isn't correct
                         $pro->setStatus(409);
                         $res[] = $pro;
                         $this->app->response->setStatus(409);
                         continue;
                     }
                     // save the submission
                     #region Form to PDF
                     if ($pro->getSubmission() === null) {
                         $raw = $pro->getRawSubmission();
                         $exerciseName = '';
                         if ($raw !== null) {
                             $exerciseName = $raw->getExerciseName();
                         }
                         $answer = "";
                         if ($forms->getType() == 0) {
                             $answer = $formdata->getChoices()[0]->getText();
                         }
                         if ($forms->getType() == 1) {
                             $answer = $this->ChoiceIdToText($formdata->getChoices()[0]->getText(), $forms->getChoices());
                         }
                         if ($forms->getType() == 2) {
                             foreach ($formdata->getChoices() as $chosen) {
                                 $answer .= $this->ChoiceIdToText($chosen->getText(), $forms->getChoices()) . '<br>';
                             }
                         }
                         $Text = "<h1>AUFGABE {$exerciseName}</h1>" . "<hr>";
                         if ($forms->getTask() !== null && trim($forms->getTask()) != '') {
                             $Text .= "<p>" . "<h2>Aufgabenstellung:</h2>" . $forms->getTask() . "</p>";
                         }
                         $Text .= "<p>" . "<h2>Antwort:</h2>" . $answer . "</p>";
                         $pdf = Pdf::createPdf($Text);
                         $pdf->setText($Text);
                         //echo Pdf::encodePdf($pdf);return;
                         $result = Request::routeRequest('POST', '/pdf', array(), Pdf::encodePdf($pdf), $this->_pdf, 'pdf');
                         // checks the correctness of the query
                         if ($result['status'] >= 200 && $result['status'] <= 299) {
                             $pdf = File::decodeFile($result['content']);
                             $pdf->setDisplayName($exerciseName . '.pdf');
                             $pdf->setTimeStamp($timestamp);
                             $pdf->setBody(null);
                             if (is_object($pro->getRawSubmission())) {
                                 $submission = clone $pro->getRawSubmission();
                             } else {
                                 $submission = new Submission();
                             }
                             $submission->setFile($pdf);
                             $submission->setExerciseId($eid);
                             $pro->setSubmission($submission);
                         } else {
                             $pro->setStatus(409);
                             $res[] = $pro;
                             $this->app->response->setStatus(409);
                             continue;
                         }
                     }
                     #endregion
                     $rawSubmission = $pro->getRawSubmission();
                     $rawFile = $rawSubmission->getFile();
                     $rawFile->setBody(Form::encodeForm($formdata), true);
                     $rawSubmission->setFile($rawFile);
                     $rawSubmission->setExerciseId($eid);
                     $pro->setRawSubmission($rawSubmission);
                     $pro->setStatus(201);
                     $res[] = $pro;
                     continue;
                 }
             }
         }
         $this->app->response->setStatus(409);
         $pro->setStatus(409);
         $res[] = $pro;
     }
     if (!$arr && count($res) == 1) {
         $this->app->response->setBody(Process::encodeProcess($res[0]));
     } else {
         $this->app->response->setBody(Process::encodeProcess($res));
     }
 }