preview.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. $courseInfo = $course_id ? api_get_course_info_by_id($course_id) : [];
  21. $userId = api_get_user_id();
  22. $surveyId = intval($_GET['survey_id']);
  23. $userInvited = 0;
  24. $userAnonymous = 0;
  25. //query to ask if logged user is allowed to see the preview (if he is invited of he is a teacher)
  26. $sql = "SELECT survey_invitation.user
  27. FROM $table_survey_invitation survey_invitation
  28. LEFT JOIN $table_survey survey
  29. ON survey_invitation.survey_code = survey.code
  30. WHERE
  31. survey_invitation.c_id = $course_id AND
  32. survey.survey_id = $surveyId AND
  33. survey_invitation.user = $userId";
  34. $result = Database::query($sql);
  35. if (Database::num_rows($result) > 0) {
  36. $userInvited = 1;
  37. }
  38. // We exit here if there is no valid $_GET parameter
  39. if (!isset($_GET['survey_id']) || !is_numeric($_GET['survey_id'])){
  40. Display::display_header(get_lang('SurveyPreview'));
  41. Display::display_error_message(get_lang('InvallidSurvey'), false);
  42. Display::display_footer();
  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. Display::display_header(get_lang('SurveyPreview'));
  50. Display::display_error_message(get_lang('InvallidSurvey'), false);
  51. Display::display_footer();
  52. exit;
  53. }
  54. $urlname = strip_tags($survey_data['title']);
  55. if (api_is_allowed_to_edit()) {
  56. // Breadcrumbs
  57. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php', 'name' => get_lang('SurveyList'));
  58. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id, 'name' => $urlname);
  59. }
  60. $courseCode = isset($_GET['cidReq']) ? $_GET['cidReq'] : null;
  61. $surveyAnonymous = SurveyManager::get_survey($survey_id, 0, $courseCode);
  62. $surveyAnonymous = $surveyAnonymous['anonymous'];
  63. if ($surveyAnonymous == 0 && api_is_anonymous()) {
  64. api_not_allowed(true);
  65. } elseif ($surveyAnonymous == 0 && $userInvited == 0) {
  66. if (!api_is_allowed_to_edit()) {
  67. api_not_allowed(true);
  68. }
  69. }
  70. // Header
  71. Display :: display_header(get_lang('SurveyPreview'));
  72. // We exit here is the first or last question is a pagebreak (which causes errors)
  73. SurveyUtil::check_first_last_question($survey_id, false);
  74. // Only a course admin is allowed to preview a survey: you are NOT a course admin => error message
  75. /*
  76. if (!api_is_allowed_to_edit(false, true)) {
  77. Display :: display_error_message(get_lang('NotAllowed'), false);
  78. }*/
  79. $counter_question = 0;
  80. // Only a course admin is allowed to preview a survey: you are a course admin
  81. if (api_is_course_admin() ||
  82. (api_is_course_admin() && $_GET['isStudentView'] == 'true') ||
  83. api_is_allowed_to_session_edit(false, true)
  84. ) {
  85. // Survey information
  86. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  87. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  88. // Displaying the survey introduction
  89. if (!isset($_GET['show'])) {
  90. if (!empty($survey_data['survey_introduction'])) {
  91. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  92. }
  93. $limit = 0;
  94. }
  95. // Displaying the survey thanks message
  96. if (isset($_POST['finish_survey'])) {
  97. Display::display_confirmation_message(get_lang('SurveyFinished'));
  98. echo $survey_data['survey_thanks'];
  99. Display :: display_footer();
  100. exit;
  101. }
  102. $questions = array();
  103. if (isset($_GET['show'])) {
  104. // Getting all the questions for this page and add them to a
  105. // multidimensional array where the first index is the page.
  106. // as long as there is no pagebreak fount we keep adding questions to the page
  107. $questions_displayed = array();
  108. $paged_questions = array();
  109. $counter = 0;
  110. $sql = "SELECT * FROM $table_survey_question
  111. WHERE c_id = $course_id AND survey_id = '".intval($survey_id)."'
  112. ORDER BY sort ASC";
  113. $result = Database::query($sql);
  114. $questions_exists = true;
  115. if (Database::num_rows($result)) {
  116. while ($row = Database::fetch_array($result)) {
  117. if ($row['type'] == 'pagebreak') {
  118. $counter++;
  119. } else {
  120. $paged_questions[$counter][] = $row['question_id'];
  121. }
  122. }
  123. } else {
  124. $questions_exists = false;
  125. }
  126. if (array_key_exists($_GET['show'], $paged_questions)) {
  127. $sql = "SELECT
  128. survey_question.question_id,
  129. survey_question.survey_id,
  130. survey_question.survey_question,
  131. survey_question.display,
  132. survey_question.sort,
  133. survey_question.type,
  134. survey_question.max_value,
  135. survey_question_option.question_option_id,
  136. survey_question_option.option_text,
  137. survey_question_option.sort as option_sort
  138. FROM $table_survey_question survey_question
  139. LEFT JOIN $table_survey_question_option survey_question_option
  140. ON
  141. survey_question.question_id = survey_question_option.question_id AND
  142. survey_question_option.c_id = $course_id
  143. WHERE
  144. survey_question.survey_id = '".$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. } else {
  162. // If the type is a pagebreak we are finished loading the questions for this page
  163. break;
  164. }
  165. $counter_question++;
  166. }
  167. }
  168. }
  169. // Selecting the maximum number of pages
  170. $sql = "SELECT * FROM $table_survey_question
  171. WHERE
  172. c_id = $course_id AND
  173. type='".Database::escape_string('pagebreak')."' AND
  174. survey_id='".$survey_id."'";
  175. $result = Database::query($sql);
  176. $numberofpages = Database::num_rows($result) + 1;
  177. // Displaying the form with the questions
  178. if (isset($_GET['show'])) {
  179. $show = (int) $_GET['show'] + 1;
  180. } else {
  181. $show = 0;
  182. }
  183. $url = api_get_self().'?survey_id='.$survey_id.'&show='.$show;
  184. $form = new FormValidator('question-survey', 'post', $url, null, null, FormValidator::LAYOUT_INLINE);
  185. if (is_array($questions) && count($questions) > 0) {
  186. foreach ($questions as $key => & $question) {
  187. $ch_type = 'ch_'.$question['type'];
  188. /** @var survey_question $display */
  189. $display = new $ch_type;
  190. $form->addHtml('<div class="survey_question_wrapper"><div class="survey_question">');
  191. $form->addHtml($question['survey_question']);
  192. $display->render($form, $question);
  193. $form->addHtml('</div></div>');
  194. }
  195. }
  196. if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0)) {
  197. if ($show == 0) {
  198. $form->addButton('next_survey_page', get_lang('StartSurvey'), 'arrow-right', 'success', 'large');
  199. } else {
  200. $form->addButton('next_survey_page', get_lang('NextQuestion'), 'arrow-right');
  201. }
  202. }
  203. if ($show >= $numberofpages && $_GET['show'] || (isset($_GET['show']) && count($questions) == 0)) {
  204. if ($questions_exists == false) {
  205. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  206. }
  207. $form->addButton('finish_survey', get_lang('FinishSurvey'), 'arrow-right', 'success');
  208. }
  209. $form->display();
  210. if ($courseInfo) {
  211. echo '<br><p>'.Display::toolbarButton(
  212. get_lang('ReturnToCourseHomepage'),
  213. api_get_course_url($courseInfo['code']),
  214. 'home'
  215. ).'</p>';
  216. }
  217. } else {
  218. Display :: display_error_message(get_lang('NotAllowed'), false);
  219. }
  220. // Footer
  221. Display :: display_footer();