question.php 5.0 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. // Database table definitions
  26. $table_survey = Database:: get_course_table(TABLE_SURVEY);
  27. $table_survey_question = Database:: get_course_table(TABLE_SURVEY_QUESTION);
  28. $table_survey_question_option = Database:: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  29. $table_course = Database:: get_main_table(TABLE_MAIN_COURSE);
  30. $table_user = Database:: get_main_table(TABLE_MAIN_USER);
  31. $course_id = api_get_course_int_id();
  32. // Getting the survey information
  33. $surveyData = SurveyManager::get_survey($_GET['survey_id']);
  34. if (empty($surveyData)) {
  35. Display :: display_header(get_lang('ToolSurvey'));
  36. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  37. Display :: display_footer();
  38. exit;
  39. }
  40. $urlname = api_substr(api_html_entity_decode($surveyData['title'], ENT_QUOTES), 0, 40);
  41. if (api_strlen(strip_tags($surveyData['title'])) > 40) {
  42. $urlname .= '...';
  43. }
  44. if ($surveyData['survey_type'] == 1) {
  45. $sql = 'SELECT id FROM '.Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP).'
  46. WHERE
  47. c_id = '.$course_id.' AND
  48. survey_id = '.(int)$_GET['survey_id'].' LIMIT 1';
  49. $rs = Database::query($sql);
  50. if (Database::num_rows($rs)===0) {
  51. Display::addFlash(
  52. Display::return_message(get_lang('YouNeedToCreateGroups'))
  53. );
  54. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.(int)$_GET['survey_id'].'&'.api_get_cidreq());
  55. exit;
  56. }
  57. }
  58. // Breadcrumbs
  59. $interbreadcrumb[] = array(
  60. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  61. 'name' => get_lang('SurveyList'),
  62. );
  63. $interbreadcrumb[] = array(
  64. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&'.api_get_cidreq(),
  65. 'name' => strip_tags($urlname),
  66. );
  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']).'&'.api_get_cidreq().'">'.
  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();