create_new_survey.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. require_once '../inc/global.inc.php';
  15. $this_section = SECTION_COURSES;
  16. // Database table definitions
  17. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  18. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  19. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  20. $table_gradebook_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  21. /** @todo this has to be moved to a more appropriate place (after the display_header of the code) */
  22. // If user is not teacher or if he's a coach trying to access an element out of his session
  23. if (!api_is_allowed_to_edit()) {
  24. if (!api_is_course_coach() ||
  25. (!empty($_GET['survey_id']) &&
  26. !api_is_element_in_the_session(TOOL_SURVEY, $_GET['survey_id']))
  27. ) {
  28. api_not_allowed(true);
  29. exit;
  30. }
  31. }
  32. // Getting the survey information
  33. $survey_id = isset($_GET['survey_id']) ? intval($_GET['survey_id']) : null;
  34. $survey_data = SurveyManager::get_survey($survey_id);
  35. // Additional information
  36. $course_id = api_get_course_id();
  37. $session_id = api_get_session_id();
  38. $gradebook_link_type = 8;
  39. $urlname = isset($survey_data['title']) ? strip_tags($survey_data['title']) : null;
  40. // Breadcrumbs
  41. if ($_GET['action'] == 'add') {
  42. $interbreadcrumb[] = array(
  43. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  44. 'name' => get_lang('SurveyList')
  45. );
  46. $tool_name = get_lang('CreateNewSurvey');
  47. }
  48. if ($_GET['action'] == 'edit' && is_numeric($survey_id)) {
  49. $interbreadcrumb[] = array(
  50. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  51. 'name' => get_lang('SurveyList')
  52. );
  53. $interbreadcrumb[] = array(
  54. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  55. 'name' => Security::remove_XSS($urlname)
  56. );
  57. $tool_name = get_lang('EditSurvey');
  58. }
  59. $gradebook_link_id = null;
  60. // Getting the default values
  61. if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  62. $defaults = $survey_data;
  63. $defaults['survey_id'] = $survey_id;
  64. $defaults['anonymous'] = $survey_data['anonymous'];
  65. $link_info = GradebookUtils::is_resource_in_course_gradebook($course_id, $gradebook_link_type, $survey_id, $session_id);
  66. $gradebook_link_id = $link_info['id'];
  67. if ($link_info) {
  68. if ($sql_result_array = Database::fetch_array(Database::query('SELECT weight FROM '.$table_gradebook_link.' WHERE id='.$gradebook_link_id))) {
  69. $defaults['survey_qualify_gradebook'] = $gradebook_link_id;
  70. $defaults['survey_weight'] = number_format($sql_result_array['weight'], 2, '.', '');
  71. }
  72. }
  73. } else {
  74. $defaults['survey_language'] = $_course['language'];
  75. $defaults['start_date'] = date('Y-m-d', api_strtotime(api_get_local_time()));
  76. $startdateandxdays = time() + 864000; // today + 10 days
  77. $defaults['end_date'] = date('Y-m-d', $startdateandxdays);
  78. //$defaults['survey_share']['survey_share'] = 0;
  79. //$form_share_value = 1;
  80. $defaults['anonymous'] = 0;
  81. }
  82. // Initialize the object
  83. $form = new FormValidator(
  84. 'survey',
  85. 'post',
  86. api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.$survey_id
  87. );
  88. $form->addElement('header', '', $tool_name);
  89. // Setting the form elements
  90. if ($_GET['action'] == 'edit' && isset($survey_id) && is_numeric($survey_id)) {
  91. $form->addElement('hidden', 'survey_id');
  92. }
  93. $survey_code = $form->addElement(
  94. 'text',
  95. 'survey_code',
  96. get_lang('SurveyCode'),
  97. array('size' => '20', 'maxlength' => '20', 'autofocus' => 'autofocus')
  98. );
  99. if ($_GET['action'] == 'edit') {
  100. //$survey_code->freeze();
  101. $form->applyFilter('survey_code', 'api_strtoupper');
  102. }
  103. $form->addElement('html_editor', 'survey_title', get_lang('SurveyTitle'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '200'));
  104. $form->addElement('html_editor', 'survey_subtitle', get_lang('SurveySubTitle'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '100', 'ToolbarStartExpanded' => false));
  105. // Pass the language of the survey in the form
  106. $form->addElement('hidden', 'survey_language');
  107. $form->addElement('date_picker', 'start_date', get_lang('StartDate'));
  108. $form->addElement('date_picker', 'end_date', get_lang('EndDate'));
  109. $form->addElement('checkbox', 'anonymous', null, get_lang('Anonymous'));
  110. $visibleResults = array(
  111. SURVEY_VISIBLE_TUTOR => get_lang('Coach'),
  112. SURVEY_VISIBLE_TUTOR_STUDENT => get_lang('CoachAndStudent'),
  113. SURVEY_VISIBLE_PUBLIC => get_lang('Everyone')
  114. );
  115. $form->addElement('select', 'visible_results', get_lang('ResultsVisibility'), $visibleResults);
  116. //$defaults['visible_results'] = 0;
  117. $form->addElement('html_editor', 'survey_introduction', get_lang('SurveyIntroduction'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  118. $form->addElement('html_editor', 'survey_thanks', get_lang('SurveyThanks'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '130', 'ToolbarStartExpanded' => false));
  119. // Additional Parameters
  120. $form->addButtonAdvancedSettings('advanced_params');
  121. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  122. if (Gradebook::is_active()) {
  123. // An option: Qualify the fact that survey has been answered in the gradebook
  124. $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\'; }"');
  125. $form->addElement('html', '<div id="gradebook_options"'.($gradebook_link_id ? '' : ' style="display:none"').'>');
  126. $form->addElement('text', 'survey_weight', get_lang('QualifyWeight'), 'value="0.00" style="width: 40px;" onfocus="javascript: this.select();"');
  127. $form->applyFilter('survey_weight', 'html_filter');
  128. $form->addElement('html', '</div>');
  129. }
  130. // Personality/Conditional Test Options
  131. $surveytypes[0] = get_lang('Normal');
  132. $surveytypes[1] = get_lang('Conditional');
  133. if ($_GET['action'] == 'add') {
  134. $form->addElement('hidden', 'survey_type', 0);
  135. $survey_tree = new SurveyTree();
  136. $list_surveys = $survey_tree->createList($survey_tree->surveylist);
  137. $list_surveys[0] = '';
  138. $form->addElement('select', 'parent_id', get_lang('ParentSurvey'), $list_surveys);
  139. $defaults['parent_id'] = 0;
  140. }
  141. if (isset($survey_data['survey_type']) && $survey_data['survey_type'] == 1 || $_GET['action'] == 'add') {
  142. $form->addElement('checkbox', 'one_question_per_page', null, get_lang('OneQuestionPerPage'));
  143. $form->addElement('checkbox', 'shuffle', null, get_lang('ActivateShuffle'));
  144. }
  145. $input_name_list = null;
  146. if (isset($_GET['action']) && $_GET['action'] == 'edit' && !empty($survey_id)) {
  147. if ($survey_data['anonymous'] == 0) {
  148. $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\';}"');
  149. if ($survey_data['show_form_profile'] == 1) {
  150. $form->addElement('html', '<div id="options_field" style="display:block">');
  151. } else {
  152. $form->addElement('html', '<div id="options_field" style="display:none">');
  153. }
  154. $field_list = SurveyUtil::make_field_list();
  155. if (is_array($field_list)) {
  156. // TODO hide and show the list in a fancy DIV
  157. foreach ($field_list as $key => & $field) {
  158. if ($field['visibility'] == 1) {
  159. $form->addElement('checkbox', 'profile_'.$key, ' ', '&nbsp;&nbsp;'.$field['name']);
  160. $input_name_list.= 'profile_'.$key.',';
  161. }
  162. }
  163. // Necessary to know the fields
  164. $form->addElement('hidden', 'input_name_list', $input_name_list);
  165. // Set defaults form fields
  166. if ($survey_data['form_fields']) {
  167. $form_fields = explode('@', $survey_data['form_fields']);
  168. foreach ($form_fields as & $field) {
  169. $field_value = explode(':', $field);
  170. if ($field_value[0] != '' && $field_value[1] != '') {
  171. $defaults[$field_value[0]] = $field_value[1];
  172. }
  173. }
  174. }
  175. }
  176. $form->addElement('html', '</div>');
  177. }
  178. }
  179. $form->addElement('html', '</div><br />');
  180. if (isset($_GET['survey_id']) && $_GET['action'] == 'edit') {
  181. $class = 'save';
  182. $text = get_lang('ModifySurvey');
  183. $form->addButtonUpdate(get_lang('ModifySurvey'), 'submit_survey');
  184. } else {
  185. $class = 'add';
  186. $text = get_lang('CreateSurvey');
  187. $form->addButtonCreate(get_lang('CreateSurvey'), 'submit_survey');
  188. }
  189. // Setting the rules
  190. if ($_GET['action'] == 'add') {
  191. $form->addRule('survey_code', get_lang('ThisFieldIsRequired'), 'required');
  192. $form->addRule('survey_code', '', 'maxlength', 20);
  193. }
  194. $form->addRule('survey_title', get_lang('ThisFieldIsRequired'), 'required');
  195. $form->addRule('start_date', get_lang('InvalidDate'), 'date');
  196. $form->addRule('end_date', get_lang('InvalidDate'), 'date');
  197. $form->addRule(array('start_date', 'end_date'), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  198. // Setting the default values
  199. $form->setDefaults($defaults);
  200. // The validation or display
  201. if ($form->validate()) {
  202. // Exporting the values
  203. $values = $form->exportValues();
  204. // Storing the survey
  205. $return = SurveyManager::store_survey($values);
  206. /* // Deleting the shared survey if the survey is getting unshared (this only happens when editing)
  207. if (is_numeric($survey_data['survey_share']) && $values['survey_share']['survey_share'] == 0 && $values['survey_id'] != '') {
  208. SurveyManager::delete_survey($survey_data['survey_share'], true);
  209. }
  210. // Storing the already existing questions and options of a survey that gets shared (this only happens when editing)
  211. if ($survey_data['survey_share'] == 0 && $values['survey_share']['survey_share'] !== 0 && $values['survey_id'] != '') {
  212. SurveyManager::get_complete_survey_structure($return['id']);
  213. }
  214. */
  215. if ($return['type'] == 'error') {
  216. // Display the error
  217. Display::display_error_message(get_lang($return['message']), false);
  218. // Displaying the header
  219. Display::display_header($tool_name);
  220. // Display the form
  221. $form->display();
  222. } else {
  223. $gradebook_option = false;
  224. if (isset($values['survey_qualify_gradebook'])) {
  225. $gradebook_option = $values['survey_qualify_gradebook'] > 0;
  226. }
  227. if ($gradebook_option) {
  228. $survey_id = intval($return['id']);
  229. if ($survey_id > 0) {
  230. $title_gradebook = ''; // Not needed here.
  231. $description_gradebook = ''; // Not needed here.
  232. $survey_weight = floatval($_POST['survey_weight']);
  233. $max_score = 1;
  234. $date = time(); // TODO: Maybe time zones implementation is needed here.
  235. $visible = 1; // 1 = visible
  236. $link_info = GradebookUtils::is_resource_in_course_gradebook(
  237. $course_id,
  238. $gradebook_link_type,
  239. $survey_id,
  240. $session_id
  241. );
  242. $gradebook_link_id = $link_info['id'];
  243. if (!$gradebook_link_id) {
  244. GradebookUtils::add_resource_to_course_gradebook(
  245. $course_id,
  246. $gradebook_link_type,
  247. $survey_id,
  248. $title_gradebook,
  249. $survey_weight,
  250. $max_score,
  251. $description_gradebook,
  252. 1,
  253. $session_id
  254. );
  255. } else {
  256. Database::query('UPDATE '.$table_gradebook_link.' SET weight='.$survey_weight.' WHERE id='.$gradebook_link_id);
  257. }
  258. }
  259. }
  260. }
  261. Display::addFlash(Display::return_message(($return['message']=='SurveyUpdatedSuccesfully'?get_lang($return['message']):$return['message']), false));
  262. // Redirecting to the survey page (whilst showing the return message)
  263. header('location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$return['id'].'&message='.$return['message'].'&'.api_get_cidreq());
  264. exit;
  265. } else {
  266. // Displaying the header
  267. Display::display_header($tool_name);
  268. $form->display();
  269. }
  270. // Footer
  271. Display :: display_footer();