fillsurvey.php 54 KB

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