fillsurvey.php 57 KB

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