Beispiel #1
0
 /**
  * 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);
     }
 }
Beispiel #2
0
                    if ($mimeType == '') {
                        continue;
                    }
                    $mimeType = explode('.', trim(strtolower($mimeType)));
                    $ending = isset($mimeType[1]) ? $mimeType[1] : null;
                    $mimeType = $mimeType[0];
                    if (FILE_TYPE::checkSupportedFileType($mimeType) == false) {
                        $errormsg = Language::Get('main', 'invalidFileType', $langTemplate);
                        array_push($notifications, MakeNotification('warning', $errormsg));
                        $correctExercise = true;
                        //break;
                    } else {
                        // if mime-type is supported add mimeTypes
                        $mimes = FILE_TYPE::getMimeTypeByFileEnding(trim(strtolower($mimeType)));
                        foreach ($mimes as &$mime) {
                            $mime = ExerciseFileType::createExerciseFileType(null, $mime . '' . (isset($ending) ? ' *.' . $ending : ''), null);
                        }
                        $mimeTypes = array_merge($mimeTypes, $mimes);
                    }
                }
            }
            // save mimeTypes in validated Exercises
            $validatedExercises[$key1][$key2]['mime-type'] = $mimeTypes;
        }
    }
}
$forms = array();
$processors = array();
if (isset($_POST['action'])) {
    if ($correctExercise == true) {
        // get sheetPDF