preview.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  7. * @author Julio Montoya Armas <gugli100@gmail.com>, Chamilo: Personality Test modifications
  8. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  9. *
  10. * @todo use quickforms for the forms
  11. */
  12. // Language file that needs to be included
  13. $language_file = 'survey';
  14. // Including the global initialization file
  15. require '../inc/global.inc.php';
  16. require_once 'survey.lib.php';
  17. $this_section = SECTION_COURSES;
  18. // Database table definitions
  19. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  20. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  21. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  22. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  23. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  24. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  25. $course_id = api_get_course_int_id();
  26. $userId = api_get_user_id();
  27. $surveyId = Database::escape_string($_GET['survey_id']);
  28. $userInvited = 0;
  29. //query to ask if logged user is allowed to see the preview (if he is invited of he is a teacher)
  30. $sql = "SELECT survey_invitation.user
  31. FROM $table_survey_invitation survey_invitation
  32. LEFT JOIN $table_survey survey
  33. ON survey_invitation.survey_code = survey.code
  34. WHERE
  35. survey_invitation.c_id = $course_id AND
  36. survey.survey_id = $surveyId AND
  37. survey_invitation.user = $userId";
  38. $result = Database::query($sql);
  39. if (Database::num_rows($result) > 0) {
  40. $userInvited = 1;
  41. }
  42. if ($userInvited == 0) {
  43. if(!api_is_allowed_to_edit()) {
  44. api_not_allowed();
  45. }
  46. }
  47. // We exit here if ther is no valid $_GET parameter
  48. if (!isset($_GET['survey_id']) || !is_numeric($_GET['survey_id'])){
  49. Display::display_header(get_lang('SurveyPreview'));
  50. Display::display_error_message(get_lang('InvallidSurvey'), false);
  51. Display::display_footer();
  52. exit;
  53. }
  54. // Getting the survey information
  55. $survey_id = intval($_GET['survey_id']);
  56. $survey_data = survey_manager::get_survey($survey_id);
  57. if (empty($survey_data)) {
  58. Display::display_header(get_lang('SurveyPreview'));
  59. Display::display_error_message(get_lang('InvallidSurvey'), false);
  60. Display::display_footer();
  61. exit;
  62. }
  63. $urlname = strip_tags($survey_data['title']);
  64. // Breadcrumbs
  65. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php', 'name' => get_lang('SurveyList'));
  66. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id, 'name' => $urlname);
  67. // Header
  68. Display :: display_header(get_lang('SurveyPreview'));
  69. // We exit here is the first or last question is a pagebreak (which causes errors)
  70. SurveyUtil::check_first_last_question($survey_id, false);
  71. // Only a course admin is allowed to preview a survey: you are NOT a course admin => error message
  72. /*
  73. if (!api_is_allowed_to_edit(false, true)) {
  74. Display :: display_error_message(get_lang('NotAllowed'), false);
  75. }*/
  76. $counter_question = 0;
  77. // Only a course admin is allowed to preview a survey: you are a course admin
  78. if (api_is_course_admin() || (api_is_course_admin() && $_GET['isStudentView'] == 'true') || api_is_allowed_to_session_edit(false, true)) {
  79. // Survey information
  80. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  81. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  82. // Displaying the survey introduction
  83. if (!isset($_GET['show'])) {
  84. if (!empty($survey_data['survey_introduction'])) {
  85. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  86. }
  87. $limit = 0;
  88. }
  89. // Displaying the survey thanks message
  90. if (isset($_POST['finish_survey'])) {
  91. Display::display_confirmation_message(get_lang('SurveyFinished'));
  92. echo $survey_data['survey_thanks'];
  93. Display :: display_footer();
  94. exit;
  95. }
  96. $questions = array();
  97. if (isset($_GET['show'])) {
  98. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  99. // as long as there is no pagebreak fount we keep adding questions to the page
  100. $questions_displayed = array();
  101. $paged_questions = array();
  102. $counter = 0;
  103. $sql = "SELECT * FROM $table_survey_question
  104. WHERE c_id = $course_id AND survey_id = '".Database::escape_string($survey_id)."'
  105. ORDER BY sort ASC";
  106. $result = Database::query($sql);
  107. $questions_exists = true;
  108. if (Database::num_rows($result)) {
  109. while ($row = Database::fetch_array($result)) {
  110. if ($row['type'] == 'pagebreak') {
  111. $counter++;
  112. } else {
  113. $paged_questions[$counter][] = $row['question_id'];
  114. }
  115. }
  116. } else {
  117. $questions_exists = false;
  118. }
  119. if (array_key_exists($_GET['show'], $paged_questions)) {
  120. $sql = "SELECT
  121. survey_question.question_id,
  122. survey_question.survey_id,
  123. survey_question.survey_question,
  124. survey_question.display,
  125. survey_question.sort,
  126. survey_question.type,
  127. survey_question.max_value,
  128. survey_question_option.question_option_id,
  129. survey_question_option.option_text,
  130. survey_question_option.sort as option_sort
  131. FROM $table_survey_question survey_question
  132. LEFT JOIN $table_survey_question_option survey_question_option
  133. ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id
  134. WHERE
  135. survey_question.survey_id = '".Database::escape_string($survey_id)."' AND
  136. survey_question.question_id IN (".Database::escape_string(implode(',',$paged_questions[$_GET['show']])).") AND
  137. survey_question.c_id = $course_id
  138. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  139. $result = Database::query($sql);
  140. $question_counter_max = Database::num_rows($result);
  141. $limit = 0;
  142. while ($row = Database::fetch_array($result)) {
  143. // If the type is not a pagebreak we store it in the $questions array
  144. if ($row['type'] != 'pagebreak') {
  145. $questions[$row['sort']]['question_id'] = $row['question_id'];
  146. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  147. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  148. $questions[$row['sort']]['display'] = $row['display'];
  149. $questions[$row['sort']]['type'] = $row['type'];
  150. $questions[$row['sort']]['options'][intval($row['option_sort'])] = $row['option_text'];
  151. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  152. }
  153. // If the type is a pagebreak we are finished loading the questions for this page
  154. else {
  155. break;
  156. }
  157. $counter_question++;
  158. }
  159. }
  160. }
  161. // Selecting the maximum number of pages
  162. $sql = "SELECT * FROM $table_survey_question
  163. WHERE
  164. c_id = $course_id AND
  165. type='".Database::escape_string('pagebreak')."' AND
  166. survey_id='".Database::escape_string($survey_id)."'";
  167. $result = Database::query($sql);
  168. $numberofpages = Database::num_rows($result) + 1;
  169. // Displaying the form with the questions
  170. if (isset($_GET['show'])) {
  171. $show = (int)$_GET['show'] + 1;
  172. } else {
  173. $show = 0;
  174. }
  175. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.Security::remove_XSS($survey_id).'&show='.$show.'">';
  176. if (is_array($questions) && count($questions) > 0) {
  177. foreach ($questions as $key => & $question) {
  178. $ch_type = 'ch_'.$question['type'];
  179. $display = new $ch_type;
  180. $display->render_question($question);
  181. }
  182. }
  183. if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0)) {
  184. echo '<br /><button type="submit" name="next_survey_page" class="next">'.get_lang('NextQuestion').' </button>';
  185. }
  186. if ($show >= $numberofpages && $_GET['show'] || (isset($_GET['show']) && count($questions) == 0)) {
  187. if ($questions_exists == false) {
  188. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  189. }
  190. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').' </button>';
  191. }
  192. echo '</form>';
  193. } else {
  194. Display :: display_error_message(get_lang('NotAllowed'), false);
  195. }
  196. // Footer
  197. Display :: display_footer();