preview.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // including additional libraries
  30. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  31. require_once('survey.lib.php');
  32. // Database table definitions
  33. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  34. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  35. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  36. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  37. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  38. // We exit here if ther is no valid $_GET parameter
  39. if (!isset($_GET['survey_id']) OR !is_numeric($_GET['survey_id']))
  40. {
  41. Display :: display_header();
  42. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  43. Display :: display_footer();
  44. exit;
  45. }
  46. // getting the survey information
  47. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  48. $urlname = substr(html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40);
  49. if (strlen(strip_tags($survey_data['title'])) > 40)
  50. {
  51. $urlname .= '...';
  52. }
  53. // breadcrumbs
  54. $interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
  55. $interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$_GET['survey_id'], "name" => $urlname);
  56. // Header
  57. Display :: display_header(get_lang('SurveyPreview'));
  58. // We exit here is the first or last question is a pagebreak (which causes errors)
  59. SurveyUtil::check_first_last_question($_GET['survey_id'], false);
  60. // only a course admin is allowed to preview a survey: you are NOT a course admin => error message
  61. if (!api_is_allowed_to_edit(false,true))
  62. {
  63. Display :: display_error_message(get_lang('NotAllowed'), false);
  64. }
  65. // only a course admin is allowed to preview a survey: you are a course admin
  66. else
  67. {
  68. // survey information
  69. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  70. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  71. // displaying the survey introduction
  72. if (!isset($_GET['show']))
  73. {
  74. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  75. $limit = 0;
  76. }
  77. // displaying the survey thanks message
  78. if ($_POST['finish_survey'])
  79. {
  80. echo '<div id="survey_content" class="survey_content"><strong>'.get_lang('SurveyFinished').' </strong>'.$survey_data['survey_thanks'].'</div>';
  81. Display :: display_footer();
  82. exit;
  83. }
  84. if (isset($_GET['show']))
  85. {
  86. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  87. // as long as there is no pagebreak fount we keep adding questions to the page
  88. $questions_displayed = array();
  89. $counter = 0;
  90. $sql = "SELECT * FROM $table_survey_question
  91. WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."'
  92. ORDER BY sort ASC";
  93. $result = api_sql_query($sql, __FILE__, __LINE__);
  94. while ($row = Database::fetch_array($result))
  95. {
  96. if($row['type'] == 'pagebreak')
  97. {
  98. $counter++;
  99. }
  100. else
  101. {
  102. $paged_questions[$counter][] = $row['question_id'];
  103. }
  104. }
  105. if (key_exists($_GET['show'],$paged_questions))
  106. {
  107. $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,
  108. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  109. FROM $table_survey_question survey_question
  110. LEFT JOIN $table_survey_question_option survey_question_option
  111. ON survey_question.question_id = survey_question_option.question_id
  112. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  113. AND survey_question.question_id IN (".Database::escape_string(implode(',',$paged_questions[$_GET['show']])).")
  114. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  115. $result = api_sql_query($sql, __FILE__, __LINE__);
  116. $question_counter_max = Database::num_rows($result);
  117. $counter = 0;
  118. $limit=0;
  119. $questions = array();
  120. while ($row = Database::fetch_array($result))
  121. {
  122. // if the type is not a pagebreak we store it in the $questions array
  123. if($row['type'] <> 'pagebreak')
  124. {
  125. $questions[$row['sort']]['question_id'] = $row['question_id'];
  126. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  127. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  128. $questions[$row['sort']]['display'] = $row['display'];
  129. $questions[$row['sort']]['type'] = $row['type'];
  130. $questions[$row['sort']]['options'][intval($row['option_sort'])] = $row['option_text'];
  131. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  132. }
  133. // if the type is a pagebreak we are finished loading the questions for this page
  134. else
  135. {
  136. break;
  137. }
  138. $counter++;
  139. }
  140. }
  141. }
  142. // selecting the maximum number of pages
  143. $sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($_GET['survey_id'])."'";
  144. $result = api_sql_query($sql, __FILE__, __LINE__);
  145. $numberofpages = Database::num_rows($result) + 1;
  146. // Displaying the form with the questions
  147. if (isset($_GET['show']))
  148. {
  149. $show = (int)$_GET['show'] + 1;
  150. }
  151. else
  152. {
  153. $show = 0;
  154. }
  155. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.Security::remove_XSS($_GET['survey_id']).'&show='.$show.'">';
  156. if(is_array($questions) && count($questions)>0)
  157. {
  158. foreach ($questions as $key=>$question)
  159. {
  160. $display = new $question['type'];
  161. $display->render_question($question);
  162. }
  163. }
  164. if (($show < $numberofpages) OR !$_GET['show'])
  165. {
  166. //echo '<a href="'.api_get_self().'?survey_id='.$_GET['survey_id'].'&amp;show='.$limit.'">NEXT</a>';
  167. echo '<br /><input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
  168. }
  169. if ($show >= $numberofpages AND $_GET['show'])
  170. {
  171. echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
  172. }
  173. echo '</form>';
  174. }
  175. // Footer
  176. Display :: display_footer();
  177. ?>