preview.php 7.8 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. // 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. Display :: display_header(get_lang('SurveyPreview'));
  41. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  42. Display :: display_footer();
  43. exit;
  44. }
  45. // getting the survey information
  46. $survey_id = Security::remove_XSS($_GET['survey_id']);
  47. $survey_data = survey_manager::get_survey($survey_id);
  48. if (empty($survey_data)) {
  49. Display :: display_header(get_lang('SurveyPreview'));
  50. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  51. Display :: display_footer();
  52. exit;
  53. }
  54. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'],ENT_QUOTES,$charset), 0, 40));
  55. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  56. $urlname .= '...';
  57. }
  58. // breadcrumbs
  59. $interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
  60. $interbreadcrumb[] = array ("url" => "survey.php?survey_id=".$survey_id, "name" => $urlname);
  61. // Header
  62. Display :: display_header(get_lang('SurveyPreview'));
  63. // We exit here is the first or last question is a pagebreak (which causes errors)
  64. SurveyUtil::check_first_last_question($survey_id, false);
  65. // only a course admin is allowed to preview a survey: you are NOT a course admin => error message
  66. /*
  67. if (!api_is_allowed_to_edit(false,true))
  68. {
  69. Display :: display_error_message(get_lang('NotAllowed'), false);
  70. }*/
  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')) {
  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. $counter = 0;
  95. $sql = "SELECT * FROM $table_survey_question
  96. WHERE survey_id = '".Database::escape_string($survey_id)."'
  97. ORDER BY sort ASC";
  98. $result = api_sql_query($sql, __FILE__, __LINE__);
  99. while ($row = Database::fetch_array($result))
  100. {
  101. if($row['type'] == 'pagebreak')
  102. {
  103. $counter++;
  104. }
  105. else
  106. {
  107. $paged_questions[$counter][] = $row['question_id'];
  108. }
  109. }
  110. if (key_exists($_GET['show'],$paged_questions))
  111. {
  112. $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,
  113. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  114. FROM $table_survey_question survey_question
  115. LEFT JOIN $table_survey_question_option survey_question_option
  116. ON survey_question.question_id = survey_question_option.question_id
  117. WHERE survey_question.survey_id = '".Database::escape_string($survey_id)."'
  118. AND survey_question.question_id IN (".Database::escape_string(implode(',',$paged_questions[$_GET['show']])).")
  119. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  120. $result = api_sql_query($sql, __FILE__, __LINE__);
  121. $question_counter_max = Database::num_rows($result);
  122. $counter = 0;
  123. $limit=0;
  124. $questions = array();
  125. while ($row = Database::fetch_array($result))
  126. {
  127. // if the type is not a pagebreak we store it in the $questions array
  128. if($row['type'] <> 'pagebreak')
  129. {
  130. $questions[$row['sort']]['question_id'] = $row['question_id'];
  131. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  132. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  133. $questions[$row['sort']]['display'] = $row['display'];
  134. $questions[$row['sort']]['type'] = $row['type'];
  135. $questions[$row['sort']]['options'][intval($row['option_sort'])] = $row['option_text'];
  136. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  137. }
  138. // if the type is a pagebreak we are finished loading the questions for this page
  139. else
  140. {
  141. break;
  142. }
  143. $counter++;
  144. }
  145. }
  146. }
  147. // selecting the maximum number of pages
  148. $sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_id)."'";
  149. $result = api_sql_query($sql, __FILE__, __LINE__);
  150. $numberofpages = Database::num_rows($result) + 1;
  151. // Displaying the form with the questions
  152. if (isset($_GET['show']))
  153. {
  154. $show = (int)$_GET['show'] + 1;
  155. }
  156. else
  157. {
  158. $show = 0;
  159. }
  160. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.Security::remove_XSS($survey_id).'&show='.$show.'">';
  161. if(is_array($questions) && count($questions)>0)
  162. {
  163. foreach ($questions as $key=>$question)
  164. {
  165. $display = new $question['type'];
  166. $display->render_question($question);
  167. }
  168. }
  169. if (($show < $numberofpages) OR !$_GET['show'])
  170. {
  171. //echo '<a href="'.api_get_self().'?survey_id='.$survey_id.'&amp;show='.$limit.'">NEXT</a>';
  172. echo '<br /><button type="submit" name="next_survey_page" class="next">'.get_lang('NextQuestion').' </button>';
  173. }
  174. if ($show >= $numberofpages AND $_GET['show'])
  175. {
  176. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').' </button>';
  177. }
  178. echo '</form>';
  179. } else {
  180. Display :: display_error_message(get_lang('NotAllowed'), false);
  181. }
  182. // Footer
  183. Display :: display_footer();
  184. ?>