fillsurvey.php 59 KB

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