create_new_survey.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. //Language selection has been disabled. If you want to re-enable, please
  136. //disable the following line (hidden language field).
  137. $lang_array = api_get_languages();
  138. foreach ($lang_array['name'] as $key=>$value) {
  139. $languages[$lang_array['folder'][$key]] = $value;
  140. }
  141. $form->addElement('select', 'survey_language', get_lang('Language'), $languages);
  142. */
  143. // Pass the language of the survey in the form
  144. $form->addElement('hidden', 'survey_language');
  145. $form->addElement('datepickerdate', 'start_date', get_lang('StartDate'), array('form_name'=>'survey'));
  146. $form->addElement('datepickerdate', 'end_date', get_lang('EndDate'), array('form_name'=>'survey'));
  147. //$group='';
  148. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('Yes'),$form_share_value);
  149. /** TODO maybe it is better to change this into false instead see line 95 in survey.lib.php */
  150. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('No'),0);
  151. //$form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;');
  152. $form->addElement('checkbox', 'anonymous', get_lang('Anonymous'));
  153. $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  154. $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  155. /*
  156. // Aditional Parameters
  157. $form -> addElement('html','<div class="row">
  158. <div class="label">&nbsp;</div>
  159. <div class="formw">
  160. <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>
  161. </div>
  162. </div>');*/
  163. // Personality/Conditional Test Options
  164. $surveytypes[0] = get_lang('Normal');
  165. $surveytypes[1] = get_lang('Conditional');
  166. if ($_GET['action'] == 'add')
  167. {
  168. $form->addElement('hidden','survey_type',0);
  169. $form -> addElement('html','<div id="options" style="display: none;">');
  170. require_once(api_get_path(LIBRARY_PATH).'surveymanager.lib.php');
  171. $survey_tree = new SurveyTree();
  172. $list_surveys = $survey_tree->createList($survey_tree->surveylist);
  173. $list_surveys[0]='';
  174. $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
  175. $defaults['parent_id']=0;
  176. }
  177. if ($survey_data['survey_type']==1 || $_GET['action'] == 'add' )
  178. {
  179. $form->addElement('checkbox', 'one_question_per_page', get_lang('OneQuestionPerPage'));
  180. $form->addElement('checkbox', 'shuffle', get_lang('ActivateShuffle'));
  181. }
  182. if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id) )
  183. {
  184. if ($survey_data['anonymous']==0 ) {
  185. // Aditional Parameters
  186. $form -> addElement('html','<div class="row">
  187. <div class="label">
  188. <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>
  189. </div>
  190. <div class="formw">
  191. &nbsp;
  192. </div>
  193. </div>');
  194. $form -> addElement('html','<div id="options" style="display:none">');
  195. $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\';}"');
  196. if ($survey_data['show_form_profile']== 1) {
  197. $form -> addElement('html','<div id="options_field" style="display:block">');
  198. } else {
  199. $form -> addElement('html','<div id="options_field" style="display:none">');
  200. }
  201. $field_list=SurveyUtil::make_field_list();
  202. if (is_array ($field_list))
  203. {
  204. //TODO hide and show the list in a fancy DIV
  205. foreach ($field_list as $key=> $field)
  206. {
  207. if ($field['visibility']==1)
  208. {
  209. $form->addElement('checkbox', 'profile_'.$key, ' ','&nbsp;&nbsp;'.$field['name'] );
  210. $input_name_list.= 'profile_'.$key.',';
  211. }
  212. }
  213. // necesary to know the fields
  214. $form->addElement('hidden', 'input_name_list', $input_name_list );
  215. //set defaults form fields
  216. if ($survey_data['form_fields'])
  217. {
  218. $form_fields=explode('@',$survey_data['form_fields']);
  219. foreach($form_fields as $field)
  220. {
  221. $field_value=explode(':',$field);
  222. if ($field_value[0]!='' && $field_value[1]!= '')
  223. {
  224. $defaults[$field_value[0]]=$field_value[1];
  225. }
  226. }
  227. }
  228. }
  229. $form->addElement('html', '</div></div>');
  230. }
  231. }
  232. $form -> addElement('html','</div><br />');
  233. if(isset($_GET['survey_id']) && $_GET['action']=='edit') {
  234. $class="save";
  235. $text=get_lang('ModifySurvey');
  236. } else {
  237. $class="add";
  238. $text=get_lang('CreateSurvey');
  239. }
  240. $form->addElement('style_submit_button', 'submit_survey', $text, 'class="'.$class.'"');
  241. // setting the rules
  242. if ($_GET['action'] == 'add')
  243. {
  244. $form->addRule('survey_code', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  245. $form->addRule('survey_code', '', 'maxlength',20);
  246. }
  247. $form->addRule('survey_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  248. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  249. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  250. $form->addRule(array ('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  251. // setting the default values
  252. $form->setDefaults($defaults);
  253. // The validation or display
  254. if( $form->validate() )
  255. {
  256. // exporting the values
  257. $values = $form->exportValues();
  258. // storing the survey
  259. $return = survey_manager::store_survey($values);
  260. /*// deleting the shared survey if the survey is getting unshared (this only happens when editing)
  261. if (is_numeric($survey_data['survey_share']) AND $values['survey_share']['survey_share'] == 0 AND $values['survey_id']<>'')
  262. {
  263. survey_manager::delete_survey($survey_data['survey_share'], true);
  264. }
  265. // storing the already existing questions and options of a survey that gets shared (this only happens when editing)
  266. if ($survey_data['survey_share']== 0 AND $values['survey_share']['survey_share'] !== 0 AND $values['survey_id']<>'')
  267. {
  268. survey_manager::get_complete_survey_structure($return['id']);
  269. }
  270. */
  271. if($return['type'] == 'error')
  272. {
  273. // Displaying the header
  274. Display::display_header($tool_name);
  275. // display the error
  276. Display::display_error_message(get_lang($return['message']), false);
  277. // display the form
  278. $form->display();
  279. }
  280. if ($config['survey']['debug'])
  281. {
  282. // displaying a feedback message
  283. Display::display_confirmation_message($return['message'], false);
  284. } else {
  285. // redirecting to the survey page (whilst showing the return message
  286. header('location:survey.php?survey_id='.$return['id'].'&message='.$return['message']);
  287. }
  288. } else {
  289. // Displaying the header
  290. Display::display_header($tool_name);
  291. // Displaying the tool title
  292. //api_display_tool_title($tool_name);
  293. // display the form
  294. $form->display();
  295. }
  296. // Footer
  297. Display :: display_footer();
  298. ?>