Пример #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));
     }
 }
Пример #2
0
 public function uploadZip($userid, $courseid)
 {
     // error array of strings
     $errors = array();
     LTutor::generatepath($this->config['DIR']['temp']);
     $tempDir = $this->tempdir($this->config['DIR']['temp'], 'extractZip', $mode = 0775);
     $body = File::decodeFile($this->app->request->getBody());
     //1 file-Object
     $filename = $tempDir . '/' . $courseid . '.zip';
     file_put_contents($filename, $body->getBody(true));
     unset($body);
     $zip = new ZipArchive();
     $zip->open($filename);
     $zip->extractTo($tempDir . '/files');
     $zip->close();
     unlink($filename);
     ///$this->deleteDir(dirname($filename));
     unset($zip);
     $files = $tempDir . '/files';
     // check if csv file exists
     if (file_exists($files . '/Liste.csv')) {
         $csv = fopen($files . '/Liste.csv', "r");
         if (($transactionId = fgetcsv($csv, 0, ';')) === false) {
             fclose($csv);
             $this->deleteDir($tempDir);
             $this->app->response->setStatus(409);
             $errors[] = 'empty .csv file';
             $this->app->response->setBody(json_encode($errors));
             $this->app->stop();
         }
         $result = Request::routeRequest('GET', '/transaction/authentication/TutorCSV_' . $userid . '_' . $courseid . '/transaction/' . $transactionId[0], array(), '', $this->_getTransaction, 'transaction');
         if (isset($result['status']) && $result['status'] == 200 && isset($result['content'])) {
             $transaction = Transaction::decodeTransaction($result['content']);
             $transaction = json_decode($transaction->getContent(), true);
             unset($result);
             $defaultOrder = array('ID', 'NAME', 'USERNAME', 'POINTS', 'MAXPOINTS', 'OUTSTANDING', 'STATUS', 'TUTORCOMMENT', 'STUDENTCOMMENT', 'FILE');
             $currectOrder = $defaultOrder;
             $markings = array();
             while (($row = fgetcsv($csv, 0, ';')) !== false) {
                 if (substr($row[0], 0, 2) == '--') {
                     $row[0] = substr($row[0], 2);
                     if (in_array(strtoupper($row[0]), $defaultOrder)) {
                         $currectOrder = array();
                         foreach ($row as $ro) {
                             $currectOrder[strtoupper($ro)] = count($currectOrder);
                         }
                     }
                 } elseif (implode('', $row) != '' && substr($row[0], 0, 2) != '--') {
                     if (isset($currectOrder['ID']) && !isset($row[$currectOrder['ID']]) || isset($currectOrder['POINTS']) && !isset($row[$currectOrder['POINTS']]) || isset($currectOrder['FILE']) && !isset($row[$currectOrder['FILE']]) || isset($currectOrder['TUTORCOMMENT']) && !isset($row[$currectOrder['TUTORCOMMENT']]) || isset($currectOrder['OUTSTANDING']) && !isset($row[$currectOrder['OUTSTANDING']]) || isset($currectOrder['STATUS']) && !isset($row[$currectOrder['STATUS']])) {
                         $errors[] = 'invalid Liste.csv';
                         fclose($csv);
                         $this->deleteDir($tempDir);
                         $this->app->response->setStatus(409);
                         $this->app->response->setBody(json_encode($errors));
                         $this->app->stop();
                     }
                     $markingId = isset($currectOrder['ID']) ? $row[$currectOrder['ID']] : null;
                     $points = isset($currectOrder['POINTS']) ? $row[$currectOrder['POINTS']] : null;
                     $points = str_replace(',', '.', $points);
                     $markingFile = isset($currectOrder['FILE']) ? $row[$currectOrder['FILE']] : null;
                     // check if markingId exists in transaction
                     if (!isset($transaction['markings'][$markingId])) {
                         // unknown markingId
                         fclose($csv);
                         $this->deleteDir($tempDir);
                         $this->app->response->setStatus(409);
                         $errors[] = "unknown ID: {$markingId}";
                         $this->app->response->setBody(json_encode($errors));
                         $this->app->stop();
                     }
                     ///var_dump($transaction['markings'][$markingId]);
                     $markingData = $transaction['markings'][$markingId];
                     // checks whether the points are less or equal to the maximum points
                     if ($points > $markingData['maxPoints'] || $points < 0) {
                         // too much points
                         ///fclose($csv);
                         ///$this->deleteDir($tempDir);
                         ///$this->app->response->setStatus(409);
                         ///$errors[] = "incorrect points in marking: {$markingId}";
                         ///$this->app->response->setBody(json_encode($errors));
                         ///$this->app->stop();
                     }
                     // checks if file with this markingid exists
                     if ($markingFile == null || $markingFile == '' || file_exists($files . '/' . $markingFile)) {
                         if ($markingFile != '' && $markingFile != null) {
                             $fileAddress = $files . '/' . $markingFile;
                             ///file_get_contents($files.'/'.$markingFile);
                             // file
                             $fileInfo = pathinfo($markingFile);
                             $file = new File();
                             $file->setDisplayName($fileInfo['basename']);
                             $file->setBody(Reference::createReference($fileAddress));
                         } else {
                             $file = null;
                         }
                         if (isset($transaction['markings'][$markingId]['submissionId']) && $transaction['markings'][$markingId]['submissionId'] < 0) {
                             // create new submission object
                             $submissionId = $transaction['markings'][$markingId]['submissionId'];
                             $studentId = $transaction['markings'][$markingId]['studentId'];
                             $exerciseId = $transaction['markings'][$markingId]['exerciseId'];
                             $submission = Submission::createSubmission(null, $studentId, null, $exerciseId, null, 1, time(), null, $leaderId, 1);
                             $submission->setSelectedForGroup('1');
                             ///echo json_encode($submission);return;
                             $result = Request::routeRequest('POST', '/submission', array(), json_encode($submission), $this->_postSubmission, 'submission');
                             if ($result['status'] == 201) {
                                 $transaction['markings'][$markingId]['submissionId'] = json_decode($result['content'], true)['id'];
                             }
                         }
                         // create new marking object
                         $marking = Marking::createMarking($markingId < 0 ? null : $markingId, $userid, null, $transaction['markings'][$markingId]['submissionId'], isset($currectOrder['TUTORCOMMENT']) ? $row[$currectOrder['TUTORCOMMENT']] : null, isset($currectOrder['OUTSTANDING']) ? $row[$currectOrder['OUTSTANDING']] : null, isset($currectOrder['STATUS']) ? $row[$currectOrder['STATUS']] : null, $points, time(), $file == null ? 1 : 0);
                         $marking->setFile($file);
                         /*array(
                           'id' => ($markingId<0 ? null : $markingId),
                           'points' => $points,
                           'outstanding' => isset($currectOrder['OUTSTANDING']) ? $row[$currectOrder['OUTSTANDING']] : null,
                           'tutorId' => $userid,
                           'tutorComment' => isset($currectOrder['TUTORCOMMENT']) ? $row[$currectOrder['TUTORCOMMENT']] : null,
                           'file' => $file,
                           'status' => isset($currectOrder['STATUS']) ? $row[$currectOrder['STATUS']] : null,
                           'date' => time(),
                           'hideFile' => 
                           );*/
                         $markings[] = $marking;
                     } else {
                         //if file with this markingid not exists
                         $errors[] = 'File does not exist: ' . $markingFile;
                         fclose($csv);
                         $this->deleteDir($tempDir);
                         $this->app->response->setStatus(409);
                         $this->app->response->setBody(json_encode($errors));
                         $this->app->stop();
                     }
                 }
             }
             $mark = @json_encode($markings);
             if ($mark !== false) {
                 ///echo json_encode($markings); return;
                 //request to database to edit the markings
                 $result = Request::routeRequest('POST', '/marking', array(), $mark, $this->_postMarking, 'marking');
                 /// TODO: prüfen ob jede hochgeladen wurde
                 if ($result['status'] != 201) {
                     $errors[] = 'send markings failed';
                 }
             } else {
                 $errors[] = 'invalid input';
             }
         } else {
             $errors[] = 'no transaction data';
         }
         fclose($csv);
     } else {
         // if csv file does not exist
         $errors[] = '.csv file does not exist in uploaded zip-Archiv';
     }
     $this->deleteDir($tempDir);
     $this->app->response->setBody(json_encode($errors));
     if (!($errors == array())) {
         $this->app->response->setStatus(409);
     }
 }
