preview.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. Display::display_header(get_lang('SurveyPreview'));
  40. Display::display_error_message(get_lang('InvallidSurvey'), false);
  41. Display::display_footer();
  42. exit;
  43. }
  44. // Getting the survey information
  45. $survey_id = intval($_GET['survey_id']);
  46. $survey_data = SurveyManager::get_survey($survey_id);
  47. if (empty($survey_data)) {
  48. Display::display_header(get_lang('SurveyPreview'));
  49. Display::display_error_message(get_lang('InvallidSurvey'), false);
  50. Display::display_footer();
  51. exit;
  52. }
  53. $urlname = strip_tags($survey_data['title']);
  54. if (api_is_allowed_to_edit()) {
  55. // Breadcrumbs
  56. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(), 'name' => get_lang('SurveyList'));
  57. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(), 'name' => $urlname);
  58. }
  59. $courseCode = isset($_GET['cidReq']) ? $_GET['cidReq'] : null;
  60. $surveyAnonymous = SurveyManager::get_survey($survey_id, 0, $courseCode);
  61. $surveyAnonymous = $surveyAnonymous['anonymous'];
  62. if ($surveyAnonymous == 0 && api_is_anonymous()) {
  63. api_not_allowed(true);
  64. } elseif ($surveyAnonymous == 0 && $userInvited == 0) {
  65. if (!api_is_allowed_to_edit()) {
  66. api_not_allowed(true);
  67. }
  68. }
  69. // Header
  70. Display :: display_header(get_lang('SurveyPreview'));
  71. // We exit here is the first or last question is a pagebreak (which causes errors)
  72. SurveyUtil::check_first_last_question($survey_id, false);
  73. // Only a course admin is allowed to preview a survey: you are NOT a course admin => error message
  74. /*
  75. if (!api_is_allowed_to_edit(false, true)) {
  76. Display :: display_error_message(get_lang('NotAllowed'), false);
  77. }*/
  78. $counter_question = 0;
  79. // Only a course admin is allowed to preview a survey: you are a course admin
  80. if (api_is_course_admin() ||
  81. (api_is_course_admin() && $_GET['isStudentView'] == 'true') ||
  82. api_is_allowed_to_session_edit(false, true)
  83. ) {
  84. // Survey information
  85. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  86. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  87. // Displaying the survey introduction
  88. if (!isset($_GET['show'])) {
  89. if (!empty($survey_data['survey_introduction'])) {
  90. echo '<div id="survey_content" class="survey_content">'.
  91. $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 = '".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. } 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='".intval($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='.Security::remove_XSS($survey_id).'&show='.$show.'&'.api_get_cidreq();
  184. $form = new FormValidator('question', 'post', $url);
  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');
  208. }
  209. $form->display();
  210. } else {
  211. Display :: display_error_message(get_lang('NotAllowed'), false);
  212. }