fillsurvey.php 50 KB

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