Feature #272 - "Surveys" tool: Revision and cleaning, part 1.

skala
Ivan Tcholakov 16 years ago
parent 95e30961df
commit a77ad9efc2
  1. 214
      main/survey/create_new_survey.php

@ -1,46 +1,46 @@
<?php <?php
/* For licensing terms, see /chamilo_license.txt */ /* For licensing terms, see /license.txt */
/** /**
* @package dokeos.survey * @package chamilo.survey
* @author unknown, the initial survey that did not make it in 1.8 because of bad code * @author unknown, the initial survey that did not make it in 1.8 because of bad code
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code
* @author Julio Montoya Armas <gugli100@gmail.com>, Dokeos: Personality Test modification and rewriting large parts of the code * @author Julio Montoya Armas <gugli100@gmail.com>, Chamilo: Personality Test modification and rewriting large parts of the code
* @version $Id: create_new_survey.php 22297 2009-07-22 22:08:30Z cfasanando $ * @version $Id: create_new_survey.php 22297 2009-07-22 22:08:30Z cfasanando $
* *
* @todo only the available platform languages should be used => need an api get_languages and and api_get_available_languages (or a parameter) * @todo only the available platform languages should be used => need an api get_languages and and api_get_available_languages (or a parameter)
*/ */
// name of the language file that needs to be included // Language file that needs to be included
$language_file = 'survey'; $language_file = 'survey';
// including the global dokeos file // Including the global initialization file
require_once '../inc/global.inc.php'; require_once '../inc/global.inc.php';
$this_section=SECTION_COURSES; $this_section = SECTION_COURSES;
// including additional libraries // Including additional libraries
/** @todo check if these are all needed */ /** @todo check if these are all needed */
/** @todo check if the starting / is needed. api_get_path probably ends with an / */ /** @todo check if the starting / is needed. api_get_path probably ends with an / */
//require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php"); //require_once api_get_path(LIBRARY_PATH).'survey.lib.php';
require_once('survey.lib.php'); require_once 'survey.lib.php';
require_once (api_get_path(LIBRARY_PATH).'/fileManage.lib.php'); require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
require_once (api_get_path(CONFIGURATION_PATH) ."/add_course.conf.php"); require_once api_get_path(CONFIGURATION_PATH).'add_course.conf.php';
require_once (api_get_path(LIBRARY_PATH)."/add_course.lib.inc.php"); require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php';
require_once (api_get_path(LIBRARY_PATH)."/course.lib.php"); require_once api_get_path(LIBRARY_PATH).'course.lib.php';
require_once (api_get_path(LIBRARY_PATH)."/groupmanager.lib.php"); require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
require_once (api_get_path(LIBRARY_PATH)."/usermanager.lib.php"); require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php'); require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
$htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
$htmlHeadXtra[] = '<script type="text/javascript" language="javascript"> $htmlHeadXtra[] = '<script type="text/javascript" language="javascript">
function advanced_parameters() { function advanced_parameters() {
if(document.getElementById(\'options\').style.display == \'none\') { if(document.getElementById(\'options\').style.display == \'none\') {
document.getElementById(\'options\').style.display = \'block\'; document.getElementById(\'options\').style.display = \'block\';
document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif',get_lang('Hide'),array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\'; document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif', get_lang('Hide'), array('style' => 'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
} else { } else {
document.getElementById(\'options\').style.display = \'none\'; document.getElementById(\'options\').style.display = \'none\';
document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif',get_lang('Show'),array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\'; document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif', get_lang('Show'), array('style' => 'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
} }
} }
@ -59,11 +59,9 @@ $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
$table_course_survey_rel = Database :: get_main_table(TABLE_MAIN_COURSE_SURVEY); $table_course_survey_rel = Database :: get_main_table(TABLE_MAIN_COURSE_SURVEY);
/** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/ /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
// if user is not teacher or if he's a coach trying to access an element out of his session // If user is not teacher or if he's a coach trying to access an element out of his session
if (!api_is_allowed_to_edit()) if (!api_is_allowed_to_edit()) {
{ if (!api_is_course_coach() || (!empty($_GET['survey_id']) && !api_is_element_in_the_session(TOOL_SURVEY, intval($_GET['survey_id'])))) {
if(!api_is_course_coach() || (!empty($_GET['survey_id']) && !api_is_element_in_the_session(TOOL_SURVEY,intval($_GET['survey_id']))))
{
Display :: display_header(); Display :: display_header();
Display :: display_error_message(get_lang('NotAllowed'), false); Display :: display_error_message(get_lang('NotAllowed'), false);
Display :: display_footer(); Display :: display_footer();
@ -71,31 +69,29 @@ if (!api_is_allowed_to_edit())
} }
} }
// getting the survey information // Getting the survey information
$survey_id = Security::remove_XSS($_GET['survey_id']); $survey_id = Security::remove_XSS($_GET['survey_id']);
$survey_data = survey_manager::get_survey($survey_id); $survey_data = survey_manager::get_survey($survey_id);
$urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40)); $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
if (api_strlen(strip_tags($survey_data['title'])) > 40) { if (api_strlen(strip_tags($survey_data['title'])) > 40) {
$urlname .= '...'; $urlname .= '...';
} }
// breadcrumbs // Breadcrumbs
if ($_GET['action'] == 'add') if ($_GET['action'] == 'add') {
{ $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
$interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
$tool_name = get_lang('CreateNewSurvey'); $tool_name = get_lang('CreateNewSurvey');
} }
if ($_GET['action'] == 'edit' && is_numeric($survey_id)) if ($_GET['action'] == 'edit' && is_numeric($survey_id))
{ {
$interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList')); $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
$interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$survey_id, "name" => strip_tags($urlname)); $interbreadcrumb[] = array('url' => 'survey.php?survey_id='.$survey_id, 'name' => strip_tags($urlname));
$tool_name = get_lang('EditSurvey'); $tool_name = get_lang('EditSurvey');
} }
// getting the default values // Getting the default values
if ($_GET['action'] == 'edit' AND isset($survey_id) AND is_numeric($survey_id)) if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
{
$defaults = $survey_data; $defaults = $survey_data;
$defaults['survey_id'] = $survey_id; $defaults['survey_id'] = $survey_id;
$defaults['anonymous'] = $survey_data['anonymous']; $defaults['anonymous'] = $survey_data['anonymous'];
@ -109,18 +105,17 @@ if ($_GET['action'] == 'edit' AND isset($survey_id) AND is_numeric($survey_id))
$defaults['anonymous'] = 0; $defaults['anonymous'] = 0;
} }
// initiate the object // Initialize the object
$form = new FormValidator('survey', 'post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.$survey_id); $form = new FormValidator('survey', 'post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.$survey_id);
$form->addElement('header', '', $tool_name); $form->addElement('header', '', $tool_name);
// settting the form elements // Settting the form elements
if ($_GET['action'] == 'edit' AND isset($survey_id) AND is_numeric($survey_id)) if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
{
$form->addElement('hidden', 'survey_id'); $form->addElement('hidden', 'survey_id');
} }
$survey_code = $form->addElement('text', 'survey_code', get_lang('SurveyCode'), array('size' => '20','maxlength'=>'20', 'id'=>'surveycode_title')); $survey_code = $form->addElement('text', 'survey_code', get_lang('SurveyCode'), array('size' => '20', 'maxlength' => '20', 'id' => 'surveycode_title'));
//$form->applyFilter('survey_code', 'html_filter'); //$form->applyFilter('survey_code', 'html_filter');
if ($_GET['action'] == 'edit') { if ($_GET['action'] == 'edit') {
@ -135,33 +130,33 @@ $form->addElement('html_editor', 'survey_subtitle', get_lang('SurveySubTitle'),
//Language selection has been disabled. If you want to re-enable, please //Language selection has been disabled. If you want to re-enable, please
//disable the following line (hidden language field). //disable the following line (hidden language field).
$lang_array = api_get_languages(); $lang_array = api_get_languages();
foreach ($lang_array['name'] as $key=>$value) { foreach ($lang_array['name'] as $key => $value) {
$languages[$lang_array['folder'][$key]] = $value; $languages[$lang_array['folder'][$key]] = $value;
} }
$form->addElement('select', 'survey_language', get_lang('Language'), $languages); $form->addElement('select', 'survey_language', get_lang('Language'), $languages);
*/ */
// Pass the language of the survey in the form // Pass the language of the survey in the form
$form->addElement('hidden', 'survey_language'); $form->addElement('hidden', 'survey_language');
$form->addElement('datepickerdate', 'start_date', get_lang('StartDate'), array('form_name'=>'survey')); $form->addElement('datepickerdate', 'start_date', get_lang('StartDate'), array('form_name'=>'survey'));
$form->addElement('datepickerdate', 'end_date', get_lang('EndDate'), array('form_name'=>'survey')); $form->addElement('datepickerdate', 'end_date', get_lang('EndDate'), array('form_name'=>'survey'));
//$group=''; //$group = '';
//$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('Yes'),$form_share_value); //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share', null, get_lang('Yes'), $form_share_value);
/** TODO maybe it is better to change this into false instead see line 95 in survey.lib.php */ /** TODO Maybe it is better to change this into false instead see line 95 in survey.lib.php */
//$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('No'),0); //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share', null, get_lang('No'), 0);
//$form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;'); //$form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;');
$form->addElement('checkbox', 'anonymous', get_lang('Anonymous')); $form->addElement('checkbox', 'anonymous', get_lang('Anonymous'));
$form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false)); $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
$form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false)); $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
/* /*
// Aditional Parameters // Aditional Parameters
$form -> addElement('html','<div class="row"> $form -> addElement('html','<div class="row">
<div class="label">&nbsp;</div> <div class="label">&nbsp;</div>
<div class="formw"> <div class="formw">
<a href="javascript://" onclick="if(document.getElementById(\'options\').style.display == \'none\'){document.getElementById(\'options\').style.display = \'block\';}else{document.getElementById(\'options\').style.display = \'none\';}"><img src="../img/add_na.gif" alt="" />'.get_lang('AdvancedParameters').'</a> <a href="javascript://" onclick="javascript: if(document.getElementById(\'options\').style.display == \'none\'){document.getElementById(\'options\').style.display = \'block\';}else{document.getElementById(\'options\').style.display = \'none\';}"><img src="../img/add_na.gif" alt="" />'.get_lang('AdvancedParameters').'</a>
</div> </div>
</div>');*/ </div>');*/
@ -169,26 +164,24 @@ $form -> addElement('html','<div class="row">
$surveytypes[0] = get_lang('Normal'); $surveytypes[0] = get_lang('Normal');
$surveytypes[1] = get_lang('Conditional'); $surveytypes[1] = get_lang('Conditional');
if ($_GET['action'] == 'add') { if ($_GET['action'] == 'add') {
$form->addElement('hidden','survey_type',0); $form->addElement('hidden', 'survey_type', 0);
$form -> addElement('html','<div id="options" style="display: none;">'); $form -> addElement('html', '<div id="options" style="display: none;">');
require_once(api_get_path(LIBRARY_PATH).'surveymanager.lib.php'); require_once api_get_path(LIBRARY_PATH).'surveymanager.lib.php';
$survey_tree = new SurveyTree(); $survey_tree = new SurveyTree();
$list_surveys = $survey_tree->createList($survey_tree->surveylist); $list_surveys = $survey_tree->createList($survey_tree->surveylist);
$list_surveys[0]=''; $list_surveys[0] = '';
$form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys); $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
$defaults['parent_id']=0; $defaults['parent_id'] = 0;
} }
if ($survey_data['survey_type']==1 || $_GET['action'] == 'add' ) if ($survey_data['survey_type'] == 1 || $_GET['action'] == 'add') {
{
$form->addElement('checkbox', 'one_question_per_page', get_lang('OneQuestionPerPage')); $form->addElement('checkbox', 'one_question_per_page', get_lang('OneQuestionPerPage'));
$form->addElement('checkbox', 'shuffle', get_lang('ActivateShuffle')); $form->addElement('checkbox', 'shuffle', get_lang('ActivateShuffle'));
} }
if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id) ) { if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id)) {
if ($survey_data['anonymous']==0 ) { if ($survey_data['anonymous'] == 0) {
// Aditional Parameters // Aditional Parameters
$form -> addElement('html','<div class="row"> $form -> addElement('html','<div class="row">
@ -200,40 +193,34 @@ if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id)
</div> </div>
</div>'); </div>');
$form->addElement('html','<div id="options" style="display:none">'); $form->addElement('html', '<div id="options" style="display:none">');
$form->addElement('checkbox', 'show_form_profile', get_lang('ShowFormProfile'),'','onclick="javascript:if(this.checked==true){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"'); $form->addElement('checkbox', 'show_form_profile', get_lang('ShowFormProfile'), '', 'onclick="javascript: if(this.checked==true){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"');
if ($survey_data['show_form_profile']== 1) { if ($survey_data['show_form_profile'] == 1) {
$form -> addElement('html','<div id="options_field" style="display:block">'); $form -> addElement('html', '<div id="options_field" style="display:block">');
} else { } else {
$form -> addElement('html','<div id="options_field" style="display:none">'); $form -> addElement('html', '<div id="options_field" style="display:none">');
} }
$field_list=SurveyUtil::make_field_list(); $field_list = SurveyUtil::make_field_list();
if (is_array ($field_list)) if (is_array($field_list)) {
{ // TODO hide and show the list in a fancy DIV
//TODO hide and show the list in a fancy DIV foreach ($field_list as $key => & $field) {
foreach ($field_list as $key=> $field) if ($field['visibility'] == 1) {
{
if ($field['visibility']==1)
{
$form->addElement('checkbox', 'profile_'.$key, ' ','&nbsp;&nbsp;'.$field['name'] ); $form->addElement('checkbox', 'profile_'.$key, ' ','&nbsp;&nbsp;'.$field['name'] );
$input_name_list.= 'profile_'.$key.','; $input_name_list.= 'profile_'.$key.',';
} }
} }
// necesary to know the fields // Necesary to know the fields
$form->addElement('hidden', 'input_name_list', $input_name_list ); $form->addElement('hidden', 'input_name_list', $input_name_list );
//set defaults form fields // Set defaults form fields
if ($survey_data['form_fields']) if ($survey_data['form_fields']) {
{ $form_fields = explode('@', $survey_data['form_fields']);
$form_fields=explode('@',$survey_data['form_fields']); foreach ($form_fields as & $field) {
foreach($form_fields as $field) $field_value = explode(':', $field);
{ if ($field_value[0] != '' && $field_value[1] != '') {
$field_value=explode(':',$field); $defaults[$field_value[0]] = $field_value[1];
if ($field_value[0]!='' && $field_value[1]!= '')
{
$defaults[$field_value[0]]=$field_value[1];
} }
} }
} }
@ -242,67 +229,62 @@ if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id)
} }
} }
if ($_GET['action'] == 'add') { if ($_GET['action'] == 'add') {
$form -> addElement('html','</div><br />'); $form -> addElement('html', '</div><br />');
} }
if(isset($_GET['survey_id']) && $_GET['action']=='edit') { if (isset($_GET['survey_id']) && $_GET['action'] == 'edit') {
$class='save'; $class = 'save';
$text=get_lang('ModifySurvey'); $text = get_lang('ModifySurvey');
} else { } else {
$class='add'; $class = 'add';
$text=get_lang('CreateSurvey'); $text = get_lang('CreateSurvey');
} }
$form->addElement('style_submit_button', 'submit_survey', $text, 'class="'.$class.'"'); $form->addElement('style_submit_button', 'submit_survey', $text, 'class="'.$class.'"');
// setting the rules // Setting the rules
if ($_GET['action'] == 'add') { if ($_GET['action'] == 'add') {
$form->addRule('survey_code', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required'); $form->addRule('survey_code', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('survey_code', '', 'maxlength',20); $form->addRule('survey_code', '', 'maxlength', 20);
} }
$form->addRule('survey_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required'); $form->addRule('survey_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('start_date', get_lang('InvalidDate'), 'date'); $form->addRule('start_date', get_lang('InvalidDate'), 'date');
$form->addRule('end_date', get_lang('InvalidDate'), 'date'); $form->addRule('end_date', get_lang('InvalidDate'), 'date');
$form->addRule(array ('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte'); $form->addRule(array('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
// setting the default values // Setting the default values
$form->setDefaults($defaults); $form->setDefaults($defaults);
// The validation or display // The validation or display
if( $form->validate() ) if ($form->validate()) {
{ // Exporting the values
// exporting the values
$values = $form->exportValues(); $values = $form->exportValues();
// storing the survey // Storing the survey
$return = survey_manager::store_survey($values); $return = survey_manager::store_survey($values);
/*// deleting the shared survey if the survey is getting unshared (this only happens when editing) /*// Deleting the shared survey if the survey is getting unshared (this only happens when editing)
if (is_numeric($survey_data['survey_share']) AND $values['survey_share']['survey_share'] == 0 AND $values['survey_id']<>'') if (is_numeric($survey_data['survey_share']) && $values['survey_share']['survey_share'] == 0 && $values['survey_id'] != '') {
{
survey_manager::delete_survey($survey_data['survey_share'], true); survey_manager::delete_survey($survey_data['survey_share'], true);
} }
// storing the already existing questions and options of a survey that gets shared (this only happens when editing) // Storing the already existing questions and options of a survey that gets shared (this only happens when editing)
if ($survey_data['survey_share']== 0 AND $values['survey_share']['survey_share'] !== 0 AND $values['survey_id']<>'') if ($survey_data['survey_share'] == 0 && $values['survey_share']['survey_share'] !== 0 && $values['survey_id'] != '') {
{
survey_manager::get_complete_survey_structure($return['id']); survey_manager::get_complete_survey_structure($return['id']);
} }
*/ */
if($return['type'] == 'error') if ($return['type'] == 'error') {
{
// Displaying the header // Displaying the header
Display::display_header($tool_name); Display::display_header($tool_name);
// display the error // Display the error
Display::display_error_message(get_lang($return['message']), false); Display::display_error_message(get_lang($return['message']), false);
// display the form // Display the form
$form->display(); $form->display();
} }
if ($config['survey']['debug']) if ($config['survey']['debug']) {
{ // Displaying a feedback message
// displaying a feedback message
Display::display_confirmation_message($return['message'], false); Display::display_confirmation_message($return['message'], false);
} else { } else {
// redirecting to the survey page (whilst showing the return message // Redirecting to the survey page (whilst showing the return message
header('location:survey.php?survey_id='.$return['id'].'&message='.$return['message']); header('location:survey.php?survey_id='.$return['id'].'&message='.$return['message']);
} }
} else { } else {
@ -310,9 +292,9 @@ if( $form->validate() )
Display::display_header($tool_name); Display::display_header($tool_name);
// Displaying the tool title // Displaying the tool title
//api_display_tool_title($tool_name); //api_display_tool_title($tool_name);
// display the form // Display the form
$form->display(); $form->display();
} }
// Footer // Footer
Display :: display_footer(); Display :: display_footer();
?>

Loading…
Cancel
Save