Exemplo n.º 1
0
 /**
  * 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) {
                     // evaluate the formdata
                     $points = 0;
                     $answers = $formdata->getChoices();
                     $correctAnswers = $forms->getChoices();
                     $allcorrect = true;
                     if ($forms->getType() == 0) {
                         $parameter = explode(' ', strtolower($pro->getParameter()));
                         if ($parameter === null || count($parameter) === 0 || $parameter[0] === '') {
                             if (DefaultNormalizer::normalizeText($correctAnswers[0]->getText()) != DefaultNormalizer::normalizeText($answers[0]->getText())) {
                                 $allcorrect = false;
                             }
                         } elseif (strtolower($parameter[0]) === 'distance1') {
                             $similarity = 0;
                             similar_text(DefaultNormalizer::normalizeText($answers[0]->getText()), DefaultNormalizer::normalizeText($correctAnswers[0]->getText()), $similarity);
                             if (isset($parameter[1])) {
                                 if ($similarity < $parameter[1]) {
                                     $allcorrect = false;
                                 }
                             } else {
                                 if ($similarity < 100) {
                                     $allcorrect = false;
                                 }
                             }
                         } elseif (strtolower($parameter[0]) === 'regularexpression') {
                             $i = 1;
                             $test = $parameter[$i];
                             while ($i < count($parameter)) {
                                 if (@preg_match($test, DefaultNormalizer::normalizeText($answers[0]->getText())) !== false) {
                                     break;
                                 }
                                 $test .= ' ' . $parameter[$i];
                                 $i++;
                             }
                             $match = @preg_match($test, DefaultNormalizer::normalizeText(DefaultNormalizer::normalizeText($answers[0]->getText())));
                             if ($match === false || $match == false || $test == '') {
                                 $allcorrect = false;
                             }
                         }
                     } elseif ($forms->getType() == 1) {
                         foreach ($correctAnswers as $mask) {
                             $foundInStudentsAnswer = false;
                             foreach ($answers as $answer) {
                                 if ($answer->getText() === $mask->getChoiceId()) {
                                     $foundInStudentsAnswer = true;
                                     break;
                                 }
                             }
                             if ($mask->getCorrect() === '1' && !$foundInStudentsAnswer) {
                                 $allcorrect = false;
                                 break;
                             } elseif ($mask->getCorrect() === '0' && $foundInStudentsAnswer) {
                                 $allcorrect = false;
                                 break;
                             }
                         }
                     } elseif ($forms->getType() == 2) {
                         foreach ($correctAnswers as $mask) {
                             $foundInStudentsAnswer = false;
                             foreach ($answers as $answer) {
                                 if ($answer->getText() === $mask->getChoiceId()) {
                                     $foundInStudentsAnswer = true;
                                     break;
                                 }
                             }
                             if ($mask->getCorrect() === '1' && !$foundInStudentsAnswer) {
                                 $allcorrect = false;
                                 break;
                             } elseif ($mask->getCorrect() === '0' && $foundInStudentsAnswer) {
                                 $allcorrect = false;
                                 break;
                             }
                         }
                     }
                     if ($allcorrect) {
                         $points = $pro->getExercise()->getMaxPoints();
                     }
                     // save the marking
                     #region Form to PDF
                     if ($pro->getMarking() === 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>';
                             }
                         }
                         $answer2 = "";
                         foreach ($forms->getChoices() as $chosen) {
                             if ($chosen->getCorrect() === '1') {
                                 $answer2 .= $chosen->getText() . '<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>" . "<span style=\"color: " . ($points === 0 ? 'red' : 'black') . "\">" . $answer . "</span></p>";
                         if ($points === 0) {
                             $Text .= "<p>" . "<h2>L&ouml;sung:</h2><span style=\"color: green\">" . $answer2 . "</span></p>";
                         }
                         if ($forms->getSolution() !== null && trim($forms->getSolution()) != '') {
                             $Text .= "<p>" . "<h2>L&ouml;sungsbegr&uuml;ndung:</h2>" . $forms->getSolution() . "</p>";
                         }
                         $Text .= "<p style=\"text-align: center;\">" . "<h2><span style=\"color: red\">{$points}P</span></h2>" . "</p>";
                         $pdf = Pdf::createPdf($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);
                             $submission = $pro->getSubmission();
                             if ($submission === null) {
                                 $submission = $pro->getRawSubmission();
                             }
                             $studentId = $pro->getRawSubmission() !== null ? $pro->getRawSubmission()->getStudentId() : null;
                             if ($studentId === null) {
                                 $studentId = $pro->getSubmission() !== null ? $pro->getSubmission()->getStudentId() : null;
                             }
                             $marking = Marking::createMarking(null, $studentId, null, null, null, null, 3, $points, $submission->getDate() !== null ? $submission->getDate() : time());
                             if (is_object($submission)) {
                                 $marking->setSubmission(clone $submission);
                             }
                             $marking->setFile($pdf);
                             $pro->setMarking($marking);
                         } else {
                             $res[] = null;
                             $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);
                     $res[] = $pro;
                     continue;
                 }
             }
         }
         $this->app->response->setStatus(409);
         $res[] = null;
     }
     if (!$arr && count($res) == 1) {
         $this->app->response->setBody(Process::encodeProcess($res[0]));
     } else {
         $this->app->response->setBody(Process::encodeProcess($res));
     }
 }
Exemplo n.º 2
0
 /**
  * Processes a submissions
  *
  * Called when this component receives an HTTP POST request to
  * /submission(/).
  */
 public function postSubmission()
 {
     $this->app->response->setStatus(201);
     $body = $this->app->request->getBody();
     $submissions = Submission::decodeSubmission($body);
     // always been an array
     $arr = true;
     if (!is_array($submissions)) {
         $submissions = array($submissions);
         $arr = false;
     }
     $res = array();
     foreach ($submissions as $submission) {
         $fail = false;
         $process = new Process();
         $process->setRawSubmission($submission);
         $eid = $submission->getExerciseId();
         // load processor data from database
         $result = Request::routeRequest('GET', '/process/exercise/' . $eid, array(), '', $this->_processorDb, 'process');
         $processors = null;
         if ($result['status'] >= 200 && $result['status'] <= 299) {
             $processors = Process::decodeProcess($result['content']);
         } else {
             if ($result['status'] != 404) {
                 $submission->addMessage("Interner Fehler");
                 $res[] = $submission;
                 $this->app->response->setStatus(409);
                 continue;
             }
         }
         $result2 = Request::routeRequest('GET', '/exercisefiletype/exercise/' . $eid, array(), '', $this->_getExerciseExerciseFileType, 'exercisefiletype');
         $exerciseFileTypes = null;
         if ($result2['status'] >= 200 && $result2['status'] <= 299) {
             $exerciseFileTypes = ExerciseFileType::decodeExerciseFileType($result2['content']);
             if (!is_array($exerciseFileTypes)) {
                 $exerciseFileTypes = array($exerciseFileTypes);
             }
             $filePath = null;
             if ($submission->getFile() != null) {
                 $file = $submission->getFile()->getBody(true);
                 if ($file !== null) {
                     $fileHash = sha1($file);
                     $filePath = '/tmp/' . $fileHash;
                     if ($submission->getFile()->getDisplayName() != null) {
                         LProcessor::generatepath($filePath);
                         file_put_contents($filePath . '/' . $submission->getFile()->getDisplayName(), $file);
                         $filePath .= '/' . $submission->getFile()->getDisplayName();
                     } else {
                         LProcessor::generatepath($filePath);
                         file_put_contents($filePath . '/tmp', $file);
                         $filePath .= '/tmp';
                     }
                 }
             }
             // check file type
             if ($filePath != null) {
                 $found = false;
                 $types = array();
                 $mimeType = MimeReader::get_mime($filePath);
                 $foundExtension = isset(pathinfo($filePath)['extension']) ? pathinfo($filePath)['extension'] : '-';
                 foreach ($exerciseFileTypes as $type) {
                     $types[] = $type->getText();
                     $type = explode(' ', str_replace('*', '', $type->getText()));
                     //echo MimeReader::get_mime($filePath);
                     if (strpos($mimeType, $type[0]) !== false && (!isset($type[1]) || '.' . $foundExtension == $type[1])) {
                         $found = true;
                         break;
                     }
                 }
                 if (!$found && count($exerciseFileTypes) > 0) {
                     $submission->addMessage("falscher Dateityp \nGefunden: " . $mimeType . " ." . $foundExtension . "\nErlaubt: " . implode(',', $types));
                     $res[] = $submission;
                     $this->app->response->setStatus(409);
                     unlink($filePath);
                     continue;
                 }
                 unlink($filePath);
             }
         } else {
             if ($result2['status'] != 404) {
                 $submission->addMessage("Interner Fehler");
                 $res[] = $submission;
                 $this->app->response->setStatus(409);
                 continue;
             }
         }
         // process submission
         if ($processors !== null) {
             if (!is_array($processors)) {
                 $processors = array($processors);
             }
             foreach ($processors as $pro) {
                 $component = $pro->getTarget();
                 if ($process->getExercise() === null) {
                     $process->setExercise($pro->getExercise());
                 }
                 $process->setParameter($pro->getParameter());
                 $process->setAttachment($pro->getAttachment());
                 $process->setTarget($pro->getTarget());
                 $process->setWorkFiles($pro->getWorkFiles());
                 //echo Process::encodeProcess($process)."_______";// return;
                 $result = Request::post($component->getAddress() . '/process', array(), Process::encodeProcess($process));
                 //echo $result['content'].'_______';
                 if ($result['status'] >= 200 && $result['status'] <= 299) {
                     $process = Process::decodeProcess($result['content']);
                 } else {
                     $fail = true;
                     $submission->addMessage("Beim Verarbeiten der Einsendung ist ein Fehler aufgetreten");
                     if (isset($result['content'])) {
                         $content = Process::decodeProcess($result['content']);
                         $submission->setStatus($content->getStatus());
                         $submission->addMessages($content->getMessages());
                     }
                     break;
                 }
             }
         }
         if ($fail) {
             if (isset($submission)) {
                 $submission->setFile(null);
             }
             $res[] = $submission;
             $this->app->response->setStatus(409);
             continue;
         }
         // upload submission
         $uploadSubmission = $process->getSubmission();
         if ($uploadSubmission === null) {
             $uploadSubmission = $process->getRawSubmission();
             if ($uploadSubmission->getFile() != null) {
                 $file = $uploadSubmission->getFile();
                 if ($file->getDisplayName() == null) {
                     $file->setDisplayName($submission->getExerciseName());
                 }
             }
         }
         if ($uploadSubmission !== null) {
             ///echo Submission::encodeSubmission($uploadSubmission);return;
             $result = Request::routeRequest('POST', '/submission', array(), Submission::encodeSubmission($uploadSubmission), $this->_submission, 'submission');
             //var_dump($result);return;
             // checks the correctness of the query
             if ($result['status'] >= 200 && $result['status'] <= 299) {
                 $queryResult = Submission::decodeSubmission($result['content']);
                 $uploadSubmission->setId($queryResult->getId());
                 $uploadSubmission->setFile($queryResult->getFile());
                 if ($process->getMarking() !== null) {
                     $process->getMarking()->setSubmission($queryResult);
                 }
             } else {
                 $uploadSubmission->addMessage("Beim Speichern der Einsendung ist ein Fehler aufgetreten.");
                 //var_dump($uploadSubmission); return;
                 if (isset($result['content'])) {
                     $content = Submission::decodeSubmission($result['content']);
                     $uploadSubmission->setStatus($content->getStatus());
                     $uploadSubmission->addMessages($content->getMessages());
                 }
                 $uploadSubmission->setStatus(409);
                 $res[] = $uploadSubmission;
                 $this->app->response->setStatus(409);
                 continue;
             }
         }
         // upload marking
         if ($process->getMarking() !== null) {
             //echo Marking::encodeMarking($process->getMarking());
             $result = Request::routeRequest('POST', '/marking', array(), Marking::encodeMarking($process->getMarking()), $this->_marking, 'marking');
             // checks the correctness of the query
             if ($result['status'] >= 200 && $result['status'] <= 299) {
                 $queryResult = Marking::decodeMarking($result['content']);
             } else {
                 $uploadSubmission->addMessage("Beim Speichern der Korrektur ist ein Fehler aufgetreten");
                 if (isset($result['content'])) {
                     $content = Marking::decodeMarking($result['content']);
                     $uploadSubmission->addMessages($content->getMessages());
                 }
                 $res[] = $uploadSubmission;
                 $this->app->response->setStatus(409);
                 continue;
             }
         }
         $rr = $process->getSubmission();
         if ($rr === null) {
             $rr = $process->getRawSubmission();
         }
         $res[] = $rr;
     }
     if (!$arr && count($res) == 1) {
         $this->app->response->setBody(Submission::encodeSubmission($res[0]));
     } else {
         $this->app->response->setBody(Submission::encodeSubmission($res));
     }
 }
