preview.php 9.3 KB

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