preview.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  14. ==============================================================================
  15. */
  16. /**
  17. * @package dokeos.survey
  18. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  19. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  20. * @author Julio Montoya Armas <gugli100@gmail.com>, Dokeos: Personality Test modifications
  21. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  22. *
  23. * @todo use quickforms for the forms
  24. */
  25. // name of the language file that needs to be included
  26. $language_file = 'survey';
  27. // including the global dokeos file
  28. require ('../inc/global.inc.php');
  29. require_once('survey.lib.php');
  30. $this_section=SECTION_COURSES;
  31. // Database table definitions
  32. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  33. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  34. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  35. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  36. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  37. // We exit here if ther is no valid $_GET parameter
  38. if (!isset($_GET['survey_id']) OR !is_numeric($_GET['survey_id'])){
  39. Display :: display_header(get_lang('SurveyPreview'));
  40. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  41. Display :: display_footer();
  42. exit;
  43. }
  44. // getting the survey information
  45. $survey_id = intval($_GET['survey_id']);
  46. $survey_data = survey_manager::get_survey($survey_id);
  47. if (empty($survey_data)) {
  48. Display :: display_header(get_lang('SurveyPreview'));
  49. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  50. Display :: display_footer();
  51. exit;
  52. }
  53. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40));
  54. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  55. $urlname .= '...';
  56. }
  57. // breadcrumbs
  58. $interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
  59. $interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$survey_id, "name" => $urlname);
  60. // Header
  61. Display :: display_header(get_lang('SurveyPreview'));
  62. // We exit here is the first or last question is a pagebreak (which causes errors)
  63. SurveyUtil::check_first_last_question($survey_id, false);
  64. // only a course admin is allowed to preview a survey: you are NOT a course admin => error message
  65. /*
  66. if (!api_is_allowed_to_edit(false,true))
  67. {
  68. Display :: display_error_message(get_lang('NotAllowed'), false);
  69. }*/
  70. $counter_question = 0;
  71. // only a course admin is allowed to preview a survey: you are a course admin
  72. if (api_is_course_admin() || (api_is_course_admin() && $_GET['isStudentView']=='true') || api_is_allowed_to_session_edit(false,true)) {
  73. // survey information
  74. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  75. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  76. // displaying the survey introduction
  77. if (!isset($_GET['show']))
  78. {
  79. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  80. $limit = 0;
  81. }
  82. // displaying the survey thanks message
  83. if(isset($_POST['finish_survey']))
  84. {
  85. echo '<div id="survey_content" class="survey_content"><strong>'.get_lang('SurveyFinished').' </strong>'.$survey_data['survey_thanks'].'</div>';
  86. Display :: display_footer();
  87. exit;
  88. }
  89. if (isset($_GET['show']))
  90. {
  91. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  92. // as long as there is no pagebreak fount we keep adding questions to the page
  93. $questions_displayed = array();
  94. $paged_questions = array();
  95. $counter = 0;
  96. $sql = "SELECT * FROM $table_survey_question
  97. WHERE survey_id = '".Database::escape_string($survey_id)."'
  98. ORDER BY sort ASC";
  99. $result = Database::query($sql, __FILE__, __LINE__);
  100. while ($row = Database::fetch_array($result))
  101. {
  102. if($row['type'] == 'pagebreak') {
  103. $counter++;
  104. } else {
  105. $paged_questions[$counter][] = $row['question_id'];
  106. }
  107. }
  108. if (array_key_exists($_GET['show'], $paged_questions))
  109. {
  110. $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,
  111. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  112. FROM $table_survey_question survey_question
  113. LEFT JOIN $table_survey_question_option survey_question_option
  114. ON survey_question.question_id = survey_question_option.question_id
  115. WHERE survey_question.survey_id = '".Database::escape_string($survey_id)."'
  116. AND survey_question.question_id IN (".Database::escape_string(implode(',',$paged_questions[$_GET['show']])).")
  117. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  118. $result = Database::query($sql, __FILE__, __LINE__);
  119. $question_counter_max = Database::num_rows($result);
  120. $limit=0;
  121. while ($row = Database::fetch_array($result))
  122. {
  123. // if the type is not a pagebreak we store it in the $questions array
  124. if($row['type'] <> 'pagebreak')
  125. {
  126. $questions[$row['sort']]['question_id'] = $row['question_id'];
  127. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  128. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  129. $questions[$row['sort']]['display'] = $row['display'];
  130. $questions[$row['sort']]['type'] = $row['type'];
  131. $questions[$row['sort']]['options'][intval($row['option_sort'])] = $row['option_text'];
  132. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  133. }
  134. // if the type is a pagebreak we are finished loading the questions for this page
  135. else
  136. {
  137. break;
  138. }
  139. $counter_question++;
  140. }
  141. }
  142. }
  143. // selecting the maximum number of pages
  144. $sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_id)."'";
  145. $result = Database::query($sql, __FILE__, __LINE__);
  146. $numberofpages = Database::num_rows($result) + 1;
  147. // Displaying the form with the questions
  148. if (isset($_GET['show']))
  149. {
  150. $show = (int)$_GET['show'] + 1;
  151. }
  152. else
  153. {
  154. $show = 0;
  155. }
  156. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.Security::remove_XSS($survey_id).'&show='.$show.'">';
  157. if(is_array($questions) && count($questions)>0)
  158. {
  159. foreach ($questions as $key=>$question)
  160. {
  161. $display = new $question['type'];
  162. $display->render_question($question);
  163. }
  164. }
  165. if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0))
  166. {
  167. echo '<br /><button type="submit" name="next_survey_page" class="next">'.get_lang('NextQuestion').' </button>';
  168. }
  169. if ($show >= $numberofpages && $_GET['show'] || ( isset($_GET['show']) && count($questions) == 0))
  170. {
  171. if (count($questions) == 0) {
  172. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  173. }
  174. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').' </button>';
  175. }
  176. echo '</form>';
  177. } else {
  178. Display :: display_error_message(get_lang('NotAllowed'), false);
  179. }
  180. // Footer
  181. Display :: display_footer();