fillsurvey.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. * @todo performance could be improved if not the survey_id was stored with the invitation but the survey_code
  26. */
  27. // name of the language file that needs to be included
  28. $language_file = 'survey';
  29. // unsetting the course id (because it is in the URL)
  30. if (!isset($_GET['cidReq']))
  31. {
  32. $cidReset = true;
  33. }
  34. else
  35. {
  36. $_cid = $_GET['cidReq'];
  37. }
  38. // including the global dokeos file
  39. require ('../inc/global.inc.php');
  40. // including additional libraries
  41. //require_once (api_get_path(LIBRARY_PATH)."survey.lib.php");
  42. require_once('survey.lib.php');
  43. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  44. // getting all the course information
  45. $_course = CourseManager::get_course_information($_GET['course']);
  46. // Database table definitions
  47. $table_survey = Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
  48. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION, $_course['db_name']);
  49. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION, $_course['db_name']);
  50. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  51. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  52. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']);
  53. // breadcrumbs
  54. if (!empty($_user))
  55. {
  56. $interbreadcrumb[] = array ("url" => 'survey_list.php', 'name' => get_lang('SurveyList'));
  57. }
  58. // Header
  59. Display :: display_header(get_lang('Survey'));
  60. // first we check if the needed parameters are present
  61. if (!isset($_GET['course']) OR !isset($_GET['invitationcode']))
  62. {
  63. Display :: display_error_message(get_lang('SurveyParametersMissingUseCopyPaste'), false);
  64. Display :: display_footer();
  65. exit;
  66. }
  67. // now we check if the invitationcode is valid
  68. $sql = "SELECT * FROM $table_survey_invitation WHERE invitation_code = '".Database::escape_string($_GET['invitationcode'])."'";
  69. $result = api_sql_query($sql, __FILE__, __LINE__);
  70. if (mysql_num_rows($result) < 1)
  71. {
  72. Display :: display_error_message(get_lang('WrongInvitationCode'), false);
  73. Display :: display_footer();
  74. exit;
  75. }
  76. $survey_invitation = mysql_fetch_assoc($result);
  77. // now we check if the user already filled the survey
  78. if ($survey_invitation['answered'] == 1)
  79. {
  80. Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey'), false);
  81. Display :: display_footer();
  82. exit;
  83. }
  84. // checking if there is another survey with this code.
  85. // If this is the case there will be a language choice
  86. $sql = "SELECT * FROM $table_survey WHERE code='".Database::escape_string($survey_invitation['survey_code'])."'";
  87. $result = api_sql_query($sql, __FILE__, __LINE__);
  88. if (mysql_num_rows($result) > 1)
  89. {
  90. if ($_POST['language'])
  91. {
  92. $survey_invitation['survey_id'] = $_POST['language'];
  93. }
  94. else
  95. {
  96. echo '<form id="language" name="language" method="POST" action="'.api_get_self().'?course='.$_GET['course'].'&invitationcode='.$_GET['invitationcode'].'&cidReq='.$_GET['cidReq'].'">';
  97. echo ' <select name="language">';
  98. while ($row=mysql_fetch_assoc($result))
  99. {
  100. echo '<option value="'.$row['survey_id'].'">'.$row['lang'].'</option>';
  101. }
  102. echo '</select>';
  103. echo ' <input type="submit" name="Submit" value="'.get_lang('Ok').'" />';
  104. echo '</form>';
  105. display::display_footer();
  106. exit;
  107. }
  108. }
  109. else
  110. {
  111. $row=mysql_fetch_assoc($result);
  112. $survey_invitation['survey_id'] = $row['survey_id'];
  113. }
  114. // getting the survey information
  115. $survey_data = survey_manager::get_survey($survey_invitation['survey_id']);
  116. $survey_data['survey_id'] = $survey_invitation['survey_id'];
  117. // storing the answers
  118. if ($_POST)
  119. {
  120. // getting all the types of the question (because of the special treatment of the score question type
  121. $sql = "SELECT * FROM $table_survey_question WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'";
  122. $result = api_sql_query($sql, __FILE__, __LINE__);
  123. while ($row = mysql_fetch_assoc($result))
  124. {
  125. $types[$row['question_id']] = $row['type'];
  126. }
  127. // looping through all the post values
  128. foreach ($_POST as $key=>$value)
  129. {
  130. // if the post value key contains the string 'question' then it is an answer on a question
  131. if (strstr($key,'question'))
  132. {
  133. // finding the question id by removing 'question'
  134. $survey_question_id = str_replace('question', '',$key);
  135. // if the post value is an array then we have a multiple response question or a scoring question type
  136. // remark: when it is a multiple response then the value of the array is the option_id
  137. // when it is a scoring question then the key of the array is the option_id and the value is the value
  138. if (is_array($value))
  139. {
  140. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
  141. foreach ($value as $answer_key => $answer_value)
  142. {
  143. if ($types[$survey_question_id] == 'score')
  144. {
  145. $option_id = $answer_key;
  146. $option_value = $answer_value;
  147. }
  148. else
  149. {
  150. $option_id = $answer_value;
  151. $option_value = '';
  152. }
  153. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $option_id, $option_value, $survey_data);
  154. }
  155. }
  156. // all the other question types (open question, multiple choice, percentage, ...)
  157. else
  158. {
  159. if ($types[$survey_question_id] == 'percentage')
  160. {
  161. $sql = "SELECT * FROM $table_survey_question_option WHERE question_option_id='".Database::escape_string($value)."'";
  162. $result = api_sql_query($sql, __FILE__, __LINE__);
  163. $row = mysql_fetch_assoc($result);
  164. $option_value = $row['option_text'];
  165. }
  166. else
  167. {
  168. $option_value = 0;
  169. if($types[$survey_question_id] == 'open')
  170. {
  171. $option_value = $value;
  172. //$value = 0;
  173. }
  174. }
  175. $survey_question_answer = $value;
  176. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id);
  177. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
  178. //SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
  179. }
  180. }
  181. }
  182. }
  183. // displaying the survey title and subtitle (appears on every page)
  184. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  185. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  186. // checking time availability
  187. $start_date = mktime(0,0,0,substr($survey_data['start_date'],5,2),substr($survey_data['start_date'],8,2),substr($survey_data['start_date'],0,4));
  188. $end_date = mktime(0,0,0,substr($survey_data['end_date'],5,2),substr($survey_data['end_date'],8,2),substr($survey_data['end_date'],0,4));
  189. $cur_date = time();
  190. if($cur_date < $start_date)
  191. {
  192. Display :: display_warning_message(get_lang('SurveyNotAvailableYet'), false);
  193. Display :: display_footer();
  194. exit;
  195. }
  196. if($cur_date > $end_date)
  197. {
  198. Display :: display_warning_message(get_lang('SurveyNotAvailableAnymore'), false);
  199. Display :: display_footer();
  200. exit;
  201. }
  202. // displaying the survey introduction
  203. if (!isset($_GET['show']))
  204. {
  205. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  206. $limit = 0;
  207. }
  208. // displaying the survey thanks message
  209. if ($_POST['finish_survey'])
  210. {
  211. echo '<div id="survey_content" class="survey_content"><strong>'.get_lang('SurveyFinished').'</strong> <br />'.$survey_data['survey_thanks'].'</div>';
  212. survey_manager::update_survey_answered($survey_data['survey_id'], $survey_invitation['user'], $survey_invitation['survey_code']);
  213. Display :: display_footer();
  214. exit;
  215. }
  216. if (isset($_GET['show']))
  217. {
  218. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  219. // as long as there is no pagebreak fount we keep adding questions to the page
  220. $questions_displayed = array();
  221. $counter = 0;
  222. $sql = "SELECT * FROM $table_survey_question
  223. WHERE survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  224. ORDER BY sort ASC";
  225. $result = api_sql_query($sql, __FILE__, __LINE__);
  226. while ($row = mysql_fetch_assoc($result))
  227. {
  228. if($row['type'] == 'pagebreak')
  229. {
  230. $counter++;
  231. }
  232. else
  233. {
  234. $paged_questions[$counter][] = $row['question_id'];
  235. }
  236. }
  237. if (key_exists($_GET['show'],$paged_questions))
  238. {
  239. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type, survey_question.max_value,
  240. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  241. FROM $table_survey_question survey_question
  242. LEFT JOIN $table_survey_question_option survey_question_option
  243. ON survey_question.question_id = survey_question_option.question_id
  244. WHERE survey_question.survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  245. AND survey_question.question_id IN (".implode(',',$paged_questions[$_GET['show']]).")
  246. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  247. $result = api_sql_query($sql, __FILE__, __LINE__);
  248. $question_counter_max = mysql_num_rows($result);
  249. $counter = 0;
  250. $limit=0;
  251. $questions = array();
  252. while ($row = mysql_fetch_assoc($result))
  253. {
  254. // if the type is not a pagebreak we store it in the $questions array
  255. if($row['type'] <> 'pagebreak')
  256. {
  257. $questions[$row['sort']]['question_id'] = $row['question_id'];
  258. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  259. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  260. $questions[$row['sort']]['display'] = $row['display'];
  261. $questions[$row['sort']]['type'] = $row['type'];
  262. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  263. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  264. }
  265. // if the type is a pagebreak we are finished loading the questions for this page
  266. else
  267. {
  268. break;
  269. }
  270. $counter++;
  271. }
  272. }
  273. }
  274. // selecting the maximum number of pages
  275. $sql = "SELECT * FROM $table_survey_question WHERE type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_invitation['survey_id'])."'";
  276. $result = api_sql_query($sql, __FILE__, __LINE__);
  277. $numberofpages = mysql_num_rows($result) + 1;
  278. // Displaying the form with the questions
  279. if (isset($_GET['show']))
  280. {
  281. $show = (int)$_GET['show'] + 1;
  282. }
  283. else
  284. {
  285. $show = 0;
  286. }
  287. // Displaying the form with the questions
  288. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?course='.$_GET['course'].'&invitationcode='.$_GET['invitationcode'].'&show='.$show.'&cidReq='.$_GET['cidReq'].'">';
  289. echo '<input type="hidden" name="language" value="'.$_POST['language'].'" />';
  290. if(is_array($questions)){
  291. foreach ($questions as $key=>$question)
  292. {
  293. $display = new $question['type'];
  294. $display->render_question($question);
  295. }
  296. }
  297. if (($show < $numberofpages) OR !$_GET['show'])
  298. {
  299. //echo '<a href="'.api_get_self().'?survey_id='.$survey_invitation['survey_id'].'&amp;show='.$limit.'">NEXT</a>';
  300. echo '<input type="submit" name="next_survey_page" value="'.get_lang('Next').' >> " />';
  301. }
  302. if ($show >= $numberofpages AND $_GET['show'])
  303. {
  304. echo '<input type="submit" name="finish_survey" value="'.get_lang('FinishSurvey').' >> " />';
  305. }
  306. echo '</form>';
  307. // Footer
  308. Display :: display_footer();
  309. ?>