fillsurvey.php 52 KB

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