public function post() { if (!Session::$user['id']) { $this->message = "Unauthorized"; $this->status = 403; return; } if (!isset($this->post['name'])) { $this->message = "Missing one or more required parameters"; $this->status = 400; return; } $surveyID = Survey::create($this->post['name'], Session::$user['id']); $survey = new Survey($surveyID); $this->response[$this->slug][] = $survey->apiData(); }
/** * Create a survey * * @param string $id survey id to created sub-resource into * @param string $property * * If creating a survey request body must be an object with properties : * * title * * description (optionnal) * * type : "date" or "text" * * choices : array of mixed * * guests : optionnal array of guests * * rules : rules object * * If adding a choice to a survey (/survey/<id>/choice) request body must be an object with properties : * * value : mixed depending on survey type * * before : optionnal other choice key before which to add the new choice * * @return array * * @throws RestBadParameterException * @throws RestMissingParameterException * @throws RestNotAllowedException */ public static function post($id = null, $property = null) { // Get creation data $data = RestServer::getRequest()->input; if ($id) { // Add new something to an existing survey if (!$property) { throw new RestBadParameterException('property'); } // Get survey and update data $survey = Survey::fromId($id); // Check permissions if (!Auth::isAdmin() || !$survey->owner->is(Auth::user())) { throw new RestNotAllowedException('update survey ' . $survey->id); } $key = null; if ($property == 'choice') { if (!is_object($data) || !property_exists($data, 'value')) { throw new RestBadParameterException('choice'); } $before = property_exists($data, 'before') ? $data->before : null; $key = $survey->insertChoice($data->value, $before); } if ($property == 'guest') { $survey->addGuest($data); $key = $data; } $survey->save(); return array('path' => '/survey/' . $survey->id . '/' . $property . '/' . $key, 'data' => self::cast($survey)); } // Create survey if (!is_object($data)) { throw new RestBadParameterException('survey'); } // Check permissions if (!Survey::canCreate()) { throw new RestNotAllowedException('create survey'); } if (!property_exists($data, 'type') || !$data->type) { throw new RestMissingParameterException('survey.type'); } $survey = Survey::create($data->type); if (!property_exists($data, 'title') || !$data->title) { throw new RestMissingParameterException('survey.title'); } $survey->title = $data->title; if (property_exists($data, 'description') && $data->description) { $survey->description = $data->description; } if (!property_exists($data, 'choices')) { throw new RestMissingParameterException('survey.choices'); } if (!is_array($data->choices)) { throw new RestBadParameterException('survey.choices'); } $survey->choices = $data->choices; // throws if anything wrong if (property_exists($data, 'guests')) { $survey->guests = $data->guests; } if (property_exists($data, 'rules')) { $survey->rules = $data->rules; } $survey->save(); return array('path' => '/survey/' . $survey->id, 'data' => self::cast($survey)); }
} if (getStringFromRequest('post') == "Y") { if (!form_key_is_valid(getStringFromRequest('form_key'))) { exit_form_double_submit(); } $survey_title = getStringFromRequest('survey_title'); $to_add = getStringFromRequest('to_add'); $to_del = getStringFromRequest('to_del'); $is_active = getStringFromRequest('is_active'); if ($survey_id) { /* Modify */ $s->update($survey_title, $to_add, $to_del, $is_active); $feedback = _('UPDATE SUCCESSFUL'); } else { /* Add */ $s->create($survey_title, $to_add, $is_active); $feedback = _('Survey Inserted'); } } /* Order changes */ if (getStringFromRequest('updown') == "Y") { $question_id = getIntFromRequest('question_id'); $is_up = getStringFromRequest('is_up'); $s->updateOrder($question_id, $is_up); $feedback = _('UPDATE SUCCESSFUL'); } /* Error on previous transactions? */ if ($s->isError()) { $feedback = $s->getErrorMessage(); form_release_key(getStringFromRequest("form_key")); }