fillsurvey.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue des Palais 44 Paleizenstraat
  13. B-1030 Brussels - Belgium
  14. Tel. +32 (2) 211 34 56
  15. */
  16. /**
  17. * @package dokeos.survey
  18. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  19. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  20. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  21. *
  22. * @todo use quickforms for the forms
  23. * @todo check if the user already filled the survey and if this is the case then the answers have to be updated and not stored again.
  24. * alterantively we could not allow people from filling the survey twice.
  25. */
  26. // name of the language file that needs to be included
  27. $language_file = 'survey';
  28. // including the global dokeos file
  29. require ('../inc/global.inc.php');
  30. // including additional libraries
  31. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  32. require_once('survey.lib.php');
  33. require_once (api_get_path(LIBRARY_PATH)."/course.lib.php");
  34. // getting all the course information
  35. $_course = CourseManager::get_course_information($_GET['course']);
  36. // Database table definitions
  37. $table_survey = Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
  38. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION, $_course['db_name']);
  39. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION, $_course['db_name']);
  40. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  41. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  42. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']);
  43. // breadcrumbs
  44. // $interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
  45. // Header
  46. Display :: display_header(get_lang('Survey'));
  47. // debug
  48. /*
  49. echo '<pre>';
  50. print_r($_POST);
  51. echo '</pre>';
  52. */
  53. // first we check if the needed parameters are present
  54. if (!isset($_GET['course']) OR !isset($_GET['invitationcode']))
  55. {
  56. Display :: display_error_message(get_lang('SurveyParametersMissingUseCopyPaste'));
  57. Display :: display_footer();
  58. exit;
  59. }
  60. // now we check if the invitationcode is valid
  61. $sql = "SELECT * FROM $table_survey_invitation WHERE invitation_code = '".mysql_real_escape_string($_GET['invitationcode'])."'";
  62. $result = api_sql_query($sql, __FILE__, __LINE__);
  63. if (mysql_num_rows($result) < 1)
  64. {
  65. Display :: display_error_message(get_lang('WrongInvitationCode'));
  66. Display :: display_footer();
  67. exit;
  68. }
  69. $survey_invitation = mysql_fetch_assoc($result);
  70. // storing the answers
  71. if ($_POST)
  72. {
  73. foreach ($_POST as $key=>$value)
  74. {
  75. if (strstr($key,'question'))
  76. {
  77. $survey_question_id = str_replace('question', '',$key);
  78. if (is_array($value))
  79. {
  80. foreach ($value as $answer_key => $answer_value)
  81. {
  82. store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $answer_value);
  83. }
  84. }
  85. else // multipleresponse
  86. {
  87. $survey_question_answer = $value;
  88. store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value);
  89. }
  90. }
  91. }
  92. }
  93. // survey information
  94. $survey_data = survey_manager::get_survey($survey_invitation['survey_id']);
  95. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  96. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  97. // displaying the survey introduction
  98. if (!isset($_GET['show']))
  99. {
  100. echo '<div id="survey_content">'.$survey_data['survey_introduction'].'</div>';
  101. $limit = 0;
  102. }
  103. // displaying the survey thanks message
  104. if ($_POST['finish_survey'])
  105. {
  106. echo '<div id="survey_content"><strong>'.get_lang('SurveyFinished').'</strong>'.$survey_data['survey_thanks'].'</div>';
  107. survey_manager::update_survey_answered($survey_invitation['survey_id']);
  108. Display :: display_footer();
  109. exit;
  110. }
  111. if (isset($_GET['show']))
  112. {
  113. // Getting all the questions for this page
  114. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  115. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  116. FROM $table_survey_question survey_question
  117. LEFT JOIN $table_survey_question_option survey_question_option
  118. ON survey_question.question_id = survey_question_option.question_id
  119. WHERE survey_question.survey_id = '".mysql_real_escape_string($survey_invitation['survey_id'])."'
  120. ORDER BY survey_question.sort ASC";
  121. if ($_GET['show'])
  122. {
  123. $sql .= ' LIMIT '.($_GET['show']+1).',1000';
  124. }
  125. $result = api_sql_query($sql, __FILE__, __LINE__);
  126. $question_counter_max = mysql_num_rows($result);
  127. $counter = 0;
  128. while ($row = mysql_fetch_assoc($result))
  129. {
  130. // if the type is not a pagebreak we store it in the $questions array
  131. if($row['type'] <> 'pagebreak')
  132. {
  133. $questions[$row['sort']]['question_id'] = $row['question_id'];
  134. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  135. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  136. $questions[$row['sort']]['display'] = $row['display'];
  137. $questions[$row['sort']]['type'] = $row['type'];
  138. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  139. }
  140. // if the type is a pagebreak we are finished loading the questions for this page
  141. else
  142. {
  143. $limit = $counter;
  144. break;
  145. }
  146. $counter++;
  147. }
  148. }
  149. // Displaying the form with the questions
  150. echo '<form id="question" name="question" method="post" action="'.$_SERVER['PHP_SELF'].'?course='.$_GET['course'].'&invitationcode='.$_GET['invitationcode'].'&show='.$limit.'">';
  151. foreach ($questions as $key=>$question)
  152. {
  153. $display = new $question['type'];
  154. $display->render_question($question);
  155. }
  156. if (($limit AND $limit <> $question_counter_max) OR !$_GET['show'])
  157. {
  158. //echo '<a href="'.$_SERVER['PHP_SELF'].'?survey_id='.$survey_invitation['survey_id'].'&amp;show='.$limit.'">NEXT</a>';
  159. echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
  160. }
  161. if (!$limit AND $_GET['show'])
  162. {
  163. echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
  164. }
  165. echo '</form>';
  166. /*
  167. echo '<pre>';
  168. print_r($questions);
  169. echo '</pre>';
  170. */
  171. // Footer
  172. Display :: display_footer();
  173. /**
  174. * This function stores an answer on a survey
  175. *
  176. * @param mixed $user the user id or email of the person who fills the survey
  177. * @param integer $survey_id the survey id
  178. * @param integer $question_id the question id
  179. * @param integer $option_id the option id
  180. *
  181. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  182. * @version January 2007
  183. */
  184. function store_answer($user, $survey_id, $question_id, $option_id)
  185. {
  186. global $_course;
  187. // table definition
  188. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  189. $sql = "INSERT INTO $table_survey_answer (user, survey_id, question_id, option_id) VALUES (
  190. '".mysql_real_escape_string($user)."',
  191. '".mysql_real_escape_string($survey_id)."',
  192. '".mysql_real_escape_string($question_id)."',
  193. '".mysql_real_escape_string($option_id)."'
  194. )";
  195. $result = api_sql_query($sql, __FILE__, __LINE__);
  196. }
  197. ?>