question.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // Including the global initialization file
  10. require_once '../inc/global.inc.php';
  11. $htmlHeadXtra[] = '<script>
  12. $(document).ready( function() {
  13. $("button").click(function() {
  14. $("#is_executable").attr("value",$(this).attr("name"));
  15. });
  16. } ); </script>';
  17. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  18. if (!api_is_allowed_to_edit(false, true)) {
  19. Display :: display_header();
  20. Display :: display_error_message(get_lang('NotAllowed'), false);
  21. Display :: display_footer();
  22. exit;
  23. }
  24. // Is valid request
  25. $is_valid_request = isset($_REQUEST['is_executable']) ? $_REQUEST['is_executable'] : null;
  26. /*if ($request_index != $is_valid_request) {
  27. if ($request_index == 'save_question') {
  28. unset($_POST[$request_index]);
  29. } elseif ($request_index == 'add_answer') {
  30. unset($_POST[$request_index]);
  31. } elseif($request_index == 'remove_answer') {
  32. unset($_POST[$request_index]);
  33. }
  34. }*/
  35. // Database table definitions
  36. $table_survey = Database:: get_course_table(TABLE_SURVEY);
  37. $table_survey_question = Database:: get_course_table(TABLE_SURVEY_QUESTION);
  38. $table_survey_question_option = Database:: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  39. $table_course = Database:: get_main_table(TABLE_MAIN_COURSE);
  40. $table_user = Database:: get_main_table(TABLE_MAIN_USER);
  41. $course_id = api_get_course_int_id();
  42. // Getting the survey information
  43. $surveyData = SurveyManager::get_survey($_GET['survey_id']);
  44. if (empty($surveyData)) {
  45. Display :: display_header(get_lang('ToolSurvey'));
  46. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  47. Display :: display_footer();
  48. exit;
  49. }
  50. $urlname = api_substr(api_html_entity_decode($surveyData['title'], ENT_QUOTES), 0, 40);
  51. if (api_strlen(strip_tags($surveyData['title'])) > 40) {
  52. $urlname .= '...';
  53. }
  54. if ($surveyData['survey_type'] == 1) {
  55. $sql = 'SELECT id FROM '.Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP).'
  56. WHERE
  57. c_id = '.$course_id.' AND
  58. survey_id = '.(int)$_GET['survey_id'].' LIMIT 1';
  59. $rs = Database::query($sql);
  60. if (Database::num_rows($rs)===0) {
  61. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.(int)$_GET['survey_id'].'&message='.'YouNeedToCreateGroups');
  62. exit;
  63. }
  64. }
  65. // Breadcrumbs
  66. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php', 'name' => get_lang('SurveyList'));
  67. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']), 'name' => strip_tags($urlname));
  68. // Tool name
  69. if ($_GET['action'] == 'add') {
  70. $tool_name = get_lang('AddQuestion');
  71. }
  72. if ($_GET['action'] == 'edit') {
  73. $tool_name = get_lang('EditQuestion');
  74. }
  75. // The possible question types
  76. $possible_types = array(
  77. 'personality',
  78. 'yesno',
  79. 'multiplechoice',
  80. 'multipleresponse',
  81. 'open',
  82. 'dropdown',
  83. 'comment',
  84. 'pagebreak',
  85. 'percentage',
  86. 'score'
  87. );
  88. // Actions
  89. $actions = '<div class="actions">';
  90. $actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.
  91. Display::return_icon('back.png', get_lang('BackToSurvey'),'',ICON_SIZE_MEDIUM).'</a>';
  92. $actions .= '</div>';
  93. // Checking if it is a valid type
  94. if (!in_array($_GET['type'], $possible_types)) {
  95. Display :: display_header($tool_name, 'Survey');
  96. echo $actions;
  97. Display :: display_error_message(get_lang('TypeDoesNotExist'), false);
  98. Display :: display_footer();
  99. }
  100. $error_message = '';
  101. // Displaying the form for adding or editing the question
  102. $ch_type = 'ch_'.$_GET['type'];
  103. /** @var survey_question $surveyQuestion */
  104. $surveyQuestion = new $ch_type;
  105. // The defaults values for the form
  106. $formData = array();
  107. $formData['answers'] = array('', '');
  108. if ($_GET['type'] == 'yesno') {
  109. $formData['answers'][0] = get_lang('Yes');
  110. $formData['answers'][1] = get_lang('No');
  111. }
  112. if ($_GET['type'] == 'personality') {
  113. $formData['answers'][0] = 1;
  114. $formData['answers'][1] = 2;
  115. $formData['answers'][2] = 3;
  116. $formData['answers'][3] = 4;
  117. $formData['answers'][4] = 5;
  118. $formData['values'][0] = 0;
  119. $formData['values'][1] = 0;
  120. $formData['values'][2] = 1;
  121. $formData['values'][3] = 2;
  122. $formData['values'][4] = 3;
  123. }
  124. // We are editing a question
  125. if (isset($_GET['question_id']) && !empty($_GET['question_id'])) {
  126. $formData = SurveyManager::get_question($_GET['question_id']);
  127. }
  128. $formData = $surveyQuestion->preSave($formData);
  129. $surveyQuestion->createForm($surveyData, $formData);
  130. $surveyQuestion->getForm()->setDefaults($formData);
  131. $surveyQuestion->renderForm();
  132. if ($surveyQuestion->getForm()->validate()) {
  133. $values = $surveyQuestion->getForm()->getSubmitValues();
  134. $surveyQuestion->save($surveyData, $values);
  135. }
  136. Display::display_header($tool_name, 'Survey');
  137. echo $surveyQuestion->getForm()->return_form();
  138. // Footer
  139. Display :: display_footer();