Пример #3
0
                        // submission + marking
                        $tempMarking = Marking::decodeMarking(json_encode($exercise['submission']['marking']));
                        $tempSubmission = Submission::decodeSubmission(json_encode($exercise['submission']));
                        $tempMarking->setSubmission($tempSubmission);
                        $markings[] = $tempMarking;
                    } else {
                        // no marking
                        $tempMarking = Marking::createMarking($newMarkings, $uid, null, $exercise['submission']['id'], null, null, 1, null, $timestamp, null);
                        $newMarkings--;
                        $tempSubmission = Submission::decodeSubmission(json_encode($exercise['submission']));
                        $tempMarking->setSubmission($tempSubmission);
                        $markings[] = $tempMarking;
                    }
                } else {
                    // no submission
                    $tempMarking = Marking::createMarking($newMarkings, $uid, null, null, null, null, 1, null, $timestamp, null);
                    $tempSubmission = Submission::createSubmission($newMarkings, $group['leader']['id'], null, $exercise['id'], null, null, $timestamp, null, $group['leader']['id'], null);
                    $tempSubmission->setSelectedForGroup(1);
                    $newMarkings--;
                    $tempMarking->setSubmission($tempSubmission);
                    $markings[] = $tempMarking;
                }
            }
        }
    }
    $URI = $logicURI . '/tutor/archive/user/' . $uid . '/exercisesheet/' . $sid . '/withnames';
    $csvFile = http_post_data($URI, Marking::encodeMarking($markings), true);
    echo $csvFile;
    exit(0);
}
$markingTool_data['filesystemURI'] = $filesystemURI;
Пример #4
0
/**
 * Stores a marking in the database.
 *
 * @param $points The points of the marking
 * @param $tutorComment The tutor's comment
 * @param $status The status of the marking
 * @param $submissionID The id of the submission, if set, -1 otherwise
 * @param $markingID The id of the marking, if set, -1 otherwise
 * @param $leaderID The id of the group leader
 * @param $tutorID The id of the tutor who creates the marking
 * @param $eID The id of the exercisesheet
 *
 * @return bool Returns true on success, false otherwise 
 */
