create_new_survey.php 14 KB

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