/** * 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)); } }
/** * 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)); } }
/** * the constructor * * @param $data an assoc array with the object informations */ public function __construct($data = array()) { if ($data === null) { $data = array(); } foreach ($data as $key => $value) { if (isset($key)) { if ($key == 'attachment') { $this->{$key} = Attachment::decodeAttachment($value, false); } else { if ($key == 'workFiles') { $this->{$key} = Attachment::decodeAttachment($value, false); } else { if ($key == 'target') { $this->{$key} = Component::decodeComponent($value, false); } else { if ($key == 'submission') { $this->{$key} = Submission::decodeSubmission($value, false); } else { if ($key == 'rawSubmission') { $this->{$key} = Submission::decodeSubmission($value, false); } else { if ($key == 'marking') { $this->{$key} = Marking::decodeMarking($value, false); } else { if ($key == 'exercise') { $this->{$key} = Exercise::decodeExercise($value, false); } else { $func = 'set' . strtoupper($key[0]) . substr($key, 1); $methodVariable = array($this, $func); if (is_callable($methodVariable)) { $this->{$func}($value); } else { $this->{$key} = $value; } } } } } } } } } } }
if (isset($_GET['downloadCSV'])) { $markings = array(); $newMarkings = -1; foreach ($markingTool_data['groups'] as $key => $group) { if (isset($group['exercises'])) { foreach ($group['exercises'] as $key2 => $exercise) { if (!isset($exercise['submission']['marking']) && (isset($tutorID) && $tutorID != 'all') && (!isset($statusID) || $statusID != -1 && $statusID != 0)) { continue; } if (!isset($exercise['submission']) && (isset($statusID) && $statusID != 0 || isset($tutorID) && $tutorID != 'all')) { continue; } if (isset($exercise['submission'])) { if (isset($exercise['submission']['marking'])) { // 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);
public static function ExtractMarking($data, $singleResult = false, $FileExtension = '', $File2Extension = '', $SubmissionExtension = '', $MarkingExtension = '', $isResult = true) { // generates an assoc array of files by using a defined list of // its attributes $files = DBJson::getObjectsByAttributes($data, File::getDBPrimaryKey(), File::getDBConvert(), $FileExtension); // generates an assoc array of files by using a defined list of // its attributes $files2 = DBJson::getObjectsByAttributes($data, File::getDBPrimaryKey(), File::getDBConvert(), $File2Extension . '2'); // generates an assoc array of a submission by using a defined // list of its attributes $submissions = DBJson::getObjectsByAttributes($data, Submission::getDBPrimaryKey(), Submission::getDBConvert(), $SubmissionExtension . '2'); // concatenates the submissions and the associated files $submissions = DBJson::concatObjectListsSingleResult($data, $submissions, Submission::getDBPrimaryKey(), Submission::getDBConvert()['S_file'], $files2, File::getDBPrimaryKey(), $File2Extension . '2', $SubmissionExtension . '2'); // sets the selectedForGroup attribute foreach ($submissions as &$submission) { if (isset($submission['selectedForGroup'])) { if (isset($submission['id']) && $submission['id'] == $submission['selectedForGroup']) { $submission['selectedForGroup'] = (string) 1; } else { unset($submission['selectedForGroup']); } } } // generates an assoc array of markings by using a defined list of // its attributes $markings = DBJson::getObjectsByAttributes($data, Marking::getDBPrimaryKey(), Marking::getDBConvert(), $MarkingExtension); // concatenates the markings and the associated files $res = DBJson::concatObjectListsSingleResult($data, $markings, Marking::getDBPrimaryKey(), Marking::getDBConvert()['M_file'], $files, File::getDBPrimaryKey(), $FileExtension, $MarkingExtension); // concatenates the markings and the associated submissions $res = DBJson::concatObjectListsSingleResult($data, $res, Marking::getDBPrimaryKey(), Marking::getDBConvert()['M_submission'], $submissions, Submission::getDBPrimaryKey(), $SubmissionExtension . '2', $MarkingExtension); if ($isResult) { // to reindex $res = array_values($res); $res = Marking::decodeMarking($res, false); if ($singleResult == true) { // only one object as result if (count($res) > 0) { $res = $res[0]; } } } return $res; }