fillsurvey.php 51 KB

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