question.php 5.1 KB

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