Exemplo n.º 3
0
 /**
  * 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));
     }
 }
Exemplo n.º 4
0
 /**
  * Processes a process
  *
  * Called when this component receives an HTTP POST request to
  * /process(/).
  */
 public function postProcess()
 {
     // hier werden Einsendungen verarbeitet
     $this->app->response->setStatus(201);
     $body = $this->app->request->getBody();
     $process = Process::decodeProcess($body);
     // always been an array
     // es ist einfacher, wenn man sicherstellt, dass die Eingabedaten als Liste für foreach verarbeitet
     // werden können, um Abstürze bei übergebenen Einzelobjekten zu vermeiden
     $arr = true;
     if (!is_array($process)) {
         $process = array($process);
         $arr = false;
     }
     $res = array();
     // behandelt jede eingehende Einsendung
     foreach ($process as $pro) {
         $eid = $pro->getExercise()->getId();
         $file = $pro->getRawSubmission()->getFile();
         // die unverarbeitete Einsendung (Dateiinhalt im body oder Adresse)
         $timestamp = $file->getTimeStamp();
         // der Eingangsstempel müsste natürlich schon existieren, ansonsten gilt dieser als Eingangszeitpunkt (wird nicht verwendet)
         if ($timestamp === null) {
             $timestamp = time();
         }
         // es muss eine Einsendung vorhanden sein, welche bearbeitet werden kann
         if ($file !== null) {
             $fileName = $file->getDisplayName();
             $file = $file->getBody(true);
             // gibt den Inhalt der Einsendung an $file
             // es muss ein Einsendungsinhalt vorhanden sein
             if ($file !== null) {
                 $fail = false;
                 // eindeutigen temporären Ordner für diesen Vorgang erstellen und den Inhalt dort platzieren
                 $fileHash = sha1($file);
                 $filePath = $this->tempdir('/tmp/', $fileHash);
                 file_put_contents($filePath . '/' . $fileName, $file);
                 // der $pro->getParameter() wurden beim Erstellen der Verarbeitung festgelegt und enthält
                 // sowohl den aufzurufenden Compiler als auch weitere Aufrufparameter
                 $parameter = explode(' ', strtolower($pro->getParameter()));
                 if (count($parameter) >= 2) {
                     $type = array_shift($parameter);
                     if ($type == 'cx') {
                         // behandelt Einsendungen für den cx Compiler
                         $output = array();
                         $return = '';
                         // ersetzt $file durch den Dateinamen der Einsendung und generiert
                         // die zusätzlichen Aufrufparameter für den Compiler
                         $param = implode(' ', $parameter);
                         if ($param != '') {
                             $param = str_replace('$file', $fileName, $param);
                         } else {
                             $param = $fileName;
                         }
                         // passt das Arbeitsverzeichnis an und führt das Skript für den
                         // cx Compiler aus
                         $pathOld = getcwd();
                         chdir($filePath);
                         exec('(./start_cx ' . $param . ') 2>&1', $output, $return);
                         chdir($pathOld);
                         if (count($output) > 0 && $output[count($output) - 1] === '201') {
                             // wenn wir als Antwort eine 201 vom Skript erhalten, konnte alles problemlos
                             // kompiliert werden
                             $pro->setStatus(201);
                         } else {
                             // ansonsten gab es ein Problem, also einen Fehlerstatus zurückgeben
                             $pro->setStatus(409);
                             // die Antwort des Compilers wird nun noch für die Ausgabe der Fehlermeldung angepasst
                             if (count($output) > 0) {
                                 $text = '';
                                 unset($output[count($output) - 1]);
                                 foreach ($output as $out) {
                                     $pos = strpos($out, ',');
                                     $text .= $out . "\n";
                                 }
                                 $pro->addMessage($text);
                             }
                             $this->app->response->setStatus(409);
                         }
                     } elseif ($type == 'java') {
                         // behandelt Einsendungen für den Java Compiler
                         $output = array();
                         $return = '';
                         // ersetzt $file durch den Dateinamen der Einsendung und generiert
                         // die zusätzlichen Aufrufparameter für den Compiler
                         $param = implode(' ', $parameter);
                         if ($param != '') {
                             $param = str_replace('$file', $fileName, $param);
                         } else {
                             $param = $fileName;
                         }
                         // passt das Arbeitsverzeichnis an und führt das Skript für den
                         // java Compiler (javac) aus. Die Ausgabe erfolgt dabei in $return
                         $pathOld = getcwd();
                         chdir($filePath);
                         exec('(javac ' . $param . ') 2>&1', $output, $return);
                         chdir($pathOld);
                         if ($return == 0) {
                             // wenn wir als Antwort eine 0 erhalten, konnte alles problemlos
                             // kompiliert werden
                             $pro->setStatus(201);
                         } else {
                             // ansonsten gab es ein Problem, also einen Fehlerstatus zurückgeben
                             $pro->setStatus(409);
                             // die Antwort des Compilers muss nun noch Studentengerecht zusammengebaut werden
                             // für die Fehlermeldung
                             if (count($output) > 0) {
                                 $text = '';
                                 $outputList = array();
                                 // entfernt störende Zeichen
                                 foreach ($output as $out) {
                                     $out = trim(trim($out), '^');
                                     if ($out == '') {
                                         continue;
                                     }
                                     $outputList[] = $out;
                                 }
                                 // nur die ersten 7 Fehler und die Zusammenfassung am Ende (2 Zeilen) behalten
                                 if (count($outputList) > 10) {
                                     $outputList[7] = '...';
                                     for ($i = 8; $i < count($outputList) - 2; $i++) {
                                         $outputList[$i] = '';
                                     }
                                 }
                                 // nur nichtleere Ausgabezeilen werden zur Aufgabe zusammengefasst
                                 foreach ($outputList as $out) {
                                     if ($out == '') {
                                         continue;
                                     }
                                     $text .= $out . "\n";
                                 }
                                 // die fertige Fehlermeldung dem Prozessobjekt übergeben
                                 $pro->addMessage($text);
                             }
                             $this->app->response->setStatus(409);
                         }
                     } elseif ($type == 'custom') {
                         // es können natürlich auch individuelle Programmaufrufe damit konfiguriert werden
                         // dabei liefern die Parameter auch das aufzurufende Programm mit (Gefahr???)
                         $output = array();
                         $return = '';
                         $param = implode(' ', $parameter);
                         if ($param != '') {
                             $param = str_replace('$file', $filePath . '/' . $fileName, $param);
                         } else {
                             $param = $filePath . '/' . $fileName;
                         }
                         exec('(' . $param . ') 2>&1', $output, $return);
                         if ($return == 0) {
                             // nothing
                             $pro->setStatus(201);
                         } else {
                             $pro->setStatus(409);
                             if (count($output) > 0) {
                                 $text = '';
                                 $outputList = $output;
                                 if (count($outputList) > 10) {
                                     $outputList[4] = '...';
                                     for ($i = 5; $i < count($outputList) - 5; $i++) {
                                         $outputList[$i] = '';
                                     }
                                 }
                                 foreach ($outputList as $out) {
                                     if ($out == '') {
                                         continue;
                                     }
                                     $text .= $out . "\n";
                                 }
                                 $pro->addMessage($text);
                             }
                             $this->app->response->setStatus(409);
                         }
                     }
                 } else {
                     // es wurden womöglich nicht genug Parameter übergeben
                     // er verlangt mindestens den Compiler und einen Parameter
                     // Bsp.: java $file
                 }
                 // nachdem die Einsendung bearbeitet wurde, kann das temporäre
                 // Verzeichnis mit Inhalt entfernt werden
                 $this->deleteDir($filePath);
                 // das Prozessobjekt kann nun zur Ausgabe hinzugefügt werden
                 $res[] = $pro;
                 continue;
             }
         }
     }
     // wenn nur ein einzelnes Objekt als Eingabe kam, wir auch nur ein Einzelobjekt
     // zurückgegeben, ansonsten eine Liste
     if (!$arr && count($res) == 1) {
         $this->app->response->setBody(Process::encodeProcess($res[0]));
     } else {
         $this->app->response->setBody(Process::encodeProcess($res));
     }
 }
