fillsurvey.php 58 KB

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