create_new_survey.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.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>, Chamilo: 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. // Language file that needs to be included
  13. $language_file = 'survey';
  14. // Including the global initialization 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(LIBRARY_PATH).'groupmanager.lib.php';
  24. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  25. $htmlHeadXtra[] = '<script type="text/javascript">
  26. function advanced_parameters() {
  27. if(document.getElementById(\'options\').style.display == \'none\') {
  28. document.getElementById(\'options\').style.display = \'block\';
  29. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif', get_lang('Hide'), array('style' => 'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
  30. } else {
  31. document.getElementById(\'options\').style.display = \'none\';
  32. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif', get_lang('Show'), array('style' => 'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
  33. }
  34. }
  35. function setFocus(){
  36. $("#surveycode_title").focus();
  37. }
  38. $(document).ready(function () {
  39. setFocus();
  40. });
  41. </script>';
  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. $table_gradebook_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  48. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  49. // If user is not teacher or if he's a coach trying to access an element out of his session
  50. if (!api_is_allowed_to_edit()) {
  51. if (!api_is_course_coach() || (!empty($_GET['survey_id']) && !api_is_element_in_the_session(TOOL_SURVEY, intval($_GET['survey_id'])))) {
  52. Display :: display_header();
  53. Display :: display_error_message(get_lang('NotAllowed'), false);
  54. Display :: display_footer();
  55. exit;
  56. }
  57. }
  58. // Getting the survey information
  59. $survey_id = Security::remove_XSS($_GET['survey_id']);
  60. $survey_data = survey_manager::get_survey($survey_id);
  61. // Additional information
  62. $course_id = api_get_course_id();
  63. $session_id = api_get_session_id();
  64. $gradebook_link_type = 8; // LINK_SURVEY
  65. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  66. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  67. $urlname .= '...';
  68. }
  69. // Breadcrumbs
  70. if ($_GET['action'] == 'add') {
  71. $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  72. $tool_name = get_lang('CreateNewSurvey');
  73. }
  74. if ($_GET['action'] == 'edit' && is_numeric($survey_id))
  75. {
  76. $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  77. $interbreadcrumb[] = array('url' => 'survey.php?survey_id='.$survey_id, 'name' => strip_tags($urlname));
  78. $tool_name = get_lang('EditSurvey');
  79. }
  80. // Getting the default values
  81. if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  82. $defaults = $survey_data;
  83. $defaults['survey_id'] = $survey_id;
  84. $defaults['anonymous'] = $survey_data['anonymous'];
  85. $gradebook_link_id = is_resource_in_course_gradebook($course_id, $gradebook_link_type, $survey_id, $session_id);
  86. if ($gradebook_link_id) {
  87. if ($sql_result_array = Database::fetch_array(Database::query('SELECT weight FROM '.$table_gradebook_link.' WHERE id='.$gradebook_link_id))) {
  88. $defaults['survey_qualify_gradebook'] = $gradebook_link_id;
  89. $defaults['survey_weight'] = number_format($sql_result_array['weight'], 2, '.', '');
  90. }
  91. }
  92. } else {
  93. $defaults['survey_language'] = $_course['language'];
  94. $defaults['start_date'] = date('d-F-Y H:i');
  95. $startdateandxdays = time() + 864000; // today + 10 days
  96. $defaults['end_date'] = date('d-F-Y H:i', $startdateandxdays);
  97. //$defaults['survey_share']['survey_share'] = 0;
  98. //$form_share_value = 1;
  99. $defaults['anonymous'] = 0;
  100. }
  101. // Initialize the object
  102. $form = new FormValidator('survey', 'post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.$survey_id);
  103. $form->addElement('header', '', $tool_name);
  104. // Settting the form elements
  105. if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  106. $form->addElement('hidden', 'survey_id');
  107. }
  108. $survey_code = $form->addElement('text', 'survey_code', get_lang('SurveyCode'), array('size' => '20', 'maxlength' => '20', 'id' => 'surveycode_title'));
  109. //$form->applyFilter('survey_code', 'html_filter');
  110. if ($_GET['action'] == 'edit') {
  111. $survey_code->freeze();
  112. $form->applyFilter('survey_code', 'api_strtoupper');
  113. }
  114. $form->addElement('html_editor', 'survey_title', get_lang('SurveyTitle'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '200'));
  115. $form->addElement('html_editor', 'survey_subtitle', get_lang('SurveySubTitle'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '100', 'ToolbarStartExpanded' => false));
  116. /*
  117. //Language selection has been disabled. If you want to re-enable, please
  118. //disable the following line (hidden language field).
  119. $lang_array = api_get_languages();
  120. foreach ($lang_array['name'] as $key => $value) {
  121. $languages[$lang_array['folder'][$key]] = $value;
  122. }
  123. $form->addElement('select', 'survey_language', get_lang('Language'), $languages);
  124. */
  125. // Pass the language of the survey in the form
  126. $form->addElement('hidden', 'survey_language');
  127. $form->addElement('datepickerdate', 'start_date', get_lang('StartDate'), array('form_name'=>'survey'));
  128. $form->addElement('datepickerdate', 'end_date', get_lang('EndDate'), array('form_name'=>'survey'));
  129. //$group = '';
  130. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share', null, get_lang('Yes'), $form_share_value);
  131. /** TODO Maybe it is better to change this into false instead see line 95 in survey.lib.php */
  132. //$group[] =& HTML_QuickForm::createElement('radio', 'survey_share', null, get_lang('No'), 0);
  133. //$form->addGroup($group, 'survey_share', get_lang('ShareSurvey'), '&nbsp;');
  134. $form->addElement('checkbox', 'anonymous', null, get_lang('Anonymous'));
  135. $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  136. $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  137. // Aditional Parameters
  138. $form->addElement('html', '
  139. <div class="row">
  140. <div class="label">
  141. </div>
  142. <div class="formw">
  143. <a href="javascript: void(0);" onclick="javascript: advanced_parameters();" >
  144. <span id="plus_minus">&nbsp;'.Display::return_icon('div_show.gif',null,array('style'=>'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'</span></a>
  145. </div>
  146. </div>');
  147. $form -> addElement('html', '<div id="options" style="display: none;">');
  148. // An option: Qualify the fact that survey has been answered in the gradebook
  149. $form->addElement('checkbox', 'survey_qualify_gradebook', null, get_lang('QualifyInGradebook'), 'onclick="javascript: if (this.checked) { document.getElementById(\'gradebook_options\').style.display = \'block\'; } else { document.getElementById(\'gradebook_options\').style.display = \'none\'; }"');
  150. $form->addElement('html', '<div id="gradebook_options"'.($gradebook_link_id ? '' : ' style="display:none"').'>');
  151. $form->addElement('text', 'survey_weight', get_lang('QualifyWeight'), 'value="0.00" style="width: 40px;" onfocus="javascript: this.select();"');
  152. $form->applyFilter('survey_weight', 'html_filter');
  153. $form->addElement('html', '</div>');
  154. // Personality/Conditional Test Options
  155. $surveytypes[0] = get_lang('Normal');
  156. $surveytypes[1] = get_lang('Conditional');
  157. if ($_GET['action'] == 'add') {
  158. $form->addElement('hidden', 'survey_type', 0);
  159. require_once api_get_path(LIBRARY_PATH).'surveymanager.lib.php';
  160. $survey_tree = new SurveyTree();
  161. $list_surveys = $survey_tree->createList($survey_tree->surveylist);
  162. $list_surveys[0] = '';
  163. $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
  164. $defaults['parent_id'] = 0;
  165. }
  166. if ($survey_data['survey_type'] == 1 || $_GET['action'] == 'add') {
  167. $form->addElement('checkbox', 'one_question_per_page', null, get_lang('OneQuestionPerPage'));
  168. $form->addElement('checkbox', 'shuffle', null, get_lang('ActivateShuffle'));
  169. }
  170. if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id)) {
  171. if ($survey_data['anonymous'] == 0) {
  172. $form->addElement('checkbox', 'show_form_profile', null, get_lang('ShowFormProfile'), 'onclick="javascript: if(this.checked){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"');
  173. if ($survey_data['show_form_profile'] == 1) {
  174. $form -> addElement('html', '<div id="options_field" style="display:block">');
  175. } else {
  176. $form -> addElement('html', '<div id="options_field" style="display:none">');
  177. }
  178. $field_list = SurveyUtil::make_field_list();
  179. if (is_array($field_list)) {
  180. // TODO hide and show the list in a fancy DIV
  181. foreach ($field_list as $key => & $field) {
  182. if ($field['visibility'] == 1) {
  183. $form->addElement('checkbox', 'profile_'.$key, ' ','&nbsp;&nbsp;'.$field['name'] );
  184. $input_name_list.= 'profile_'.$key.',';
  185. }
  186. }
  187. // Necesary to know the fields
  188. $form->addElement('hidden', 'input_name_list', $input_name_list );
  189. // Set defaults form fields
  190. if ($survey_data['form_fields']) {
  191. $form_fields = explode('@', $survey_data['form_fields']);
  192. foreach ($form_fields as & $field) {
  193. $field_value = explode(':', $field);
  194. if ($field_value[0] != '' && $field_value[1] != '') {
  195. $defaults[$field_value[0]] = $field_value[1];
  196. }
  197. }
  198. }
  199. }
  200. $form->addElement('html', '</div>');
  201. }
  202. }
  203. $form -> addElement('html', '</div><br />');
  204. if (isset($_GET['survey_id']) && $_GET['action'] == 'edit') {
  205. $class = 'save';
  206. $text = get_lang('ModifySurvey');
  207. } else {
  208. $class = 'add';
  209. $text = get_lang('CreateSurvey');
  210. }
  211. $form->addElement('style_submit_button', 'submit_survey', $text, 'class="'.$class.'"');
  212. // Setting the rules
  213. if ($_GET['action'] == 'add') {
  214. $form->addRule('survey_code', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  215. $form->addRule('survey_code', '', 'maxlength', 20);
  216. }
  217. $form->addRule('survey_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  218. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  219. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  220. $form->addRule(array('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  221. // Setting the default values
  222. $form->setDefaults($defaults);
  223. // The validation or display
  224. if ($form->validate()) {
  225. // Exporting the values
  226. $values = $form->exportValues();
  227. // Storing the survey
  228. $return = survey_manager::store_survey($values);
  229. /*// Deleting the shared survey if the survey is getting unshared (this only happens when editing)
  230. if (is_numeric($survey_data['survey_share']) && $values['survey_share']['survey_share'] == 0 && $values['survey_id'] != '') {
  231. survey_manager::delete_survey($survey_data['survey_share'], true);
  232. }
  233. // Storing the already existing questions and options of a survey that gets shared (this only happens when editing)
  234. if ($survey_data['survey_share'] == 0 && $values['survey_share']['survey_share'] !== 0 && $values['survey_id'] != '') {
  235. survey_manager::get_complete_survey_structure($return['id']);
  236. }
  237. */
  238. if ($return['type'] == 'error') {
  239. // Displaying the header
  240. Display::display_header($tool_name);
  241. // Display the error
  242. Display::display_error_message(get_lang($return['message']), false);
  243. // Display the form
  244. $form->display();
  245. } else {
  246. $gradebook_option = $values['survey_qualify_gradebook'] > 0;
  247. if ($gradebook_option) {
  248. $survey_id = intval($return['id']);
  249. if ($survey_id > 0) {
  250. $title_gradebook = ''; // Not needed here.
  251. $description_gradebook = ''; // Not needed here.
  252. $survey_weight = floatval($_POST['survey_weight']);
  253. $max_score = 1;
  254. $date = time(); // TODO: Maybe time zones implementation is needed here.
  255. $visible = 1; // 1 = visible
  256. $gradebook_link_id = is_resource_in_course_gradebook($course_id, $gradebook_link_type, $survey_id, $session_id);
  257. if (!$gradebook_link_id) {
  258. add_resource_to_course_gradebook($course_id, $gradebook_link_type, $survey_id, $title_gradebook, $survey_weight, $max_score, $description_gradebook, time(), 1, $session_id);
  259. } else {
  260. Database::query('UPDATE '.$table_gradebook_link.' SET weight='.$survey_weight.' WHERE id='.$gradebook_link_id);
  261. }
  262. }
  263. }
  264. }
  265. if ($config['survey']['debug']) {
  266. // Displaying a feedback message
  267. Display::display_confirmation_message($return['message'], false);
  268. } else {
  269. // Redirecting to the survey page (whilst showing the return message)
  270. @header('location:survey.php?survey_id='.$return['id'].'&message='.$return['message']);
  271. }
  272. } else {
  273. // Displaying the header
  274. Display::display_header($tool_name);
  275. // Displaying the tool title
  276. //api_display_tool_title($tool_name);
  277. // Display the form
  278. $form->display();
  279. }
  280. // Footer
  281. Display :: display_footer();