question.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. *
  6. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  7. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup,
  8. * refactoring and rewriting large parts of the code
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $htmlHeadXtra[] = '<script>
  12. $(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. api_not_allowed(true);
  20. }
  21. // Getting the survey information
  22. $surveyData = SurveyManager::get_survey($_GET['survey_id']);
  23. if (empty($surveyData)) {
  24. api_not_allowed(true);
  25. }
  26. // Is valid request
  27. $is_valid_request = isset($_REQUEST['is_executable']) ? $_REQUEST['is_executable'] : null;
  28. // Database table definitions
  29. $table_survey = Database::get_course_table(TABLE_SURVEY);
  30. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  31. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  32. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  33. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  34. $course_id = api_get_course_int_id();
  35. $urlname = api_substr(api_html_entity_decode($surveyData['title'], ENT_QUOTES), 0, 40);
  36. if (api_strlen(strip_tags($surveyData['title'])) > 40) {
  37. $urlname .= '...';
  38. }
  39. if ($surveyData['survey_type'] == 1) {
  40. $sql = 'SELECT id FROM '.Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP).'
  41. WHERE
  42. c_id = '.$course_id.' AND
  43. survey_id = '.(int) $_GET['survey_id'].' LIMIT 1';
  44. $rs = Database::query($sql);
  45. if (Database::num_rows($rs) === 0) {
  46. Display::addFlash(
  47. Display::return_message(get_lang('YouNeedToCreateGroups'))
  48. );
  49. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.(int) $_GET['survey_id']);
  50. exit;
  51. }
  52. }
  53. // Breadcrumbs
  54. $interbreadcrumb[] = [
  55. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php',
  56. 'name' => get_lang('SurveyList'),
  57. ];
  58. $interbreadcrumb[] = [
  59. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']),
  60. 'name' => strip_tags($urlname),
  61. ];
  62. // Tool name
  63. if ($_GET['action'] == 'add') {
  64. $tool_name = get_lang('AddQuestion');
  65. }
  66. if ($_GET['action'] == 'edit') {
  67. $tool_name = get_lang('EditQuestion');
  68. }
  69. // The possible question types
  70. $possible_types = [
  71. 'personality',
  72. 'yesno',
  73. 'multiplechoice',
  74. 'multipleresponse',
  75. 'open',
  76. 'dropdown',
  77. 'comment',
  78. 'pagebreak',
  79. 'percentage',
  80. 'score',
  81. ];
  82. // Actions
  83. $actions = '<div class="actions">';
  84. $actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'">'.
  85. Display::return_icon('back.png', get_lang('BackToSurvey'), '', ICON_SIZE_MEDIUM).'</a>';
  86. $actions .= '</div>';
  87. // Checking if it is a valid type
  88. if (!in_array($_GET['type'], $possible_types)) {
  89. Display :: display_header($tool_name, 'Survey');
  90. echo $actions;
  91. echo Display::return_message(get_lang('TypeDoesNotExist'), 'error', false);
  92. Display::display_footer();
  93. }
  94. $error_message = '';
  95. // Displaying the form for adding or editing the question
  96. $ch_type = 'ch_'.$_GET['type'];
  97. /** @var survey_question $surveyQuestion */
  98. $surveyQuestion = new $ch_type();
  99. // The defaults values for the form
  100. $formData = [];
  101. $formData['answers'] = ['', ''];
  102. switch ($_GET['type']) {
  103. case 'yesno':
  104. $formData['answers'][0] = get_lang('Yes');
  105. $formData['answers'][1] = get_lang('No');
  106. break;
  107. case 'personality':
  108. $formData['answers'][0] = 1;
  109. $formData['answers'][1] = 2;
  110. $formData['answers'][2] = 3;
  111. $formData['answers'][3] = 4;
  112. $formData['answers'][4] = 5;
  113. $formData['values'][0] = 0;
  114. $formData['values'][1] = 0;
  115. $formData['values'][2] = 1;
  116. $formData['values'][3] = 2;
  117. $formData['values'][4] = 3;
  118. break;
  119. case 'open':
  120. Display::addFlash(Display::return_message(get_lang('QuestionTags')));
  121. break;
  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();