fillsurvey.php 57 KB

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