fillsurvey.php 54 KB

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