question.php 4.8 KB

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