preview.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * @author Julio Montoya Armas <gugli100@gmail.com>, Chamilo: Personality Test modifications
  8. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  9. *
  10. * @todo use quickforms for the forms
  11. */
  12. // Language file that needs to be included
  13. $language_file = 'survey';
  14. // Including the global initialization file
  15. require '../inc/global.inc.php';
  16. require_once 'survey.lib.php';
  17. $this_section = SECTION_COURSES;
  18. // Database table definitions
  19. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  20. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  21. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  22. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  23. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  24. $course_id = api_get_course_int_id();
  25. // We exit here if ther is no valid $_GET parameter
  26. if (!isset($_GET['survey_id']) || !is_numeric($_GET['survey_id'])){
  27. Display :: display_header(get_lang('SurveyPreview'));
  28. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  29. Display :: display_footer();
  30. exit;
  31. }
  32. // Getting the survey information
  33. $survey_id = intval($_GET['survey_id']);
  34. $survey_data = survey_manager::get_survey($survey_id);
  35. if (empty($survey_data)) {
  36. Display :: display_header(get_lang('SurveyPreview'));
  37. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  38. Display :: display_footer();
  39. exit;
  40. }
  41. /*$urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  42. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  43. $urlname .= '...';
  44. }*/
  45. $urlname = strip_tags($survey_data['title']);
  46. // Breadcrumbs
  47. $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  48. $interbreadcrumb[] = array('url' => 'survey.php?survey_id='.$survey_id, 'name' => $urlname);
  49. // Header
  50. Display :: display_header(get_lang('SurveyPreview'));
  51. // We exit here is the first or last question is a pagebreak (which causes errors)
  52. SurveyUtil::check_first_last_question($survey_id, false);
  53. $counter_question = 0;
  54. $questions = array();
  55. // Only a course admin is allowed to preview a survey: you are a course admin
  56. if (api_is_course_admin() || (api_is_course_admin() && $_GET['isStudentView'] == 'true') || api_is_allowed_to_session_edit(false, true)) {
  57. // Survey information
  58. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  59. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  60. // Displaying the survey introduction
  61. if (!isset($_GET['show'])) {
  62. if (!empty($survey_data['survey_introduction'])) {
  63. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  64. }
  65. $limit = 0;
  66. }
  67. // Displaying the survey thanks message
  68. if (isset($_POST['finish_survey'])) {
  69. Display::display_confirmation_message(get_lang('SurveyFinished'));
  70. echo $survey_data['survey_thanks'];
  71. Display :: display_footer();
  72. exit;
  73. }
  74. if (isset($_GET['show'])) {
  75. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  76. // as long as there is no pagebreak fount we keep adding questions to the page
  77. $questions_displayed = array();
  78. $paged_questions = array();
  79. $counter = 0;
  80. $sql = "SELECT * FROM $table_survey_question
  81. WHERE c_id = $course_id AND survey_id = '".Database::escape_string($survey_id)."'
  82. ORDER BY sort ASC";
  83. $result = Database::query($sql);
  84. $questions_exists = true;
  85. if (Database::num_rows($result)) {
  86. while ($row = Database::fetch_array($result)) {
  87. if ($row['type'] == 'pagebreak') {
  88. $counter++;
  89. } else {
  90. $paged_questions[$counter][] = $row['question_id'];
  91. }
  92. }
  93. } else {
  94. $questions_exists = false;
  95. }
  96. if (array_key_exists($_GET['show'], $paged_questions)) {
  97. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value,
  98. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  99. FROM $table_survey_question survey_question LEFT JOIN $table_survey_question_option survey_question_option
  100. ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id
  101. WHERE survey_question.survey_id = '".Database::escape_string($survey_id)."' AND
  102. survey_question.question_id IN (".Database::escape_string(implode(',',$paged_questions[$_GET['show']])).") AND
  103. survey_question.c_id = $course_id
  104. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  105. $result = Database::query($sql);
  106. $question_counter_max = Database::num_rows($result);
  107. $limit = 0;
  108. while ($row = Database::fetch_array($result)) {
  109. // If the type is not a pagebreak we store it in the $questions array
  110. if ($row['type'] != 'pagebreak') {
  111. $questions[$row['sort']]['question_id'] = $row['question_id'];
  112. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  113. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  114. $questions[$row['sort']]['display'] = $row['display'];
  115. $questions[$row['sort']]['type'] = $row['type'];
  116. $questions[$row['sort']]['options'][intval($row['option_sort'])] = $row['option_text'];
  117. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  118. }
  119. // If the type is a pagebreak we are finished loading the questions for this page
  120. else {
  121. break;
  122. }
  123. $counter_question++;
  124. }
  125. }
  126. }
  127. // Selecting the maximum number of pages
  128. $sql = "SELECT * FROM $table_survey_question WHERE c_id = $course_id AND type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_id)."'";
  129. $result = Database::query($sql);
  130. $numberofpages = Database::num_rows($result) + 1;
  131. // Displaying the form with the questions
  132. if (isset($_GET['show'])) {
  133. $show = (int)$_GET['show'] + 1;
  134. } else {
  135. $show = 0;
  136. }
  137. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.Security::remove_XSS($survey_id).'&show='.$show.'">';
  138. if (isset($questions) && is_array($questions) && count($questions) > 0) {
  139. foreach ($questions as $key => & $question) {
  140. $ch_type = 'ch_'.$question['type'];
  141. $display = new $ch_type;
  142. $display->render_question($question);
  143. }
  144. }
  145. if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0)) {
  146. echo '<br /><button type="submit" name="next_survey_page" class="next">'.get_lang('NextQuestion').' </button>';
  147. }
  148. if ($show >= $numberofpages && $_GET['show'] || (isset($_GET['show']) && count($questions) == 0)) {
  149. if ($questions_exists == false) {
  150. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  151. }
  152. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').' </button>';
  153. }
  154. echo '</form>';
  155. } else {
  156. Display :: display_error_message(get_lang('NotAllowed'), false);
  157. }
  158. // Footer
  159. Display :: display_footer();