fillsurvey.php 57 KB

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