question.php 6.4 KB

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