preview.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. *
  6. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  7. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting code
  8. * @author Julio Montoya <gugli100@gmail.com>, Chamilo: Personality Test modifications
  9. *
  10. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  11. */
  12. require_once __DIR__.'/../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. $courseInfo = $course_id ? api_get_course_info_by_id($course_id) : [];
  23. $userId = api_get_user_id();
  24. $surveyId = (int) $_GET['survey_id'];
  25. $userInvited = 0;
  26. $userAnonymous = 0;
  27. $allowRequiredSurveyQuestions = api_get_configuration_value('allow_required_survey_questions');
  28. //query to ask if logged user is allowed to see the preview (if he is invited of he is a teacher)
  29. $sql = "SELECT survey_invitation.user
  30. FROM $table_survey_invitation survey_invitation
  31. LEFT JOIN $table_survey survey
  32. ON survey_invitation.survey_code = survey.code
  33. WHERE
  34. survey_invitation.c_id = $course_id AND
  35. survey.survey_id = $surveyId AND
  36. survey_invitation.user = $userId";
  37. $result = Database::query($sql);
  38. if (Database::num_rows($result) > 0) {
  39. $userInvited = 1;
  40. }
  41. // We exit here if there is no valid $_GET parameter
  42. if (!isset($_GET['survey_id']) || !is_numeric($_GET['survey_id'])) {
  43. api_not_allowed(
  44. true,
  45. Display::return_message(get_lang('InvallidSurvey'), 'error', false)
  46. );
  47. }
  48. // Getting the survey information
  49. $survey_data = SurveyManager::get_survey($surveyId);
  50. if (empty($survey_data)) {
  51. api_not_allowed(
  52. true,
  53. Display::return_message(get_lang('InvallidSurvey'), 'error', false)
  54. );
  55. }
  56. $urlname = strip_tags($survey_data['title']);
  57. if (api_is_allowed_to_edit()) {
  58. // Breadcrumbs
  59. $interbreadcrumb[] = [
  60. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  61. 'name' => get_lang('SurveyList'),
  62. ];
  63. $interbreadcrumb[] = [
  64. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$surveyId.'&'.api_get_cidreq(),
  65. 'name' => $urlname,
  66. ];
  67. }
  68. $courseCode = isset($_GET['cidReq']) ? $_GET['cidReq'] : null;
  69. $surveyAnonymous = SurveyManager::get_survey($surveyId, 0, $courseCode);
  70. $surveyAnonymous = $surveyAnonymous['anonymous'];
  71. if ($surveyAnonymous == 0 && api_is_anonymous()) {
  72. api_not_allowed(true);
  73. } elseif ($surveyAnonymous == 0 && $userInvited == 0) {
  74. if (!api_is_allowed_to_edit()) {
  75. api_not_allowed(true);
  76. }
  77. }
  78. $show = 0;
  79. Display::display_header(get_lang('SurveyPreview'));
  80. // We exit here is the first or last question is a pagebreak (which causes errors)
  81. SurveyUtil::check_first_last_question($surveyId, false);
  82. $counter_question = 0;
  83. // Only a course admin is allowed to preview a survey: you are a course admin
  84. if (api_is_course_admin() ||
  85. (api_is_course_admin() && $_GET['isStudentView'] == 'true') ||
  86. api_is_allowed_to_session_edit(false, true)
  87. ) {
  88. // Survey information
  89. echo '<div class="page-header"><h2>'.$survey_data['survey_title'].'</h2></div>';
  90. if (!empty($survey_data['survey_subtitle'])) {
  91. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  92. }
  93. // Displaying the survey introduction
  94. if (!isset($_GET['show'])) {
  95. if (!empty($survey_data['survey_introduction'])) {
  96. echo '<div class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  97. }
  98. }
  99. // Displaying the survey thanks message
  100. if (isset($_POST['finish_survey'])) {
  101. echo Display::return_message(get_lang('SurveyFinished'), 'confirm');
  102. echo $survey_data['survey_thanks'];
  103. Display::display_footer();
  104. exit;
  105. }
  106. $questions = [];
  107. if (isset($_GET['show'])) {
  108. // Getting all the questions for this page and add them to a
  109. // multidimensional array where the first index is the page.
  110. // as long as there is no pagebreak fount we keep adding questions to the page
  111. $questions_displayed = [];
  112. $paged_questions = [];
  113. $counter = 0;
  114. $sql = "SELECT * FROM $table_survey_question
  115. WHERE
  116. survey_question NOT LIKE '%{{%' AND
  117. c_id = $course_id AND
  118. survey_id = '".$surveyId."'
  119. ORDER BY sort ASC";
  120. $result = Database::query($sql);
  121. $questions_exists = true;
  122. if (Database::num_rows($result)) {
  123. while ($row = Database::fetch_array($result)) {
  124. if ($survey_data['one_question_per_page'] == 1) {
  125. if ($row['type'] != 'pagebreak') {
  126. $paged_questions[$counter][] = $row['question_id'];
  127. $counter++;
  128. continue;
  129. }
  130. } else {
  131. if ($row['type'] == 'pagebreak') {
  132. $counter++;
  133. } else {
  134. $paged_questions[$counter][] = $row['question_id'];
  135. }
  136. }
  137. }
  138. } else {
  139. $questions_exists = false;
  140. }
  141. if (array_key_exists($_GET['show'], $paged_questions)) {
  142. $sql = "SELECT
  143. survey_question.question_id,
  144. survey_question.survey_id,
  145. survey_question.survey_question,
  146. survey_question.display,
  147. survey_question.sort,
  148. survey_question.type,
  149. survey_question.max_value,
  150. survey_question_option.question_option_id,
  151. survey_question_option.option_text,
  152. survey_question_option.sort as option_sort
  153. ".($allowRequiredSurveyQuestions ? ', survey_question.is_required' : '')."
  154. FROM $table_survey_question survey_question
  155. LEFT JOIN $table_survey_question_option survey_question_option
  156. ON
  157. survey_question.question_id = survey_question_option.question_id AND
  158. survey_question_option.c_id = $course_id
  159. WHERE
  160. survey_question.survey_id = '".$surveyId."' AND
  161. survey_question.question_id IN (".Database::escape_string(implode(',', $paged_questions[$_GET['show']]), null, false).") AND
  162. survey_question.c_id = $course_id AND
  163. survey_question NOT LIKE '%{{%'
  164. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  165. $result = Database::query($sql);
  166. while ($row = Database::fetch_array($result)) {
  167. // If the type is not a pagebreak we store it in the $questions array
  168. if ($row['type'] != 'pagebreak') {
  169. $sort = $row['sort'];
  170. $questions[$sort]['question_id'] = $row['question_id'];
  171. $questions[$sort]['survey_id'] = $row['survey_id'];
  172. $questions[$sort]['survey_question'] = $row['survey_question'];
  173. $questions[$sort]['display'] = $row['display'];
  174. $questions[$sort]['type'] = $row['type'];
  175. //$questions[$sort]['options'][intval($row['option_sort'])] = $row['option_text'];
  176. $questions[$sort]['options'][$row['question_option_id']] = $row['option_text'];
  177. $questions[$sort]['maximum_score'] = $row['max_value'];
  178. $questions[$row['sort']]['is_required'] = $allowRequiredSurveyQuestions && $row['is_required'];
  179. } else {
  180. // If the type is a pagebreak we are finished loading the questions for this page
  181. //break;
  182. }
  183. $counter_question++;
  184. }
  185. }
  186. }
  187. $before = 0;
  188. if (isset($_GET['show']) && isset($paged_questions[$_GET['show'] - 1])) {
  189. $before = count($paged_questions[$_GET['show'] - 1]);
  190. }
  191. $numberOfPages = SurveyManager::getCountPages($survey_data);
  192. // Displaying the form with the questions
  193. if (isset($_GET['show'])) {
  194. $show = (int) $_GET['show'] + 1;
  195. }
  196. $originalShow = isset($_GET['show']) ? (int) $_GET['show'] : 0;
  197. $url = api_get_self().'?survey_id='.$surveyId.'&show='.$show.'&'.api_get_cidreq();
  198. $form = new FormValidator(
  199. 'question-survey',
  200. 'post',
  201. $url,
  202. null,
  203. null,
  204. FormValidator::LAYOUT_INLINE
  205. );
  206. if (is_array($questions) && count($questions) > 0) {
  207. $counter = 1;
  208. if (!empty($originalShow)) {
  209. $before = 0;
  210. foreach ($paged_questions as $keyQuestion => $list) {
  211. if ($originalShow > $keyQuestion) {
  212. $before += count($list);
  213. }
  214. }
  215. $counter = $before + 1;
  216. }
  217. foreach ($questions as $key => &$question) {
  218. $ch_type = 'ch_'.$question['type'];
  219. /** @var survey_question $display */
  220. $display = new $ch_type();
  221. $form->addHtml('<div class="survey_question '.$ch_type.'">');
  222. $form->addHtml('<div style="float:left; font-weight: bold; margin-right: 5px;"> '.$counter.'. </div>');
  223. $form->addHtml('<div>'.Security::remove_XSS($question['survey_question']).'</div> ');
  224. $display->render($form, $question);
  225. $form->addHtml('</div>');
  226. $counter++;
  227. }
  228. }
  229. $form->addHtml('<div class="start-survey">');
  230. if ($show < $numberOfPages) {
  231. if ($show == 0) {
  232. $form->addButton(
  233. 'next_survey_page',
  234. get_lang('StartSurvey'),
  235. 'arrow-right',
  236. 'success'
  237. );
  238. } else {
  239. $form->addButton(
  240. 'next_survey_page',
  241. get_lang('NextQuestion'),
  242. 'arrow-right',
  243. 'success'
  244. );
  245. }
  246. }
  247. if ($show >= $numberOfPages && isset($_GET['show']) ||
  248. (isset($_GET['show']) && count($questions) == 0)
  249. ) {
  250. if ($questions_exists == false) {
  251. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  252. }
  253. $form->addButton(
  254. 'finish_survey',
  255. get_lang('FinishSurvey'),
  256. 'arrow-right',
  257. 'success'
  258. );
  259. }
  260. $form->addHtml('</div>');
  261. $form->display();
  262. if ($courseInfo) {
  263. echo Display::toolbarButton(
  264. get_lang('ReturnToCourseHomepage'),
  265. api_get_course_url($courseInfo['code']),
  266. 'home'
  267. );
  268. }
  269. } else {
  270. echo Display::return_message(get_lang('NotAllowed'), 'error', false);
  271. }
  272. Display::display_footer();