create_new_survey.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  14. Mail: info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. * @package dokeos.survey
  19. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  20. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code
  21. * @version $Id: create_new_survey.php 14940 2008-04-17 17:44:03Z juliomontoya $
  22. *
  23. * @todo only the available platform languages should be used => need an api get_languages and and api_get_available_languages (or a parameter)
  24. */
  25. // name of the language file that needs to be included
  26. $language_file = 'survey';
  27. // including the global dokeos file
  28. require_once ('../inc/global.inc.php');
  29. // including additional libraries
  30. /** @todo check if these are all needed */
  31. /** @todo check if the starting / is needed. api_get_path probably ends with an / */
  32. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  33. require_once('survey.lib.php');
  34. require_once (api_get_path(LIBRARY_PATH).'/fileManage.lib.php');
  35. require_once (api_get_path(CONFIGURATION_PATH) ."/add_course.conf.php");
  36. require_once (api_get_path(LIBRARY_PATH)."/add_course.lib.inc.php");
  37. require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
  38. require_once (api_get_path(LIBRARY_PATH)."/groupmanager.lib.php");
  39. require_once (api_get_path(LIBRARY_PATH)."/usermanager.lib.php");
  40. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  41. // Database table definitions
  42. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  43. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  44. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  45. $table_course_survey_rel = Database :: get_main_table(TABLE_MAIN_COURSE_SURVEY);
  46. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  47. if (!api_is_allowed_to_edit())
  48. {
  49. Display :: display_header();
  50. Display :: display_error_message(get_lang('NotAllowed'), false);
  51. Display :: display_footer();
  52. exit;
  53. }
  54. // getting the survey information
  55. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  56. $urlname = substr(strip_tags($survey_data['title']), 0, 40);
  57. if (strlen(strip_tags($survey_data['title'])) > 40)
  58. {
  59. $urlname .= '...';
  60. }
  61. // breadcrumbs
  62. if ($_GET['action'] == 'add')
  63. {
  64. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  65. $tool_name = get_lang('CreateNewSurvey');
  66. }
  67. if ($_GET['action'] == 'edit' AND is_numeric($_GET['survey_id']))
  68. {
  69. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  70. $interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$_GET['survey_id'], "name" => $urlname);
  71. $tool_name = get_lang('EditSurvey');
  72. }
  73. // getting the default values
  74. if ($_GET['action'] == 'edit' AND isset($_GET['survey_id']) AND is_numeric($_GET['survey_id']))
  75. {
  76. $defaults = $survey_data;
  77. $defaults['survey_id'] = $_GET['survey_id'];
  78. /*
  79. $defaults['survey_share'] = array();
  80. $defaults['survey_share']['survey_share'] = $survey_data['survey_share'];
  81. if (!is_numeric($survey_data['survey_share']) OR $survey_data['survey_share'] == 0)
  82. {
  83. $form_share_value = 'true';
  84. }
  85. else
  86. {
  87. $form_share_value = $defaults['survey_share']['survey_share'];
  88. }
  89. */
  90. $defaults['anonymous'] = $survey_data['anonymous'];
  91. }
  92. else
  93. {
  94. $defaults['survey_language'] = $_course['language'];
  95. $defaults['start_date'] = date('d-F-Y H:i');
  96. $startdateandxdays = time() + 864000; // today + 10 days
  97. $defaults['end_date'] = date('d-F-Y H:i', $startdateandxdays);
  98. //$defaults['survey_share']['survey_share'] = 0;
  99. //$form_share_value = 1;
  100. $defaults['anonymous'] = 0;
  101. }
  102. // initiate the object
  103. $form = new FormValidator('survey', 'post', api_get_self().'?action='.$_GET['action'].'&survey_id='.$_GET['survey_id']);
  104. // settting the form elements
  105. if ($_GET['action'] == 'edit' AND isset($_GET['survey_id']) AND is_numeric($_GET['survey_id']))
  106. {
  107. $form->addElement('hidden', 'survey_id');
  108. }
  109. $form->addElement('text', 'survey_code', get_lang('SurveyCode'), array('size' => '40'));
  110. $fck_attribute['Width'] = '100%';
  111. $fck_attribute['Height'] = '100';
  112. $fck_attribute['ToolbarSet'] = 'Survey';
  113. $form->addElement('html_editor', 'survey_title', get_lang('SurveyTitle'));
  114. $form->addElement('html_editor', 'survey_subtitle', get_lang('SurveySubTitle'));
  115. $lang_array = api_get_languages();
  116. foreach ($lang_array['name'] as $key=>$value)
  117. {
  118. $languages[$lang_array['folder'][$key]] = $value;
  119. }
  120. $form->addElement('select', 'survey_language', get_lang('Language'), $languages);
  121. $form->addElement('datepickerdate', 'start_date', get_lang('StartDate'), array('form_name'=>'survey'));
  122. $form->addElement('datepickerdate', 'end_date', get_lang('EndDate'), array('form_name'=>'survey'));
  123. //$group='';
  124. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('Yes'),$form_share_value);
  125. /** @todo maybe it is better to change this into false instead see line 95 in survey.lib.php */
  126. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('No'),0);
  127. $fck_attribute['Height'] = '200';
  128. //$form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;');
  129. $form->addElement('checkbox', 'anonymous', get_lang('Anonymous'));
  130. $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'));
  131. $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'));
  132. $form->addElement('submit', 'submit_survey', get_lang('Ok'));
  133. // setting the rules
  134. $form->addRule('survey_code', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  135. $form->addRule('survey_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  136. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  137. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  138. $form->addRule(array ('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  139. // setting the default values
  140. $form->setDefaults($defaults);
  141. // The validation or display
  142. if( $form->validate() )
  143. {
  144. // exporting the values
  145. $values = $form->exportValues();
  146. // storing the survey
  147. $return = survey_manager::store_survey($values);
  148. /*// deleting the shared survey if the survey is getting unshared (this only happens when editing)
  149. if (is_numeric($survey_data['survey_share']) AND $values['survey_share']['survey_share'] == 0 AND $values['survey_id']<>'')
  150. {
  151. survey_manager::delete_survey($survey_data['survey_share'], true);
  152. }
  153. // storing the already existing questions and options of a survey that gets shared (this only happens when editing)
  154. if ($survey_data['survey_share']== 0 AND $values['survey_share']['survey_share'] !== 0 AND $values['survey_id']<>'')
  155. {
  156. survey_manager::get_complete_survey_structure($return['id']);
  157. }
  158. */
  159. if ($config['survey']['debug'])
  160. {
  161. // displaying a feedback message
  162. Display::display_confirmation_message($return['message'], false);
  163. }
  164. else
  165. {
  166. // redirecting to the survey page (whilst showing the return message
  167. header('location:survey.php?survey_id='.$return['id'].'&message='.$return['message']);
  168. }
  169. }
  170. else
  171. {
  172. // Displaying the header
  173. Display::display_header($tool_name);
  174. // Displaying the tool title
  175. //api_display_tool_title($tool_name);
  176. // display the form
  177. $form->display();
  178. }
  179. // Footer
  180. Display :: display_footer();
  181. ?>