preview.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. $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. api_not_allowed(
  41. true,
  42. Display::return_message(get_lang('InvallidSurvey'), 'error', false)
  43. );
  44. exit;
  45. }
  46. // Getting the survey information
  47. $survey_id = intval($_GET['survey_id']);
  48. $survey_data = SurveyManager::get_survey($survey_id);
  49. if (empty($survey_data)) {
  50. api_not_allowed(
  51. true,
  52. Display::return_message(get_lang('InvallidSurvey'), 'error', false)
  53. );
  54. exit;
  55. }
  56. $urlname = strip_tags($survey_data['title']);
  57. if (api_is_allowed_to_edit()) {
  58. // Breadcrumbs
  59. $interbreadcrumb[] = array(
  60. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  61. 'name' => get_lang('SurveyList')
  62. );
  63. $interbreadcrumb[] = array(
  64. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  65. 'name' => $urlname,
  66. );
  67. }
  68. $courseCode = isset($_GET['cidReq']) ? $_GET['cidReq'] : null;
  69. $surveyAnonymous = SurveyManager::get_survey($survey_id, 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. // Header
  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($survey_id, 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. $limit = 0;
  99. }
  100. // Displaying the survey thanks message
  101. if (isset($_POST['finish_survey'])) {
  102. echo Display::return_message(get_lang('SurveyFinished'), 'confirm');
  103. echo $survey_data['survey_thanks'];
  104. Display :: display_footer();
  105. exit;
  106. }
  107. $questions = array();
  108. if (isset($_GET['show'])) {
  109. // Getting all the questions for this page and add them to a
  110. // multidimensional array where the first index is the page.
  111. // as long as there is no pagebreak fount we keep adding questions to the page
  112. $questions_displayed = array();
  113. $paged_questions = array();
  114. $counter = 0;
  115. $sql = "SELECT * FROM $table_survey_question
  116. WHERE c_id = $course_id AND survey_id = '".$survey_id."'
  117. ORDER BY sort ASC";
  118. $result = Database::query($sql);
  119. $questions_exists = true;
  120. if (Database::num_rows($result)) {
  121. while ($row = Database::fetch_array($result)) {
  122. if ($row['type'] == 'pagebreak') {
  123. $counter++;
  124. } else {
  125. $paged_questions[$counter][] = $row['question_id'];
  126. }
  127. }
  128. } else {
  129. $questions_exists = false;
  130. }
  131. if (array_key_exists($_GET['show'], $paged_questions)) {
  132. $sql = "SELECT
  133. survey_question.question_id,
  134. survey_question.survey_id,
  135. survey_question.survey_question,
  136. survey_question.display,
  137. survey_question.sort,
  138. survey_question.type,
  139. survey_question.max_value,
  140. survey_question_option.question_option_id,
  141. survey_question_option.option_text,
  142. survey_question_option.sort as option_sort
  143. FROM $table_survey_question survey_question
  144. LEFT JOIN $table_survey_question_option survey_question_option
  145. ON
  146. survey_question.question_id = survey_question_option.question_id AND
  147. survey_question_option.c_id = $course_id
  148. WHERE
  149. survey_question.survey_id = '".$survey_id."' AND
  150. survey_question.question_id IN (".Database::escape_string(implode(',', $paged_questions[$_GET['show']]), null, false).") AND
  151. survey_question.c_id = $course_id
  152. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  153. $result = Database::query($sql);
  154. $question_counter_max = Database::num_rows($result);
  155. $limit = 0;
  156. while ($row = Database::fetch_array($result)) {
  157. // If the type is not a pagebreak we store it in the $questions array
  158. if ($row['type'] != 'pagebreak') {
  159. $questions[$row['sort']]['question_id'] = $row['question_id'];
  160. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  161. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  162. $questions[$row['sort']]['display'] = $row['display'];
  163. $questions[$row['sort']]['type'] = $row['type'];
  164. $questions[$row['sort']]['options'][intval($row['option_sort'])] = $row['option_text'];
  165. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  166. } else {
  167. // If the type is a pagebreak we are finished loading the questions for this page
  168. break;
  169. }
  170. $counter_question++;
  171. }
  172. }
  173. }
  174. // Selecting the maximum number of pages
  175. $sql = "SELECT * FROM $table_survey_question
  176. WHERE
  177. c_id = $course_id AND
  178. type='".Database::escape_string('pagebreak')."' AND
  179. survey_id='".$survey_id."'";
  180. $result = Database::query($sql);
  181. $numberofpages = Database::num_rows($result) + 1;
  182. // Displaying the form with the questions
  183. if (isset($_GET['show'])) {
  184. $show = (int) $_GET['show'] + 1;
  185. } else {
  186. $show = 0;
  187. }
  188. $url = api_get_self().'?survey_id='.$survey_id.'&show='.$show;
  189. $form = new FormValidator(
  190. 'question-survey',
  191. 'post',
  192. $url,
  193. null,
  194. null,
  195. FormValidator::LAYOUT_INLINE
  196. );
  197. if (is_array($questions) && count($questions) > 0) {
  198. foreach ($questions as $key => & $question) {
  199. $ch_type = 'ch_'.$question['type'];
  200. /** @var survey_question $display */
  201. $display = new $ch_type;
  202. $form->addHtml('<div class="survey_question '.$ch_type.'">');
  203. $form->addHtml('<h5 class="title">'.$question['question_id'].'. '.strip_tags($question['survey_question']).'</h5>');
  204. $display->render($form, $question);
  205. $form->addHtml('</div>');
  206. }
  207. }
  208. $form->addHtml('<div class="start-survey">');
  209. if (($show < $numberofpages) || (!$_GET['show'] && count($questions) > 0)) {
  210. if ($show == 0) {
  211. $form->addButton(
  212. 'next_survey_page',
  213. get_lang('StartSurvey'),
  214. 'arrow-right',
  215. 'success'
  216. );
  217. } else {
  218. $form->addButton(
  219. 'next_survey_page',
  220. get_lang('NextQuestion'),
  221. 'arrow-right',
  222. 'success'
  223. );
  224. }
  225. }
  226. if ($show >= $numberofpages && $_GET['show'] ||
  227. (isset($_GET['show']) && count($questions) == 0)
  228. ) {
  229. if ($questions_exists == false) {
  230. echo '<p>'.get_lang('ThereAreNotQuestionsForthisSurvey').'</p>';
  231. }
  232. $form->addButton(
  233. 'finish_survey',
  234. get_lang('FinishSurvey'),
  235. 'arrow-right',
  236. 'success'
  237. );
  238. }
  239. $form->addHtml('</div>');
  240. $form->display();
  241. if ($courseInfo) {
  242. echo Display::toolbarButton(
  243. get_lang('ReturnToCourseHomepage'),
  244. api_get_course_url($courseInfo['code']),
  245. 'home'
  246. );
  247. }
  248. } else {
  249. echo Display::return_message(get_lang('NotAllowed'), 'error', false);
  250. }
  251. Display :: display_footer();