fillsurvey.php 50 KB

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