Fix survey buttons actions add/remove.

1.10.x
Julio Montoya 10 years ago
parent ba14abce06
commit a05032c82d
  1. 7
      main/inc/lib/formvalidator/FormValidator.class.php
  2. 14
      main/inc/lib/pear/HTML/QuickForm/button.php
  3. 3
      main/inc/lib/pear/HTML/QuickForm/element.php
  4. 6
      main/survey/ch_dropdown.php
  5. 11
      main/survey/ch_multiplechoice.php
  6. 11
      main/survey/ch_multipleresponse.php
  7. 4
      main/survey/ch_personality.php
  8. 6
      main/survey/ch_score.php
  9. 9
      main/survey/ch_yesno.php
  10. 4
      main/survey/fillsurvey.php
  11. 2
      main/survey/preview.php
  12. 33
      main/survey/question.php
  13. 101
      main/survey/survey_question.php

@ -207,16 +207,17 @@ EOT;
* @param string $name
* @param string $label
* @param string $icon font-awesome
* @param string $style default|primary|success|info|warning|danger|link
* @param string $size large|default|small|extra-small
* @param string $class Example plus is transformed to icon fa fa-plus
* @param array $attributes
*
* @return HTML_QuickForm_button
*/
public function addButton($name, $label, $icon = 'check', $class = 'btn btn-default', $attributes = array())
public function addButton($name, $label, $icon = 'check', $style = 'default', $size = 'default', $class = 'btn', $attributes = array())
{
//$attributes['class'] = isset($attributes['class']) ? $attributes['class'] : 'btn btn-default';
$attributes['icon'] = $icon;
$attributes['class'] = $class;
$attributes['class'] = $class.' btn-'.$style.' btn-'.$size;
return $this->addElement('button', $name, $label, $attributes);
}

