fillsurvey.php 57 KB

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