Exemplo n.º 5
0
 public function get($functionName, $sqlFile, $pre = '', $processid, $courseid, $esid, $eid, $comid, $singleResult = false, $checkSession = true)
 {
     $this->loadConfig($pre);
     $pre = ($pre === '' ? '' : '_') . $pre;
     Logger::Log('starts GET ' . $functionName, LogLevel::DEBUG);
     // checks whether incoming data has the correct data type
     $pre = DBJson::mysql_real_escape_string($pre);
     $courseid = DBJson::mysql_real_escape_string($courseid);
     $esid = DBJson::mysql_real_escape_string($esid);
     $eid = DBJson::mysql_real_escape_string($eid);
     $processid = DBJson::mysql_real_escape_string($processid);
     $comid = DBJson::mysql_real_escape_string($comid);
     // starts a query, by using a given file
     $result = DBRequest::getRoutedSqlFile($this->query, $sqlFile, array('pre' => $pre, 'processid' => $processid, 'courseid' => $courseid, 'esid' => $esid, 'eid' => $eid, 'comid' => $comid), $checkSession);
     // checks the correctness of the query
     if ($result['status'] >= 200 && $result['status'] <= 299) {
         $query = Query::decodeQuery($result['content']);
         if (is_array($query)) {
             $query = $query[count($query) - 1];
         }
         if ($query->getNumRows() > 0) {
             $res = Process::ExtractProcess($query->getResponse(), $singleResult);
             $this->_app->response->setBody(Process::encodeProcess($res));
             $this->_app->response->setStatus(200);
             if (isset($result['headers']['Content-Type'])) {
                 $this->_app->response->headers->set('Content-Type', $result['headers']['Content-Type']);
             }
             $this->_app->stop();
         } else {
             $result['status'] = 404;
         }
     }
     Logger::Log('GET ' . $functionName . ' failed', LogLevel::ERROR);
     $this->_app->response->setStatus(isset($result['status']) ? $result['status'] : 409);
     $this->_app->response->setBody(Process::encodeProcess(new Process()));
     $this->_app->stop();
 }
Exemplo n.º 6
0
                        foreach ($processorParameter as $tempKey => $Data) {
                            $Data2 = array();
                            foreach ($Data as &$dat) {
                                if ($dat !== '') {
                                    $Data2[] = $dat;
                                }
                            }
                            if (isset($tempProcessors[$tempKey])) {
                                $tempProcessors[$tempKey]->setParameter(implode(' ', array_values($Data2)));
                            }
                        }
                    }
                    $processors = array_merge($processors, $tempProcessors);
                }
            }
            $processes = json_decode(Process::encodeProcess($processors), true);
            #############################
            ### end create_processors ###
            #############################
            #endregion
        }
        $sheet_data['exercises'] = json_decode(Exercise::encodeExercise($exercises), true);
    }
}
if (isset($_POST['action'])) {
    // && $_POST['action'] == "new"
    $timestamp = time();
    $errorInSent = false;
    // validate all sheet data
    $f = new FormEvaluator($_POST);
    $f->checkStringForKey('sheetName', FormEvaluator::REQUIRED, 'error', Language::Get('main', 'invalidSheetName', $langTemplate));