preview.php 8.6 KB

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