fillsurvey.php 55 KB

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