';
//$form->addRadio('choose', get_lang('Primary'));
//$form->addRadio('choose', get_lang('Secondary'));
}
$this->setForm($form);
return $form;
}
/**
* Adds submit button
*
*/
public function renderForm()
{
if (isset($_GET['question_id']) and !empty($_GET['question_id'])) {
$this->buttonList[] = $this->getForm()->addButtonUpdate(get_lang('ModifyQuestionSurvey'), 'save', true);
} else {
$this->buttonList[] = $this->getForm()->addButtonSave(get_lang('CreateQuestionSurvey'), 'save', true);
}
$this->getForm()->addGroup($this->buttonList, 'buttons');
}
/**
* @return FormValidator
*/
public function getForm()
{
return $this->form;
}
/**
* @param FormValidator $form
*/
public function setForm($form)
{
$this->form = $form;
}
/**
* @param array $formData
*
* @return mixed
*/
public function preSave($formData)
{
$counter = Session::read('answer_count');
$answerList = Session::read('answer_list');
if (empty($answerList)) {
$answerList = isset($formData['answers']) ? $formData['answers'] : array();
Session::write('answer_list', $answerList);
}
if (isset($_POST['answers'])) {
$formData['answers'] = $_POST['answers'];
}
if (empty($counter)) {
$counter = count($answerList) - 1;
Session::write('answer_count', $counter);
}
// Moving an answer up
if (isset($_POST['move_up']) && $_POST['move_up']) {
foreach ($_POST['move_up'] as $key => & $value) {
$id1 = $key;
$content1 = $formData['answers'][$id1];
$id2 = $key - 1;
$content2 = $formData['answers'][$id2];
$formData['answers'][$id1] = $content2;
$formData['answers'][$id2] = $content1;
}
}
// Moving an answer down
if (isset($_POST['move_down']) && $_POST['move_down']) {
foreach ($_POST['move_down'] as $key => & $value) {
$id1 = $key;
$content1 = $formData['answers'][$id1];
$id2 = $key + 1;
$content2 = $formData['answers'][$id2];
$formData['answers'][$id1] = $content2;
$formData['answers'][$id2] = $content1;
}
}
/**
* This solution is a little bit strange but I could not find a different solution.
*/
if (isset($_POST['delete_answer'])) {
$deleted = false;
foreach ($_POST['delete_answer'] as $key => & $value) {
$deleted = $key;
$counter--;
Session::write('answer_count', $counter);
}
foreach ($formData['answers'] as $key => & $value) {
if ($key > $deleted) {
$formData['answers'][$key - 1] = $formData['answers'][$key];
unset($formData['answers'][$key]);
}
}
}
// Adding an answer
if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) {
$counter++;
Session::write('answer_count', $counter);
}
// Removing an answer
if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) {
$counter--;
Session::write('answer_count', $counter);
foreach ($formData['answers'] as $index => &$data) {
if ($index > $counter) {
unset($formData['answers'][$index]);
}
}
}
if (!isset($_POST['delete_answer'])) {
if (isset($formData['answers'])) {
foreach ($formData['answers'] as $index => $data) {
if ($index > $counter) {
unset($formData['answers'][$index]);
}
}
for ($i = 0; $i <= $counter; $i++) {
if (!isset($formData['answers'][$i])) {
$formData['answers'][$i] = '';
}
}
}
}
$formData['answers'] = isset($formData['answers']) ? $formData['answers'] : [];
Session::write('answer_list', $formData['answers']);
return $formData;
}
/**
* @param array $surveyData
* @param array $formData
*
* @return mixed
*/
public function save($surveyData, $formData)
{
// Saving a question
if (isset($_POST['buttons']) && isset($_POST['buttons']['save'])) {
Session::erase('answer_count');
Session::erase('answer_list');
$message = SurveyManager::save_question(
$surveyData,
$formData
);
if ($message == 'QuestionAdded' || $message == 'QuestionUpdated') {
header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message.'&'.api_get_cidreq());
exit;
}
}
return $formData;
}
/**
* Adds two buttons. One to add an option, one to remove an option
*
* @param array $data
*
*/
public function addRemoveButtons($data)
{
$this->buttonList['remove_answer'] = $this->getForm()->createElement(
'button',
'remove_answer',
get_lang('RemoveAnswer'),
'minus'
);
if (count($data['answers']) <= 2) {
$this->buttonList['remove_answer']->updateAttributes(
array('disabled' => 'disabled')
);
}
$this->buttonList['add_answer'] = $this->getForm()->createElement(
'button',
'add_answer',
get_lang('AddAnswer'),
'plus'
);
}
/**
* @param FormValidator $form
* @param array $questionData
* @param array $answers
*/
public function render(FormValidator $form, $questionData = array(), $answers = array())
{
return null;
}
}
|