fillsurvey.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 modification and rewriting large parts of the code as well
  8. * @version $Id: survey_list.php 10680 2007-01-11 21:26:23Z pcool $
  9. *
  10. * @todo use quickforms for the forms
  11. * @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.
  12. * alterantively we could not allow people from filling the survey twice.
  13. * @todo performance could be improved if not the survey_id was stored with the invitation but the survey_code
  14. */
  15. // Language file that needs to be included
  16. $language_file = 'survey';
  17. // Unsetting the course id (because it is in the URL)
  18. if (!isset($_GET['cidReq'])) {
  19. $cidReset = true;
  20. } else {
  21. $_cid = $_GET['cidReq'];
  22. }
  23. // Including the global initialization file
  24. require_once '../inc/global.inc.php';
  25. // Including additional libraries
  26. require_once 'survey.lib.php';
  27. // Breadcrumbs
  28. if (!empty($_user)) {
  29. $interbreadcrumb[] = array('url' => 'survey_list.php?cidReq='.Security::remove_XSS($_GET['course']), 'name' => get_lang('SurveyList'));
  30. }
  31. if (($_GET['invitationcode'] == 'auto' && isset($_GET['scode'])) && ((!(isset($_user['user_id']) && $_user['user_id']) || api_is_anonymous($_user['user_id'], true)))) {
  32. // anonymous users not allowed
  33. api_not_allowed();
  34. }
  35. // getting all the course information
  36. if (isset($_GET['course'])) {
  37. $course_info = api_get_course_info($_GET['course']);
  38. } else {
  39. $course_info = api_get_course_info();
  40. }
  41. if (empty($course_info)) {
  42. api_not_allowed();
  43. }
  44. $course_id = $course_info['real_id'];
  45. // Header
  46. Display :: display_header(get_lang('ToolSurvey'));
  47. // Database table definitions
  48. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  49. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  50. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  51. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  52. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  53. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  54. // First we check if the needed parameters are present
  55. if ((!isset($_GET['course']) || !isset($_GET['invitationcode'])) && !isset($_GET['user_id'])) {
  56. Display :: display_error_message(get_lang('SurveyParametersMissingUseCopyPaste'), false);
  57. Display :: display_footer();
  58. exit;
  59. }
  60. $invitationcode = $_GET['invitationcode'];
  61. // Start auto-invitation feature FS#3403 (all-users-can-do-the-survey-URL handling)
  62. if ($invitationcode == 'auto' && isset($_GET['scode'])) {
  63. $userid = $_user['user_id'];
  64. $scode = Database::escape_string($_GET['scode']); // Survey_code of the survey
  65. $autoInvitationcode = "auto-$userid-$scode"; // New invitation code from userid
  66. // The survey code must exist in this course, or the URL is invalid
  67. $sql = "SELECT * FROM $table_survey WHERE c_id = $course_id AND code ='".$scode."'";
  68. $result = Database::query($sql);
  69. if (Database :: num_rows($result) > 0) { // Ok
  70. // Check availability
  71. $row = Database :: fetch_array($result, 'ASSOC');
  72. $tempdata = survey_manager :: get_survey($row['survey_id']);
  73. check_time_availability($tempdata); //exit if survey not available anymore
  74. // Check for double invitation records (insert should be done once)
  75. $sql = "SELECT user from $table_survey_invitation WHERE c_id = $course_id AND invitation_code = '".Database::escape_string($autoInvitationcode)."'";
  76. $result = Database::query($sql);
  77. if (Database :: num_rows($result) == 0) { // Ok
  78. $sql = "INSERT INTO $table_survey_invitation (c_id, survey_code,user, invitation_code, invitation_date) ";
  79. $sql .= " VALUES ($course_id, \"$scode\", \"$userid\", \"$autoInvitationcode\", now())";
  80. Database::query($sql);
  81. }
  82. // From here we use the new invitationcode auto-userid-surveycode string
  83. $_GET['invitationcode'] = $autoInvitationcode;
  84. $invitationcode = $autoInvitationcode;
  85. }
  86. }
  87. // Now we check if the invitationcode is valid
  88. $sql = "SELECT * FROM $table_survey_invitation WHERE c_id = $course_id AND invitation_code = '".Database :: escape_string($invitationcode)."'";
  89. $result = Database::query($sql); // false = suppress errors
  90. if (Database::num_rows($result) < 1) {
  91. Display :: display_error_message(get_lang('WrongInvitationCode'), false);
  92. Display :: display_footer();
  93. exit;
  94. }
  95. $survey_invitation = Database::fetch_array($result, 'ASSOC');
  96. // Now we check if the user already filled the survey
  97. if ($survey_invitation['answered'] == 1 && !isset($_GET['user_id'])) {
  98. Display :: display_error_message(get_lang('YouAlreadyFilledThisSurvey'), false);
  99. Display :: display_footer();
  100. exit;
  101. }
  102. // Checking if there is another survey with this code.
  103. // If this is the case there will be a language choice
  104. $sql = "SELECT * FROM $table_survey WHERE c_id = $course_id AND code='".Database::escape_string($survey_invitation['survey_code'])."'";
  105. $result = Database::query($sql);
  106. if (Database::num_rows($result) > 1) {
  107. if ($_POST['language']) {
  108. $survey_invitation['survey_id'] = $_POST['language'];
  109. } else {
  110. echo '<form id="language" name="language" method="POST" action="'.api_get_self().'?course='.$_GET['course'].'&invitationcode='.$_GET['invitationcode'].'&cidReq='.$_GET['cidReq'].'">';
  111. echo ' <select name="language">';
  112. while ($row = Database::fetch_array($result, 'ASSOC')) {
  113. echo '<option value="'.$row['survey_id'].'">'.$row['lang'].'</option>';
  114. }
  115. echo '</select>';
  116. echo '<button type="submit" name="Submit" class="next">'.get_lang('Ok').'</button>';
  117. echo '</form>';
  118. Display::display_footer();
  119. exit();
  120. }
  121. } else {
  122. $row = Database::fetch_array($result, 'ASSOC');
  123. $survey_invitation['survey_id'] = $row['survey_id'];
  124. }
  125. // Getting the survey information
  126. $survey_data = survey_manager::get_survey($survey_invitation['survey_id']);
  127. $survey_data['survey_id'] = $survey_invitation['survey_id'];
  128. // Storing the answers
  129. if (count($_POST) > 0) {
  130. if ($survey_data['survey_type'] === '0') {
  131. // Getting all the types of the question (because of the special treatment of the score question type
  132. $sql = "SELECT * FROM $table_survey_question WHERE c_id = $course_id AND survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'";
  133. $result = Database::query($sql);
  134. while ($row = Database::fetch_array($result, 'ASSOC')) {
  135. $types[$row['question_id']] = $row['type'];
  136. }
  137. // Looping through all the post values
  138. foreach ($_POST as $key => & $value) {
  139. // If the post value key contains the string 'question' then it is an answer on a question
  140. if (strpos($key, 'question') !== false) {
  141. // Finding the question id by removing 'question'
  142. $survey_question_id = str_replace('question', '', $key);
  143. // If the post value is an array then we have a multiple response question or a scoring question type
  144. // remark: when it is a multiple response then the value of the array is the option_id
  145. // when it is a scoring question then the key of the array is the option_id and the value is the value
  146. if (is_array($value)) {
  147. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $course_id);
  148. foreach ($value as $answer_key => & $answer_value) {
  149. if ($types[$survey_question_id] == 'score') {
  150. $option_id = $answer_key;
  151. $option_value = $answer_value;
  152. } else {
  153. $option_id = $answer_value;
  154. $option_value = '';
  155. }
  156. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $option_id, $option_value, $survey_data);
  157. }
  158. }
  159. // All the other question types (open question, multiple choice, percentage, ...)
  160. else {
  161. if ($types[$survey_question_id] == 'percentage') {
  162. $sql = "SELECT * FROM $table_survey_question_option WHERE c_id = $course_id AND question_option_id='".Database::escape_string($value)."'";
  163. $result = Database::query($sql);
  164. $row = Database::fetch_array($result, 'ASSOC');
  165. $option_value = $row['option_text'];
  166. } else {
  167. $option_value = 0;
  168. if ($types[$survey_question_id] == 'open') {
  169. $option_value = $value;
  170. //$value = 0;
  171. }
  172. }
  173. $survey_question_answer = $value;
  174. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $course_id);
  175. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
  176. }
  177. }
  178. }
  179. } elseif ($survey_data['survey_type'] === '1') {
  180. //conditional/personality-test type surveys
  181. // Getting all the types of the question (because of the special treatment of the score question type
  182. $shuffle = '';
  183. if ($survey_data['shuffle'] == '1') {
  184. $shuffle = ' ORDER BY RAND() ';
  185. }
  186. $sql = "SELECT * FROM $table_survey_question
  187. WHERE c_id = $course_id AND survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  188. AND survey_group_pri = '0' $shuffle";
  189. $result = Database::query($sql);
  190. // There is only one question type for conditional surveys
  191. while ($row = Database::fetch_array($result, 'ASSOC')) {
  192. $types[$row['question_id']] = $row['type'];
  193. }
  194. // Looping through all the post values
  195. foreach ($_POST as $key => & $value) {
  196. // If the post value key contains the string 'question' then it is an answer to a question
  197. if (strpos($key, 'question') !== false) {
  198. // Finding the question id by removing 'question'
  199. $survey_question_id = str_replace('question', '', $key);
  200. // We select the correct answer and the puntuacion
  201. echo $sql = "SELECT value FROM $table_survey_question_option WHERE c_id = $course_id AND question_option_id='".Database::escape_string($value)."'";
  202. $result = Database::query($sql);
  203. $row = Database::fetch_array($result, 'ASSOC');
  204. $option_value = $row['value'];
  205. //$option_value = 0;
  206. $survey_question_answer = $value;
  207. // We save the answer after making sure that a possible previous attempt is deleted
  208. SurveyUtil::remove_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $course_id);
  209. SurveyUtil::store_answer($survey_invitation['user'], $survey_invitation['survey_id'], $survey_question_id, $value, $option_value, $survey_data);
  210. //SurveyUtil::store_answer($user,$survey_id,$question_id, $option_id, $option_value, $survey_data);
  211. }
  212. }
  213. } else { // In case it's another type than 0 or 1
  214. die(get_lang('ErrorSurveyTypeUnknown'));
  215. }
  216. }
  217. // Displaying the survey title and subtitle (appears on every page)
  218. echo '<div id="survey_title">'.$survey_data['survey_title'].'</div>';
  219. echo '<div id="survey_subtitle">'.$survey_data['survey_subtitle'].'</div>';
  220. // Checking time availability
  221. check_time_availability($survey_data);
  222. // Displaying the survey introduction
  223. if (!isset($_GET['show'])) {
  224. // The first thing we do is delete the session
  225. unset($_SESSION['paged_questions']);
  226. unset($_SESSION['page_questions_sec']);
  227. $paged_questions_sec = array();
  228. if (!empty($survey_data['survey_introduction'])) {
  229. echo '<div id="survey_content" class="survey_content">'.$survey_data['survey_introduction'].'</div>';
  230. }
  231. $limit = 0;
  232. }
  233. $user_id = api_get_user_id();
  234. if ($user_id == 0) {
  235. $user_id = $survey_invitation['user'];
  236. }
  237. $user_data = UserManager :: get_user_info_by_id($user_id);
  238. if ($survey_data['form_fields'] != '' && $survey_data['anonymous'] == 0 && is_array($user_data)) {
  239. $form_fields = explode('@', $survey_data['form_fields']);
  240. $list = array();
  241. foreach ($form_fields as $field) {
  242. $field_value = explode(':', $field);
  243. if ($field_value[1] == 1) {
  244. if ($field_value[0] != '') {
  245. $val = api_substr($field_value[0], 8, api_strlen($field_value[0]));
  246. $list[$val] = 1;
  247. }
  248. }
  249. }
  250. // We use the same form as in auth/profile.php
  251. $form = new FormValidator('profile', 'post', api_get_self()."?".str_replace('&show_form=1', '&show_form=1', $_SERVER['QUERY_STRING']), null,
  252. array('style' => 'width: 75%; float: '.($text_dir == 'rtl' ? 'right;' : 'left;'))
  253. );
  254. if (api_is_western_name_order()) {
  255. if ($list['firstname'] == 1) {
  256. //FIRST NAME
  257. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  258. if (api_get_setting('profile', 'name') !== 'true') {
  259. $form->freeze(array('firstname'));
  260. }
  261. $form->applyFilter(array('firstname'), 'stripslashes');
  262. $form->applyFilter(array('firstname'), 'trim');
  263. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  264. }
  265. if ($list['lastname'] == 1) {
  266. // LAST NAME
  267. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  268. if (api_get_setting('profile', 'name') !== 'true') {
  269. $form->freeze(array('lastname'));
  270. }
  271. $form->applyFilter(array('lastname'), 'stripslashes');
  272. $form->applyFilter(array('lastname'), 'trim');
  273. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  274. }
  275. } else {
  276. if ($list['lastname'] == 1) {
  277. // LAST NAME
  278. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  279. if (api_get_setting('profile', 'name') !== 'true') {
  280. $form->freeze(array('lastname'));
  281. }
  282. $form->applyFilter(array('lastname'), 'stripslashes');
  283. $form->applyFilter(array('lastname'), 'trim');
  284. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  285. }
  286. if ($list['firstname'] == 1) {
  287. //FIRST NAME
  288. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  289. if (api_get_setting('profile', 'name') !== 'true') {
  290. $form->freeze(array('firstname'));
  291. }
  292. $form->applyFilter(array('firstname'), 'stripslashes');
  293. $form->applyFilter(array('firstname'), 'trim');
  294. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  295. }
  296. }
  297. if ($list['official_code'] == 1) {
  298. // OFFICIAL CODE
  299. if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
  300. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => 40));
  301. if (api_get_setting('profile', 'officialcode') !== 'true') {
  302. $form->freeze('official_code');
  303. }
  304. $form->applyFilter('official_code', 'stripslashes');
  305. $form->applyFilter('official_code', 'trim');
  306. if (api_get_setting('registration', 'officialcode') == 'true' && api_get_setting('profile', 'officialcode') == 'true') {
  307. $form->addRule('official_code', get_lang('ThisFieldIsRequired'), 'required');
  308. }
  309. }
  310. }
  311. if ($list['email'] == 1) {
  312. // EMAIL
  313. $form->addElement('text', 'email', get_lang('Email'), array('size' => 40));
  314. if (api_get_setting('profile', 'email') !== 'true') {
  315. $form->freeze('email');
  316. }
  317. $form->applyFilter('email', 'stripslashes');
  318. $form->applyFilter('email', 'trim');
  319. if (api_get_setting('registration', 'email') == 'true') {
  320. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  321. }
  322. $form->addRule('email', get_lang('EmailWrong'), 'email');
  323. }
  324. if ($list['phone'] == 1) {
  325. // PHONE
  326. $form->addElement('text', 'phone', get_lang('phone'), array('size' => 20));
  327. if (api_get_setting('profile', 'phone') !== 'true') {
  328. $form->freeze('phone');
  329. }
  330. $form->applyFilter('phone', 'stripslashes');
  331. $form->applyFilter('phone', 'trim');
  332. if (api_get_setting('profile', 'phone') == 'true') {
  333. $form->addRule('phone', get_lang('ThisFieldIsRequired'), 'required');
  334. }
  335. }
  336. if ($list['language'] == 1) {
  337. //LANGUAGE
  338. $form->addElement('select_language', 'language', get_lang('Language'));
  339. if (api_get_setting('profile', 'language') !== 'true') {
  340. $form->freeze('language');
  341. }
  342. if (api_get_setting('profile', 'language') == 'true') {
  343. $form->addRule('language', get_lang('ThisFieldIsRequired'), 'required');
  344. }
  345. }
  346. // EXTRA FIELDS
  347. $extra_data = UserManager :: get_extra_user_data($user_id, true);
  348. UserManager::set_extra_fields_in_form($form, $extra_data, 'profile');
  349. $form->addElement('style_submit_button', '', get_lang('Next'), array('class' => 'next'));
  350. $user_data = array_merge($user_data, $extra_data);
  351. $form->setDefaults($user_data);
  352. }
  353. if ($survey_data['form_fields'] && $survey_data['anonymous'] == 0 && is_array($user_data) && !isset($_GET['show'])) {
  354. if ($form->validate()) {
  355. $user_data = $form->exportValues();
  356. if (is_array($user_data)) {
  357. if (count($user_data) > 0) {
  358. $extras = array();
  359. // Build SQL query
  360. $sql = "UPDATE $table_user SET";
  361. foreach ($user_data as $key => $value) {
  362. if (substr($key, 0, 6) == 'extra_') { //an extra field
  363. $extras[substr($key, 6)] = $value;
  364. } else {
  365. $sql .= " $key = '".Database :: escape_string($value)."',";
  366. }
  367. }
  368. // Remove trailing , from the query we have so far
  369. $sql = rtrim($sql, ',');
  370. $sql .= " WHERE user_id = '".$user_id."'";
  371. Database::query($sql);
  372. // Update the extra fields
  373. if (is_array($extras)) {
  374. foreach ($extras as $key => $value) {
  375. $myres = UserManager :: update_extra_field_value($user_id, $key, $value);
  376. }
  377. }
  378. echo '<div id="survey_content" class="survey_content">'.get_lang('InformationUpdated').' '.get_lang('PleaseFillSurvey').'</div>';
  379. }
  380. }
  381. $_GET['show'] = 0;
  382. $show = 0;
  383. // We unset the sessions
  384. unset($_SESSION['paged_questions']);
  385. unset($_SESSION['page_questions_sec']);
  386. $paged_questions_sec = array();
  387. } else {
  388. echo '<div id="survey_content" class="survey_content">'.get_lang('UpdateInformation').'</div>';
  389. // We unset the sessions
  390. unset($_SESSION['paged_questions']);
  391. unset($_SESSION['page_questions_sec']);
  392. $paged_questions_sec = array();
  393. $form->display();
  394. }
  395. }
  396. // Displaying the survey thanks message
  397. if (isset($_POST['finish_survey'])) {
  398. Display::display_confirmation_message(get_lang('SurveyFinished'));
  399. echo $survey_data['survey_thanks'];
  400. survey_manager::update_survey_answered($survey_data, $survey_invitation['user'], $survey_invitation['survey_code']);
  401. unset($_SESSION['paged_questions']);
  402. unset($_SESSION['page_questions_sec']);
  403. Display :: display_footer();
  404. exit();
  405. }
  406. // Sets the random questions
  407. $shuffle = '';
  408. if ($survey_data['shuffle'] == 1) {
  409. $shuffle = ' BY RAND() ';
  410. }
  411. if (isset($_GET['show']) || isset($_POST['personality'])) {
  412. // Getting all the questions for this page and add them to a multidimensional array where the first index is the page.
  413. // As long as there is no pagebreak fount we keep adding questions to the page
  414. $questions_displayed = array();
  415. $counter = 0;
  416. $paged_questions = array();
  417. // If non-conditional survey
  418. if ($survey_data['survey_type'] === '0') {
  419. if (empty($_SESSION['paged_questions'])) {
  420. $sql = "SELECT * FROM $table_survey_question
  421. WHERE c_id = $course_id AND survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  422. ORDER BY sort ASC";
  423. $result = Database::query($sql);
  424. while ($row = Database::fetch_array($result, 'ASSOC')) {
  425. if ($row['type'] == 'pagebreak') {
  426. $counter++;
  427. } else {
  428. // ids from question of the current survey
  429. $paged_questions[$counter][] = $row['question_id'];
  430. }
  431. }
  432. $_SESSION['paged_questions'] = $paged_questions;
  433. } else {
  434. $paged_questions = $_SESSION['paged_questions'];
  435. }
  436. // Redefinition of variables and session ids to fix issue of survey not
  437. // showing questions - see support.chamilo.org #5529
  438. $course_id = $survey_invitation['c_id'];
  439. $_SESSION['_cid'] = $course_id;
  440. $_SESSION['_real_cid'] = $course_id;
  441. if (key_exists($_GET['show'], $paged_questions)) {
  442. if (isset($_GET['user_id'])) {
  443. // Get the user into survey answer table (user or anonymus)
  444. $my_user_id = ($survey_data['anonymous'] == 1) ? $_SESSION['surveyuser'] : api_get_user_id();
  445. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  446. 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,
  447. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  448. FROM $table_survey_question survey_question
  449. LEFT JOIN $table_survey_question_option survey_question_option
  450. ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id
  451. WHERE survey_question.survey_id = '".Database :: escape_string($survey_invitation['survey_id'])."'
  452. AND survey_question.question_id NOT IN (SELECT sa.question_id FROM ".$table_survey_answer." sa WHERE sa.user='".$my_user_id."') AND
  453. survey_question.c_id = $course_id
  454. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  455. } else {
  456. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  457. 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,
  458. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  459. FROM $table_survey_question survey_question
  460. LEFT JOIN $table_survey_question_option survey_question_option
  461. ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id
  462. WHERE survey_question.survey_id = '".Database::escape_string($survey_invitation['survey_id'])."' AND
  463. survey_question.question_id IN (".implode(',', $paged_questions[$_GET['show']]).") AND
  464. survey_question.c_id = $course_id
  465. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  466. }
  467. $result = Database::query($sql);
  468. $question_counter_max = Database::num_rows($result);
  469. $counter = 0;
  470. $limit = 0;
  471. $questions = array();
  472. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  473. // If the type is not a pagebreak we store it in the $questions array
  474. if ($row['type'] != 'pagebreak') {
  475. $questions[$row['sort']]['question_id'] = $row['question_id'];
  476. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  477. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  478. $questions[$row['sort']]['display'] = $row['display'];
  479. $questions[$row['sort']]['type'] = $row['type'];
  480. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  481. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  482. }
  483. // If the type is a pagebreak we are finished loading the questions for this page
  484. else {
  485. break;
  486. }
  487. $counter++;
  488. }
  489. }
  490. } elseif ($survey_data['survey_type'] === '1') {
  491. $my_survey_id = Database::escape_string($survey_invitation['survey_id']);
  492. $current_user = Database::escape_string($survey_invitation['user']);
  493. if (isset($_POST['personality'])) {
  494. // Compute the results to get the 3 groups nearest to the user's personality
  495. if ($shuffle == '') {
  496. $order = 'BY sort ASC ';
  497. } else {
  498. $order = $shuffle;
  499. }
  500. $answer_list = array();
  501. // Get current user results
  502. $results = array();
  503. $sql = "SELECT survey_group_pri, user, SUM(value) as value
  504. FROM $table_survey_answer as survey_answer INNER JOIN $table_survey_question as survey_question
  505. ON (survey_question.question_id = survey_answer.question_id)
  506. WHERE survey_answer.survey_id='".$my_survey_id."' AND
  507. survey_answer.user='".$current_user."' AND
  508. survey_answer.c_id = $course_id AND
  509. survey_question.c_id = $course_id AND
  510. GROUP BY survey_group_pri
  511. ORDER BY survey_group_pri
  512. ";
  513. $result = Database::query($sql);
  514. while ($row = Database :: fetch_array($result)) {
  515. $answer_list['value'] = $row['value'];
  516. $answer_list['group'] = $row['survey_group_pri'];
  517. $results[] = $answer_list;
  518. }
  519. //echo '<br />'; print_r($results); echo '<br />';
  520. // Get the total score for each group of questions
  521. $totals = array();
  522. $sql = "SELECT SUM(temp.value) as value, temp.survey_group_pri FROM
  523. (
  524. SELECT MAX(value) as value, survey_group_pri, survey_question.question_id
  525. FROM $table_survey_question as survey_question
  526. INNER JOIN $table_survey_question_option as survey_question_option
  527. ON (survey_question.question_id = survey_question_option.question_id)
  528. WHERE survey_question.survey_id='".$my_survey_id."' AND
  529. survey_question.c_id = $course_id AND
  530. survey_question_option.c_id = $course_id AND
  531. survey_group_sec1='0' AND
  532. survey_group_sec2='0'
  533. GROUP BY survey_group_pri, survey_question.question_id
  534. ) as temp
  535. GROUP BY temp.survey_group_pri
  536. ORDER BY temp.survey_group_pri";
  537. $result = Database::query($sql);
  538. while ($row = Database::fetch_array($result)) {
  539. $list['value'] = $row['value'];
  540. $list['group'] = $row['survey_group_pri'];
  541. $totals[] = $list;
  542. }
  543. //echo '<pre>'; print_r($totals);
  544. $final_results = array();
  545. // Get a percentage score for each group
  546. for ($i = 0; $i < count($totals); $i++) {
  547. for ($j = 0; $j < count($results); $j++) {
  548. if ($totals[$i]['group'] == $results[$j]['group']) {
  549. $group = $totals[$i]['group'];
  550. $porcen = ($results[$j]['value'] / $totals[$i]['value']);
  551. $final_results[$group] = $porcen;
  552. }
  553. }
  554. }
  555. // Sort the results by score (getting a list of group IDs by score into $groups)
  556. arsort($final_results);
  557. $groups = array_keys($final_results);
  558. $result = array();
  559. $count_result = 0;
  560. foreach ($final_results as $key => & $sub_result) {
  561. $result[] = array('group' => $key, 'value' => $sub_result);
  562. $count_result++;
  563. }
  564. /*
  565. //i.e 70% - 70% -70% 70% $equal_count =3
  566. while (1) {
  567. if ($result[$i]['value'] == $result[$i+1]['value']) {
  568. $equal_count++;
  569. } else {
  570. break;
  571. }
  572. $i++;
  573. }
  574. echo 'eq'. $equal_count;
  575. echo '<br />';
  576. if ($equal_count == 0) {
  577. //i.e 70% 70% -60% 60% $equal_count = 1 only we get the first 2 options
  578. if (($result[0]['value'] == $result[1]['value']) && ($result[2]['value'] == $result[3]['value'])) {
  579. $group_cant = 1;
  580. } else {
  581. // By default we chose the highest 3
  582. $group_cant=2;
  583. }
  584. } elseif ($equal_count == 2) {
  585. $group_cant = 2;
  586. } else {
  587. $group_cant = -1;
  588. }
  589. */
  590. // i.e 70% - 70% -70% 70% $equal_count =3
  591. $i = 0;
  592. $group_cant = 0;
  593. $equal_count = 0;
  594. // This is the case if the user does not select any question
  595. if ($count_result > 0) {
  596. // Count the number of scores equal to the first
  597. while (1) {
  598. if ($result[$i]['value'] == $result[$i + 1]['value']) {
  599. $equal_count++;
  600. } else {
  601. break;
  602. }
  603. $i++;
  604. }
  605. } else {
  606. // We force the exit of the survey undeterminated
  607. $equal_count = 10;
  608. }
  609. // If we have only 3 or less equal scores (i.e. 0,1 or 2 equalities), then we can use the three first groups
  610. if ($equal_count < 4) {
  611. // If there is one or less score equalities
  612. if ($equal_count === 0 || $equal_count === 1) {
  613. // i.e 70% - 70% -60% - 60% $equal_count = 1 we only get the first 2 options
  614. if (($result[0]['value'] == $result[1]['value']) && ($result[2]['value'] == $result[3]['value'])) {
  615. $group_cant = 1;
  616. }
  617. // i.e 70% - 70% -0% - 0% - $equal_count = 0 we only get the first 2 options
  618. /* elseif (($result[0]['value'] == $result[1]['value']) && ($result[1]['value'] != $result[2]['value'])) {
  619. $group_cant = 0;
  620. } */
  621. /*
  622. // i.e 70% - 70% -60% - 60% $equal_count = 0 we only get the first 2 options
  623. elseif (($result[0]['value'] == $result[1]['value']) && ($result[2]['value'] == $result[3]['value'])) {
  624. $group_cant = 0;
  625. } */
  626. // i.e. 80% - 70% - 70% - 70%
  627. elseif (($result[0]['value'] != $result[1]['value']) && ($result[1]['value'] == $result[2]['value']) && ($result[2]['value'] == $result[3]['value'])) {
  628. $group_cant = 0;
  629. } else {
  630. // i.e. 80% - 70% - 70% - 50
  631. // i.e. 80% - 80% - 70% - 50
  632. // By default we choose the highest 3
  633. $group_cant = 2;
  634. }
  635. } else {
  636. // If there are two score equalities
  637. $group_cant = $equal_count;
  638. }
  639. //@todo Translate these comments.
  640. // conditional_status
  641. // 0 no determinado
  642. // 1 determinado
  643. // 2 un solo valor
  644. // 3 valores iguales
  645. if ($group_cant > 0) {
  646. //echo '$equal_count'.$group_cant;
  647. // We only get highest 3
  648. $secondary = '';
  649. $combi = '';
  650. for ($i = 0; $i <= $group_cant; $i++) {
  651. $group1 = $groups[$i];
  652. $group2 = $groups[$i + 1];
  653. // Here we made all the posibilities with the 3 groups
  654. if ($group_cant == 2 && $i == $group_cant) {
  655. $group2 = $groups[0];
  656. $secondary .= " OR ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  657. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  658. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
  659. } else {
  660. if ($i != 0) {
  661. $secondary .= " OR ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  662. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  663. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
  664. } else {
  665. $secondary .= " ( survey_group_sec1 = '$group1' AND survey_group_sec2 = '$group2') ";
  666. $secondary .= " OR ( survey_group_sec1 = '$group2' AND survey_group_sec2 = '$group1' ) ";
  667. $combi.= $group1.' - '.$group2." or ".$group2.' - '.$group1.'<br />';
  668. }
  669. }
  670. }
  671. /*
  672. echo '<pre>';
  673. echo 'Pair of Groups <br /><br />';
  674. echo $combi;
  675. echo '</pre>';
  676. */
  677. // Create the new select with the questions from the secondary phase
  678. if (empty($_SESSION['page_questions_sec']) && !is_array($_SESSION['page_questions_sec']) && count($_SESSION['page_questions_sec'] == 0)) {
  679. $sql = "SELECT * FROM $table_survey_question
  680. WHERE c_id = $course_id AND survey_id = '".$my_survey_id."'
  681. AND ($secondary )
  682. ORDER BY sort ASC";
  683. $result = Database::query($sql);
  684. $counter = 0;
  685. while ($row = Database::fetch_array($result, 'ASSOC')) {
  686. if ($survey_data['one_question_per_page'] == 1) {
  687. $paged_questions_sec[$counter][] = $row['question_id'];
  688. $counter++;
  689. } elseif ($row['type'] == 'pagebreak') {
  690. $counter++;
  691. } else {
  692. // ids from question of the current survey
  693. $paged_questions_sec[$counter][] = $row['question_id'];
  694. }
  695. }
  696. $_SESSION['paged_questions_sec'] = $paged_questions_sec;
  697. } else {
  698. $paged_questions_sec = $_SESSION['paged_questions_sec'];
  699. }
  700. //print_r($paged_questions_sec);
  701. $paged_questions = $_SESSION['paged_questions']; // For the sake of pages counting
  702. //$paged_questions = $paged_questions_sec; // For the sake of pages counting coming up at display time...
  703. if ($shuffle == '') {
  704. $shuffle = ' BY survey_question.sort, survey_question_option.sort ASC ';
  705. }
  706. //$val = 0;
  707. //if ($survey_data['one_question_per_page'] == 0) {
  708. $val = (int) $_POST['personality'];
  709. //}
  710. //echo '<pre>'; print_r($paged_questions_sec); echo '</pre>';
  711. if (is_array($paged_questions_sec)) {
  712. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  713. 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,
  714. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  715. FROM $table_survey_question survey_question
  716. LEFT JOIN $table_survey_question_option survey_question_option
  717. ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id
  718. WHERE survey_question.survey_id = '".$my_survey_id."' AND
  719. survey_question.c_id = $course_id AND
  720. survey_question.question_id IN (".implode(',', $paged_questions_sec[$val]).")
  721. ORDER $shuffle ";
  722. $result = Database::query($sql);
  723. $question_counter_max = Database::num_rows($result);
  724. $counter = 0;
  725. $limit = 0;
  726. $questions = array();
  727. while ($row = Database::fetch_array($result, 'ASSOC')) {
  728. // If the type is not a pagebreak we store it in the $questions array
  729. if ($row['type'] != 'pagebreak') {
  730. $questions[$row['sort']]['question_id'] = $row['question_id'];
  731. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  732. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  733. $questions[$row['sort']]['display'] = $row['display'];
  734. $questions[$row['sort']]['type'] = $row['type'];
  735. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  736. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  737. // Personality params
  738. $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
  739. $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
  740. $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
  741. } else {
  742. // If the type is a pagebreak we are finished loading the questions for this page
  743. break;
  744. }
  745. $counter++;
  746. }
  747. } else {
  748. echo get_lang('SurveyUndetermined');
  749. }
  750. } else {
  751. echo get_lang('SurveyUndetermined');
  752. }
  753. } else {
  754. echo get_lang('SurveyUndetermined');
  755. }
  756. } else {
  757. // We need this variable only in the 2nd set of questions when personality is set.
  758. unset($_SESSION['page_questions_sec']);
  759. $paged_questions_sec = array();
  760. // Only the questions from the basic group
  761. // the 50 questions A B C D E F G
  762. $order_sql = $shuffle;
  763. if ($shuffle == '') {
  764. $order_sql = ' BY question_id ';
  765. }
  766. if (empty($_SESSION['paged_questions'])) {
  767. $sql = "SELECT * FROM $table_survey_question
  768. WHERE c_id = $course_id AND survey_id = '".Database::escape_string($survey_invitation['survey_id'])."'
  769. AND survey_group_sec1='0' AND survey_group_sec2='0'
  770. ORDER ".$order_sql." ";
  771. //echo '<br />'; echo '<br />';
  772. $result = Database::query($sql);
  773. $counter = 0;
  774. while ($row = Database::fetch_array($result, 'ASSOC')) {
  775. if ($survey_data['one_question_per_page'] == 1) {
  776. $paged_questions[$counter][] = $row['question_id'];
  777. $counter++;
  778. } else {
  779. if ($row['type'] == 'pagebreak') {
  780. $counter++;
  781. } else {
  782. // ids from question of the current survey
  783. $paged_questions[$counter][] = $row['question_id'];
  784. }
  785. }
  786. }
  787. $_SESSION['paged_questions'] = $paged_questions;
  788. } else {
  789. $paged_questions = $_SESSION['paged_questions'];
  790. }
  791. //print_r($paged_questions);
  792. //print_r($paged_questions);
  793. //if (key_exists($_GET['show'], $paged_questions)) {
  794. $order_sql = $shuffle;
  795. if ($shuffle == '') {
  796. $order_sql = ' BY survey_question.sort, survey_question_option.sort ASC ';
  797. }
  798. //$val = 0;
  799. //if ($survey_data['one_question_per_page'] == 0) {
  800. $val = $_GET['show'];
  801. //}
  802. //echo '<pre>'; print_r($paged_questions); echo $val;
  803. $result = null;
  804. if ($val != '') {
  805. $imploded = implode(',', $paged_questions[$val]);
  806. if ($imploded != '') {
  807. // The answers are always in the same order NO shuffle
  808. $order_sql = ' BY survey_question.sort, survey_question_option.sort ASC ';
  809. $sql = "SELECT survey_question.survey_group_sec1, survey_question.survey_group_sec2, survey_question.survey_group_pri,
  810. 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,
  811. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  812. FROM $table_survey_question survey_question
  813. LEFT JOIN $table_survey_question_option survey_question_option
  814. ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id
  815. WHERE survey_question.survey_id = '".Database :: escape_string($survey_invitation['survey_id'])."' AND
  816. survey_question.c_id = $course_id AND
  817. survey_question.question_id IN (".$imploded.")
  818. ORDER $order_sql ";
  819. $result = Database::query($sql);
  820. $question_counter_max = Database :: num_rows($result);
  821. }
  822. }
  823. if (!is_null($result)) {
  824. $counter = 0;
  825. $limit = 0;
  826. $questions = array();
  827. while ($row = Database :: fetch_array($result, 'ASSOC')) {
  828. // If the type is not a pagebreak we store it in the $questions array
  829. if ($row['type'] != 'pagebreak') {
  830. $questions[$row['sort']]['question_id'] = $row['question_id'];
  831. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  832. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  833. $questions[$row['sort']]['display'] = $row['display'];
  834. $questions[$row['sort']]['type'] = $row['type'];
  835. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  836. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  837. // Personality params
  838. $questions[$row['sort']]['survey_group_sec1'] = $row['survey_group_sec1'];
  839. $questions[$row['sort']]['survey_group_sec2'] = $row['survey_group_sec2'];
  840. $questions[$row['sort']]['survey_group_pri'] = $row['survey_group_pri'];
  841. }
  842. // If the type is a pagebreak we are finished loading the questions for this page
  843. else {
  844. break;
  845. }
  846. $counter++;
  847. }
  848. }
  849. }
  850. } else { // In case it's another type than 0 or 1
  851. echo get_lang('ErrorSurveyTypeUnknown');
  852. }
  853. }
  854. // Selecting the maximum number of pages
  855. $sql = "SELECT * FROM $table_survey_question WHERE c_id = $course_id AND type='".Database::escape_string('pagebreak')."' AND survey_id='".Database::escape_string($survey_invitation['survey_id'])."'";
  856. $result = Database::query($sql);
  857. $numberofpages = Database::num_rows($result) + 1;
  858. // Displaying the form with the questions
  859. if (isset($_GET['show'])) {
  860. $show = (int) $_GET['show'] + 1;
  861. } else {
  862. $show = 0;
  863. }
  864. // Displaying the form with the questions
  865. if (isset($_POST['personality'])) {
  866. $personality = (int) $_POST['personality'] + 1;
  867. } else {
  868. $personality = 0;
  869. }
  870. // Displaying the form with the questions
  871. $g_c = isset($_GET['course']) ? Security::remove_XSS($_GET['course']) : '';
  872. $g_ic = isset($_GET['invitationcode']) ? Security::remove_XSS($_GET['invitationcode']) : '';
  873. $g_cr = isset($_GET['cidReq']) ? Security::remove_XSS($_GET['cidReq']) : '';
  874. $p_l = isset($_POST['language']) ? Security::remove_XSS($_POST['language']) : '';
  875. $add_parameters = isset($_GET['user_id']) ? 'user_id='.$_GET['user_id'].'&amp;' : '';
  876. echo '<form id="question" name="question" method="post" action="'.api_get_self().'?'.$add_parameters.'course='.$g_c.'&invitationcode='.$g_ic.'&show='.$show.'&cidReq='.$g_cr.'">';
  877. echo '<input type="hidden" name="language" value="'.$p_l.'" />';
  878. if (isset($questions) && is_array($questions)) {
  879. foreach ($questions as $key => & $question) {
  880. $ch_type = 'ch_'.$question['type'];
  881. $display = new $ch_type;
  882. $display->render_question($question);
  883. }
  884. }
  885. if ($survey_data['survey_type'] === '0') {
  886. if ($survey_data['show_form_profile'] == 0) {
  887. // The normal survey as always
  888. if (($show < $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
  889. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('NextQuestion').'</button>';
  890. }
  891. if ($show >= $numberofpages && $_GET['show']) {
  892. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').'</button>';
  893. }
  894. } else {
  895. // The normal survey as always but with the form profile
  896. if (isset($_GET['show'])) {
  897. $numberofpages = count($paged_questions);
  898. if (($show < $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
  899. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  900. }
  901. if ($show >= $numberofpages && $_GET['show']) {
  902. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').'</button>';
  903. }
  904. }
  905. }
  906. } elseif ($survey_data['survey_type'] === '1') { //conditional/personality-test type survey
  907. if (isset($_GET['show']) || isset($_POST['personality'])) {
  908. $numberofpages = count($paged_questions);
  909. //echo '<br />'; echo 'num pages norma:'.$numberofpages; echo '<br />';
  910. //echo '<pre>'; print_r($paged_questions_sec);
  911. if (!empty($paged_questions_sec) && count($paged_questions_sec) > 0) {
  912. // In case we're in the second phase, also sum the second group questions
  913. //echo 'pagesec :'.count($paged_questions_sec);
  914. $numberofpages += count($paged_questions_sec);
  915. //echo 'pagesec :';
  916. } else {
  917. // We need this variable only if personality == 1
  918. unset($_SESSION['page_questions_sec']);
  919. $paged_questions_sec = array();
  920. }
  921. /* echo '<br />';
  922. echo 'num pages:'.$numberofpages; echo '<br />';
  923. echo 'show :'.$show;echo '<br />';
  924. echo 'personality :'.$personality;
  925. echo '<br />';
  926. */
  927. //echo $show.' / '.$numberofpages.'<br />';
  928. if ($personality == 0)
  929. if (($show <= $numberofpages) || !$_GET['show']) { //$show = $_GET['show'] + 1
  930. //echo '<input type="submit" name="next_survey_page" value="' . get_lang('Next') . ' " class="next" />';
  931. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  932. if ($survey_data['one_question_per_page'] == 0) {
  933. if ($personality >= 0) {
  934. echo '<input type="hidden" name="personality" value="'.$personality.'">';
  935. }
  936. } else {
  937. if ($personality > 0) {
  938. echo '<input type="hidden" name="personality" value="'.$personality.'">';
  939. }
  940. }
  941. if ($numberofpages == $show) {
  942. echo '<input type="hidden" name="personality" value="'.$personality.'">';
  943. }
  944. }
  945. if ($show > $numberofpages && $_GET['show'] && $personality == 0) {
  946. echo '<input type="hidden" name="personality" value="'.$personality.'">';
  947. //$numberofpages = count($paged_questions);
  948. //echo $numberofpages = count($paged_questions_sec);
  949. //echo $personality.' / '.$numberofpages;
  950. //echo '<br />';
  951. //if ($personality > count($paged_questions_sec) - 1)
  952. //|| $numberofpages == $show +$personality +1
  953. //echo $show + $personality;
  954. //echo $numberofpages;
  955. } elseif ($personality > 0) {
  956. if ($survey_data['one_question_per_page'] == 1) {
  957. if ($show >= $numberofpages) {
  958. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').'</button>';
  959. } else {
  960. echo '<input type="hidden" name="personality" value="'.$personality.'">';
  961. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  962. }
  963. } else {
  964. // if the personality test hidden input was set.
  965. echo '<button type="submit" name="finish_survey" class="next">'.get_lang('FinishSurvey').'</button>';
  966. }
  967. }
  968. }
  969. // This is the case when the show_profile_form is true but there are not form_fields
  970. elseif ($survey_data['form_fields'] == '') {
  971. //echo '<input type="submit" name="next_survey_page" value="' . get_lang('Next') . ' " class="next" />';
  972. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  973. } elseif (!is_array($user_data)) {
  974. // If the user is not registered in the platform we do not show the form to update his information
  975. echo '<button type="submit" name="next_survey_page" class="next">'.get_lang('Next').'</button>';
  976. }
  977. }
  978. echo '</form>';
  979. // Footer
  980. Display :: display_footer();
  981. /**
  982. * Check whether this survey has ended. If so, display message and exit rhis script
  983. */
  984. function check_time_availability($surv_data) {
  985. $start_date = mktime(0, 0, 0, substr($surv_data['start_date'], 5, 2), substr($surv_data['start_date'], 8, 2), substr($surv_data['start_date'], 0, 4));
  986. $end_date = mktime(0, 0, 0, substr($surv_data['end_date'], 5, 2), substr($surv_data['end_date'], 8, 2), substr($surv_data['end_date'], 0, 4));
  987. $cur_date = time();
  988. if ($cur_date < $start_date) {
  989. Display :: display_warning_message(get_lang('SurveyNotAvailableYet'), false);
  990. Display :: display_footer();
  991. exit;
  992. }
  993. if ($cur_date > $end_date) {
  994. Display :: display_warning_message(get_lang('SurveyNotAvailableAnymore'), false);
  995. Display :: display_footer();
  996. exit;
  997. }
  998. }