preview.php 7.2 KB

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