preview.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 __DIR__.'/../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(
  40. true,
  41. Display::return_message(get_lang('InvallidSurvey'), 'error', false)
  42. );
  43. exit;
  44. }
  45. // Getting the survey information
  46. $survey_id = intval($_GET['survey_id']);
  47. $survey_data = SurveyManager::get_survey($survey_id);
  48. if (empty($survey_data)) {
  49. api_not_allowed(
  50. true,
  51. Display::return_message(get_lang('InvallidSurvey'), 'error', false)
  52. );
  53. exit;
  54. }
  55. $urlname = strip_tags($survey_data['title']);
  56. if (api_is_allowed_to_edit()) {
  57. // Breadcrumbs
  58. $interbreadcrumb[] = array(
  59. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  60. 'name' => get_lang('SurveyList')
  61. );
  62. $interbreadcrumb[] = array(
  63. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  64. 'name' => $urlname,
  65. );
  66. }
  67. $courseCode = isset($_GET['cidReq']) ? $_GET['cidReq'] : null;
  68. $surveyAnonymous = SurveyManager::get_survey($survey_id, 0, $courseCode);
  69. $surveyAnonymous = $surveyAnonymous['anonymous'];
  70. if ($surveyAnonymous == 0 && api_is_anonymous()) {
  71. api_not_allowed(true);
  72. } elseif ($surveyAnonymous == 0 && $userInvited == 0) {
  73. if (!api_is_allowed_to_edit()) {
  74. api_not_allowed(true);
  75. }
  76. }
  77. // Header
  78. Display :: display_header(get_lang('SurveyPreview'));
  79. // We exit here is the first or last question is a pagebreak (which causes errors)
  80. SurveyUtil::check_first_last_question($survey_id, false);
  81. $counter_question = 0;
  82. // Only a course admin is allowed to preview a survey: you are a course admin
  83. if (api_is_course_admin() ||
  84. (api_is_course_admin() && $_GET['isStudentView'] == 'true') ||
  85. api_is_allowed_to_session_edit(false, true)
  86. ) {
  87. // Survey information
  88. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  89. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  90. // Displaying the survey introduction
  91. if (!isset($_GET['show'])) {
  92. if (!empty($survey_data['survey_introduction'])) {
  93. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  94. }
  95. $limit = 0;
  96. }
  97. // Displaying the survey thanks message
  98. if (isset($_POST['finish_survey'])) {
  99. echo Display::return_message(get_lang('SurveyFinished'), 'confirm');
  100. echo $survey_data['survey_thanks'];
  101. Display :: display_footer();
  102. exit;
  103. }
  104. $questions = array();
  105. if (isset($_GET['show'])) {
  106. // Getting all the questions for this page and add them to a
  107. // 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
  143. survey_question.question_id = survey_question_option.question_id AND
  144. survey_question_option.c_id = $course_id
  145. WHERE
  146. survey_question.survey_id = '".intval($survey_id)."' AND
  147. survey_question.question_id IN (".Database::escape_string(implode(',', $paged_questions[$_GET['show']]), null, false).") AND
  148. survey_question.c_id = $course_id
  149. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  150. $result = Database::query($sql);
  151. $question_counter_max = Database::num_rows($result);
  152. $limit = 0;
  153. while ($row = Database::fetch_array($result)) {
  154. // If the type is not a pagebreak we store it in the $questions array
  155. if ($row['type'] != 'pagebreak') {
  156. $questions[$row['sort']]['question_id'] = $row['question_id'];
  157. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  158. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  159. $questions[$row['sort']]['display'] = $row['display'];
  160. $questions[$row['sort']]['type'] = $row['type'];
  161. $questions[$row['sort']]['options'][intval($row['option_sort'])] = $row['option_text'];
  162. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  163. } else {
  164. // If the type is a pagebreak we are finished loading the questions for this page
  165. break;
  166. }
  167. $counter_question++;
  168. }
  169. }
  170. }
  171. // Selecting the maximum number of pages
  172. $sql = "SELECT * FROM $table_survey_question
  173. WHERE
  174. c_id = $course_id AND
  175. type='".Database::escape_string('pagebreak')."' AND
  176. survey_id='".intval($survey_id)."'";
  177. $result = Database::query($sql);
  178. $numberofpages = Database::num_rows($result) + 1;
  179. // Displaying the form with the questions
  180. if (isset($_GET['show'])) {
  181. $show = (int) $_GET['show'] + 1;
  182. } else {
  183. $show = 0;
  184. }
  185. $url = api_get_self().'?survey_id='.Security::remove_XSS($survey_id).'&show='.$show;
  186. $form = new FormValidator('question', 'post', $url);
  187. if (is_array($questions) && count($questions) > 0) {
  188. foreach ($questions as $key => & $question) {
  189. $ch_type = 'ch_'.$question['type'];
  190. /** @var survey_question $display */
  191. $display = new $ch_type;
  192. $form->addHtml('<div class="survey_question_wrapper"><div class="survey_question">');
  193. $form->addHtml($question['survey_question']);
  194. $display->render($form, $question);
  195. $form->addHtml('</div></div>');
  196. }
  197. }
  198. if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0)) {
  199. if ($show == 0) {
  200. $form->addButton('next_survey_page', get_lang('StartSurvey'), 'arrow-right', 'success', 'large');
  201. } else {
  202. $form->addButton('next_survey_page', get_lang('NextQuestion'), 'arrow-right');
  203. }
  204. }
  205. if ($show >= $numberofpages && $_GET['show'] || (isset($_GET['show']) && count($questions) == 0)) {
  206. if ($questions_exists == false) {
  207. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  208. }
  209. $form->addButton('finish_survey', get_lang('FinishSurvey'), 'arrow-right');
  210. }
  211. $form->display();
  212. } else {
  213. echo Display::return_message(get_lang('NotAllowed'), 'error', false);
  214. }
  215. Display :: display_footer();