create_new_survey.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  23. $htmlHeadXtra[] = '<script>
  24. function advanced_parameters() {
  25. if(document.getElementById(\'options\').style.display == \'none\') {
  26. document.getElementById(\'options\').style.display = \'block\';
  27. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon(
  28. 'div_hide.gif',
  29. get_lang('Hide'),
  30. array('style' => 'vertical-align:middle')
  31. ).'&nbsp;'.get_lang('AdvancedParameters').'\';
  32. } else {
  33. document.getElementById(\'options\').style.display = \'none\';
  34. document.getElementById(\'plus_minus\').innerHTML=\'&nbsp;'.Display::return_icon(
  35. 'div_show.gif',
  36. get_lang('Show'),
  37. array('style' => 'vertical-align:middle')
  38. ).'&nbsp;'.get_lang('AdvancedParameters').'\';
  39. }
  40. }
  41. function setFocus(){
  42. $("#surveycode_title").focus();
  43. }
  44. $(document).ready(function () {
  45. setFocus();
  46. });
  47. </script>';
  48. // Database table definitions
  49. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  50. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  51. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  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(
  57. TOOL_SURVEY,
  58. intval($_GET['survey_id'])
  59. ))
  60. ) {
  61. api_not_allowed(true);
  62. exit;
  63. }
  64. }
  65. // Getting the survey information
  66. $survey_id = Security::remove_XSS($_GET['survey_id']);
  67. $survey_data = survey_manager::get_survey($survey_id);
  68. // Additional information
  69. $course_id = api_get_course_id();
  70. $session_id = api_get_session_id();
  71. $gradebook_link_type = 8; // LINK_SURVEY
  72. $urlname = $survey_data['title'];
  73. // Breadcrumbs
  74. if ($_GET['action'] == 'add') {
  75. $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  76. $tool_name = get_lang('CreateNewSurvey');
  77. }
  78. if ($_GET['action'] == 'edit' && is_numeric($survey_id)) {
  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' && isset($survey_id) && is_numeric($survey_id)) {
  85. $defaults = $survey_data;
  86. $defaults['survey_id'] = $survey_id;
  87. $defaults['anonymous'] = $survey_data['anonymous'];
  88. $link_info = is_resource_in_course_gradebook($course_id, $gradebook_link_type, $survey_id, $session_id);
  89. $gradebook_link_id = $link_info['id'];
  90. if ($link_info) {
  91. if ($sql_result_array = Database::fetch_array(
  92. Database::query('SELECT weight FROM '.$table_gradebook_link.' WHERE id='.$gradebook_link_id)
  93. )
  94. ) {
  95. $defaults['survey_qualify_gradebook'] = $gradebook_link_id;
  96. $defaults['survey_weight'] = number_format($sql_result_array['weight'], 2, '.', '');
  97. }
  98. }
  99. } else {
  100. $defaults['survey_language'] = $_course['language'];
  101. $defaults['start_date'] = date('d-F-Y H:i');
  102. $startdateandxdays = time() + 864000; // today + 10 days
  103. $defaults['end_date'] = date('d-F-Y H:i', $startdateandxdays);
  104. $defaults['anonymous'] = 0;
  105. }
  106. // Initialize the object
  107. $form = new FormValidator('survey', 'post', api_get_self().'?action='.Security::remove_XSS(
  108. $_GET['action']
  109. ).'&survey_id='.$survey_id);
  110. $form->addElement('header', '', $tool_name);
  111. // Settting the form elements
  112. if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  113. $form->addElement('hidden', 'survey_id');
  114. }
  115. $survey_code = $form->addElement(
  116. 'text',
  117. 'survey_code',
  118. get_lang('SurveyCode'),
  119. array('size' => '20', 'maxlength' => '20', 'id' => 'surveycode_title')
  120. );
  121. $form->addElement(
  122. 'html_editor',
  123. 'survey_title',
  124. get_lang('SurveyTitle'),
  125. null,
  126. array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '200')
  127. );
  128. $form->addElement(
  129. 'html_editor',
  130. 'survey_subtitle',
  131. get_lang('SurveySubTitle'),
  132. null,
  133. array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '100', 'ToolbarStartExpanded' => false)
  134. );
  135. // Pass the language of the survey in the form
  136. $form->addElement('hidden', 'survey_language');
  137. $form->addElement('datepickerdate', 'start_date', get_lang('StartDate'), array('form_name' => 'survey'));
  138. $form->addElement('datepickerdate', 'end_date', get_lang('EndDate'), array('form_name' => 'survey'));
  139. $form->addElement('checkbox', 'anonymous', null, get_lang('Anonymous'));
  140. $form->addElement(
  141. 'html_editor',
  142. 'survey_introduction',
  143. get_lang('SurveyIntroduction'),
  144. null,
  145. array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false)
  146. );
  147. $form->addElement(
  148. 'html_editor',
  149. 'survey_thanks',
  150. get_lang('SurveyThanks'),
  151. null,
  152. array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false)
  153. );
  154. // Aditional Parameters
  155. $form->addElement(
  156. 'advanced_settings',
  157. '<a href="javascript: void(0);" onclick="javascript: advanced_parameters();" >
  158. <span id="plus_minus">&nbsp;'.Display::return_icon(
  159. 'div_show.gif',
  160. null,
  161. array('style' => 'vertical-align:middle')
  162. ).'&nbsp;'.get_lang('AdvancedParameters').'</span></a>'
  163. );
  164. $form->addElement('html', '<div id="options" style="display: none;">');
  165. if (Gradebook::is_active()) {
  166. // An option: Qualify the fact that survey has been answered in the gradebook
  167. $form->addElement(
  168. 'checkbox',
  169. 'survey_qualify_gradebook',
  170. null,
  171. get_lang('QualifyInGradebook'),
  172. 'onclick="javascript: if (this.checked) { document.getElementById(\'gradebook_options\').style.display = \'block\'; } else { document.getElementById(\'gradebook_options\').style.display = \'none\'; }"'
  173. );
  174. $form->addElement('html', '<div id="gradebook_options"'.($gradebook_link_id ? '' : ' style="display:none"').'>');
  175. $form->addElement(
  176. 'text',
  177. 'survey_weight',
  178. get_lang('QualifyWeight'),
  179. 'value="0.00" style="width: 40px;" onfocus="javascript: this.select();"'
  180. );
  181. $form->applyFilter('survey_weight', 'html_filter');
  182. $form->addElement('html', '</div>');
  183. }
  184. // Personality/Conditional Test Options
  185. $surveytypes[0] = get_lang('Normal');
  186. $surveytypes[1] = get_lang('Conditional');
  187. if ($_GET['action'] == 'add') {
  188. $form->addElement('hidden', 'survey_type', 0);
  189. require_once api_get_path(LIBRARY_PATH).'surveymanager.lib.php';
  190. $survey_tree = new SurveyTree();
  191. $list_surveys = $survey_tree->createList($survey_tree->surveylist);
  192. $list_surveys[0] = '';
  193. $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
  194. $defaults['parent_id'] = 0;
  195. }
  196. if ($survey_data['survey_type'] == 1 || $_GET['action'] == 'add') {
  197. $form->addElement('checkbox', 'one_question_per_page', null, get_lang('OneQuestionPerPage'));
  198. $form->addElement('checkbox', 'shuffle', null, get_lang('ActivateShuffle'));
  199. }
  200. if ((isset($_GET['action']) && $_GET['action'] == 'edit') && !empty($survey_id)) {
  201. if ($survey_data['anonymous'] == 0) {
  202. $form->addElement(
  203. 'checkbox',
  204. 'show_form_profile',
  205. null,
  206. get_lang('ShowFormProfile'),
  207. 'onclick="javascript: if(this.checked){document.getElementById(\'options_field\').style.display = \'block\';}else{document.getElementById(\'options_field\').style.display = \'none\';}"'
  208. );
  209. if ($survey_data['show_form_profile'] == 1) {
  210. $form->addElement('html', '<div id="options_field" style="display:block">');
  211. } else {
  212. $form->addElement('html', '<div id="options_field" style="display:none">');
  213. }
  214. $input_name_list = null;
  215. $field_list = SurveyUtil::make_field_list();
  216. if (is_array($field_list)) {
  217. // TODO hide and show the list in a fancy DIV
  218. foreach ($field_list as $key => & $field) {
  219. if ($field['visibility'] == 1) {
  220. $form->addElement('checkbox', 'profile_'.$key, ' ', '&nbsp;&nbsp;'.$field['name']);
  221. $input_name_list .= 'profile_'.$key.',';
  222. }
  223. }
  224. // Needed to know the fields
  225. $form->addElement('hidden', 'input_name_list', $input_name_list);
  226. // Set defaults form fields
  227. if ($survey_data['form_fields']) {
  228. $form_fields = explode('@', $survey_data['form_fields']);
  229. foreach ($form_fields as & $field) {
  230. $field_value = explode(':', $field);
  231. if ($field_value[0] != '' && $field_value[1] != '') {
  232. $defaults[$field_value[0]] = $field_value[1];
  233. }
  234. }
  235. }
  236. }
  237. $form->addElement('html', '</div>');
  238. }
  239. }
  240. $form->addElement('html', '</div><br />');
  241. if (isset($_GET['survey_id']) && $_GET['action'] == 'edit') {
  242. $class = 'save';
  243. $text = get_lang('ModifySurvey');
  244. } else {
  245. $class = 'add';
  246. $text = get_lang('CreateSurvey');
  247. }
  248. $form->addElement('style_submit_button', 'submit_survey', $text, 'class="'.$class.'"');
  249. // Setting the rules
  250. if ($_GET['action'] == 'add') {
  251. $form->addRule('survey_code', get_lang('ThisFieldIsRequired'), 'required');
  252. $form->addRule('survey_code', '', 'maxlength', 20);
  253. }
  254. $form->addRule('survey_title', get_lang('ThisFieldIsRequired'), 'required');
  255. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  256. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  257. $form->addRule(array('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  258. // Setting the default values
  259. $form->setDefaults($defaults);
  260. // The validation or display
  261. if ($form->validate()) {
  262. // Exporting the values
  263. $values = $form->exportValues();
  264. // Storing the survey
  265. $return = survey_manager::store_survey($values);
  266. if ($return['type'] == 'error') {
  267. // Displaying the header
  268. Display::display_header($tool_name);
  269. // Display the error
  270. Display::display_error_message(get_lang($return['message']), false);
  271. // Display the form
  272. $form->display();
  273. } else {
  274. $gradebook_option = isset($values['survey_qualify_gradebook']) && $values['survey_qualify_gradebook'] > 0;
  275. if ($gradebook_option) {
  276. $survey_id = intval($return['id']);
  277. if ($survey_id > 0) {
  278. $survey_weight = floatval($_POST['survey_weight']);
  279. $max_score = 1;
  280. $link_info = is_resource_in_course_gradebook($course_id, $gradebook_link_type, $survey_id, $session_id);
  281. $gradebook_link_id = $link_info['id'];
  282. if (!$gradebook_link_id) {
  283. add_resource_to_course_gradebook(
  284. $course_id,
  285. $gradebook_link_type,
  286. $survey_id,
  287. null,
  288. $survey_weight,
  289. $max_score,
  290. null,
  291. 1,
  292. $session_id
  293. );
  294. } else {
  295. Database::query(
  296. 'UPDATE '.$table_gradebook_link.' SET weight='.$survey_weight.' WHERE id='.$gradebook_link_id
  297. );
  298. }
  299. }
  300. }
  301. // Redirecting to the survey page (whilst showing the return message)
  302. header('location:survey.php?survey_id='.$return['id'].'&message='.$return['message']);
  303. exit;
  304. }
  305. } else {
  306. // Displaying the header
  307. Display::display_header($tool_name);
  308. $form->display();
  309. }
  310. // Footer
  311. Display :: display_footer();