} } $notifications[] = MakeNotification('error', $errormsg); continue; } else { $result = Submission::decodeSubmission($result); // if using forms, upload user input if (isset($exercise['choices'])) { $i = 0; foreach ($formdata as &$form) { $choices = $form->getChoices(); foreach ($choices as &$choice) { $choice->setSubmissionId($result->getId()); } $URL = $serverURI . '/DB/DBChoice/formResult/choice'; $result2 = http_post_data($URL, Choice::encodeChoice($choices), true, $message); if ($message != "201") { $result2 = Choice::decodeChoice($result2); $exercise = $key + 1; $errormsg = Language::Get('main', 'errorUploadSubmission', $langTemplate, array('status' => $message, 'exerciseName' => $exercise['name'])); if ($result2 !== null) { $errormsg .= "<br><br>"; $messages2 = $result2->getMessages(); foreach ($messages2 as $message) { $errormsg .= str_replace("\n", '<br>', $message) . '<br>'; } } $notifications[] = MakeNotification('error', $errormsg); continue; } $i++;
/** * Adds a form. * * Called when this component receives an HTTP POST request to * /form(/). */ public function addForm() { Logger::Log('starts POST AddForm', LogLevel::DEBUG); $header = $this->app->request->headers->all(); $body = $this->app->request->getBody(); $this->app->response->setStatus(201); $forms = Form::decodeForm($body); // always been an array $arr = true; if (!is_array($forms)) { $forms = array($forms); $arr = false; } // this array contains the indices of the inserted objects $res = array(); $choices = array(); foreach ($forms as $key => $form) { $choices[] = $form->getChoices(); $forms[$key]->setChoices(null); } $resForms = array(); foreach ($forms as $form) { $method = 'POST'; $URL = '/form'; if ($form->getFormId() !== null) { $method = 'PUT'; $URL = '/form/' . $form->getFormId(); } $result = Request::routeRequest($method, $URL, array(), Form::encodeForm($form), $this->_form, 'form'); // checks the correctness of the query if ($result['status'] >= 200 && $result['status'] <= 299 && isset($result['content'])) { $newform = Form::decodeForm($result['content']); if ($form->getFormId() === null) { $form->setFormId($newform->getFormId()); } $resForms[] = $form; } else { $f = new Form(); $f->setStatus(409); $resForms[] = $f; } } $forms = $resForms; $i = 0; foreach ($choices as &$choicelist) { foreach ($choicelist as $key2 => $choice) { if ($forms[$i]->getFormId() !== null) { $formId = $forms[$i]->getFormId(); $choicelist[$key2]->setFormId($formId); $method = 'POST'; $URL = '/choice'; if ($choicelist[$key2]->getChoiceId() !== null) { $method = 'PUT'; $URL = '/choice/' . $choice->getChoiceId(); } $result = Request::routeRequest($method, $URL, array(), Choice::encodeChoice($choicelist[$key2]), $this->_choice, 'choice'); if ($result['status'] >= 200 && $result['status'] <= 299) { $newchoice = Choice::decodeChoice($result['content']); if ($choicelist[$key2]->getChoiceId() === null) { $choicelist[$key2]->setChoiceId($newchoice->getChoiceId()); } $choicelist[$key2]->setStatus(201); } else { $choicelist[$key2]->setStatus(409); } } } $forms[$i]->setChoices($choicelist); $i++; } // checks the correctness of the query /*if ( $result['status'] >= 200 && $result['status'] <= 299 && isset($result['content'])){ $newforms = Form::decodeForm($result['content']); if ( !is_array( $newforms ) ) $newforms = array($newforms); $i=0; foreach ($forms as &$form){ if ($form->getFormId() === null) $form->setFormId($newforms[$i]->getFormId()); $i++; } $sendChoices = array(); $i=0; foreach ( $choices as $choicelist ){ foreach ( $choicelist as $choice ){ $choice->setFormId($forms[$i]->getFormId()); $sendChoices[] = $choice; } $i++; } $choices = $sendChoices; $result = Request::routeRequest( 'POST', '/choice', $this->app->request->headers->all( ), Choice::encodeChoice($choices), $this->_choice, 'choice' ); // checks the correctness of the query if ( $result['status'] >= 200 && $result['status'] <= 299 ){ $newchoices = Choice::decodeChoice($result['content']); $choicelist = array(); $i=0; foreach ( $choices as &$choice ){ $choice->setChoiceId($newchoices[$i]->getChoiceId()); if (!isset($choicelist[$choice->getFormId()])) $choicelist[$choice->getFormId()] = array(); $choicelist[$choice->getFormId()][] = $choice; $i++; } foreach ( $forms as &$form ){ $form->setChoices($choicelist[$form->getFormId()]); } $res[] = $forms; } else{ // remove forms on failure foreach ($forms as $form){ $result = Request::routeRequest( 'DELETE', '/form/'.$form->getFormId(), $this->app->request->headers->all( ), '', $this->_form, 'form' ); } $res[] = null; $this->app->response->setStatus( 409 ); } } else { $res[] = null; $this->app->response->setStatus( 409 ); }*/ if ($this->app->response->getStatus() != 201) { Logger::Log('POST AddForms failed', LogLevel::ERROR); } if (!$arr && count($res) == 1) { $this->app->response->setBody(Form::encodeForm($res[0])); } else { $this->app->response->setBody(Form::encodeForm($res)); } }
public function get($functionName, $sqlFile, $preChoice, $preForm, $preExercise, $formid, $choiceid, $courseid, $esid, $eid, $singleResult = false, $checkSession = true) { $this->loadConfig($preChoice, $preForm, $preExercise); $preChoice = ($preChoice === '' ? '' : '_') . $preChoice; $preForm = ($preForm === '' ? '' : '_') . $preForm; $preExercise = ($preExercise === '' ? '' : '_') . $preExercise; Logger::Log('starts GET ' . $functionName, LogLevel::DEBUG); // checks whether incoming data has the correct data type $choiceid = DBJson::mysql_real_escape_string($choiceid); $formid = DBJson::mysql_real_escape_string($formid); $preChoice = DBJson::mysql_real_escape_string($preChoice); $preForm = DBJson::mysql_real_escape_string($preForm); $preExercise = DBJson::mysql_real_escape_string($preExercise); // starts a query, by using a given file $result = DBRequest::getRoutedSqlFile($this->query, $sqlFile, array('formid' => $formid, 'choiceid' => $choiceid, 'courseid' => $courseid, 'esid' => $esid, 'eid' => $eid, 'preChoice' => $preChoice, 'preForm' => $preForm, 'preExercise' => $preExercise), $checkSession); // 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 = Choice::ExtractChoice($query->getResponse(), $singleResult); $this->_app->response->setBody(Choice::encodeChoice($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(Choice::encodeChoice(new Choice())); $this->_app->stop(); }