question.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. });
  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 = isset($_REQUEST['is_executable']) ? $_REQUEST['is_executable'] : null;
  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. $course_id = api_get_course_int_id();
  47. // Getting the survey information
  48. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  49. if (empty($survey_data)) {
  50. Display :: display_header(get_lang('ToolSurvey'));
  51. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  52. Display :: display_footer();
  53. exit;
  54. }
  55. $urlname = api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40);
  56. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  57. $urlname .= '...';
  58. }
  59. if ($survey_data['survey_type'] == 1) {
  60. $sql = 'SELECT id FROM '.Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP).' WHERE c_id = '.$course_id.' AND survey_id = '.(int)$_GET['survey_id'].' LIMIT 1';
  61. $rs = Database::query($sql);
  62. if(Database::num_rows($rs)===0) {
  63. header('Location: survey.php?survey_id='.(int)$_GET['survey_id'].'&message='.'YouNeedToCreateGroups');
  64. exit;
  65. }
  66. }
  67. // Breadcrumbs
  68. $interbreadcrumb[] = array ('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  69. $interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']), 'name' => strip_tags($urlname));
  70. // Tool name
  71. if ($_GET['action'] == 'add') {
  72. $tool_name = get_lang('AddQuestion');
  73. }
  74. if ($_GET['action'] == 'edit') {
  75. $tool_name = get_lang('EditQuestion');
  76. }
  77. // The possible question types
  78. $possible_types = array('personality', 'yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score');
  79. // Actions
  80. $actions = '<div class="actions">';
  81. $actions .= '<a href="survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.Display::return_icon('back.png', get_lang('BackToSurvey'),'',ICON_SIZE_MEDIUM).'</a>';
  82. $actions .= '</div>';
  83. // Checking if it is a valid type
  84. if (!in_array($_GET['type'], $possible_types)) {
  85. Display :: display_header($tool_name,'Survey');
  86. echo $actions;
  87. Display :: display_error_message(get_lang('TypeDoesNotExist'), false);
  88. Display :: display_footer();
  89. }
  90. // Displaying the form for adding or editing the question
  91. if (empty($_POST['save_question']) && in_array($_GET['type'], $possible_types)) {
  92. if (!isset($_POST['save_question'])) {
  93. // Displaying the header
  94. Display::display_header($tool_name, 'Survey');
  95. echo $actions;
  96. $error_message = '';
  97. // Displys message if exists
  98. if (isset($_SESSION['temp_sys_message'])) {
  99. $error_message=$_SESSION['temp_sys_message'];
  100. unset($_SESSION['temp_sys_message']);
  101. if ($error_message == 'PleaseEnterAQuestion' || $error_message == 'PleasFillAllAnswer'|| $error_message == 'PleaseChooseACondition'|| $error_message == 'ChooseDifferentCategories') {
  102. Display::display_error_message(get_lang($error_message), true);
  103. }
  104. }
  105. }
  106. $ch_type = 'ch_'.$_GET['type'];
  107. $form = new $ch_type;
  108. // The defaults values for the form
  109. $form_content['answers'] = array('', '');
  110. if ($_GET['type'] == 'yesno') {
  111. $form_content['answers'][0] = get_lang('Yes');
  112. $form_content['answers'][1] = get_lang('No');
  113. }
  114. if ($_GET['type'] == 'personality') {
  115. $form_content['answers'][0] = 1;
  116. $form_content['answers'][1] = 2;
  117. $form_content['answers'][2] = 3;
  118. $form_content['answers'][3] = 4;
  119. $form_content['answers'][4] = 5;
  120. $form_content['values'][0] = 0;
  121. $form_content['values'][1] = 0;
  122. $form_content['values'][2] = 1;
  123. $form_content['values'][3] = 2;
  124. $form_content['values'][4] = 3;
  125. }
  126. // We are editing a question
  127. if (isset($_GET['question_id']) && !empty($_GET['question_id'])) {
  128. $form_content = survey_manager::get_question($_GET['question_id']);
  129. }
  130. // An action has been performed (for instance adding a possible answer, moving an answer, ...)
  131. if ($_POST) {
  132. $form_content = $_POST;
  133. $form_content = $form->handle_action($form_content);
  134. }
  135. if ($error_message != '') {
  136. $form_content['question'] = $_SESSION['temp_user_message'];
  137. $form_content['answers'] = $_SESSION['temp_answers'];
  138. $form_content['values'] = $_SESSION['temp_values'];
  139. $form_content['horizontalvertical'] = $_SESSION['temp_horizontalvertical'];
  140. unset($_SESSION['temp_user_message']);
  141. unset($_SESSION['temp_answers']);
  142. unset($_SESSION['temp_values']);
  143. unset($_SESSION['temp_horizontalvertical']);
  144. }
  145. $form->create_form($form_content);
  146. $form->render_form();
  147. } else {
  148. $form_content = $_POST;
  149. $form = new survey_question();
  150. $form->handle_action($form_content);
  151. }
  152. // Footer
  153. Display :: display_footer();