preview.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. $paged_questions = [];
  66. $counter = 0;
  67. $sql = "SELECT * FROM $table_survey_question
  68. WHERE
  69. survey_question NOT LIKE '%{{%' AND
  70. c_id = $course_id AND
  71. survey_id = '".$surveyId."'
  72. ORDER BY sort ASC";
  73. $result = Database::query($sql);
  74. $questions_exists = true;
  75. if (Database::num_rows($result)) {
  76. while ($row = Database::fetch_array($result)) {
  77. if ($survey_data['one_question_per_page'] == 1) {
  78. if ($row['type'] != 'pagebreak') {
  79. $paged_questions[$counter][] = $row['question_id'];
  80. $counter++;
  81. continue;
  82. }
  83. } else {
  84. if ($row['type'] == 'pagebreak') {
  85. $counter++;
  86. } else {
  87. $paged_questions[$counter][] = $row['question_id'];
  88. }
  89. }
  90. }
  91. } else {
  92. $questions_exists = false;
  93. }
  94. if (array_key_exists($_GET['show'], $paged_questions)) {
  95. $sql = "SELECT
  96. survey_question.question_id,
  97. survey_question.survey_id,
  98. survey_question.survey_question,
  99. survey_question.display,
  100. survey_question.sort,
  101. survey_question.type,
  102. survey_question.max_value,
  103. survey_question_option.question_option_id,
  104. survey_question_option.option_text,
  105. survey_question_option.sort as option_sort
  106. ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')."
  107. FROM $table_survey_question survey_question
  108. LEFT JOIN $table_survey_question_option survey_question_option
  109. ON
  110. survey_question.question_id = survey_question_option.question_id AND
  111. survey_question_option.c_id = survey_question.c_id
  112. WHERE
  113. survey_question.survey_id = '".$surveyId."' AND
  114. survey_question.question_id IN (".Database::escape_string(implode(',', $paged_questions[$_GET['show']]), null, false).") AND
  115. survey_question.c_id = $course_id AND
  116. survey_question NOT LIKE '%{{%'
  117. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  118. $result = Database::query($sql);
  119. while ($row = Database::fetch_array($result)) {
  120. // If the type is not a pagebreak we store it in the $questions array
  121. if ($row['type'] != 'pagebreak') {
  122. $sort = $row['sort'];
  123. $questions[$sort]['question_id'] = $row['question_id'];
  124. $questions[$sort]['survey_id'] = $row['survey_id'];
  125. $questions[$sort]['survey_question'] = $row['survey_question'];
  126. $questions[$sort]['display'] = $row['display'];
  127. $questions[$sort]['type'] = $row['type'];
  128. $questions[$sort]['options'][$row['question_option_id']] = $row['option_text'];
  129. $questions[$sort]['maximum_score'] = $row['max_value'];
  130. $questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required'];
  131. }
  132. }
  133. }
  134. }
  135. $numberOfPages = SurveyManager::getCountPages($survey_data);
  136. // Displaying the form with the questions
  137. if (isset($_GET['show'])) {
  138. $show = (int) $_GET['show'] + 1;
  139. }
  140. $originalShow = isset($_GET['show']) ? (int) $_GET['show'] : 0;
  141. $url = api_get_self().'?survey_id='.$surveyId.'&show='.$show.'&'.api_get_cidreq();
  142. $form = new FormValidator(
  143. 'question-survey',
  144. 'post',
  145. $url,
  146. null,
  147. null,
  148. FormValidator::LAYOUT_INLINE
  149. );
  150. if (is_array($questions) && count($questions) > 0) {
  151. $counter = 1;
  152. if (!empty($originalShow)) {
  153. $before = 0;
  154. foreach ($paged_questions as $keyQuestion => $list) {
  155. if ($originalShow > $keyQuestion) {
  156. $before += count($list);
  157. }
  158. }
  159. $counter = $before + 1;
  160. }
  161. foreach ($questions as $key => &$question) {
  162. $ch_type = 'ch_'.$question['type'];
  163. $display = survey_question::createQuestion($question['type']);
  164. $form->addHtml('<div class="survey_question '.$ch_type.'">');
  165. $form->addHtml('<div style="float:left; font-weight: bold; margin-right: 5px;"> '.$counter.'. </div>');
  166. $form->addHtml('<div>'.Security::remove_XSS($question['survey_question']).'</div> ');
  167. $display->render($form, $question);
  168. $form->addHtml('</div>');
  169. $counter++;
  170. }
  171. }
  172. $form->addHtml('<div class="start-survey">');
  173. if ($show < $numberOfPages) {
  174. if ($show == 0) {
  175. $form->addButton(
  176. 'next_survey_page',
  177. get_lang('StartSurvey'),
  178. 'arrow-right',
  179. 'success'
  180. );
  181. } else {
  182. $form->addButton(
  183. 'next_survey_page',
  184. get_lang('NextQuestion'),
  185. 'arrow-right',
  186. 'success'
  187. );
  188. }
  189. }
  190. if (isset($_GET['show'])) {
  191. if ($show >= $numberOfPages || count($questions) == 0) {
  192. if ($questions_exists == false) {
  193. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  194. }
  195. $form->addButton(
  196. 'finish_survey',
  197. get_lang('FinishSurvey'),
  198. 'arrow-right',
  199. 'success'
  200. );
  201. }
  202. }
  203. $form->addHtml('</div>');
  204. $form->display();
  205. echo Display::toolbarButton(
  206. get_lang('ReturnToCourseHomepage'),
  207. api_get_course_url($courseInfo['code']),
  208. 'home'
  209. );
  210. Display::display_footer();