create_new_survey.php 13 KB

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