preview.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. *
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  7. * @author Julio Montoya <gugli100@gmail.com>
  8. */
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. api_protect_course_script(true);
  11. if (!api_is_allowed_to_edit()) {
  12. api_not_allowed(true);
  13. }
  14. $surveyId = (int) $_GET['survey_id'];
  15. if (empty($surveyId)) {
  16. api_not_allowed(true);
  17. }
  18. // Getting the survey information
  19. $survey_data = SurveyManager::get_survey($surveyId);
  20. if (empty($survey_data)) {
  21. api_not_allowed(true);
  22. }
  23. $this_section = SECTION_COURSES;
  24. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  25. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  26. $course_id = api_get_course_int_id();
  27. $courseInfo = api_get_course_info();
  28. $allowRequiredSurveyQuestions = api_get_configuration_value('allow_required_survey_questions');
  29. // Breadcrumbs
  30. $interbreadcrumb[] = [
  31. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  32. 'name' => get_lang('SurveyList'),
  33. ];
  34. $interbreadcrumb[] = [
  35. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$surveyId.'&'.api_get_cidreq(),
  36. 'name' => strip_tags($survey_data['title']),
  37. ];
  38. $show = 0;
  39. Display::display_header(get_lang('SurveyPreview'));
  40. // We exit here is the first or last question is a pagebreak (which causes errors)
  41. SurveyUtil::check_first_last_question($surveyId, false);
  42. // Survey information
  43. echo '<div class="page-header"><h2>'.$survey_data['survey_title'].'</h2></div>';
  44. if (!empty($survey_data['survey_subtitle'])) {
  45. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  46. }
  47. // Displaying the survey introduction
  48. if (!isset($_GET['show'])) {
  49. if (!empty($survey_data['survey_introduction'])) {
  50. echo '<div class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  51. }
  52. }
  53. // Displaying the survey thanks message
  54. if (isset($_POST['finish_survey'])) {
  55. echo Display::return_message(get_lang('SurveyFinished'), 'confirm');
  56. echo $survey_data['survey_thanks'];
  57. Display::display_footer();
  58. exit;
  59. }
  60. $questions = [];
  61. if (isset($_GET['show'])) {
  62. // Getting all the questions for this page and add them to a
  63. // multidimensional array where the first index is the page.
  64. // as long as there is no pagebreak fount we keep adding questions to the page
  65. $questions_displayed = [];
  66. $paged_questions = [];
  67. $counter = 0;
  68. $sql = "SELECT * FROM $table_survey_question
  69. WHERE
  70. survey_question NOT LIKE '%{{%' AND
  71. c_id = $course_id AND
  72. survey_id = '".$surveyId."'
  73. ORDER BY sort ASC";
  74. $result = Database::query($sql);
  75. $questions_exists = true;
  76. if (Database::num_rows($result)) {
  77. while ($row = Database::fetch_array($result)) {
  78. if ($survey_data['one_question_per_page'] == 1) {
  79. if ($row['type'] != 'pagebreak') {
  80. $paged_questions[$counter][] = $row['question_id'];
  81. $counter++;
  82. continue;
  83. }
  84. } else {
  85. if ($row['type'] == 'pagebreak') {
  86. $counter++;
  87. } else {
  88. $paged_questions[$counter][] = $row['question_id'];
  89. }
  90. }
  91. }
  92. } else {
  93. $questions_exists = false;
  94. }
  95. if (array_key_exists($_GET['show'], $paged_questions)) {
  96. $sql = "SELECT
  97. survey_question.question_id,
  98. survey_question.survey_id,
  99. survey_question.survey_question,
  100. survey_question.display,
  101. survey_question.sort,
  102. survey_question.type,
  103. survey_question.max_value,
  104. survey_question_option.question_option_id,
  105. survey_question_option.option_text,
  106. survey_question_option.sort as option_sort
  107. ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')."
  108. FROM $table_survey_question survey_question
  109. LEFT JOIN $table_survey_question_option survey_question_option
  110. ON
  111. survey_question.question_id = survey_question_option.question_id AND
  112. survey_question_option.c_id = survey_question.c_id
  113. WHERE
  114. survey_question.survey_id = '".$surveyId."' AND
  115. survey_question.question_id IN (".Database::escape_string(implode(',', $paged_questions[$_GET['show']]), null, false).") AND
  116. survey_question.c_id = $course_id AND
  117. survey_question NOT LIKE '%{{%'
  118. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  119. $result = Database::query($sql);
  120. while ($row = Database::fetch_array($result)) {
  121. // If the type is not a pagebreak we store it in the $questions array
  122. if ($row['type'] != 'pagebreak') {
  123. $sort = $row['sort'];
  124. $questions[$sort]['question_id'] = $row['question_id'];
  125. $questions[$sort]['survey_id'] = $row['survey_id'];
  126. $questions[$sort]['survey_question'] = $row['survey_question'];
  127. $questions[$sort]['display'] = $row['display'];
  128. $questions[$sort]['type'] = $row['type'];
  129. $questions[$sort]['options'][$row['question_option_id']] = $row['option_text'];
  130. $questions[$sort]['maximum_score'] = $row['max_value'];
  131. $questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required'];
  132. }
  133. }
  134. }
  135. }
  136. $before = 0;
  137. if (isset($_GET['show']) && isset($paged_questions[$_GET['show'] - 1])) {
  138. $before = count($paged_questions[$_GET['show'] - 1]);
  139. }
  140. $numberOfPages = SurveyManager::getCountPages($survey_data);
  141. // Displaying the form with the questions
  142. if (isset($_GET['show'])) {
  143. $show = (int) $_GET['show'] + 1;
  144. }
  145. $originalShow = isset($_GET['show']) ? (int) $_GET['show'] : 0;
  146. $url = api_get_self().'?survey_id='.$surveyId.'&show='.$show.'&'.api_get_cidreq();
  147. $form = new FormValidator(
  148. 'question-survey',
  149. 'post',
  150. $url,
  151. null,
  152. null,
  153. FormValidator::LAYOUT_INLINE
  154. );
  155. if (is_array($questions) && count($questions) > 0) {
  156. $counter = 1;
  157. if (!empty($originalShow)) {
  158. $before = 0;
  159. foreach ($paged_questions as $keyQuestion => $list) {
  160. if ($originalShow > $keyQuestion) {
  161. $before += count($list);
  162. }
  163. }
  164. $counter = $before + 1;
  165. }
  166. foreach ($questions as $key => &$question) {
  167. $ch_type = 'ch_'.$question['type'];
  168. /** @var survey_question $display */
  169. $display = new $ch_type();
  170. $form->addHtml('<div class="survey_question '.$ch_type.'">');
  171. $form->addHtml('<div style="float:left; font-weight: bold; margin-right: 5px;"> '.$counter.'. </div>');
  172. $form->addHtml('<div>'.Security::remove_XSS($question['survey_question']).'</div> ');
  173. $display->render($form, $question);
  174. $form->addHtml('</div>');
  175. $counter++;
  176. }
  177. }
  178. $form->addHtml('<div class="start-survey">');
  179. if ($show < $numberOfPages) {
  180. if ($show == 0) {
  181. $form->addButton(
  182. 'next_survey_page',
  183. get_lang('StartSurvey'),
  184. 'arrow-right',
  185. 'success'
  186. );
  187. } else {
  188. $form->addButton(
  189. 'next_survey_page',
  190. get_lang('NextQuestion'),
  191. 'arrow-right',
  192. 'success'
  193. );
  194. }
  195. }
  196. if (isset($_GET['show'])) {
  197. if ($show >= $numberOfPages || count($questions) == 0) {
  198. if ($questions_exists == false) {
  199. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  200. }
  201. $form->addButton(
  202. 'finish_survey',
  203. get_lang('FinishSurvey'),
  204. 'arrow-right',
  205. 'success'
  206. );
  207. }
  208. }
  209. $form->addHtml('</div>');
  210. $form->display();
  211. echo Display::toolbarButton(
  212. get_lang('ReturnToCourseHomepage'),
  213. api_get_course_url($courseInfo['code']),
  214. 'home'
  215. );
  216. Display::display_footer();