fillsurvey.php 51 KB

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