@ -48,8 +48,17 @@ class HTML_QuickForm_button extends HTML_QuickForm_input
public function HTML_QuickForm_button(
$elementName = null,
$value = null,
$attributes = null
$icon = 'check',
$style = 'default',
$size = 'default',
$class = 'btn',
$attributes = array()
) {
$attributes['icon'] = $icon;
$attributes['style'] = $style;
$attributes['size'] = $size;
$attributes['class'] = $class;
HTML_QuickForm_input::HTML_QuickForm_input(
$elementName,
null,
@ -70,8 +79,9 @@ class HTML_QuickForm_button extends HTML_QuickForm_input
} else {
$value = $this->_attributes['value'];
unset($this->_attributes['value']);
//$class = isset($this->_attributes['class']) ? $this->_attributes['class'] : 'btn btn-large';
$icon = isset($this->_attributes['icon']) ? $this->_attributes['icon'] : 'check';
unset($this->_attributes['icon']);
$icon = '<i class="fa fa-'.$icon.'"></i> ';
return

@ -394,7 +394,7 @@ class HTML_QuickForm_element extends HTML_Common
* @access public
* @return void
*/
function onQuickFormEvent($event, $arg, &$caller)
public function onQuickFormEvent($event, $arg, &$caller)
{
switch ($event) {
case 'createElement':
@ -409,6 +409,7 @@ class HTML_QuickForm_element extends HTML_Common
case 'updateValue':
// constant values override both default and submitted ones
// default values are overriden by submitted
$value = $this->_findValue($caller->_constantValues);
if (null === $value) {

@ -11,9 +11,9 @@ class ch_dropdown extends survey_question
* @param $formData
* @return FormValidator
*/
public function create_form($survey_data, $formData)
public function createForm($survey_data, $formData)
{
parent::create_form($survey_data, $formData);
parent::createForm($survey_data, $formData);
// The answers
/*$this->html .= ' <div class="row">';
@ -51,7 +51,7 @@ class ch_dropdown extends survey_question
}
}
return parent :: add_remove_buttons($formData);
parent :: addRemoveButtons($formData);
}
/**

@ -11,9 +11,9 @@ class ch_multiplechoice extends survey_question
* @param array $formData
* @return FormValidator
*/
public function create_form($survey_data, $formData)
public function createForm($survey_data, $formData)
{
parent::create_form($survey_data, $formData);
parent::createForm($survey_data, $formData);
$options = array(
'horizontal' => get_lang('Horizontal'),
@ -22,12 +22,12 @@ class ch_multiplechoice extends survey_question
$this->getForm()->addRadio('horizontalvertical', get_lang('DisplayAnswersHorVert'), $options);
$formData['horizontalvertical'] = isset($formData['horizontalvertical']) ? $formData['horizontalvertical'] : 'horizontal';
$this->getForm()->setDefaults($formData);
$config = array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120');
$total = count($formData['answers']);
if (is_array($formData['answers'])) {
foreach ($formData['answers'] as $key => $value) {
$this->getForm()->addHtmlEditor('answers['.$key.']', null, false, false, $config);
if ($key < $total-1) {
@ -39,13 +39,12 @@ class ch_multiplechoice extends survey_question
}
if ($total> 2) {
$this->getForm()->addButton("delete_answer[$key]", get_lang('Delete'));
$this->getForm()->addButton("delete_answer[$key]", get_lang('Delete'), 'trash', 'danger');
}
}
}
$this->getForm()->setDefaults($formData);
return parent :: add_remove_buttons($formData);
parent :: addRemoveButtons($formData);
}
/**

@ -7,12 +7,12 @@
class ch_multipleresponse extends survey_question
{
/**
* @param array $survey_data
* @param array $surveyData
* @param array $formData
*/
public function create_form($survey_data, $formData)
public function createForm($surveyData, $formData)
{
parent::create_form($survey_data, $formData);
parent::createForm($surveyData, $formData);
$options = array(
'horizontal' => get_lang('Horizontal'),
'vertical' => get_lang('Vertical')
@ -20,6 +20,7 @@ class ch_multipleresponse extends survey_question
$this->getForm()->addRadio('horizontalvertical', get_lang('DisplayAnswersHorVert'), $options);
$formData['horizontalvertical'] = isset($formData['horizontalvertical']) ? $formData['horizontalvertical'] : 'horizontal';
$this->getForm()->setDefaults($formData);
$config = array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120');
if (is_array($formData['answers'])) {
@ -45,9 +46,7 @@ class ch_multipleresponse extends survey_question
}
}
$this->getForm()->setDefaults($formData);
return parent :: add_remove_buttons($formData);
parent :: addRemoveButtons($formData);
}
/**

@ -13,9 +13,9 @@ class ch_personality extends survey_question
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version January 2007
*/
public function create_form($survey_data, $form_content)
public function createForm($survey_data, $form_content)
{
parent::create_form($survey_data, $form_content);
parent::createForm($survey_data, $form_content);
$this->html .= ' <tr>';
$this->html .= ' <td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
$this->html .= ' </tr>';

@ -10,9 +10,9 @@ class ch_score extends survey_question
* @param array $survey_data
* @param $form_content
*/
public function create_form($survey_data, $formData)
public function createForm($survey_data, $formData)
{
parent::create_form($survey_data, $formData);
parent::createForm($survey_data, $formData);
$this->getForm()->addText('maximum_score', get_lang('MaximumScore'));
@ -40,7 +40,7 @@ class ch_score extends survey_question
}
}
return parent :: add_remove_buttons($formData);
parent :: addRemoveButtons($formData);
}
/**

@ -7,12 +7,12 @@
class ch_yesno extends survey_question
{
/**
* @param array $survey_data
* @param array $surveyData
* @param array $formData
*/
public function create_form($survey_data, $formData)
public function createForm($surveyData, $formData)
{
parent::create_form($survey_data, $formData);
parent::createForm($surveyData, $formData);
$options = array(
'horizontal' => get_lang('Horizontal'),
@ -21,6 +21,7 @@ class ch_yesno extends survey_question
$this->getForm()->addRadio('horizontalvertical', get_lang('DisplayAnswersHorVert'), $options);
$formData['horizontalvertical'] = isset($formData['horizontalvertical']) ? $formData['horizontalvertical'] : 'horizontal';
$this->getForm()->setDefaults($formData);
/*// Horizontal or vertical
$this->html .= ' <div class="control-group">';
@ -46,8 +47,6 @@ class ch_yesno extends survey_question
$this->getForm()->addHtmlEditor('answers[0]', get_lang('AnswerOptions'), true, false, $config);
$this->getForm()->addHtmlEditor('answers[1]', null, true, false, $config);
$this->getForm()->setDefaults($formData);
/*$this->html .= ' <div class="row">';
$this->html .= ' <label class="control-label">';
$this->html .= get_lang('AnswerOptions');

@ -1171,7 +1171,7 @@ if ($survey_data['survey_type'] === '0') {
// The normal survey as always
if (($show < $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
if ($show == 0) {
$form->addButton('next_survey_page', get_lang('StartSurvey'), 'arrow-right', 'btn btn-success btn-large');
$form->addButton('next_survey_page', get_lang('StartSurvey'), 'arrow-right', 'success', 'large');
// echo '<button type="submit" name="next_survey_page" class="survey-next">'.get_lang('StartSurvey').'</button>';
} else {
$form->addButton('next_survey_page', get_lang('Next'), 'arrow-right');
@ -1188,7 +1188,7 @@ if ($survey_data['survey_type'] === '0') {
$numberofpages = count($paged_questions);
if (($show < $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
if ($show == 0) {
$form->addButton('next_survey_page', get_lang('StartSurvey'), 'arrow-right', 'btn btn-success btn-large');
$form->addButton('next_survey_page', get_lang('StartSurvey'), 'arrow-right', 'success', 'large');
//echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('StartSurvey').'</button>';
} else {
$form->addButton('next_survey_page', get_lang('Next'), 'arrow-right');

@ -225,7 +225,7 @@ if (api_is_course_admin() ||
if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0)) {
if ($show == 0) {
$form->addButton('next_survey_page', get_lang('StartSurvey'), 'arrow-right', 'btn btn-success btn-large');
$form->addButton('next_survey_page', get_lang('StartSurvey'), 'arrow-right', 'success', 'large');
//echo '<br /><button type="submit" name="next_survey_page" class="next">'.get_lang('StartSurvey').' </button>';
} else {
$form->addButton('next_survey_page', get_lang('NextQuestion'), 'arrow-right');

@ -79,8 +79,8 @@ if ($surveyData['survey_type'] == 1) {
}
// Breadcrumbs
$interbreadcrumb[] = array ('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php', 'name' => get_lang('SurveyList'));
$interbreadcrumb[] = array ('url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']), 'name' => strip_tags($urlname));
$interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php', 'name' => get_lang('SurveyList'));
$interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']), 'name' => strip_tags($urlname));
// Tool name
if ($_GET['action'] == 'add') {
@ -111,7 +111,7 @@ $actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id
$actions .= '</div>';
// Checking if it is a valid type
if (!in_array($_GET['type'], $possible_types)) {
Display :: display_header($tool_name,'Survey');
Display :: display_header($tool_name, 'Survey');
echo $actions;
Display :: display_error_message(get_lang('TypeDoesNotExist'), false);
Display :: display_footer();
@ -121,23 +121,6 @@ $error_message = '';
// Displaying the form for adding or editing the question
/*if (!isset($_POST['save_question'])) {
// Displaying the header
Display::display_header($tool_name, 'Survey');
echo $actions;
// Displys message if exists
if (isset($_SESSION['temp_sys_message'])) {
$error_message = $_SESSION['temp_sys_message'];
unset($_SESSION['temp_sys_message']);
if ($error_message == 'PleaseEnterAQuestion' ||
$error_message == 'PleasFillAllAnswer'||
$error_message == 'PleaseChooseACondition'||
$error_message == 'ChooseDifferentCategories'
) {
Display::display_error_message(get_lang($error_message), true);
}
}
}*/
$ch_type = 'ch_'.$_GET['type'];
/** @var survey_question $surveyQuestion */
$surveyQuestion = new $ch_type;
@ -170,19 +153,17 @@ if (isset($_GET['question_id']) && !empty($_GET['question_id'])) {
$formData = survey_manager::get_question($_GET['question_id']);
}
$formData = $surveyQuestion->preAction($formData);
$surveyQuestion->create_form($surveyData, $formData);
$formData = $surveyQuestion->preSave($formData);
$surveyQuestion->createForm($surveyData, $formData);
$surveyQuestion->getForm()->setDefaults($formData);
$surveyQuestion->render_form();
$surveyQuestion->renderForm();
if ($surveyQuestion->getForm()->validate()) {
$values = $surveyQuestion->getForm()->getSubmitValues();
$surveyQuestion->handle_action($surveyData, $values);
$surveyQuestion->save($surveyData, $values);
}
Display::display_header($tool_name, 'Survey');
$surveyQuestion->getForm()->setDefaults($formData);
echo $surveyQuestion->getForm()->return_form();
// Footer

@ -10,6 +10,7 @@ class survey_question
{
/** @var FormValidator */
private $form;
public $buttonList = array();
/**
* Generic part of any survey question: the question field
@ -18,7 +19,7 @@ class survey_question
*
* @return FormValidator
*/
public function create_form($surveyData, $formData)
public function createForm($surveyData, $formData)
{
$action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null;
$questionId = isset($_GET['question_id']) ? intval($_GET['question_id']) : null;
@ -104,7 +105,6 @@ class survey_question
//$form->addRadio('choose', get_lang('Secondary'));
}
$form->setDefaults($formData);
$this->setForm($form);
return $form;
@ -114,7 +114,7 @@ class survey_question
* Adds submit button
*
*/
public function render_form()
public function renderForm()
{
if (isset($_GET['question_id']) and !empty($_GET['question_id'])) {
$icon = 'pencil';
@ -124,7 +124,9 @@ class survey_question
$text = get_lang('CreateQuestionSurvey');
}
$this->getForm()->addButton('save', $text, $icon);
$this->buttonList[] = $this->getForm()->createElement('button', 'save', $text, $icon);
$this->getForm()->addGroup($this->buttonList, 'buttons');
}
/**
@ -145,10 +147,28 @@ class survey_question
/**
* @param array $formData
*
* @return mixed
*/
public function preAction($formData)
public function preSave($formData)
{
$counter = Session::read('answer_count');
$answerList = Session::read('answer_list');
if (empty($answerList)) {
$answerList = $formData['answers'];
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) {
@ -177,9 +197,11 @@ class survey_question
* 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) {
unset($formData['answers'][$key]);
$deleted = $key;
$counter--;
Session::write('answer_count', $counter);
}
foreach ($formData['answers'] as $key => & $value) {
if ($key > $deleted) {
@ -189,11 +211,6 @@ class survey_question
}
}
$counter = Session::read('answer_count');
if (empty($counter)) {
$counter = count($formData['answers']) - 1;
Session::write('answer_count', $counter);
}
// Adding an answer
if (isset($_POST['add_answer'])) {
@ -212,28 +229,32 @@ class survey_question
}
}
foreach ($formData['answers'] as $index => $data) {
if ($index > $counter) {
unset($formData['answers'][$index]);
if (!isset($_POST['delete_answer'])) {
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] = '';
for ($i = 0; $i <= $counter; $i++) {
if (!isset($formData['answers'][$i])) {
$formData['answers'][$i] = '';
}
}
}
Session::write('answer_list', $formData['answers']);
return $formData;
}
/**
* Handles the actions on a question and its answers
* @param array $surveyData
* @param array $formData
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version January 2007
* @return mixed
*/
public function handle_action($surveyData, $formData)
public function save($surveyData, $formData)
{
// Saving a question
if (isset($_POST['save_question']) &&
@ -244,6 +265,7 @@ class survey_question
!isset($_POST['move_up'])
) {
Session::erase('answer_count');
Session::erase('answer_list');
$message = survey_manager::save_question(
$surveyData,
$formData
@ -252,17 +274,6 @@ class survey_question
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;
} else {
/*if ($message == 'PleaseEnterAQuestion' || $message == 'PleasFillAllAnswer'|| $message == 'PleaseChooseACondition'|| $message == 'ChooseDifferentCategories') {
$_SESSION['temp_user_message'] = $formData['question'];
$_SESSION['temp_horizontalvertical'] = $formData['horizontalvertical'];
$_SESSION['temp_sys_message'] = $message;
$_SESSION['temp_answers'] = $formData['answers'];
$_SESSION['temp_values'] = $formData['values'];
header('location: '.api_get_path(WEB_CODE_PATH).'survey/question.php?'.api_get_cidreq().'&question_id='.intval($_GET['question_id']).'&survey_id='.intval($_GET['survey_id']).'&action='.Security::remove_XSS($_GET['action']).'&type='.Security::remove_XSS($_GET['type']).'');
exit;
}*/
}
}
@ -272,18 +283,30 @@ class survey_question
/**
* Adds two buttons. One to add an option, one to remove an option
*
* @param FormValidator $form
* @param array $data
*
* @return FormValidator
*/
public function add_remove_buttons($data)
public function addRemoveButtons($data)
{
$removeButton = $this->getForm()->addButton('remove_answer', get_lang('RemoveAnswer'), 'minus');
$this->buttonList['remove_answer'] = $this->getForm()->createElement(
'button',
'remove_answer',
get_lang('RemoveAnswer'),
'minus'
);
if (count($data['answers']) <= 2) {
$removeButton->updateAttributes(array('disabled' => 'disabled'));
$this->buttonList['remove_answer']->updateAttributes(
array('disabled' => 'disabled')
);
}
$this->getForm()->addButton('add_answer', get_lang('AddAnswer'), 'plus');
$this->buttonList['add_answer'] = $this->getForm()->createElement(
'button',
'add_answer',
get_lang('AddAnswer'),
'plus'
);
}
/**

Loading…
Cancel
Save