question.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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(
  81. 'personality',
  82. 'yesno',
  83. 'multiplechoice',
  84. 'multipleresponse',
  85. 'open',
  86. 'dropdown',
  87. 'comment',
  88. 'pagebreak',
  89. 'percentage',
  90. 'score'
  91. );
  92. // Actions
  93. $actions = '<div class="actions">';
  94. $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>';
  95. $actions .= '</div>';
  96. // Checking if it is a valid type
  97. if (!in_array($_GET['type'], $possible_types)) {
  98. Display :: display_header($tool_name,'Survey');
  99. echo $actions;
  100. Display :: display_error_message(get_lang('TypeDoesNotExist'), false);
  101. Display :: display_footer();
  102. }
  103. $error_message = '';
  104. // Displaying the form for adding or editing the question
  105. if (empty($_POST['save_question']) && in_array($_GET['type'], $possible_types)) {
  106. if (!isset($_POST['save_question'])) {
  107. // Displaying the header
  108. Display::display_header($tool_name, 'Survey');
  109. echo $actions;
  110. // Displys message if exists
  111. if (isset($_SESSION['temp_sys_message'])) {
  112. $error_message = $_SESSION['temp_sys_message'];
  113. unset($_SESSION['temp_sys_message']);
  114. if ($error_message == 'PleaseEnterAQuestion' ||
  115. $error_message == 'PleasFillAllAnswer'||
  116. $error_message == 'PleaseChooseACondition'||
  117. $error_message == 'ChooseDifferentCategories'
  118. ) {
  119. Display::display_error_message(get_lang($error_message), true);
  120. }
  121. }
  122. }
  123. $ch_type = 'ch_'.$_GET['type'];
  124. $form = new $ch_type;
  125. // The defaults values for the form
  126. $form_content['answers'] = array('', '');
  127. if ($_GET['type'] == 'yesno') {
  128. $form_content['answers'][0] = get_lang('Yes');
  129. $form_content['answers'][1] = get_lang('No');
  130. }
  131. if ($_GET['type'] == 'personality') {
  132. $form_content['answers'][0] = 1;
  133. $form_content['answers'][1] = 2;
  134. $form_content['answers'][2] = 3;
  135. $form_content['answers'][3] = 4;
  136. $form_content['answers'][4] = 5;
  137. $form_content['values'][0] = 0;
  138. $form_content['values'][1] = 0;
  139. $form_content['values'][2] = 1;
  140. $form_content['values'][3] = 2;
  141. $form_content['values'][4] = 3;
  142. }
  143. // We are editing a question
  144. if (isset($_GET['question_id']) && !empty($_GET['question_id'])) {
  145. $form_content = survey_manager::get_question($_GET['question_id']);
  146. }
  147. // An action has been performed (for instance adding a possible answer, moving an answer, ...)
  148. if ($_POST) {
  149. $form_content = $_POST;
  150. $form_content = $form->handle_action(
  151. $survey_data,
  152. $form_content
  153. );
  154. }
  155. if ($error_message != '') {
  156. $form_content['question'] = $_SESSION['temp_user_message'];
  157. $form_content['answers'] = $_SESSION['temp_answers'];
  158. $form_content['values'] = $_SESSION['temp_values'];
  159. $form_content['horizontalvertical'] = $_SESSION['temp_horizontalvertical'];
  160. unset($_SESSION['temp_user_message']);
  161. unset($_SESSION['temp_answers']);
  162. unset($_SESSION['temp_values']);
  163. unset($_SESSION['temp_horizontalvertical']);
  164. }
  165. $form->create_form($survey_data, $form_content);
  166. $form->render_form();
  167. } else {
  168. $form_content = $_POST;
  169. $form = new survey_question();
  170. $form->handle_action($survey_data, $form_content);
  171. }
  172. // Footer
  173. Display :: display_footer();