/** * 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)); } }
$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++; } }
public static function ExtractChoice($data, $singleResult = false, $ChoiceExtension = '', $isResult = true) { // generates an assoc array of choices by using a defined list of // its attributes $res = DBJson::getResultObjectsByAttributes($data, Choice::getDBPrimaryKey(), Choice::getDBConvert(), $ChoiceExtension); if ($isResult) { // to reindex $res = array_values($res); $res = Choice::decodeChoice($res, false); if ($singleResult == true) { // only one object as result if (count($res) > 0) { $res = $res[0]; } } } return $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 == 'choices') { $this->{$key} = Choice::decodeChoice($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; } } } } }
/** * Adds a choice. * * Called when this component receives an HTTP POST request to * (/$preChoice(/$preForm(/$preExercise)))/choice(/). * * @param int $preForm A optional prefix for the Form table. * @param int $preExercise A optional prefix for the Exercise table. * @param int $choiceid The id of the choice. */ public function addChoice($preChoice = '', $preForm = '', $preExercise = '') { $this->loadConfig($preChoice, $preForm, $preExercise); $preChoice = ($preChoice === '' ? '' : '_') . $preChoice; $preForm = ($preForm === '' ? '' : '_') . $preForm; $preExercise = ($preExercise === '' ? '' : '_') . $preExercise; Logger::Log('starts POST AddChoice', LogLevel::DEBUG); $preChoice = DBJson::mysql_real_escape_string($preChoice); $preForm = DBJson::mysql_real_escape_string($preForm); $preExercise = DBJson::mysql_real_escape_string($preExercise); // decode the received choice data, as an object $insert = Choice::decodeChoice($this->_app->request->getBody()); // always been an array $arr = true; if (!is_array($insert)) { $insert = array($insert); $arr = false; } // this array contains the indices of the inserted objects $res = array(); foreach ($insert as $in) { // starts a query, by using a given file $result = DBRequest::getRoutedSqlFile($this->query, dirname(__FILE__) . '/Sql/AddChoice.sql', array('object' => $in, 'preChoice' => $preChoice, 'preForm' => $preForm, 'preExercise' => $preExercise)); // checks the correctness of the query if ($result['status'] >= 200 && $result['status'] <= 299) { $queryResult = Query::decodeQuery($result['content']); // sets the new auto-increment id $obj = new Choice(); $course = Course::ExtractCourse($queryResult[count($queryResult) - 1]->getResponse(), true); $obj->setChoiceId($course->getId() . '_' . $queryResult[count($queryResult) - 2]->getInsertId()); $res[] = $obj; $this->_app->response->setStatus(201); if (isset($result['headers']['Content-Type'])) { $this->_app->response->headers->set('Content-Type', $result['headers']['Content-Type']); } } else { Logger::Log('POST AddChoice failed', LogLevel::ERROR); $this->_app->response->setStatus(isset($result['status']) ? $result['status'] : 409); $this->_app->response->setBody(Choice::encodeChoice($res)); $this->_app->stop(); } } if (!$arr && count($res) == 1) { $this->_app->response->setBody(Choice::encodeChoice($res[0])); } else { $this->_app->response->setBody(Choice::encodeChoice($res)); } }