question.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 of the code
  7. * @version $Id: question.php 21734 2009-07-02 17:12:41Z cvargas1 $
  8. */
  9. // Language file that needs to be included
  10. $language_file = 'survey';
  11. // Including the global initialization file
  12. require '../inc/global.inc.php';
  13. // Including additional libraries
  14. //require_once api_get_path(LIBRARY_PATH).'survey.lib.php';
  15. require_once 'survey.lib.php';
  16. $htmlHeadXtra[] = '<script type="text/javascript">
  17. $(document).ready( function() {
  18. $("button").click(function() {
  19. $("#is_executable").attr("value",$(this).attr("name"));
  20. });
  21. } ); </script>';
  22. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  23. if (!api_is_allowed_to_edit(false, true)) {
  24. Display :: display_header();
  25. Display :: display_error_message(get_lang('NotAllowed'), false);
  26. Display :: display_footer();
  27. exit;
  28. }
  29. // Is valid request
  30. $is_valid_request = $_REQUEST['is_executable'];
  31. if ($request_index != $is_valid_request) {
  32. if ($request_index == 'save_question') {
  33. unset($_POST[$request_index]);
  34. } elseif ($request_index == 'add_answer') {
  35. unset($_POST[$request_index]);
  36. } elseif($request_index == 'remove_answer') {
  37. unset($_POST[$request_index]);
  38. }
  39. }
  40. // Database table definitions
  41. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  42. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  43. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  44. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  45. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  46. // Getting the survey information
  47. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  48. if (empty($survey_data)) {
  49. Display :: display_header(get_lang('ToolSurvey'));
  50. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  51. Display :: display_footer();
  52. exit;
  53. }
  54. $urlname = api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40);
  55. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  56. $urlname .= '...';
  57. }
  58. if ($survey_data['survey_type'] == 1) {
  59. $sql = 'SELECT id FROM '.Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP).' WHERE survey_id = '.(int)$_GET['survey_id'].' LIMIT 1';
  60. $rs = Database::query($sql);
  61. if(Database::num_rows($rs)===0) {
  62. header('Location: survey.php?survey_id='.(int)$_GET['survey_id'].'&message='.'YouNeedToCreateGroups');
  63. exit;
  64. }
  65. }
  66. // Breadcrumbs
  67. $interbreadcrumb[] = array ('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  68. $interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']), 'name' => strip_tags($urlname));
  69. // Tool name
  70. if ($_GET['action'] == 'add') {
  71. $tool_name = get_lang('AddQuestion');
  72. }
  73. if ($_GET['action'] == 'edit') {
  74. $tool_name = get_lang('EditQuestion');
  75. }
  76. // The possible question types
  77. $possible_types = array('personality', 'yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score');
  78. // Actions
  79. $actions = '<div class="actions">';
  80. $actions .= '<a href="survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.Display::return_icon('back.png', get_lang('BackToSurvey'),'','32').'</a>';
  81. $actions .= '</div>';
  82. // Checking if it is a valid type
  83. if (!in_array($_GET['type'], $possible_types)) {
  84. Display :: display_header($tool_name,'Survey');
  85. echo $actions;
  86. Display :: display_error_message(get_lang('TypeDoesNotExist'), false);
  87. Display :: display_footer();
  88. }
  89. // Displaying the form for adding or editing the question
  90. if (empty($_POST['save_question']) && in_array($_GET['type'], $possible_types)) {
  91. if (!isset($_POST['save_question'])) {
  92. // Displaying the header
  93. Display::display_header($tool_name, 'Survey');
  94. echo $actions;
  95. $error_message = '';
  96. // Displys message if exists
  97. if (isset($_SESSION['temp_sys_message'])) {
  98. $error_message=$_SESSION['temp_sys_message'];
  99. unset($_SESSION['temp_sys_message']);
  100. if ($error_message == 'PleaseEnterAQuestion' || $error_message == 'PleasFillAllAnswer'|| $error_message == 'PleaseChooseACondition'|| $error_message == 'ChooseDifferentCategories') {
  101. Display::display_error_message(get_lang($error_message), true);
  102. }
  103. }
  104. }
  105. $ch_type = 'ch_'.$_GET['type'];
  106. $form = new $ch_type;
  107. // The defaults values for the form
  108. $form_content['answers'] = array('', '');
  109. if ($_GET['type'] == 'yesno') {
  110. $form_content['answers'][0] = get_lang('Yes');
  111. $form_content['answers'][1] = get_lang('No');
  112. }
  113. if ($_GET['type'] == 'personality') {
  114. $form_content['answers'][0] = get_lang('1');
  115. $form_content['answers'][1] = get_lang('2');
  116. $form_content['answers'][2] = get_lang('3');
  117. $form_content['answers'][3] = get_lang('4');
  118. $form_content['answers'][4] = get_lang('5');
  119. $form_content['values'][0] = 0;
  120. $form_content['values'][1] = 0;
  121. $form_content['values'][2] = 1;
  122. $form_content['values'][3] = 2;
  123. $form_content['values'][4] = 3;
  124. }
  125. // We are editing a question
  126. if (isset($_GET['question_id']) && !empty($_GET['question_id'])) {
  127. $form_content = survey_manager::get_question($_GET['question_id']);
  128. }
  129. // An action has been performed (for instance adding a possible answer, moving an answer, ...)
  130. if ($_POST) {
  131. $form_content = $_POST;
  132. $form_content = $form->handle_action($form_content);
  133. }
  134. if ($error_message != '') {
  135. $form_content['question'] = $_SESSION['temp_user_message'];
  136. $form_content['answers'] = $_SESSION['temp_answers'];
  137. $form_content['values'] = $_SESSION['temp_values'];
  138. $form_content['horizontalvertical'] = $_SESSION['temp_horizontalvertical'];
  139. unset($_SESSION['temp_user_message']);
  140. unset($_SESSION['temp_answers']);
  141. unset($_SESSION['temp_values']);
  142. unset($_SESSION['temp_horizontalvertical']);
  143. }
  144. $form->create_form($form_content);
  145. $form->render_form();
  146. } else {
  147. $form_content = $_POST;
  148. $form = new survey_question();
  149. $form->handle_action($form_content);
  150. }
  151. // Footer
  152. Display :: display_footer();