create_new_survey.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup,
  6. * refactoring and rewriting large parts (if not all) of the code
  7. * @author Julio Montoya Armas <gugli100@gmail.com>, Chamilo: Personality
  8. * Test modification and rewriting large parts of the code
  9. * @version $Id: create_new_survey.php 22297 2009-07-22 22:08:30Z cfasanando $
  10. *
  11. * @todo only the available platform languages should be used => need an
  12. * api get_languages and and api_get_available_languages (or a parameter)
  13. */
  14. // Language file that needs to be included
  15. $language_file = 'survey';
  16. // Including the global initialization file
  17. require_once '../inc/global.inc.php';
  18. $this_section = SECTION_COURSES;
  19. // Including additional libraries
  20. /** @todo check if these are all needed */
  21. /** @todo check if the starting / is needed. api_get_path probably ends with an / */
  22. //require_once api_get_path(LIBRARY_PATH).'survey.lib.php';
  23. require_once 'survey.lib.php';
  24. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  25. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  26. $htmlHeadXtra[] = '<script>
  27. function advanced_parameters() {
  28. if (document.getElementById(\'options\').style.display == \'none\') {
  29. document.getElementById(\'options\').style.display = \'block\';
  30. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif', get_lang('Hide'), array('style' => 'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
  31. } else {
  32. document.getElementById(\'options\').style.display = \'none\';
  33. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif', get_lang('Show'), array('style' => 'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'\';
  34. }
  35. }
  36. function setFocus(){
  37. $("#surveycode_title").focus();
  38. }
  39. $(document).ready(function () {
  40. setFocus();
  41. });
  42. </script>';
  43. // Database table definitions
  44. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  45. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  46. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  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() ||
  52. (!empty($_GET['survey_id']) &&
  53. !api_is_element_in_the_session(TOOL_SURVEY, $_GET['survey_id']))
  54. ) {
  55. api_not_allowed(true);
  56. exit;
  57. }
  58. }
  59. // Getting the survey information
  60. $survey_id = isset($_GET['survey_id']) ? intval($_GET['survey_id']) : null;
  61. $survey_data = survey_manager::get_survey($survey_id);
  62. // Additional information
  63. $course_id = api_get_course_id();
  64. $session_id = api_get_session_id();
  65. $gradebook_link_type = 8;
  66. $urlname = isset($survey_data['title']) ? strip_tags($survey_data['title']) : null;
  67. // Breadcrumbs
  68. if ($_GET['action'] == 'add') {
  69. $interbreadcrumb[] = array(
  70. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  71. 'name' => get_lang('SurveyList')
  72. );
  73. $tool_name = get_lang('CreateNewSurvey');
  74. }
  75. if ($_GET['action'] == 'edit' && is_numeric($survey_id)) {
  76. $interbreadcrumb[] = array(
  77. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  78. 'name' => get_lang('SurveyList')
  79. );
  80. $interbreadcrumb[] = array(
  81. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  82. 'name' => Security::remove_XSS($urlname)
  83. );
  84. $tool_name = get_lang('EditSurvey');
  85. }
  86. $gradebook_link_id = null;
  87. // Getting the default values
  88. if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  89. $defaults = $survey_data;
  90. $defaults['survey_id'] = $survey_id;
  91. $defaults['anonymous'] = $survey_data['anonymous'];
  92. $link_info = is_resource_in_course_gradebook($course_id, $gradebook_link_type, $survey_id, $session_id);
  93. $gradebook_link_id = $link_info['id'];
  94. if ($link_info) {
  95. if ($sql_result_array = Database::fetch_array(Database::query('SELECT weight FROM '.$table_gradebook_link.' WHERE id='.$gradebook_link_id))) {
  96. $defaults['survey_qualify_gradebook'] = $gradebook_link_id;
  97. $defaults['survey_weight'] = number_format($sql_result_array['weight'], 2, '.', '');
  98. }
  99. }
  100. } else {
  101. $defaults['survey_language'] = $_course['language'];
  102. $defaults['start_date'] = date('Y-m-d', api_strtotime(api_get_local_time()));
  103. $startdateandxdays = time() + 864000; // today + 10 days
  104. $defaults['end_date'] = date('Y-m-d', $startdateandxdays);
  105. //$defaults['survey_share']['survey_share'] = 0;
  106. //$form_share_value = 1;
  107. $defaults['anonymous'] = 0;
  108. }
  109. // Initialize the object
  110. $form = new FormValidator('survey', 'post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.$survey_id);
  111. $form->addElement('header', '', $tool_name);
  112. // Setting the form elements
  113. if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  114. $form->addElement('hidden', 'survey_id');
  115. }
  116. $survey_code = $form->addElement('text', 'survey_code', get_lang('SurveyCode'), array('size' => '20', 'maxlength' => '20', 'id' => 'surveycode_title'));
  117. if ($_GET['action'] == 'edit') {
  118. //$survey_code->freeze();
  119. $form->applyFilter('survey_code', 'api_strtoupper');
  120. }
  121. $form->addElement('html_editor', 'survey_title', get_lang('SurveyTitle'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '200'));
  122. $form->addElement('html_editor', 'survey_subtitle', get_lang('SurveySubTitle'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '100', 'ToolbarStartExpanded' => false));
  123. // Pass the language of the survey in the form
  124. $form->addElement('hidden', 'survey_language');
  125. $form->addElement('date_picker', 'start_date', get_lang('StartDate'));
  126. $form->addElement('date_picker', 'end_date', get_lang('EndDate'));
  127. $form->addElement('checkbox', 'anonymous', null, get_lang('Anonymous'));
  128. $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  129. $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  130. // Additional Parameters
  131. $form->addElement(
  132. 'advanced_settings',
  133. '<a href="javascript: void(0);" onclick="javascript: advanced_parameters();">
  134. <span id="plus_minus">&nbsp;'.
  135. Display::return_icon('div_show.gif', null, array('style' => 'vertical-align:middle')).'&nbsp;'.get_lang('AdvancedParameters').'</span></a>'
  136. );
  137. $form->addElement('html', '<div id="options" style="display: none;">');
  138. if (Gradebook::is_active()) {
  139. // An option: Qualify the fact that survey has been answered in the gradebook
  140. $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\'; }"');
  141. $form->addElement('html', '<div id="gradebook_options"'.($gradebook_link_id ? '' : ' style="display:none"').'>');
  142. $form->addElement('text', 'survey_weight', get_lang('QualifyWeight'), 'value="0.00" style="width: 40px;" onfocus="javascript: this.select();"');
  143. $form->applyFilter('survey_weight', 'html_filter');
  144. $form->addElement('html', '</div>');
  145. }
  146. // Personality/Conditional Test Options
  147. $surveytypes[0] = get_lang('Normal');
  148. $surveytypes[1] = get_lang('Conditional');
  149. if ($_GET['action'] == 'add') {
  150. $form->addElement('hidden', 'survey_type', 0);
  151. require_once api_get_path(LIBRARY_PATH).'surveymanager.lib.php';
  152. $survey_tree = new SurveyTree();
  153. $list_surveys = $survey_tree->createList($survey_tree->surveylist);
  154. $list_surveys[0] = '';
  155. $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
  156. $defaults['parent_id'] = 0;
  157. }
  158. if (isset($survey_data['survey_type']) && $survey_data['survey_type'] == 1 || $_GET['action'] == 'add') {
  159. $form->addElement('checkbox', 'one_question_per_page', null, get_lang('OneQuestionPerPage'));
  160. $form->addElement('checkbox', 'shuffle', null, get_lang('ActivateShuffle'));
  161. }
  162. $input_name_list = null;
  163. if (isset($_GET['action']) && $_GET['action'] == 'edit' && !empty($survey_id)) {
  164. if ($survey_data['anonymous'] == 0) {
  165. $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\';}"');
  166. if ($survey_data['show_form_profile'] == 1) {
  167. $form->addElement('html', '<div id="options_field" style="display:block">');
  168. } else {
  169. $form->addElement('html', '<div id="options_field" style="display:none">');
  170. }
  171. $field_list = SurveyUtil::make_field_list();
  172. if (is_array($field_list)) {
  173. // TODO hide and show the list in a fancy DIV
  174. foreach ($field_list as $key => & $field) {
  175. if ($field['visibility'] == 1) {
  176. $form->addElement('checkbox', 'profile_'.$key, ' ', '&nbsp;&nbsp;'.$field['name']);
  177. $input_name_list.= 'profile_'.$key.',';
  178. }
  179. }
  180. // Necessary to know the fields
  181. $form->addElement('hidden', 'input_name_list', $input_name_list);
  182. // Set defaults form fields
  183. if ($survey_data['form_fields']) {
  184. $form_fields = explode('@', $survey_data['form_fields']);
  185. foreach ($form_fields as & $field) {
  186. $field_value = explode(':', $field);
  187. if ($field_value[0] != '' && $field_value[1] != '') {
  188. $defaults[$field_value[0]] = $field_value[1];
  189. }
  190. }
  191. }
  192. }
  193. $form->addElement('html', '</div>');
  194. }
  195. }
  196. $form->addElement('html', '</div><br />');
  197. if (isset($_GET['survey_id']) && $_GET['action'] == 'edit') {
  198. $class = 'save';
  199. $text = get_lang('ModifySurvey');
  200. } else {
  201. $class = 'add';
  202. $text = get_lang('CreateSurvey');
  203. }
  204. $form->addElement('style_submit_button', 'submit_survey', $text, 'class="'.$class.'"');
  205. // Setting the rules
  206. if ($_GET['action'] == 'add') {
  207. $form->addRule('survey_code', get_lang('ThisFieldIsRequired'), 'required');
  208. $form->addRule('survey_code', '', 'maxlength', 20);
  209. }
  210. $form->addRule('survey_title', get_lang('ThisFieldIsRequired'), 'required');
  211. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  212. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  213. $form->addRule(array('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  214. // Setting the default values
  215. $form->setDefaults($defaults);
  216. // The validation or display
  217. if ($form->validate()) {
  218. // Exporting the values
  219. $values = $form->exportValues();
  220. // Storing the survey
  221. $return = survey_manager::store_survey($values);
  222. /* // Deleting the shared survey if the survey is getting unshared (this only happens when editing)
  223. if (is_numeric($survey_data['survey_share']) && $values['survey_share']['survey_share'] == 0 && $values['survey_id'] != '') {
  224. survey_manager::delete_survey($survey_data['survey_share'], true);
  225. }
  226. // Storing the already existing questions and options of a survey that gets shared (this only happens when editing)
  227. if ($survey_data['survey_share'] == 0 && $values['survey_share']['survey_share'] !== 0 && $values['survey_id'] != '') {
  228. survey_manager::get_complete_survey_structure($return['id']);
  229. }
  230. */
  231. if ($return['type'] == 'error') {
  232. // Display the error
  233. Display::display_error_message(get_lang($return['message']), false);
  234. // Displaying the header
  235. Display::display_header($tool_name);
  236. // Display the form
  237. $form->display();
  238. } else {
  239. $gradebook_option = false;
  240. if (isset($values['survey_qualify_gradebook'])) {
  241. $gradebook_option = $values['survey_qualify_gradebook'] > 0;
  242. }
  243. if ($gradebook_option) {
  244. $survey_id = intval($return['id']);
  245. if ($survey_id > 0) {
  246. $title_gradebook = ''; // Not needed here.
  247. $description_gradebook = ''; // Not needed here.
  248. $survey_weight = floatval($_POST['survey_weight']);
  249. $max_score = 1;
  250. $date = time(); // TODO: Maybe time zones implementation is needed here.
  251. $visible = 1; // 1 = visible
  252. $link_info = is_resource_in_course_gradebook(
  253. $course_id,
  254. $gradebook_link_type,
  255. $survey_id,
  256. $session_id
  257. );
  258. $gradebook_link_id = $link_info['id'];
  259. if (!$gradebook_link_id) {
  260. add_resource_to_course_gradebook(
  261. $course_id,
  262. $gradebook_link_type,
  263. $survey_id,
  264. $title_gradebook,
  265. $survey_weight,
  266. $max_score,
  267. $description_gradebook,
  268. 1,
  269. $session_id
  270. );
  271. } else {
  272. Database::query('UPDATE '.$table_gradebook_link.' SET weight='.$survey_weight.' WHERE id='.$gradebook_link_id);
  273. }
  274. }
  275. }
  276. }
  277. if ($config['survey']['debug']) {
  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: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$return['id'].'&message='.$return['message'].'&'.api_get_cidreq());
  283. exit;
  284. }
  285. } else {
  286. // Displaying the header
  287. Display::display_header($tool_name);
  288. $form->display();
  289. }
  290. // Footer
  291. Display :: display_footer();