function saveMarking($points, $tutorComment, $status, $submissionID, $markingID, $leaderID, $tutorID, $eID)
{
    global $databaseURI;
    // submission and marking already exist and don't
    // need to be created before adding the marking data
    if ($submissionID != -1 && $markingID != -1) {
        $newMarking = Marking::createMarking($markingID, $tutorID, null, null, $tutorComment, null, $status, $points, time());
        $newMarking = Marking::encodeMarking($newMarking);
        $URI = $databaseURI . "/marking/{$markingID}";
        http_put_data($URI, $newMarking, true, $message);
        if ($message != 201) {
            return false;
        } else {
            return true;
        }
    } elseif ($submissionID != -1 && $markingID == -1) {
        // only the submission exists, the marking still
        // needs to be created before adding the marking data
        // creates the marking in the database
        $marking = createMarking($points, $tutorComment, $status, $submissionID, $tutorID);
        if (empty($marking)) {
            return false;
        } else {
            return true;
        }
    } elseif ($submissionID == -1 && $markingID == -1) {
        // neither the submission nor the marking exist - they both
        // need to be created before adding the marking data
        // creates the submission in the database
        $submission = createSubmission($leaderID, $eID);
        if (!empty($submission)) {
            // creates the marking in the database
            $submissionID = $submission['id'];
            $marking = createMarking($points, $tutorComment, $status, $submissionID, $tutorID);
            if (!empty($marking)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}