question.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $course_id = api_get_course_int_id();
  27. $urlname = api_substr(api_html_entity_decode($surveyData['title'], ENT_QUOTES), 0, 40);
  28. if (api_strlen(strip_tags($surveyData['title'])) > 40) {
  29. $urlname .= '...';
  30. }
  31. if ($surveyData['survey_type'] == 1) {
  32. $sql = 'SELECT id FROM '.Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP).'
  33. WHERE
  34. c_id = '.$course_id.' AND
  35. survey_id = '.(int) $_GET['survey_id'].' LIMIT 1';
  36. $rs = Database::query($sql);
  37. if (Database::num_rows($rs) === 0) {
  38. Display::addFlash(
  39. Display::return_message(get_lang('You need to create groups'))
  40. );
  41. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.(int) $_GET['survey_id']);
  42. exit;
  43. }
  44. }
  45. // Breadcrumbs
  46. $interbreadcrumb[] = [
  47. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php',
  48. 'name' => get_lang('Survey list'),
  49. ];
  50. $interbreadcrumb[] = [
  51. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']),
  52. 'name' => strip_tags($urlname),
  53. ];
  54. // Tool name
  55. if ($_GET['action'] == 'add') {
  56. $tool_name = get_lang('Add a question');
  57. }
  58. if ($_GET['action'] == 'edit') {
  59. $tool_name = get_lang('Edit question');
  60. }
  61. // The possible question types
  62. $possible_types = [
  63. 'personality',
  64. 'yesno',
  65. 'multiplechoice',
  66. 'multipleresponse',
  67. 'open',
  68. 'dropdown',
  69. 'comment',
  70. 'pagebreak',
  71. 'percentage',
  72. 'score',
  73. ];
  74. // Actions
  75. $actions = '<div class="actions">';
  76. $actions .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'">'.
  77. Display::return_icon('back.png', get_lang('Back to survey'), '', ICON_SIZE_MEDIUM).'</a>';
  78. $actions .= '</div>';
  79. // Checking if it is a valid type
  80. if (!in_array($_GET['type'], $possible_types)) {
  81. Display :: display_header($tool_name, 'Survey');
  82. echo $actions;
  83. echo Display::return_message(get_lang('This type does not exist'), 'error', false);
  84. Display::display_footer();
  85. }
  86. // Displaying the form for adding or editing the question
  87. $surveyQuestion = survey_question::createQuestion($_GET['type']);
  88. // The defaults values for the form
  89. $formData = [];
  90. $formData['answers'] = ['', ''];
  91. switch ($_GET['type']) {
  92. case 'yesno':
  93. $formData['answers'][0] = get_lang('Yes');
  94. $formData['answers'][1] = get_lang('No');
  95. break;
  96. case 'personality':
  97. $formData['answers'][0] = 1;
  98. $formData['answers'][1] = 2;
  99. $formData['answers'][2] = 3;
  100. $formData['answers'][3] = 4;
  101. $formData['answers'][4] = 5;
  102. $formData['values'][0] = 0;
  103. $formData['values'][1] = 0;
  104. $formData['values'][2] = 1;
  105. $formData['values'][3] = 2;
  106. $formData['values'][4] = 3;
  107. break;
  108. case 'open':
  109. Display::addFlash(Display::return_message(get_lang('You can use the tags {{class_name}} and {{student_full_name}} in the question to be able to multiplicate questions.')));
  110. break;
  111. }
  112. // We are editing a question
  113. if (isset($_GET['question_id']) && !empty($_GET['question_id'])) {
  114. $formData = SurveyManager::get_question($_GET['question_id']);
  115. }
  116. $formData = $surveyQuestion->preSave($formData);
  117. $surveyQuestion->createForm($surveyData, $formData);
  118. $surveyQuestion->getForm()->setDefaults($formData);
  119. $surveyQuestion->renderForm();
  120. if ($surveyQuestion->getForm()->validate()) {
  121. $values = $surveyQuestion->getForm()->getSubmitValues();
  122. $surveyQuestion->save($surveyData, $values);
  123. }
  124. Display::display_header($tool_name, 'Survey');
  125. echo $surveyQuestion->getForm()->returnForm();
  126. Display::display_footer();