/** * 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); } }
/** * 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 == 'exercises') { $this->{$key} = Exercise::decodeExercise($value, false); } elseif ($key == 'sheetFile' || $key == 'sampleSolution') { $this->{$key} = File::decodeFile($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; } } } } }
/** * 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; } } } } } } } } } } }
$URL = $databaseURI . "/exercisefiletype/exercisesheet/{$sheetId}"; $removed = http_delete($URL, true); $exerciseMap = array(); foreach ($exercises as $key => &$exercise) { $exercise->setSheetId($sheetId); $exerciseMap[$key] = array($key, $exercise->getId()); if ($exercise->getId() < 0) { $exercise->setId(null); } } // Post Excercise to logic Controller to create it $exercisesJSON = Exercise::encodeExercise($exercises); ///echo $exercisesJSON; $output2 = http_post_data($logicURI . "/exercise", $exercisesJSON, true, $message); if ($message == 201) { $exercises2 = Exercise::decodeExercise($output2); foreach ($exercises2 as $key => &$exercise2) { if ($exercises[$key]->getId() !== null) { $exerciseMap[$key][] = $exercises[$key]->getId(); } else { $exerciseMap[$key][] = $exercise2->getId(); } if ($exercise2->getId() !== null) { $exercises[$key]->setId($exercise2->getId()); } } #region create_forms ########################## ### begin create_forms ### ########################## if ($errorInSent == false) {
public static function ExtractExercise($data, $singleResult = false, $ExerciseExtension = '', $AttachmentExtension = '', $SubmissionExtension = '', $FileTypeExtension = '', $isResult = true) { // generates an assoc array of an exercise by using a defined // list of its attributes $exercise = DBJson::getObjectsByAttributes($data, Exercise::getDBPrimaryKey(), Exercise::getDBConvert(), $ExerciseExtension); // generates an assoc array of files by using a defined // list of its attributes $attachments = DBJson::getObjectsByAttributes($data, File::getDBPrimaryKey(), File::getDBConvert(), $AttachmentExtension); // generates an assoc array of submissions by using a defined // list of its attributes $submissions = DBJson::getObjectsByAttributes($data, Submission::getDBPrimaryKey(), Submission::getDBConvert(), $SubmissionExtension . '2'); // generates an assoc array of exercise file types by using a defined // list of its attributes $fileTypes = DBJson::getObjectsByAttributes($data, ExerciseFileType::getDBPrimaryKey(), ExerciseFileType::getDBConvert(), $FileTypeExtension); // 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']); } } } // concatenates the exercise and the associated filetypes $exercise = DBJson::concatObjectListResult($data, $exercise, Exercise::getDBPrimaryKey(), Exercise::getDBConvert()['E_fileTypes'], $fileTypes, ExerciseFileType::getDBPrimaryKey(), $FileTypeExtension, $ExerciseExtension); // concatenates the exercise and the associated attachments $res = DBJson::concatObjectListResult($data, $exercise, Exercise::getDBPrimaryKey(), Exercise::getDBConvert()['E_attachments'], $attachments, File::getDBPrimaryKey(), $AttachmentExtension, $ExerciseExtension); // concatenates the exercise and the associated submissions $res = DBJson::concatObjectLists($data, $res, Exercise::getDBPrimaryKey(), Exercise::getDBConvert()['E_submissions'], $submissions, Submission::getDBPrimaryKey(), $SubmissionExtension . '2', $ExerciseExtension); if ($isResult) { // to reindex $res = array_values($res); $res = Exercise::decodeExercise($res, false); if ($singleResult) { // only one object as result if (count($res) > 0) { $res = $res[0]; } } } return $res; }