create_new_survey.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /* For licensing terms, see /chamilo_license.txt */
  3. /**
  4. * @package dokeos.survey
  5. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code
  7. * @author Julio Montoya Armas <gugli100@gmail.com>, Dokeos: Personality Test modification and rewriting large parts of the code
  8. * @version $Id: create_new_survey.php 22297 2009-07-22 22:08:30Z cfasanando $
  9. *
  10. * @todo only the available platform languages should be used => need an api get_languages and and api_get_available_languages (or a parameter)
  11. */
  12. // name of the language file that needs to be included
  13. $language_file = 'survey';
  14. // including the global dokeos file
  15. require_once '../inc/global.inc.php';
  16. $this_section=SECTION_COURSES;
  17. // including additional libraries
  18. /** @todo check if these are all needed */
  19. /** @todo check if the starting / is needed. api_get_path probably ends with an / */
  20. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  21. require_once('survey.lib.php');
  22. require_once (api_get_path(LIBRARY_PATH).'/fileManage.lib.php');
  23. require_once (api_get_path(CONFIGURATION_PATH) ."/add_course.conf.php");
  24. require_once (api_get_path(LIBRARY_PATH)."/add_course.lib.inc.php");
  25. require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
  26. require_once (api_get_path(LIBRARY_PATH)."/groupmanager.lib.php");
  27. require_once (api_get_path(LIBRARY_PATH)."/usermanager.lib.php");
  28. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  29. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  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. function setFocus(){
  41. $("#surveycode_title").focus();
  42. }
  43. $(document).ready(function () {
  44. setFocus();
  45. });
  46. </script>';
  47. // Database table definitions
  48. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  49. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  50. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  51. $table_course_survey_rel = Database :: get_main_table(TABLE_MAIN_COURSE_SURVEY);
  52. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  53. // if user is not teacher or if he's a coach trying to access an element out of his session
  54. if (!api_is_allowed_to_edit())
  55. {
  56. if(!api_is_course_coach() || (!empty($_GET['survey_id']) && !api_is_element_in_the_session(TOOL_SURVEY,intval($_GET['survey_id']))))
  57. {
  58. Display :: display_header();
  59. Display :: display_error_message(get_lang('NotAllowed'), false);
  60. Display :: display_footer();
  61. exit;
  62. }
  63. }
  64. // getting the survey information
  65. $survey_id = Security::remove_XSS($_GET['survey_id']);
  66. $survey_data = survey_manager::get_survey($survey_id);
  67. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40));
  68. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  69. $urlname .= '...';
  70. }
  71. // breadcrumbs
  72. if ($_GET['action'] == 'add')
  73. {
  74. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  75. $tool_name = get_lang('CreateNewSurvey');
  76. }
  77. if ($_GET['action'] == 'edit' && is_numeric($survey_id))
  78. {
  79. $interbreadcrumb[] = array ("url" => "survey_list.php", "name" => get_lang('SurveyList'));
  80. $interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$survey_id, "name" => strip_tags($urlname));
  81. $tool_name = get_lang('EditSurvey');
  82. }
  83. // getting the default values
  84. if ($_GET['action'] == 'edit' AND isset($survey_id) AND is_numeric($survey_id))
  85. {
  86. $defaults = $survey_data;
  87. $defaults['survey_id'] = $survey_id;
  88. $defaults['anonymous'] = $survey_data['anonymous'];
  89. } else {
  90. $defaults['survey_language'] = $_course['language'];
  91. $defaults['start_date'] = date('d-F-Y H:i');
  92. $startdateandxdays = time() + 864000; // today + 10 days
  93. $defaults['end_date'] = date('d-F-Y H:i', $startdateandxdays);
  94. //$defaults['survey_share']['survey_share'] = 0;
  95. //$form_share_value = 1;
  96. $defaults['anonymous'] = 0;
  97. }
  98. // initiate the object
  99. $form = new FormValidator('survey', 'post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.$survey_id);
  100. $form->addElement('header', '', $tool_name);
  101. // settting the form elements
  102. if ($_GET['action'] == 'edit' AND isset($survey_id) AND is_numeric($survey_id))
  103. {
  104. $form->addElement('hidden', 'survey_id');
  105. }
  106. $survey_code = $form->addElement('text', 'survey_code', get_lang('SurveyCode'), array('size' => '20','maxlength'=>'20', 'id'=>'surveycode_title'));
  107. //$form->applyFilter('survey_code', 'html_filter');
  108. if ($_GET['action'] == 'edit') {
  109. $survey_code->freeze();
  110. $form->applyFilter('survey_code', 'api_strtoupper');
  111. }
  112. $form->addElement('html_editor', 'survey_title', get_lang('SurveyTitle'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '200'));
  113. $form->addElement('html_editor', 'survey_subtitle', get_lang('SurveySubTitle'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '100', 'ToolbarStartExpanded' => false));
  114. /*
  115. //Language selection has been disabled. If you want to re-enable, please
  116. //disable the following line (hidden language field).
  117. $lang_array = api_get_languages();
  118. foreach ($lang_array['name'] as $key=>$value) {
  119. $languages[$lang_array['folder'][$key]] = $value;
  120. }
  121. $form->addElement('select', 'survey_language', get_lang('Language'), $languages);
  122. */
  123. // Pass the language of the survey in the form
  124. $form->addElement('hidden', 'survey_language');
  125. $form->addElement('datepickerdate', 'start_date', get_lang('StartDate'), array('form_name'=>'survey'));
  126. $form->addElement('datepickerdate', 'end_date', get_lang('EndDate'), array('form_name'=>'survey'));
  127. //$group='';
  128. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('Yes'),$form_share_value);
  129. /** TODO maybe it is better to change this into false instead see line 95 in survey.lib.php */
  130. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share',null, get_lang('No'),0);
  131. //$form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;');
  132. $form->addElement('checkbox', 'anonymous', get_lang('Anonymous'));
  133. $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  134. $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  135. /*
  136. // Aditional Parameters
  137. $form -> addElement('html','<div class="row">
  138. <div class="label">&nbsp;</div>
  139. <div class="formw">
  140. <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>
  141. </div>
  142. </div>');*/
  143. // Personality/Conditional Test Options
  144. $surveytypes[0] = get_lang('Normal');
  145. $surveytypes[1] = get_lang('Conditional');
  146. if ($_GET['action'] == 'add') {
  147. $form->addElement('hidden','survey_type',0);
  148. $form -> addElement('html','<div id="options" style="display: none;">');
  149. require_once(api_get_path(LIBRARY_PATH).'surveymanager.lib.php');
  150. $survey_tree = new SurveyTree();
  151. $list_surveys = $survey_tree->createList($survey_tree->surveylist);
  152. $list_surveys[0]='';
  153. $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
  154. $defaults['parent_id']=0;
  155. }
  156. if ($survey_data['survey_type']==1 || $_GET['action'] == 'add' )
  157. {
  158. $form->addElement('checkbox', 'one_question_per_page', get_lang('OneQuestionPerPage'));
  159. $form->addElement('checkbox', 'shuffle', get_lang('ActivateShuffle'));
  160. }
  161. if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id) ) {
  162. if ($survey_data['anonymous']==0 ) {
  163. // Aditional Parameters
  164. $form -> addElement('html','<div class="row">
  165. <div class="label">
  166. <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>
  167. </div>
  168. <div class="formw">
  169. &nbsp;
  170. </div>
  171. </div>');
  172. $form->addElement('html','<div id="options" style="display:none">');
  173. $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\';}"');
  174. if ($survey_data['show_form_profile']== 1) {
  175. $form -> addElement('html','<div id="options_field" style="display:block">');
  176. } else {
  177. $form -> addElement('html','<div id="options_field" style="display:none">');
  178. }
  179. $field_list=SurveyUtil::make_field_list();
  180. if (is_array ($field_list))
  181. {
  182. //TODO hide and show the list in a fancy DIV
  183. foreach ($field_list as $key=> $field)
  184. {
  185. if ($field['visibility']==1)
  186. {
  187. $form->addElement('checkbox', 'profile_'.$key, ' ','&nbsp;&nbsp;'.$field['name'] );
  188. $input_name_list.= 'profile_'.$key.',';
  189. }
  190. }
  191. // necesary to know the fields
  192. $form->addElement('hidden', 'input_name_list', $input_name_list );
  193. //set defaults form fields
  194. if ($survey_data['form_fields'])
  195. {
  196. $form_fields=explode('@',$survey_data['form_fields']);
  197. foreach($form_fields as $field)
  198. {
  199. $field_value=explode(':',$field);
  200. if ($field_value[0]!='' && $field_value[1]!= '')
  201. {
  202. $defaults[$field_value[0]]=$field_value[1];
  203. }
  204. }
  205. }
  206. }
  207. $form->addElement('html', '</div></div>');
  208. }
  209. }
  210. if ($_GET['action'] == 'add') {
  211. $form -> addElement('html','</div><br />');
  212. }
  213. if(isset($_GET['survey_id']) && $_GET['action']=='edit') {
  214. $class='save';
  215. $text=get_lang('ModifySurvey');
  216. } else {
  217. $class='add';
  218. $text=get_lang('CreateSurvey');
  219. }
  220. $form->addElement('style_submit_button', 'submit_survey', $text, 'class="'.$class.'"');
  221. // setting the rules
  222. if ($_GET['action'] == 'add') {
  223. $form->addRule('survey_code', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  224. $form->addRule('survey_code', '', 'maxlength',20);
  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. } else {
  264. // redirecting to the survey page (whilst showing the return message
  265. header('location:survey.php?survey_id='.$return['id'].'&message='.$return['message']);
  266. }
  267. } else {
  268. // Displaying the header
  269. Display::display_header($tool_name);
  270. // Displaying the tool title
  271. //api_display_tool_title($tool_name);
  272. // display the form
  273. $form->display();
  274. }
  275. // Footer
  276. Display :: display_footer();
  277. ?>