create_new_survey.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. * @author Julio Montoya Armas <gugli100@gmail.com>, Dokeos: Personality Test modification and rewriting large parts of the code
  22. * @version $Id: create_new_survey.php 16486 2008-10-10 13:32:05Z elixir_inter $
  23. *
  24. * @todo only the available platform languages should be used => need an api get_languages and and api_get_available_languages (or a parameter)
  25. */
  26. // name of the language file that needs to be included
  27. $language_file = 'survey';
  28. // including the global dokeos file
  29. require_once ('../inc/global.inc.php');
  30. // including additional libraries
  31. /** @todo check if these are all needed */
  32. /** @todo check if the starting / is needed. api_get_path probably ends with an / */
  33. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  34. require_once('survey.lib.php');
  35. require_once (api_get_path(LIBRARY_PATH).'/fileManage.lib.php');
  36. require_once (api_get_path(CONFIGURATION_PATH) ."/add_course.conf.php");
  37. require_once (api_get_path(LIBRARY_PATH)."/add_course.lib.inc.php");
  38. require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
  39. require_once (api_get_path(LIBRARY_PATH)."/groupmanager.lib.php");
  40. require_once (api_get_path(LIBRARY_PATH)."/usermanager.lib.php");
  41. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  42. // Database table definitions
  43. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  44. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  45. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  46. $table_course_survey_rel = Database :: get_main_table(TABLE_MAIN_COURSE_SURVEY);
  47. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  48. // if user is not teacher or if he's a coach trying to access an element out of his session
  49. if (!api_is_allowed_to_edit())
  50. {
  51. if(!api_is_course_coach() || (!empty($_GET['survey_id']) && !api_is_element_in_the_session(TOOL_SURVEY,intval($_GET['survey_id']))))
  52. {
  53. Display :: display_header();
  54. Display :: display_error_message(get_lang('NotAllowed'), false);
  55. Display :: display_footer();
  56. exit;
  57. }
  58. }
  59. // getting the survey information
  60. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  61. $urlname =substr(html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40);
  62. if (strlen(strip_tags($survey_data['title'])) > 40)
  63. {
  64. $urlname .= '...';
  65. }
  66. // breadcrumbs
  67. if ($_GET['action'] == 'add')
  68. {
  69. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  70. $tool_name = get_lang('CreateNewSurvey');
  71. }
  72. if ($_GET['action'] == 'edit' && is_numeric($_GET['survey_id']))
  73. {
  74. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  75. $interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$_GET['survey_id'], "name" => $urlname);
  76. $tool_name = get_lang('EditSurvey');
  77. }
  78. // getting the default values
  79. if ($_GET['action'] == 'edit' AND isset($_GET['survey_id']) AND is_numeric($_GET['survey_id']))
  80. {
  81. $defaults = $survey_data;
  82. $defaults['survey_id'] = $_GET['survey_id'];
  83. /*
  84. $defaults['survey_share'] = array();
  85. $defaults['survey_share']['survey_share'] = $survey_data['survey_share'];
  86. if (!is_numeric($survey_data['survey_share']) OR $survey_data['survey_share'] == 0)
  87. {
  88. $form_share_value = 'true';
  89. }
  90. else
  91. {
  92. $form_share_value = $defaults['survey_share']['survey_share'];
  93. }
  94. */
  95. $defaults['anonymous'] = $survey_data['anonymous'];
  96. }
  97. else
  98. {
  99. $defaults['survey_language'] = $_course['language'];
  100. $defaults['start_date'] = date('d-F-Y H:i');
  101. $startdateandxdays = time() + 864000; // today + 10 days
  102. $defaults['end_date'] = date('d-F-Y H:i', $startdateandxdays);
  103. //$defaults['survey_share']['survey_share'] = 0;
  104. //$form_share_value = 1;
  105. $defaults['anonymous'] = 0;
  106. }
  107. // initiate the object
  108. $form = new FormValidator('survey', 'post', api_get_self().'?action='.$_GET['action'].'&survey_id='.$_GET['survey_id']);
  109. // settting the form elements
  110. if ($_GET['action'] == 'edit' AND isset($_GET['survey_id']) AND is_numeric($_GET['survey_id']))
  111. {
  112. $form->addElement('hidden', 'survey_id');
  113. }
  114. $survey_code = $form->addElement('text', 'survey_code', get_lang('SurveyCode'), array('size' => '40'));
  115. if ($_GET['action'] == 'edit') {
  116. $survey_code->freeze();
  117. $form->applyFilter('survey_code', 'strtoupper');
  118. }
  119. $fck_attribute['Width'] = '100%';
  120. $fck_attribute['Height'] = '100';
  121. $fck_attribute['ToolbarSet'] = 'Survey';
  122. $form->addElement('html_editor', 'survey_title', get_lang('SurveyTitle'));
  123. $form->addElement('html_editor', 'survey_subtitle', get_lang('SurveySubTitle'));
  124. $lang_array = api_get_languages();
  125. foreach ($lang_array['name'] as $key=>$value)
  126. {
  127. $languages[$lang_array['folder'][$key]] = $value;
  128. }
  129. $form->addElement('select', 'survey_language', get_lang('Language'), $languages);
  130. $form->addElement('datepickerdate', 'start_date', get_lang('StartDate'), array('form_name'=>'survey'));
  131. $form->addElement('datepickerdate', 'end_date', get_lang('EndDate'), array('form_name'=>'survey'));
  132. //$group='';
  133. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('Yes'),$form_share_value);
  134. /** TODO maybe it is better to change this into false instead see line 95 in survey.lib.php */
  135. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('No'),0);
  136. $fck_attribute['Height'] = '200';
  137. //$form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;');
  138. $form->addElement('checkbox', 'anonymous', get_lang('Anonymous'));
  139. $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'));
  140. $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'));
  141. /*
  142. // Aditional Parameters
  143. $form -> addElement('html','<div class="row">
  144. <div class="label">&nbsp;</div>
  145. <div class="formw">
  146. <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>
  147. </div>
  148. </div>');*/
  149. // Personality/Conditional Test Options
  150. $surveytypes[0] = get_lang('Normal');
  151. $surveytypes[1] = get_lang('Conditional');
  152. if ($_GET['action'] == 'add')
  153. {
  154. $form->addElement('select', 'survey_type', get_lang('SelectType'), $surveytypes, array('onchange' => 'if(document.getElementById(\'options\').style.display == \'none\'){document.getElementById(\'options\').style.display = \'block\';}else{document.getElementById(\'options\').style.display = \'none\';}'));
  155. $form -> addElement('html','<div id="options" style="display: none;">');
  156. require_once(api_get_path(LIBRARY_PATH).'surveymanager.lib.php');
  157. $survey_tree = new SurveyTree();
  158. $list_surveys = $survey_tree->createList($survey_tree->surveylist);
  159. $list_surveys[0]='';
  160. $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
  161. $defaults['parent_id']=0;
  162. }
  163. else
  164. {
  165. // Aditional Parameters
  166. $form -> addElement('html','<div class="row">
  167. <div class="label">&nbsp;</div>
  168. <div class="formw">
  169. <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>
  170. </div>
  171. </div>');
  172. $form -> addElement('html','<div id="options" style="display: none;">');
  173. }
  174. if ($survey_data['survey_type']==1 || $_GET['action'] == 'add' )
  175. {
  176. $form->addElement('checkbox', 'one_question_per_page', get_lang('OneQuestionPerPage'));
  177. $form->addElement('checkbox', 'shuffle', get_lang('ActivateShuffle'));
  178. }
  179. if ($survey_data['anonymous']==0 )
  180. {
  181. $form->addElement('checkbox', 'show_form_profile', get_lang('ShowFormProfile'));
  182. }
  183. if ($_GET['action'] == 'edit' && is_numeric($_GET['survey_id']) )
  184. {
  185. if ($survey_data['show_form_profile']==1 && $survey_data['anonymous']==0 )
  186. {
  187. $field_list=SurveyUtil::make_field_list();
  188. if (is_array ($field_list))
  189. {
  190. //TODO hide and show the list in a fancy DIV
  191. //$form->addElement('html', '<a href=javascript>'.get_lang('ViewFields').'</a> <div id="fields" style="display: none;" >');
  192. foreach ($field_list as $key=> $field)
  193. {
  194. if ($field['visibility']==1)
  195. {
  196. $form->addElement('checkbox', 'profile_'.$key, ' ','&nbsp;&nbsp;'.$field['name'] );
  197. $input_name_list.= 'profile_'.$key.',';
  198. }
  199. }
  200. // necesary to know the fields
  201. $form->addElement('hidden', 'input_name_list', $input_name_list );
  202. //set defaults form fields
  203. if ($survey_data['form_fields'])
  204. {
  205. $form_fields=explode('@',$survey_data['form_fields']);
  206. foreach($form_fields as $field)
  207. {
  208. $field_value=explode(':',$field);
  209. if ($field_value[0]!='' && $field_value[1]!= '')
  210. {
  211. $defaults[$field_value[0]]=$field_value[1];
  212. }
  213. }
  214. }
  215. //$form->addElement('html', '</div>');
  216. }
  217. }
  218. }
  219. $form -> addElement('html','</div>');
  220. $form->addElement('submit', 'submit_survey', get_lang('Ok'));
  221. // setting the rules
  222. if ($_GET['action'] == 'add')
  223. {
  224. $form->addRule('survey_code', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  225. }
  226. $form->addRule('survey_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  227. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  228. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  229. $form->addRule(array ('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  230. // setting the default values
  231. $form->setDefaults($defaults);
  232. // The validation or display
  233. if( $form->validate() )
  234. {
  235. // exporting the values
  236. $values = $form->exportValues();
  237. // storing the survey
  238. $return = survey_manager::store_survey($values);
  239. /*// deleting the shared survey if the survey is getting unshared (this only happens when editing)
  240. if (is_numeric($survey_data['survey_share']) AND $values['survey_share']['survey_share'] == 0 AND $values['survey_id']<>'')
  241. {
  242. survey_manager::delete_survey($survey_data['survey_share'], true);
  243. }
  244. // storing the already existing questions and options of a survey that gets shared (this only happens when editing)
  245. if ($survey_data['survey_share']== 0 AND $values['survey_share']['survey_share'] !== 0 AND $values['survey_id']<>'')
  246. {
  247. survey_manager::get_complete_survey_structure($return['id']);
  248. }
  249. */
  250. if($return['type'] == 'error')
  251. {
  252. // Displaying the header
  253. Display::display_header($tool_name);
  254. // display the error
  255. Display::display_error_message(get_lang($return['message']), false);
  256. // display the form
  257. $form->display();
  258. }
  259. if ($config['survey']['debug'])
  260. {
  261. // displaying a feedback message
  262. Display::display_confirmation_message($return['message'], false);
  263. }
  264. else
  265. {
  266. // redirecting to the survey page (whilst showing the return message
  267. header('location:survey.php?survey_id='.$return['id'].'&message='.$return['message']);
  268. }
  269. }
  270. else
  271. {
  272. // Displaying the header
  273. Display::display_header($tool_name);
  274. // Displaying the tool title
  275. //api_display_tool_title($tool_name);
  276. // display the form
  277. $form->display();
  278. }
  279. // Footer
  280. Display :: display_footer();
  281. ?>