/** * Adds an exercise. * * Called when this component receives an HTTP POST request to * /exercise(/). * The request body should contain a JSON object representing an array of exercises */ public function addExercise() { $header = $this->app->request->headers->all(); $body = json_decode($this->app->request->getBody(), true); $allright = true; $result = array(); if (isset($body) == true && empty($body) == false) { foreach ($body as $subexercise) { // create exercise in DB $FileTypesArrayTemp = null; if (isset($subexercise['fileTypes'])) { $FileTypesArrayTemp = $subexercise['fileTypes']; unset($subexercise['fileTypes']); } $subexerciseJSON = json_encode($subexercise); $URL = $this->lURL . '/DB/exercise'; $method = 'POST'; if (isset($subexercise['id']) && $subexercise['id'] !== null) { $method = 'PUT'; $URL = $this->lURL . '/DB/exercise/' . $subexercise['id']; } $subexerciseAnswer = Request::custom($method, $URL, $header, $subexerciseJSON); if ($subexerciseAnswer['status'] == 201) { $subexerciseOutput = json_decode($subexerciseAnswer['content'], true); if (isset($subexercise['id'])) { $result[] = $subexercise; $subexerciseOutput = $subexercise; } else { $result[] = Exercise::decodeExercise($subexerciseAnswer['content']); } if (isset($subexerciseOutput['id'])) { $linkid = $subexerciseOutput['id']; } // create attachement in DB and FS if (isset($subexercise['attachments']) && !empty($subexercise['attachments'])) { foreach ($subexercise['attachments'] as &$attachment) { $attachment['exerciseId'] = $linkid; } $attachments = $subexercise['attachments']; $tempAttachments = array(); foreach ($attachments as $attachment) { $temp = Attachment::createAttachment(null, $attachment['exerciseId'], null, null); $temp->setFile($attachment); $tempAttachments[] = $temp; } $res = Request::routeRequest('POST', '/attachment', $header, Attachment::encodeAttachment($tempAttachments), $this->_postAttachment, 'attachment'); // checks the correctness of the query if ($res['status'] >= 200 && $res['status'] <= 299) { // ... } else { $allright = false; break; } } // create ExerciseFileTypes if (isset($FileTypesArrayTemp) && !empty($FileTypesArrayTemp)) { foreach ($FileTypesArrayTemp as $fileType) { $myExerciseFileType = ExerciseFileType::createExerciseFileType(NULL, $fileType['text'], $linkid); $myExerciseFileTypeJSON = ExerciseFileType::encodeExerciseFileType($myExerciseFileType); $URL = $this->lURL . "/DB/exercisefiletype"; $FileTypesAnswer = Request::custom('POST', $URL, $header, $myExerciseFileTypeJSON); if ($FileTypesAnswer['status'] != 201) { $allright = false; break; } } } if ($allright == false) { break; } } else { $allright = false; break; } } } if ($allright == true) { $this->app->response->setBody(Exercise::encodeExercise($result)); $this->app->response->setStatus(201); } else { $this->app->response->setStatus(409); } }
/** * Adds a process. * * Called when this component receives an HTTP POST request to * /process(/). */ public function AddProcess() { $this->app->response->setStatus(201); $body = $this->app->request->getBody(); $processes = Process::decodeProcess($body); // always been an array $arr = true; if (!is_array($processes)) { $processes = array($processes); $arr = false; } $res = array(); foreach ($processes as $process) { // create process $method = 'POST'; $URL = '/process'; if ($process->getProcessId() !== null) { $method = 'PUT'; $URL = '/process/' . $process->getProcessId(); } $result = Request::routeRequest($method, $URL, array(), Process::encodeProcess($process), $this->_processorDb, 'process'); if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = Process::decodeProcess($result['content']); if ($process->getProcessId() === null) { $process->setProcessId($queryResult->getProcessId()); } $res[] = $process; } else { $res[] = null; $this->app->response->setStatus(409); continue; } // create attachment $attachments = $process->getAttachment(); $process->setAttachment(array()); foreach ($attachments as $attachment) { if ($attachment->getId() === null) { $attachment->setExerciseId($process->getExercise()->getId()); $attachment->setProcessId($process->getProcessId()); // upload file $result = Request::routeRequest('POST', '/file', array(), File::encodeFile($attachment->getFile()), $this->_file, 'file'); if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = File::decodeFile($result['content']); $attachment->setFile($queryResult); } else { $attachment->setFile(null); $this->app->response->setStatus(409); continue; } // upload attachment $attachment->setProcessId($process->getProcessId()); $result = Request::routeRequest('POST', '/attachment', array(), Attachment::encodeAttachment($attachment), $this->_attachment, 'attachment'); if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = Attachment::decodeAttachment($result['content']); $attachment->setId($queryResult->getId()); $process->getAttachment()[] = $attachment; } else { $process->getAttachment()[] = null; $this->app->response->setStatus(409); continue; } } } // create workFiles $workFiles = $process->getWorkFiles(); $process->setWorkFiles(array()); foreach ($workFiles as $workFile) { if ($workFile->getId() === null) { $workFile->setExerciseId($process->getExercise()->getId()); $workFile->setProcessId($process->getProcessId()); // upload file $result = Request::routeRequest('POST', '/file', array(), File::encodeFile($workFile->getFile()), $this->_file, 'file'); if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = File::decodeFile($result['content']); $workFile->setFile($queryResult); } else { $workFile->setFile(null); $this->app->response->setStatus(409); continue; } // upload attachment $workFile->setProcessId($process->getProcessId()); $result = Request::routeRequest('POST', '/attachment', array(), Attachment::encodeAttachment($workFile), $this->_workFiles, 'attachment'); if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = Attachment::decodeAttachment($result['content']); $workFile->setId($queryResult->getId()); $process->getWorkFiles()[] = $workFile; } else { $process->getWorkFiles()[] = null; $this->app->response->setStatus(409); continue; } } } } if (!$arr && count($res) == 1) { $this->app->response->setBody(Process::encodeProcess($res[0])); } else { $this->app->response->setBody(Process::encodeProcess($res)); } }
public function get($functionName, $sqlFile, $userid, $courseid, $esid, $eid, $suid, $aid, $singleResult = false, $checkSession = true) { Logger::Log('starts GET ' . $functionName, LogLevel::DEBUG); // checks whether incoming data has the correct data type DBJson::checkInput($this->_app, $userid == '' ? true : ctype_digit($userid), $courseid == '' ? true : ctype_digit($courseid), $esid == '' ? true : ctype_digit($esid), $eid == '' ? true : ctype_digit($eid), $suid == '' ? true : ctype_digit($suid), $aid == '' ? true : ctype_digit($aid)); // starts a query, by using a given file $result = DBRequest::getRoutedSqlFile($this->query, $sqlFile, array('userid' => $userid, 'courseid' => $courseid, 'esid' => $esid, 'eid' => $eid, 'suid' => $suid, 'aid' => $aid), $checkSession); // checks the correctness of the query if ($result['status'] >= 200 && $result['status'] <= 299) { $query = Query::decodeQuery($result['content']); if ($query->getNumRows() > 0) { $res = Attachment::ExtractAttachment($query->getResponse(), $singleResult); $this->_app->response->setBody(Attachment::encodeAttachment($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(Attachment::encodeAttachment(new Attachment())); $this->_app->stop(); }
public function get($functionName, $sqlFile, $pre = '', $userid, $courseid, $esid, $eid, $suid, $aid, $singleResult = false) { $this->loadConfig($pre); $pre = ($pre === '' ? '' : '_') . $pre; Logger::Log('starts GET ' . $functionName, LogLevel::DEBUG); $pre = DBJson::mysql_real_escape_string($pre); $userid = DBJson::mysql_real_escape_string($userid); $courseid = DBJson::mysql_real_escape_string($courseid); $esid = DBJson::mysql_real_escape_string($esid); $eid = DBJson::mysql_real_escape_string($eid); $suid = DBJson::mysql_real_escape_string($suid); $aid = DBJson::mysql_real_escape_string($aid); // starts a query, by using a given file $result = DBRequest::getRoutedSqlFile($this->query, $sqlFile, array('pre' => $pre, 'userid' => $userid, 'courseid' => $courseid, 'esid' => $esid, 'eid' => $eid, 'suid' => $suid, 'aid' => $aid)); // 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 = Attachment::ExtractAttachment($query->getResponse(), $singleResult); $this->_app->response->setBody(Attachment::encodeAttachment($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(Attachment::encodeAttachment(new Attachment())); $this->_app->stop(); }
/** * Adds an attachment. * * Called when this component receives an HTTP POST request to * /attachment(/). * The request body should contain a JSON object representing the * attachment's attributes. */ public function addAttachment() { $header = $this->app->request->headers->all(); $body = $this->app->request->getBody(); $fileObjects = Attachment::decodeAttachment($body); // always been an array $arr = true; if (!is_array($fileObjects)) { $fileObjects = array($fileObjects); $arr = false; } $res = array(); $files = array(); foreach ($fileObjects as $fileObject) { $files[] = $fileObject->getFile(); } //add the Files $result = Request::routeRequest('POST', '/file', array(), File::encodeFile($files), $this->_postFile, 'file'); $tempFiles = File::decodeFile($result['content']); // checks the correctness of the query if ($result['status'] === 201 && isset($result['content'])) { // upload files $countObjects = count($fileObjects); for ($i = 0; $i < $countObjects; $i++) { if ($tempFiles[$i]->getStatus() === 201) { $fileObjects[$i]->setFile($tempFiles[$i]); if ($files[$i] !== null) { $files[$i]->setStatus(201); } } else { $fileObjects[$i]->setStatus(409); $fileObjects[$i]->addMessage("Die Datei konnte nicht gespeichert werden."); if ($files[$i] !== null) { $files[$i]->setBody(); } } } } else { $this->app->response->setStatus(409); $this->app->response->setBody(Attachment::encodeAttachment(new Attachment())); $this->app->stop(); } // upload attachments $result = Request::routeRequest('POST', '/attachment', array(), Attachment::encodeAttachment($fileObjects), $this->_postAttachment, 'attachment'); if ($result['status'] === 201 && isset($result['content'])) { $tempAttachments = Attachment::decodeAttachment($result['content']); $countObjects = count($fileObjects); for ($i = 0; $i < $countObjects; $i++) { $fileObjects[$i]->setStatus($tempAttachments[$i]->getStatus()); $fileObjects[$i]->addMessages($tempAttachments[$i]->getMessages()); if ($tempAttachments[$i]->getStatus() !== 201) { $fileObjects[$i]->addMessage('Anhang konnte nicht erstellt werden.'); $fileObjects[$i]->getFile()->setBody(); } else { $fileObjects[$i]->setId($tempAttachments[$i]->getId()); } $res[] = $fileObjects[$i]; } } else { $this->app->response->setStatus(409); $this->app->response->setBody(Attachment::encodeAttachment(new Attachment())); $this->app->stop(); } if (!$arr && count($res) == 1) { $res = $res[0]; } $this->app->response->setBody(Attachment::encodeAttachment($res)); $this->app->response->setStatus(201); }