preview.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue des Palais 44 Paleizenstraat
  13. B-1030 Brussels - Belgium
  14. Tel. +32 (2) 211 34 56
  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. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  21. *
  22. * @todo use quickforms for the forms
  23. */
  24. // name of the language file that needs to be included
  25. $language_file = 'survey';
  26. // including the global dokeos file
  27. require ('../inc/global.inc.php');
  28. // including additional libraries
  29. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  30. require_once('survey.lib.php');
  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. {
  40. Display :: display_header();
  41. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  42. Display :: display_footer();
  43. exit;
  44. }
  45. // getting the survey information
  46. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  47. $urlname = substr(strip_tags($survey_data['title']), 0, 40);
  48. if (strlen(strip_tags($survey_data['title'])) > 40)
  49. {
  50. $urlname .= '...';
  51. }
  52. // breadcrumbs
  53. $interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
  54. $interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$_GET['survey_id'], "name" => $urlname);
  55. // Header
  56. Display :: display_header(get_lang('SurveyPreview'));
  57. // We exit here is the first or last question is a pagebreak (which causes errors)
  58. SurveyUtil::check_first_last_question($_GET['survey_id'], false);
  59. // only a course admin is allowed to preview a survey: you are NOT a course admin => error message
  60. if (!api_is_allowed_to_edit())
  61. {
  62. Display :: display_error_message(get_lang('NotAllowed'), false);
  63. }
  64. // only a course admin is allowed to preview a survey: you are a course admin
  65. else
  66. {
  67. // survey information
  68. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  69. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  70. // displaying the survey introduction
  71. if (!isset($_GET['show']))
  72. {
  73. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  74. $limit = 0;
  75. }
  76. // displaying the survey thanks message
  77. if ($_POST['finish_survey'])
  78. {
  79. echo '<div id="survey_content" class="survey_content"><strong>'.get_lang('SurveyFinished').' </strong>'.$survey_data['survey_thanks'].'</div>';
  80. Display :: display_footer();
  81. exit;
  82. }
  83. if (isset($_GET['show']))
  84. {
  85. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  86. // as long as there is no pagebreak fount we keep adding questions to the page
  87. $questions_displayed = array();
  88. $counter = 0;
  89. $sql = "SELECT * FROM $table_survey_question
  90. WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."'
  91. ORDER BY sort ASC";
  92. $result = api_sql_query($sql, __FILE__, __LINE__);
  93. while ($row = Database::fetch_array($result))
  94. {
  95. if($row['type'] == 'pagebreak')
  96. {
  97. $counter++;
  98. }
  99. else
  100. {
  101. $paged_questions[$counter][] = $row['question_id'];
  102. }
  103. }
  104. if (key_exists($_GET['show'],$paged_questions))
  105. {
  106. $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,
  107. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  108. FROM $table_survey_question survey_question
  109. LEFT JOIN $table_survey_question_option survey_question_option
  110. ON survey_question.question_id = survey_question_option.question_id
  111. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  112. AND survey_question.question_id IN (".Database::escape_string(implode(',',$paged_questions[$_GET['show']])).")
  113. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  114. $result = api_sql_query($sql, __FILE__, __LINE__);
  115. $question_counter_max = Database::num_rows($result);
  116. $counter = 0;
  117. $limit=0;
  118. $questions = array();
  119. while ($row = Database::fetch_array($result))
  120. {
  121. // if the type is not a pagebreak we store it in the $questions array
  122. if($row['type'] <> 'pagebreak')
  123. {
  124. $questions[$row['sort']]['question_id'] = $row['question_id'];
  125. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  126. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  127. $questions[$row['sort']]['display'] = $row['display'];
  128. $questions[$row['sort']]['type'] = $row['type'];
  129. $questions[$row['sort']]['options'][intval($row['option_sort'])] = $row['option_text'];
  130. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  131. }
  132. // if the type is a pagebreak we are finished loading the questions for this page
  133. else
  134. {
  135. break;
  136. }
  137. $counter++;
  138. }
  139. }
  140. }
  141. // selecting the maximum number of pages
  142. $sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($_GET['survey_id'])."'";
  143. $result = api_sql_query($sql, __FILE__, __LINE__);
  144. $numberofpages = Database::num_rows($result) + 1;
  145. // Displaying the form with the questions
  146. if (isset($_GET['show']))
  147. {
  148. $show = (int)$_GET['show'] + 1;
  149. }
  150. else
  151. {
  152. $show = 0;
  153. }
  154. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.Security::remove_XSS($_GET['survey_id']).'&show='.$show.'">';
  155. if(is_array($questions) && count($questions)>0)
  156. {
  157. foreach ($questions as $key=>$question)
  158. {
  159. $display = new $question['type'];
  160. $display->render_question($question);
  161. }
  162. }
  163. if (($show < $numberofpages) OR !$_GET['show'])
  164. {
  165. //echo '<a href="'.api_get_self().'?survey_id='.$_GET['survey_id'].'&amp;show='.$limit.'">NEXT</a>';
  166. echo '<br /><input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
  167. }
  168. if ($show >= $numberofpages AND $_GET['show'])
  169. {
  170. echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
  171. }
  172. echo '</form>';
  173. }
  174. // Footer
  175. Display :: display_footer();
  176. ?>