preview.php 9.0 KB

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